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 5 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
15 changes: 15 additions & 0 deletions src/Reflection/BetterReflection/BetterReflectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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 +361,25 @@ 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) {
$deprecatedDescription = $resolvedPhpDoc->getDeprecatedTag()->getMessage();
}
}

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(),
'8.1', // deprecation message used in e.g. https://github.com/JetBrains/phpstorm-stubs/blob/9608c953230b08f07b703ecfe459cc58d5421437/filter/filter.php#L478
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to get rid of it, right. If the message === X.Y or X.Y.Z, we can remove it using regex

Copy link
Contributor Author

@staabm staabm Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filtered it out using regex.

looking into the CI errors https://github.com/phpstan/phpstan-src/actions/runs/8152892522/job/22283354469?pr=2953 I got the idea, we could also turn the deprecation-message into Use of constant %s is deprecated since PHP %s. instead of removing it completly. wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could also turn the deprecation-message into Use of constant %s is deprecated since PHP %s. instead of removing it completly. wdyt?

We don't do this for any other feature, I don't think it's worth it. Passing different phpVersion or simply analysing it on phpstan.org/try makes it obvious what's going on.

];
}
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';