-
Notifications
You must be signed in to change notification settings - Fork 426
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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6029f5e
Report uses of deprecated constants
staabm 13f039d
fix build
staabm 40be91f
implement getDeprecatedDescription()
staabm b49bde2
extract logic from RuntimeConstantReflection
staabm e51deb7
Update RuntimeConstantReflectionTest.php
staabm e7b57e5
filter raw version number messages
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
tests/PHPStan/Reflection/Constant/RuntimeConstantReflectionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; | ||
} | ||
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
15
tests/PHPStan/Reflection/Constant/data/deprecated-constant.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.