From 6d523028e399c15dc77aec3affd2ea97ff735925 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 16 Mar 2021 10:00:00 +0100 Subject: [PATCH] Fixed getting class constants PHPDoc from wrong file --- src/Reflection/ClassReflection.php | 9 +++++---- tests/PHPStan/Reflection/ClassReflectionTest.php | 9 +++++++++ tests/PHPStan/Reflection/data/IRouter.php | 11 +++++++++++ tests/PHPStan/Reflection/data/SecuredRouter.php | 8 ++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 tests/PHPStan/Reflection/data/IRouter.php create mode 100644 tests/PHPStan/Reflection/data/SecuredRouter.php diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index a3bbb1663c..2ed3641c19 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -691,10 +691,11 @@ public function getConstant(string $name): ConstantReflection $deprecatedDescription = null; $isDeprecated = false; $isInternal = false; - if ($reflectionConstant->getDocComment() !== false && $this->getFileName() !== false) { + $declaringClass = $reflectionConstant->getDeclaringClass(); + $fileName = $declaringClass->getFileName(); + if ($reflectionConstant->getDocComment() !== false && $fileName !== false) { $docComment = $reflectionConstant->getDocComment(); - $fileName = $this->getFileName(); - $className = $reflectionConstant->getDeclaringClass()->getName(); + $className = $declaringClass->getName(); $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc($fileName, $className, null, null, $docComment); $deprecatedDescription = $resolvedPhpDoc->getDeprecatedTag() !== null ? $resolvedPhpDoc->getDeprecatedTag()->getMessage() : null; @@ -703,7 +704,7 @@ public function getConstant(string $name): ConstantReflection } $this->constants[$name] = new ClassConstantReflection( - $this->reflectionProvider->getClass($reflectionConstant->getDeclaringClass()->getName()), + $this->reflectionProvider->getClass($declaringClass->getName()), $reflectionConstant, $deprecatedDescription, $isDeprecated, diff --git a/tests/PHPStan/Reflection/ClassReflectionTest.php b/tests/PHPStan/Reflection/ClassReflectionTest.php index bf5c66ac59..25251fc4cf 100644 --- a/tests/PHPStan/Reflection/ClassReflectionTest.php +++ b/tests/PHPStan/Reflection/ClassReflectionTest.php @@ -9,6 +9,7 @@ use PHPStan\Broker\Broker; use PHPStan\Php\PhpVersion; use PHPStan\Type\FileTypeMapper; +use WrongClassConstantFile\SecuredRouter; class ClassReflectionTest extends \PHPStan\Testing\TestCase { @@ -211,4 +212,12 @@ public function testIsAttributeClass(string $className, bool $expected, int $exp $this->assertSame($expectedFlags, $reflection->getAttributeClassFlags()); } + public function testDeprecatedConstantFromAnotherFile(): void + { + $reflectionProvider = $this->createBroker(); + $reflection = $reflectionProvider->getClass(SecuredRouter::class); + $constant = $reflection->getConstant('SECURED'); + $this->assertTrue($constant->isDeprecated()->yes()); + } + } diff --git a/tests/PHPStan/Reflection/data/IRouter.php b/tests/PHPStan/Reflection/data/IRouter.php new file mode 100644 index 0000000000..184e235b4e --- /dev/null +++ b/tests/PHPStan/Reflection/data/IRouter.php @@ -0,0 +1,11 @@ +