Skip to content

Commit b3aa47d

Browse files
cs278ondrejmirtes
authored andcommitted
Rule to check for use of deprecated constants
1 parent 5a5b33f commit b3aa47d

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\ConstFetch;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Reflection\GlobalConstantReflection;
9+
use PHPStan\Reflection\ReflectionProvider;
10+
11+
/**
12+
* @implements \PHPStan\Rules\Rule<ConstFetch>
13+
*/
14+
class FetchingDeprecatedConstRule implements \PHPStan\Rules\Rule
15+
{
16+
17+
/** @var ReflectionProvider */
18+
private $reflectionProvider;
19+
20+
/** @var array<string,string> */
21+
private $deprecatedConstants = [
22+
'FILTER_FLAG_SCHEME_REQUIRED' => 'Use of constant %s is deprecated since PHP 7.3.',
23+
'FILTER_FLAG_HOST_REQUIRED' => 'Use of constant %s is deprecated since PHP 7.3.',
24+
];
25+
26+
public function __construct(ReflectionProvider $reflectionProvider)
27+
{
28+
$this->reflectionProvider = $reflectionProvider;
29+
}
30+
31+
public function getNodeType(): string
32+
{
33+
return ConstFetch::class;
34+
}
35+
36+
public function processNode(Node $node, Scope $scope): array
37+
{
38+
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
39+
return [];
40+
}
41+
42+
if (!$this->reflectionProvider->hasConstant($node->name, $scope)) {
43+
return [];
44+
}
45+
46+
$constantReflection = $this->reflectionProvider->getConstant($node->name, $scope);
47+
48+
if ($this->isDeprecated($constantReflection)) {
49+
return [sprintf(
50+
$this->deprecatedConstants[$constantReflection->getName()] ?? 'Use of constant %s is deprecated.',
51+
$constantReflection->getName()
52+
)];
53+
}
54+
55+
return [];
56+
}
57+
58+
private function isDeprecated(GlobalConstantReflection $constantReflection): bool
59+
{
60+
return $constantReflection->isDeprecated()->yes()
61+
|| isset($this->deprecatedConstants[$constantReflection->getName()]);
62+
}
63+
64+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
/**
6+
* @extends \PHPStan\Testing\RuleTestCase<FetchingDeprecatedConstRule>
7+
*/
8+
class FetchingDeprecatedConstRuleTest extends \PHPStan\Testing\RuleTestCase
9+
{
10+
11+
protected function getRule(): \PHPStan\Rules\Rule
12+
{
13+
return new FetchingDeprecatedConstRule($this->createReflectionProvider());
14+
}
15+
16+
public function testFetchingDeprecatedConst(): void
17+
{
18+
if (!defined('FILTER_FLAG_SCHEME_REQUIRED') || !defined('FILTER_FLAG_HOST_REQUIRED')) {
19+
$this->markTestSkipped('Required constants are not available, PHP≥8?');
20+
}
21+
22+
require_once __DIR__ . '/data/fetching-deprecated-const-definition.php';
23+
$this->analyse(
24+
[__DIR__ . '/data/fetching-deprecated-const.php'],
25+
[
26+
[
27+
'Use of constant FILTER_FLAG_SCHEME_REQUIRED is deprecated since PHP 7.3.',
28+
5,
29+
],
30+
[
31+
'Use of constant FILTER_FLAG_HOST_REQUIRED is deprecated since PHP 7.3.',
32+
6,
33+
],
34+
[
35+
'Use of constant FILTER_FLAG_SCHEME_REQUIRED is deprecated since PHP 7.3.',
36+
7,
37+
],
38+
[
39+
'Use of constant FILTER_FLAG_HOST_REQUIRED is deprecated since PHP 7.3.',
40+
8,
41+
],
42+
[
43+
'Use of constant FILTER_FLAG_SCHEME_REQUIRED is deprecated since PHP 7.3.',
44+
37,
45+
],
46+
[
47+
'Use of constant FILTER_FLAG_HOST_REQUIRED is deprecated since PHP 7.3.',
48+
38,
49+
],
50+
]
51+
);
52+
}
53+
54+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace FetchingDeprecatedConst\Redefined {
4+
5+
const FILTER_FLAG_SCHEME_REQUIRED = 'foo2';
6+
const FILTER_FLAG_HOST_REQUIRED = 'bar2';
7+
8+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace FetchingDeprecatedConst {
4+
5+
\FILTER_FLAG_SCHEME_REQUIRED;
6+
\FILTER_FLAG_HOST_REQUIRED;
7+
FILTER_FLAG_SCHEME_REQUIRED;
8+
FILTER_FLAG_HOST_REQUIRED;
9+
10+
/**
11+
* @deprecated
12+
*/
13+
function deprecated_scope()
14+
{
15+
\FILTER_FLAG_SCHEME_REQUIRED;
16+
\FILTER_FLAG_HOST_REQUIRED;
17+
}
18+
19+
/**
20+
* @deprecated nothing to see
21+
*/
22+
class deprecated_scope2
23+
{
24+
25+
public function test()
26+
{
27+
\FILTER_FLAG_SCHEME_REQUIRED;
28+
\FILTER_FLAG_HOST_REQUIRED;
29+
}
30+
31+
}
32+
33+
}
34+
35+
namespace FetchingDeprecatedConst\Redefined {
36+
37+
\FILTER_FLAG_SCHEME_REQUIRED;
38+
\FILTER_FLAG_HOST_REQUIRED;
39+
FILTER_FLAG_SCHEME_REQUIRED; // ok
40+
FILTER_FLAG_HOST_REQUIRED; // ok
41+
42+
}

0 commit comments

Comments
 (0)