Skip to content

Commit

Permalink
PhpStormStubsSourceStubber - report class filename
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 2, 2021
1 parent 67c3efa commit e416d4d
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ private function addEnumMethods(array $methods): array
return $methods;
}

$internalLocatedSource = new InternalLocatedSource('', $this->getName(), 'Core');
$internalLocatedSource = new InternalLocatedSource('', $this->getName(), 'Core', $this->getFileName());
$createMethod = fn (string $name, array $params, Node\Identifier|Node\NullableType $returnType): ReflectionMethod => ReflectionMethod::createFromNode(
$this->reflector,
new ClassMethod(
Expand Down
4 changes: 2 additions & 2 deletions src/SourceLocator/Located/InternalLocatedSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
*/
class InternalLocatedSource extends LocatedSource
{
public function __construct(string $source, string $name, private string $extensionName)
public function __construct(string $source, string $name, private string $extensionName, ?string $fileName = null)
{
parent::__construct($source, $name);
parent::__construct($source, $name, $fileName);
}

public function isInternal(): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public function generateClassStub(string $className): ?StubData
$stub = str_replace('PS_UNRESERVE_PREFIX_throw', 'throw', $stub);
}

return new StubData($stub, $extension);
return new StubData($stub, $extension, $this->getAbsoluteFilePath($filePath));
}

public function generateFunctionStub(string $functionName): ?StubData
Expand All @@ -302,7 +302,7 @@ public function generateFunctionStub(string $functionName): ?StubData

$extension = $this->getExtensionFromFilePath($filePath);

return new StubData($this->createStub($functionNode), $extension);
return new StubData($this->createStub($functionNode), $extension, $this->getAbsoluteFilePath($filePath));
}

public function generateConstantStub(string $constantName): ?StubData
Expand Down Expand Up @@ -338,7 +338,7 @@ public function generateConstantStub(string $constantName): ?StubData

$extension = $this->getExtensionFromFilePath($filePath);

return new StubData($this->createStub($constantNode), $extension);
return new StubData($this->createStub($constantNode), $extension, $this->getAbsoluteFilePath($filePath));
}

private function parseFile(string $filePath): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,6 @@ private function generateStub(Node $node): string

private function createStubData(string $stub, ?string $extensionName): StubData
{
return new StubData($stub, $extensionName);
return new StubData($stub, $extensionName, null);
}
}
7 changes: 6 additions & 1 deletion src/SourceLocator/SourceStubber/StubData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class StubData
{
public function __construct(private string $stub, private ?string $extensionName)
public function __construct(private string $stub, private ?string $extensionName, private ?string $fileName)
{
}

Expand All @@ -22,4 +22,9 @@ public function getExtensionName(): ?string
{
return $this->extensionName;
}

public function getFileName(): ?string
{
return $this->fileName;
}
}
5 changes: 5 additions & 0 deletions src/SourceLocator/Type/EvaledCodeSourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ protected function createLocatedSource(Identifier $identifier): ?LocatedSource
return null;
}

$fileName = null;
if ($classReflection->getFileName() !== false) {
$fileName = $classReflection->getFileName();
}

return new EvaledLocatedSource($stubData->getStub(), $classReflection->getName());
}

Expand Down
1 change: 1 addition & 0 deletions src/SourceLocator/Type/PhpInternalSourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private function createLocatedSourceFromStubData(Identifier $identifier, ?StubDa
$stubData->getStub(),
$identifier->getName(),
$extensionName,
$stubData->getFileName(),
);
}
}
17 changes: 9 additions & 8 deletions test/unit/NodeCompiler/CompileNodeToValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Roave\BetterReflectionTest\Fixture\MagicConstantsTrait;

use function define;
use function preg_quote;
use function realpath;
use function sprintf;
use function uniqid;
Expand Down Expand Up @@ -204,9 +205,9 @@ public function testVariousNodeCompilations(string $phpCode, mixed $expectedValu
public function testExceptionThrownWhenInvalidNodeGiven(): void
{
$this->expectException(UnableToCompileNode::class);
$this->expectExceptionMessage(sprintf(
'Unable to compile expression in global namespace: unrecognized node type %s in file "" (line -1)',
Yield_::class,
$this->expectExceptionMessageMatches(sprintf(
'#^Unable to compile expression in global namespace: unrecognized node type %s in file#',
preg_quote(Yield_::class, '#'),
));

(new CompileNodeToValue())->__invoke(new Yield_(), $this->getDummyContextWithGlobalNamespace());
Expand All @@ -215,15 +216,15 @@ public function testExceptionThrownWhenInvalidNodeGiven(): void
public function testExceptionThrownWhenUndefinedConstUsed(): void
{
$this->expectException(UnableToCompileNode::class);
$this->expectExceptionMessage('Could not locate constant "FOO" while evaluating expression in global namespace in file "" (line -1)');
$this->expectExceptionMessageMatches('#^Could not locate constant "FOO" while evaluating expression in global namespace in file#');

(new CompileNodeToValue())->__invoke(new ConstFetch(new Name('FOO')), $this->getDummyContextWithGlobalNamespace());
}

public function testExceptionThrownWhenUndefinedClassConstUsed(): void
{
$this->expectException(UnableToCompileNode::class);
$this->expectExceptionMessage('Could not locate constant EmptyClass::FOO while trying to evaluate constant expression in global namespace in file "" (line -1)');
$this->expectExceptionMessageMatches('#^Could not locate constant EmptyClass::FOO while trying to evaluate constant expression in global namespace in file#');

(new CompileNodeToValue())
->__invoke(
Expand Down Expand Up @@ -509,11 +510,11 @@ public function testSelfStaticOrParentAsPropertyDefaultValue(): void
{
$phpCode = <<<'PHP'
<?php
class Baz {
const PARENT_CONSTANT = 'parentConstant';
}
class Foo extends Baz {
const SELF_CONSTANT = 'selfConstant';
const STATIC_CONSTANT = 'staticConstant';
Expand All @@ -522,7 +523,7 @@ class Foo extends Baz {
public $selfClass = self::class;
public $staticClass = static::class;
public $parentClass = parent::class;
public $selfConstant = self::SELF_CONSTANT;
public $staticConstant = self::STATIC_CONSTANT;
public $parentConstant = parent::PARENT_CONSTANT;
Expand Down
16 changes: 8 additions & 8 deletions test/unit/NodeCompiler/Exception/UnableToCompileNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public function testBecauseOfInitializer(CompilerContext $context, string $conte
new New_(new Name('SomeClass')),
);

self::assertSame(
self::assertStringContainsString(
sprintf(
'Unable to compile initializer in %s in file "" (line -1)',
'Unable to compile initializer in %s in file',
$contextName,
),
$exception->getMessage(),
Expand All @@ -64,9 +64,9 @@ public function testBecauseOfNotFoundConstantReference(CompilerContext $context,
$constantName,
);

self::assertSame(
self::assertStringContainsString(
sprintf(
'Could not locate constant "%s" while evaluating expression in %s in file "" (line -1)',
'Could not locate constant "%s" while evaluating expression in %s in file',
$constantName,
$contextName,
),
Expand All @@ -86,9 +86,9 @@ public function testBecauseOfNotFoundClassConstantReference(CompilerContext $con
->method('getName')
->willReturn('An\\Example');

self::assertSame(
self::assertStringContainsString(
sprintf(
'Could not locate constant An\Example::SOME_CONSTANT while trying to evaluate constant expression in %s in file "" (line -1)',
'Could not locate constant An\Example::SOME_CONSTANT while trying to evaluate constant expression in %s in file',
$contextName,
),
UnableToCompileNode::becauseOfNotFoundClassConstantReference(
Expand All @@ -105,9 +105,9 @@ public function testBecauseOfNotFoundClassConstantReference(CompilerContext $con
/** @dataProvider supportedContextTypes */
public function testForUnRecognizedExpressionInContext(CompilerContext $context, string $contextName): void
{
self::assertSame(
self::assertStringContainsString(
sprintf(
'Unable to compile expression in %s: unrecognized node type %s in file "" (line -1)',
'Unable to compile expression in %s: unrecognized node type %s in file',
$contextName,
Yield_::class,
),
Expand Down
2 changes: 1 addition & 1 deletion test/unit/SourceLocator/Ast/LocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function testReflectingTopLevelConstantByConst(): void

$constantInfo = $this->locator->findReflection(
new DefaultReflector(new StringSourceLocator($php, $this->locator)),
new LocatedSource($php, 'FOO'),
new LocatedSource($php, 'FOO', null),
$this->getIdentifier('FOO', IdentifierType::IDENTIFIER_CONSTANT),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testTraverseAllGivenSourceStubbersAndSucceedToGenerateClassStub(
$sourceStubber3 = $this->createMock(SourceStubber::class);
$sourceStubber4 = $this->createMock(SourceStubber::class);

$stubData = new StubData('<?php class SomeClass {}', null);
$stubData = new StubData('<?php class SomeClass {}', null, null);

$sourceStubber1->expects($this->once())->method('generateClassStub');
$sourceStubber2->expects($this->once())->method('generateClassStub');
Expand Down Expand Up @@ -68,7 +68,7 @@ public function testTraverseAllGivenSourceStubbersAndSucceedToGenerateFunctionSt
$sourceStubber3 = $this->createMock(SourceStubber::class);
$sourceStubber4 = $this->createMock(SourceStubber::class);

$stubData = new StubData('<?php function someFunction () {}', null);
$stubData = new StubData('<?php function someFunction () {}', null, null);

$sourceStubber1->expects($this->once())->method('generateFunctionStub');
$sourceStubber2->expects($this->once())->method('generateFunctionStub');
Expand Down Expand Up @@ -104,7 +104,7 @@ public function testTraverseAllGivenSourceStubbersAndSucceedToGenerateConstantSt
$sourceStubber3 = $this->createMock(SourceStubber::class);
$sourceStubber4 = $this->createMock(SourceStubber::class);

$stubData = new StubData('<?php const SOME_CONSTANT = 1;', null);
$stubData = new StubData('<?php const SOME_CONSTANT = 1;', null, null);

$sourceStubber1->expects($this->once())->method('generateConstantStub');
$sourceStubber2->expects($this->once())->method('generateConstantStub');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1158,4 +1158,10 @@ public function testModifiedStubForGeneratorClass(): void
self::assertInstanceOf(ReflectionClass::class, $classReflection);
self::assertTrue($classReflection->hasMethod('throw'));
}

public function testFilename(): void
{
$reflection = $this->reflector->reflectClass('XMLReader');
$this->assertSame(realpath(__DIR__ . '/../../../../vendor/jetbrains/phpstorm-stubs/xmlreader/xmlreader.php'), realpath($reflection->getFileName()));
}
}
2 changes: 1 addition & 1 deletion test/unit/SourceLocator/SourceStubber/StubDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testGetters(): void
{
$stub = '<?php';
$extensionName = 'Core';
$stubData = new StubData($stub, $extensionName);
$stubData = new StubData($stub, $extensionName, null);

self::assertSame($stub, $stubData->getStub());
self::assertSame($extensionName, $stubData->getExtensionName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public function testReturnsNullForNonInternal(): void
$sourceStubber
->method('generateClassStub')
->with('Foo')
->willReturn(new StubData('stub', null));
->willReturn(new StubData('stub', null, null));

$phpInternalSourceLocator = new PhpInternalSourceLocator(BetterReflectionSingleton::instance()->astLocator(), $sourceStubber);

Expand Down

0 comments on commit e416d4d

Please sign in to comment.