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 all 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
22 changes: 22 additions & 0 deletions src/Reflection/BetterReflection/BetterReflectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Reflection\BetterReflection;

use Closure;
use Nette\Utils\Strings;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\BetterReflection\Identifier\Exception\InvalidIdentifierName;
Expand Down Expand Up @@ -43,6 +44,7 @@
use PHPStan\Reflection\SignatureMap\NativeFunctionReflectionProvider;
use PHPStan\Reflection\SignatureMap\SignatureMapProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -360,11 +362,31 @@ public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn
$constantReflection = $this->reflector->reflectConstant($constantName);
$fileName = $constantReflection->getFileName();
$constantValueType = $this->initializerExprTypeResolver->getType($constantReflection->getValueExpression(), InitializerExprContext::fromGlobalConstant($constantReflection));
$docComment = $constantReflection->getDocComment();

$isDeprecated = TrinaryLogic::createNo();
$deprecatedDescription = null;
if ($docComment !== null) {
$resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc($fileName, null, null, null, $docComment);
$isDeprecated = TrinaryLogic::createFromBoolean($resolvedPhpDoc->isDeprecated());

if ($resolvedPhpDoc->isDeprecated() && $resolvedPhpDoc->getDeprecatedTag() !== null) {
$deprecatedMessage = $resolvedPhpDoc->getDeprecatedTag()->getMessage();

// filter raw version number messages like in
// https://github.com/JetBrains/phpstorm-stubs/blob/9608c953230b08f07b703ecfe459cc58d5421437/filter/filter.php#L478
if (Strings::match($deprecatedMessage ?? '', '#^\d+\.\d+(\.\d+)?$#') === null) {
$deprecatedDescription = $deprecatedMessage;
}
}
}

return $this->cachedConstants[$constantName] = new RuntimeConstantReflection(
$constantName,
$constantValueType,
$fileName,
$isDeprecated,
$deprecatedDescription,
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Reflection/Constant/RuntimeConstantReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public function __construct(
private string $name,
private Type $valueType,
private ?string $fileName,
private TrinaryLogic $isDeprecated,
private ?string $deprecatedDescription,
)
{
}
Expand All @@ -34,12 +36,12 @@ public function getFileName(): ?string

public function isDeprecated(): TrinaryLogic
{
return TrinaryLogic::createNo();
return $this->isDeprecated;
}

public function getDeprecatedDescription(): ?string
{
return null;
return $this->deprecatedDescription;
}

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

namespace PHPStan\Reflection\Constant;

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

class RuntimeConstantReflectionTest extends PHPStanTestCase
{

public function dataDeprecatedConstants(): iterable
{
if (PHP_VERSION_ID >= 80100) {
yield [
new Name('\FILTER_SANITIZE_STRING'),
TrinaryLogic::createYes(),
null,
];
}
yield [
new Name('\CURLOPT_FTP_SSL'),
TrinaryLogic::createYes(),
'use <b>CURLOPT_USE_SSL</b> instead.',
];

yield [
new Name('\DeprecatedConst\FINE'),
TrinaryLogic::createNo(),
null,
];
yield [
new Name('\DeprecatedConst\MY_CONST'),
TrinaryLogic::createYes(),
null,
];
yield [
new Name('\DeprecatedConst\MY_CONST2'),
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';