Skip to content

Commit 6d52302

Browse files
committed
Fixed getting class constants PHPDoc from wrong file
1 parent c5ad971 commit 6d52302

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

src/Reflection/ClassReflection.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -691,10 +691,11 @@ public function getConstant(string $name): ConstantReflection
691691
$deprecatedDescription = null;
692692
$isDeprecated = false;
693693
$isInternal = false;
694-
if ($reflectionConstant->getDocComment() !== false && $this->getFileName() !== false) {
694+
$declaringClass = $reflectionConstant->getDeclaringClass();
695+
$fileName = $declaringClass->getFileName();
696+
if ($reflectionConstant->getDocComment() !== false && $fileName !== false) {
695697
$docComment = $reflectionConstant->getDocComment();
696-
$fileName = $this->getFileName();
697-
$className = $reflectionConstant->getDeclaringClass()->getName();
698+
$className = $declaringClass->getName();
698699
$resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc($fileName, $className, null, null, $docComment);
699700

700701
$deprecatedDescription = $resolvedPhpDoc->getDeprecatedTag() !== null ? $resolvedPhpDoc->getDeprecatedTag()->getMessage() : null;
@@ -703,7 +704,7 @@ public function getConstant(string $name): ConstantReflection
703704
}
704705

705706
$this->constants[$name] = new ClassConstantReflection(
706-
$this->reflectionProvider->getClass($reflectionConstant->getDeclaringClass()->getName()),
707+
$this->reflectionProvider->getClass($declaringClass->getName()),
707708
$reflectionConstant,
708709
$deprecatedDescription,
709710
$isDeprecated,

tests/PHPStan/Reflection/ClassReflectionTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Broker\Broker;
1010
use PHPStan\Php\PhpVersion;
1111
use PHPStan\Type\FileTypeMapper;
12+
use WrongClassConstantFile\SecuredRouter;
1213

1314
class ClassReflectionTest extends \PHPStan\Testing\TestCase
1415
{
@@ -211,4 +212,12 @@ public function testIsAttributeClass(string $className, bool $expected, int $exp
211212
$this->assertSame($expectedFlags, $reflection->getAttributeClassFlags());
212213
}
213214

215+
public function testDeprecatedConstantFromAnotherFile(): void
216+
{
217+
$reflectionProvider = $this->createBroker();
218+
$reflection = $reflectionProvider->getClass(SecuredRouter::class);
219+
$constant = $reflection->getConstant('SECURED');
220+
$this->assertTrue($constant->isDeprecated()->yes());
221+
}
222+
214223
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace WrongClassConstantFile;
4+
5+
interface IRouter
6+
{
7+
8+
/** @deprecated */
9+
const SECURED = 's';
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace WrongClassConstantFile;
4+
5+
class SecuredRouter implements IRouter
6+
{
7+
8+
}

0 commit comments

Comments
 (0)