Skip to content

Commit

Permalink
Fixed getting class constants PHPDoc from wrong file
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 16, 2021
1 parent c5ad971 commit 6d52302
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Reflection/ClassReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
}

}
11 changes: 11 additions & 0 deletions tests/PHPStan/Reflection/data/IRouter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace WrongClassConstantFile;

interface IRouter
{

/** @deprecated */
const SECURED = 's';

}
8 changes: 8 additions & 0 deletions tests/PHPStan/Reflection/data/SecuredRouter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace WrongClassConstantFile;

class SecuredRouter implements IRouter
{

}

0 comments on commit 6d52302

Please sign in to comment.