Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report uses of deprecated constants #2953

Merged
merged 6 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Reflection/BetterReflection/BetterReflectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn
$constantName,
$constantValueType,
$fileName,
$this->fileTypeMapper,
$constantReflection->getDocComment(),
);
}

Expand Down
46 changes: 44 additions & 2 deletions src/Reflection/Constant/RuntimeConstantReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@

namespace PHPStan\Reflection\Constant;

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,
staabm marked this conversation as resolved.
Show resolved Hide resolved
private ?string $docComment,
)
{
}
Expand All @@ -34,12 +45,43 @@ public function getFileName(): ?string

public function isDeprecated(): TrinaryLogic
{
return TrinaryLogic::createNo();
$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 */
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
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'),
staabm marked this conversation as resolved.
Show resolved Hide resolved
TrinaryLogic::createYes(),
"don't use it!",
],
];
}

/**
* @dataProvider dataDeprecatedConstants
*/
public function testDeprecatedConstants(Name $constName, TrinaryLogic $isDeprecated, ?string $deprecationMessage): void
staabm marked this conversation as resolved.
Show resolved Hide resolved
{
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';