Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringSourceLocator is forced to be built with a non-empty-string #1138

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -20,19 +19,10 @@
*/
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
16 changes: 12 additions & 4 deletions test/unit/Reflection/ReflectionClassConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ public function testGetDeclaringClass(): void
self::assertSame($classInfo, $const->getDeclaringClass());
}

/** @dataProvider startEndLineProvider */
/**
* @param non-empty-string $php
*
* @dataProvider startEndLineProvider
*/
public function testStartEndLine(string $php, int $startLine, int $endLine): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
Expand All @@ -136,7 +140,7 @@ public function testStartEndLine(string $php, int $startLine, int $endLine): voi
self::assertEquals($endLine, $constReflection->getEndLine());
}

/** @return list<array{0: string, 1: int, 2: int}> */
/** @return list<array{0: non-empty-string, 1: int, 2: int}> */
public function startEndLineProvider(): array
{
return [
Expand All @@ -147,7 +151,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
{
return [
Expand All @@ -157,7 +161,11 @@ public function columnsProvider(): array
];
}

/** @dataProvider columnsProvider */
/**
* @param non-empty-string $php
*
* @dataProvider columnsProvider
*/
public function testGetStartColumnAndEndColumn(string $php, int $startColumn, int $endColumn): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
Expand Down
16 changes: 12 additions & 4 deletions test/unit/Reflection/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ public function testGetParentClassNames(): void
self::assertSame(['Roave\\BetterReflectionTest\\Fixture\\ClassWithParent', 'Roave\\BetterReflectionTest\\Fixture\\ExampleClass'], $childReflection->getParentClassNames());
}

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

/** @dataProvider startEndLineProvider */
/**
* @param non-empty-string $php
*
* @dataProvider startEndLineProvider
*/
public function testStartEndLine(string $php, int $expectedStart, int $expectedEnd): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
Expand All @@ -834,7 +838,7 @@ public function testStartEndLine(string $php, int $expectedStart, int $expectedE
self::assertSame($expectedEnd, $classInfo->getEndLine());
}

/** @return list<array{0: string, 1: int, 2: int}> */
/** @return list<array{0: non-empty-string, 1: int, 2: int}> */
public function columnsProvider(): array
{
return [
Expand All @@ -844,7 +848,11 @@ public function columnsProvider(): array
];
}

/** @dataProvider columnsProvider */
/**
* @param non-empty-string $php
*
* @dataProvider columnsProvider
*/
public function testGetStartColumnAndEndColumn(string $php, int $startColumn, int $endColumn): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
Expand Down
16 changes: 12 additions & 4 deletions test/unit/Reflection/ReflectionConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public function testGetDocCommentByDefine(): void
self::assertStringContainsString('This constant comment should be used.', $reflection->getDocComment());
}

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

/** @dataProvider startEndLineProvider */
/**
* @param non-empty-string $php
*
* @dataProvider startEndLineProvider
*/
public function testStartEndLine(string $php, int $expectedStart, int $expectedEnd): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
Expand All @@ -275,7 +279,7 @@ public function testStartEndLine(string $php, int $expectedStart, int $expectedE
self::assertSame($expectedEnd, $reflection->getEndLine());
}

/** @return list<array{0: string, 1: int, 2: int}> */
/** @return list<array{0: non-empty-string, 1: int, 2: int}> */
public function columnsProvider(): array
{
return [
Expand All @@ -284,7 +288,11 @@ public function columnsProvider(): array
];
}

/** @dataProvider columnsProvider */
/**
* @param non-empty-string $php
*
* @dataProvider columnsProvider
*/
public function testGetStartColumnAndEndColumn(string $php, int $startColumn, int $endColumn): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
Expand Down
40 changes: 30 additions & 10 deletions test/unit/Reflection/ReflectionFunctionAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function testIsInternal(): void
self::assertNull($function->getExtensionName());
}

/** @return list<array{0: string, 1: bool}> */
/** @return list<array{0: non-empty-string, 1: bool}> */
public function variadicProvider(): array
{
return [
Expand All @@ -187,7 +187,11 @@ public function variadicProvider(): array
];
}

/** @dataProvider variadicProvider */
/**
* @param non-empty-string $php
*
* @dataProvider variadicProvider
*/
public function testIsVariadic(string $php, bool $expectingVariadic): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
Expand All @@ -202,7 +206,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 @@ -227,7 +231,11 @@ public function generatorProvider(): array
];
}

/** @dataProvider generatorProvider */
/**
* @param non-empty-string $php
*
* @dataProvider generatorProvider
*/
public function testIsGenerator(string $php, bool $expectingGenerator): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
Expand All @@ -236,7 +244,7 @@ public function testIsGenerator(string $php, bool $expectingGenerator): void
self::assertSame($expectingGenerator, $function->isGenerator());
}

/** @return list<array{0: string, 1: int, 2: int}> */
/** @return list<array{0: non-empty-string, 1: int, 2: int}> */
public function startEndLineProvider(): array
{
return [
Expand All @@ -246,7 +254,11 @@ public function startEndLineProvider(): array
];
}

/** @dataProvider startEndLineProvider */
/**
* @param non-empty-string $php
*
* @dataProvider startEndLineProvider
*/
public function testStartEndLine(string $php, int $expectedStart, int $expectedEnd): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
Expand All @@ -256,7 +268,7 @@ public function testStartEndLine(string $php, int $expectedStart, int $expectedE
self::assertSame($expectedEnd, $function->getEndLine());
}

/** @return list<array{0: string, 1: int, 2: int}> */
/** @return list<array{0: non-empty-string, 1: int, 2: int}> */
public function columnsProvider(): array
{
return [
Expand All @@ -266,7 +278,11 @@ public function columnsProvider(): array
];
}

/** @dataProvider columnsProvider */
/**
* @param non-empty-string $php
*
* @dataProvider columnsProvider
*/
public function testGetStartColumnAndEndColumn(string $php, int $startColumn, int $endColumn): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
Expand All @@ -276,7 +292,7 @@ public function testGetStartColumnAndEndColumn(string $php, int $startColumn, in
self::assertSame($endColumn, $function->getEndColumn());
}

/** @return list<array{0: string, 1: bool}> */
/** @return list<array{0: non-empty-string, 1: bool}> */
public function returnsReferenceProvider(): array
{
return [
Expand All @@ -285,7 +301,11 @@ public function returnsReferenceProvider(): array
];
}

/** @dataProvider returnsReferenceProvider */
/**
* @param non-empty-string $php
*
* @dataProvider returnsReferenceProvider
*/
public function testReturnsReference(string $php, bool $expectingReturnsReference): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
Expand Down
8 changes: 6 additions & 2 deletions test/unit/Reflection/ReflectionParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ public function testGetClassFromObjectTypeHintedProperty(): void
self::assertSame('object', $type->__toString());
}

/** @return list<array{0: string, 1: int, 2: int}> */
/** @return list<array{0: non-empty-string, 1: int, 2: int}> */
public function columnsProvider(): array
{
return [
Expand All @@ -698,7 +698,11 @@ public function columnsProvider(): array
];
}

/** @dataProvider columnsProvider */
/**
* @param non-empty-string $php
*
* @dataProvider columnsProvider
*/
public function testGetStartColumnAndEndColumn(string $php, int $startColumn, int $endColumn): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
Expand Down
16 changes: 12 additions & 4 deletions test/unit/Reflection/ReflectionPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,11 @@ public function testCannotClone(): void
clone $publicProp;
}

/** @dataProvider startEndLineProvider */
/**
* @param non-empty-string $php
*
* @dataProvider startEndLineProvider
*/
public function testStartEndLine(string $php, int $startLine, int $endLine): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
Expand All @@ -278,7 +282,7 @@ public function testStartEndLine(string $php, int $startLine, int $endLine): voi
self::assertEquals($endLine, $constReflection->getEndLine());
}

/** @return list<array{0: string, 1: int, 2: int}> */
/** @return list<array{0: non-empty-string, 1: int, 2: int}> */
public function startEndLineProvider(): array
{
return [
Expand All @@ -289,7 +293,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
{
return [
Expand All @@ -299,7 +303,11 @@ public function columnsProvider(): array
];
}

/** @dataProvider columnsProvider */
/**
* @param non-empty-string $php
*
* @dataProvider columnsProvider
*/
public function testGetStartColumnAndEndColumn(string $php, int $startColumn, int $endColumn): void
{
$reflector = new DefaultReflector(new StringSourceLocator($php, $this->astLocator));
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 @@ -62,10 +61,4 @@ public function testReturnsReflectionWhenSourceHasClass(): void

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

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