Skip to content

Commit

Permalink
implement getDeprecatedDescription()
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Mar 3, 2024
1 parent 13f039d commit 40be91f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn
$constantName,
$constantValueType,
$fileName,
$this->fileTypeMapper,
$constantReflection->getDocComment(),
);
}
Expand Down
37 changes: 34 additions & 3 deletions src/Reflection/Constant/RuntimeConstantReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@

namespace PHPStan\Reflection\Constant;

use PHPStan\BetterReflection\Reflection\Annotation\AnnotationHelper;
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\Reflection\GlobalConstantReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\Type;

class RuntimeConstantReflection implements GlobalConstantReflection
{

private false|ResolvedPhpDocBlock $resolvedPhpDocBlock = false;

private ?string $deprecatedDescription = null;

/**
* @param non-empty-string|null $docComment
*/
public function __construct(
private string $name,
private Type $valueType,
private ?string $fileName,
private FileTypeMapper $fileTypeMapper,
private ?string $docComment,
)
{
Expand All @@ -39,12 +45,24 @@ public function getFileName(): ?string

public function isDeprecated(): TrinaryLogic
{
return TrinaryLogic::createFromBoolean(AnnotationHelper::isDeprecated($this->getDocComment()));
$resolvedPhpDoc = $this->getResolvedPhpDoc();
if ($resolvedPhpDoc === null) {
return TrinaryLogic::createNo();
}

return TrinaryLogic::createFromBoolean($resolvedPhpDoc->isDeprecated());
}

public function getDeprecatedDescription(): ?string
{
return null;
if ($this->deprecatedDescription === null && $this->isDeprecated()->yes()) {
$resolvedPhpDoc = $this->getResolvedPhpDoc();
if ($resolvedPhpDoc !== null && $resolvedPhpDoc->getDeprecatedTag() !== null) {
$this->deprecatedDescription = $resolvedPhpDoc->getDeprecatedTag()->getMessage();
}
}

return $this->deprecatedDescription;
}

/** @return non-empty-string|null */
Expand All @@ -53,6 +71,19 @@ public function getDocComment(): ?string
return $this->docComment;
}

private function getResolvedPhpDoc(): ?ResolvedPhpDocBlock
{
if ($this->docComment === null) {
return null;
}

if ($this->resolvedPhpDocBlock !== false) {
return $this->resolvedPhpDocBlock;
}

return $this->resolvedPhpDocBlock = $this->fileTypeMapper->getResolvedPhpDoc($this->getFileName(), null, null, null, $this->docComment);
}

public function isInternal(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection\Constant;

use PhpParser\Node\Name;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\TrinaryLogic;

class RuntimeConstantReflectionTest extends PHPStanTestCase
{

public function dataDeprecatedConstants(): array
{
return [
[
new Name('\DeprecatedConst\FINE'),
TrinaryLogic::createNo(),
null,
],
[
new Name('\DeprecatedConst\MY_CONST'),
TrinaryLogic::createYes(),
null,
],
[
new Name('\DeprecatedConst\MY_CONST2'),
TrinaryLogic::createYes(),
"don't use it!",
],
];
}

/**
* @dataProvider dataDeprecatedConstants
*/
public function testDeprecatedConstants(Name $constName, TrinaryLogic $isDeprecated, ?string $deprecationMessage): void
{
require_once __DIR__ . '/data/deprecated-constant.php';

$reflectionProvider = $this->createReflectionProvider();

$this->assertTrue($reflectionProvider->hasConstant($constName, null));
$this->assertSame($isDeprecated, $reflectionProvider->getConstant($constName, null)->isDeprecated());
$this->assertSame($deprecationMessage, $reflectionProvider->getConstant($constName, null)->getDeprecatedDescription());
}

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

namespace DeprecatedConst;

const FINE = '1';

/**
* @deprecated
*/
const MY_CONST = '1';

/**
* @deprecated don't use it!
*/
const MY_CONST2 = '1';

0 comments on commit 40be91f

Please sign in to comment.