Skip to content

Commit

Permalink
StringSourceLocator is forced to be built with a non-empty-string
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jun 26, 2022
1 parent 74f000a commit e10a503
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 43 deletions.
11 changes: 0 additions & 11 deletions src/SourceLocator/Exception/EmptyPhpSourceCode.php

This file was deleted.

12 changes: 1 addition & 11 deletions src/SourceLocator/Type/StringSourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use InvalidArgumentException;
use Roave\BetterReflection\Identifier\Identifier;
use Roave\BetterReflection\SourceLocator\Ast\Locator;
use Roave\BetterReflection\SourceLocator\Exception\EmptyPhpSourceCode;
use Roave\BetterReflection\SourceLocator\Exception\InvalidFileLocation;
use Roave\BetterReflection\SourceLocator\Located\LocatedSource;

Expand All @@ -21,20 +20,11 @@
class StringSourceLocator extends AbstractSourceLocator
{
/**
* @throws EmptyPhpSourceCode
* @param non-empty-string $source
*/
public function __construct(private string $source, Locator $astLocator)
{
parent::__construct($astLocator);

if (empty($source)) {
// Whilst an empty string is still "valid" PHP code, there is no
// point in us even trying to parse it because we won't find what
// we are looking for, therefore this throws an exception
throw new EmptyPhpSourceCode(
'Source code string was empty',
);
}
}

/**
Expand Down
8 changes: 6 additions & 2 deletions test/unit/Reflection/ReflectionClassConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public function testGetDeclaringClass(): void
}

/**
* @param non-empty-string $php
*
* @dataProvider startEndLineProvider
*/
public function testStartEndLine(string $php, int $startLine, int $endLine): void
Expand All @@ -138,7 +140,7 @@ public function testStartEndLine(string $php, int $startLine, int $endLine): voi
}

/**
* @return list<array{0: string, 1: int, 2: int}>
* @return list<array{0: non-empty-string, 1: int, 2: int}>
*/
public function startEndLineProvider(): array
{
Expand All @@ -151,7 +153,7 @@ public function startEndLineProvider(): array
}

/**
* @return list<array{0: string, 1: int, 2: int}>
* @return list<array{0: non-empty-string, 1: int, 2: int}>
*/
public function columnsProvider(): array
{
Expand All @@ -163,6 +165,8 @@ public function columnsProvider(): array
}

/**
* @param non-empty-string $php
*
* @dataProvider columnsProvider
*/
public function testGetStartColumnAndEndColumn(string $php, int $startColumn, int $endColumn): void
Expand Down
8 changes: 6 additions & 2 deletions test/unit/Reflection/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ public function testGetParentClassNames(): void
}

/**
* @return list<array{0: string, 1: int, 2: int}>
* @return list<array{0: non-empty-string, 1: int, 2: int}>
*/
public function startEndLineProvider(): array
{
Expand All @@ -847,6 +847,8 @@ public function startEndLineProvider(): array
}

/**
* @param non-empty-string $php
*
* @dataProvider startEndLineProvider
*/
public function testStartEndLine(string $php, int $expectedStart, int $expectedEnd): void
Expand All @@ -859,7 +861,7 @@ public function testStartEndLine(string $php, int $expectedStart, int $expectedE
}

/**
* @return list<array{0: string, 1: int, 2: int}>
* @return list<array{0: non-empty-string, 1: int, 2: int}>
*/
public function columnsProvider(): array
{
Expand All @@ -871,6 +873,8 @@ public function columnsProvider(): array
}

/**
* @param non-empty-string $php
*
* @dataProvider columnsProvider
*/
public function testGetStartColumnAndEndColumn(string $php, int $startColumn, int $endColumn): void
Expand Down
8 changes: 6 additions & 2 deletions test/unit/Reflection/ReflectionConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function testGetDocCommentByDefine(): void
}

/**
* @return list<array{0: string, 1: int, 2: int}>
* @return list<array{0: non-empty-string, 1: int, 2: int}>
*/
public function startEndLineProvider(): array
{
Expand All @@ -270,6 +270,8 @@ public function startEndLineProvider(): array
}

/**
* @param non-empty-string $php
*
* @dataProvider startEndLineProvider
*/
public function testStartEndLine(string $php, int $expectedStart, int $expectedEnd): void
Expand All @@ -282,7 +284,7 @@ public function testStartEndLine(string $php, int $expectedStart, int $expectedE
}

/**
* @return list<array{0: string, 1: int, 2: int}>
* @return list<array{0: non-empty-string, 1: int, 2: int}>
*/
public function columnsProvider(): array
{
Expand All @@ -293,6 +295,8 @@ public function columnsProvider(): array
}

/**
* @param non-empty-string $php
*
* @dataProvider columnsProvider
*/
public function testGetStartColumnAndEndColumn(string $php, int $startColumn, int $endColumn): void
Expand Down
20 changes: 15 additions & 5 deletions test/unit/Reflection/ReflectionFunctionAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function testIsInternal(): void
}

/**
* @return list<array{0: string, 1: bool}>
* @return list<array{0: non-empty-string, 1: bool}>
*/
public function variadicProvider(): array
{
Expand All @@ -196,6 +196,8 @@ public function variadicProvider(): array
}

/**
* @param non-empty-string $php
*
* @dataProvider variadicProvider
*/
public function testIsVariadic(string $php, bool $expectingVariadic): void
Expand All @@ -212,7 +214,7 @@ public function testIsVariadic(string $php, bool $expectingVariadic): void
*
* @see https://github.com/nikic/PHP-Parser/blob/1.x/test/code/parser/stmt/function/generator.test
*
* @return list<array{0: string, 1: bool}>
* @return list<array{0: non-empty-string, 1: bool}>
*/
public function generatorProvider(): array
{
Expand All @@ -238,6 +240,8 @@ public function generatorProvider(): array
}

/**
* @param non-empty-string $php
*
* @dataProvider generatorProvider
*/
public function testIsGenerator(string $php, bool $expectingGenerator): void
Expand All @@ -249,7 +253,7 @@ public function testIsGenerator(string $php, bool $expectingGenerator): void
}

/**
* @return list<array{0: string, 1: int, 2: int}>
* @return list<array{0: non-empty-string, 1: int, 2: int}>
*/
public function startEndLineProvider(): array
{
Expand All @@ -261,6 +265,8 @@ public function startEndLineProvider(): array
}

/**
* @param non-empty-string $php
*
* @dataProvider startEndLineProvider
*/
public function testStartEndLine(string $php, int $expectedStart, int $expectedEnd): void
Expand All @@ -273,7 +279,7 @@ public function testStartEndLine(string $php, int $expectedStart, int $expectedE
}

/**
* @return list<array{0: string, 1: int, 2: int}>
* @return list<array{0: non-empty-string, 1: int, 2: int}>
*/
public function columnsProvider(): array
{
Expand All @@ -285,6 +291,8 @@ public function columnsProvider(): array
}

/**
* @param non-empty-string $php
*
* @dataProvider columnsProvider
*/
public function testGetStartColumnAndEndColumn(string $php, int $startColumn, int $endColumn): void
Expand All @@ -297,7 +305,7 @@ public function testGetStartColumnAndEndColumn(string $php, int $startColumn, in
}

/**
* @return list<array{0: string, 1: bool}>
* @return list<array{0: non-empty-string, 1: bool}>
*/
public function returnsReferenceProvider(): array
{
Expand All @@ -308,6 +316,8 @@ public function returnsReferenceProvider(): array
}

/**
* @param non-empty-string $php
*
* @dataProvider returnsReferenceProvider
*/
public function testReturnsReference(string $php, bool $expectingReturnsReference): void
Expand Down
4 changes: 3 additions & 1 deletion test/unit/Reflection/ReflectionParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ public function testGetClassFromObjectTypeHintedProperty(): void
}

/**
* @return list<array{0: string, 1: int, 2: int}>
* @return list<array{0: non-empty-string, 1: int, 2: int}>
*/
public function columnsProvider(): array
{
Expand All @@ -727,6 +727,8 @@ public function columnsProvider(): array
}

/**
* @param non-empty-string $php
*
* @dataProvider columnsProvider
*/
public function testGetStartColumnAndEndColumn(string $php, int $startColumn, int $endColumn): void
Expand Down
8 changes: 6 additions & 2 deletions test/unit/Reflection/ReflectionPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ public function testCannotClone(): void
}

/**
* @param non-empty-string $php
*
* @dataProvider startEndLineProvider
*/
public function testStartEndLine(string $php, int $startLine, int $endLine): void
Expand All @@ -289,7 +291,7 @@ public function testStartEndLine(string $php, int $startLine, int $endLine): voi
}

/**
* @return list<array{0: string, 1: int, 2: int}>
* @return list<array{0: non-empty-string, 1: int, 2: int}>
*/
public function startEndLineProvider(): array
{
Expand All @@ -302,7 +304,7 @@ public function startEndLineProvider(): array
}

/**
* @return list<array{0: string, 1: int, 2: int}>
* @return list<array{0: non-empty-string, 1: int, 2: int}>
*/
public function columnsProvider(): array
{
Expand All @@ -314,6 +316,8 @@ public function columnsProvider(): array
}

/**
* @param non-empty-string $php
*
* @dataProvider columnsProvider
*/
public function testGetStartColumnAndEndColumn(string $php, int $startColumn, int $endColumn): void
Expand Down
7 changes: 0 additions & 7 deletions test/unit/SourceLocator/Type/StringSourceLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Roave\BetterReflection\Identifier\IdentifierType;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Ast\Locator;
use Roave\BetterReflection\SourceLocator\Exception\EmptyPhpSourceCode;
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
use Roave\BetterReflectionTest\BetterReflectionSingleton;

Expand Down Expand Up @@ -64,10 +63,4 @@ public function testReturnsReflectionWhenSourceHasClass(): void

self::assertSame('Foo', $reflectionClass->getName());
}

public function testConstructorThrowsExceptionIfEmptyStringGiven(): void
{
$this->expectException(EmptyPhpSourceCode::class);
new StringSourceLocator('', $this->astLocator);
}
}

0 comments on commit e10a503

Please sign in to comment.