Skip to content

Commit 9d8e7d4

Browse files
committed
Implement RestrictedClassConstantUsageExtension
1 parent 9e96248 commit 9d8e7d4

6 files changed

+164
-159
lines changed

phpstan-baseline.neon

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
parameters:
22
ignoreErrors:
3+
-
4+
message: '#^Accessing PHPStan\\Rules\\Classes\\ClassConstantRule\:\:class is not covered by backward compatibility promise\. The class might change in a minor PHPStan version\.$#'
5+
identifier: phpstanApi.classConstant
6+
count: 1
7+
path: tests/Rules/ClassConstantRuleTest.php
8+
39
-
410
message: '#^Accessing PHPStan\\Rules\\Classes\\ExistingClassesInClassImplementsRule\:\:class is not covered by backward compatibility promise\. The class might change in a minor PHPStan version\.$#'
511
identifier: phpstanApi.classConstant

rules.neon

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ services:
1616
-
1717
class: PHPStan\Rules\Deprecations\CallWithDeprecatedIniOptionRule
1818

19+
-
20+
class: PHPStan\Rules\Deprecations\RestrictedDeprecatedClassConstantUsageExtension
21+
tags:
22+
- phpstan.restrictedClassConstantUsageExtension
23+
1924
-
2025
class: PHPStan\Rules\Deprecations\RestrictedDeprecatedFunctionUsageExtension
2126
tags:
@@ -39,7 +44,6 @@ services:
3944
- phpstan.restrictedClassNameUsageExtension
4045

4146
rules:
42-
- PHPStan\Rules\Deprecations\FetchingClassConstOfDeprecatedClassRule
4347
- PHPStan\Rules\Deprecations\FetchingDeprecatedConstRule
4448
- PHPStan\Rules\Deprecations\UsageOfDeprecatedCastRule
4549

src/Rules/Deprecations/FetchingClassConstOfDeprecatedClassRule.php

-139
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PHPStan\Analyser\Scope;
6+
use PHPStan\Reflection\ClassConstantReflection;
7+
use PHPStan\Rules\RestrictedUsage\RestrictedClassConstantUsageExtension;
8+
use PHPStan\Rules\RestrictedUsage\RestrictedUsage;
9+
use function sprintf;
10+
use function strtolower;
11+
12+
class RestrictedDeprecatedClassConstantUsageExtension implements RestrictedClassConstantUsageExtension
13+
{
14+
15+
private DeprecatedScopeHelper $deprecatedScopeHelper;
16+
17+
public function __construct(DeprecatedScopeHelper $deprecatedScopeHelper)
18+
{
19+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
20+
}
21+
22+
public function isRestrictedClassConstantUsage(
23+
ClassConstantReflection $constantReflection,
24+
Scope $scope
25+
): ?RestrictedUsage
26+
{
27+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
28+
return null;
29+
}
30+
31+
if ($constantReflection->getDeclaringClass()->isDeprecated()) {
32+
$class = $constantReflection->getDeclaringClass();
33+
$classDescription = $class->getDeprecatedDescription();
34+
if ($classDescription === null) {
35+
return RestrictedUsage::create(
36+
sprintf(
37+
'Fetching class constant %s of deprecated %s %s.',
38+
$constantReflection->getName(),
39+
strtolower($constantReflection->getDeclaringClass()->getClassTypeDescription()),
40+
$constantReflection->getDeclaringClass()->getName(),
41+
),
42+
sprintf(
43+
'classConstant.deprecated%s',
44+
$constantReflection->getDeclaringClass()->getClassTypeDescription(),
45+
),
46+
);
47+
}
48+
49+
return RestrictedUsage::create(
50+
sprintf(
51+
"Fetching class constant %s of deprecated %s %s:\n%s",
52+
$constantReflection->getName(),
53+
strtolower($constantReflection->getDeclaringClass()->getClassTypeDescription()),
54+
$constantReflection->getDeclaringClass()->getName(),
55+
$classDescription,
56+
),
57+
sprintf(
58+
'classConstant.deprecated%s',
59+
$constantReflection->getDeclaringClass()->getClassTypeDescription(),
60+
),
61+
);
62+
}
63+
64+
if (!$constantReflection->isDeprecated()->yes()) {
65+
return null;
66+
}
67+
68+
$description = $constantReflection->getDeprecatedDescription();
69+
if ($description === null) {
70+
return RestrictedUsage::create(
71+
sprintf(
72+
'Fetching deprecated class constant %s of %s %s.',
73+
$constantReflection->getName(),
74+
strtolower($constantReflection->getDeclaringClass()->getClassTypeDescription()),
75+
$constantReflection->getDeclaringClass()->getName(),
76+
),
77+
'classConstant.deprecated',
78+
);
79+
}
80+
81+
return RestrictedUsage::create(
82+
sprintf(
83+
"Fetching deprecated class constant %s of %s %s:\n%s",
84+
$constantReflection->getName(),
85+
strtolower($constantReflection->getDeclaringClass()->getClassTypeDescription()),
86+
$constantReflection->getDeclaringClass()->getName(),
87+
$description,
88+
),
89+
'classConstant.deprecated',
90+
);
91+
}
92+
93+
}

tests/Rules/ClassConstantRuleTest.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules;
4+
5+
use PHPStan\Rules\Classes\ClassConstantRule;
6+
use PHPStan\Testing\RuleTestCase;
7+
8+
/**
9+
* @extends RuleTestCase<ClassConstantRule>
10+
*/
11+
class ClassConstantRuleTest extends RuleTestCase
12+
{
13+
14+
protected function getRule(): Rule
15+
{
16+
return self::getContainer()->getByType(ClassConstantRule::class);
17+
}
18+
19+
public function testRule(): void
20+
{
21+
require_once __DIR__ . '/Deprecations/data/fetching-class-const-of-deprecated-class-definition.php';
22+
$this->analyse(
23+
[__DIR__ . '/Deprecations/data/fetching-class-const-of-deprecated-class.php'],
24+
[
25+
[
26+
'Access to constant on deprecated class FetchingClassConstOfDeprecatedClass\DeprecatedFoo.',
27+
6,
28+
],
29+
[
30+
'Access to constant on deprecated class FetchingClassConstOfDeprecatedClass\DeprecatedFoo.',
31+
11,
32+
],
33+
[
34+
'Access to constant on deprecated class FetchingClassConstOfDeprecatedClass\DeprecatedFoo.',
35+
12,
36+
],
37+
],
38+
);
39+
}
40+
41+
public static function getAdditionalConfigFiles(): array
42+
{
43+
return [
44+
__DIR__ . '/../../rules.neon',
45+
...parent::getAdditionalConfigFiles(),
46+
];
47+
}
48+
49+
}

tests/Rules/Deprecations/FetchingClassConstOfDeprecatedClassRuleTest.php

+11-19
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22

33
namespace PHPStan\Rules\Deprecations;
44

5+
use PHPStan\Rules\RestrictedUsage\RestrictedClassConstantUsageRule;
56
use PHPStan\Rules\Rule;
6-
use PHPStan\Rules\RuleLevelHelper;
77
use PHPStan\Testing\RuleTestCase;
88

99
/**
10-
* @extends RuleTestCase<FetchingClassConstOfDeprecatedClassRule>
10+
* @extends RuleTestCase<RestrictedClassConstantUsageRule>
1111
*/
1212
class FetchingClassConstOfDeprecatedClassRuleTest extends RuleTestCase
1313
{
1414

1515
protected function getRule(): Rule
1616
{
17-
return new FetchingClassConstOfDeprecatedClassRule(
18-
$this->createReflectionProvider(),
19-
self::getContainer()->getByType(RuleLevelHelper::class),
20-
new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]),
21-
);
17+
return self::getContainer()->getByType(RestrictedClassConstantUsageRule::class);
2218
}
2319

2420
public function testFetchingClassConstOfDeprecatedClass(): void
@@ -27,22 +23,10 @@ public function testFetchingClassConstOfDeprecatedClass(): void
2723
$this->analyse(
2824
[__DIR__ . '/data/fetching-class-const-of-deprecated-class.php'],
2925
[
30-
[
31-
'Fetching class constant class of deprecated class FetchingClassConstOfDeprecatedClass\DeprecatedFoo.',
32-
6,
33-
],
3426
[
3527
'Fetching deprecated class constant DEPRECATED_FOO of class FetchingClassConstOfDeprecatedClass\Foo.',
3628
9,
3729
],
38-
[
39-
'Fetching class constant class of deprecated class FetchingClassConstOfDeprecatedClass\DeprecatedFoo.',
40-
11,
41-
],
42-
[
43-
'Fetching class constant class of deprecated class FetchingClassConstOfDeprecatedClass\DeprecatedFoo.',
44-
12,
45-
],
4630
[
4731
"Fetching deprecated class constant DEPRECATED_WITH_DESCRIPTION of class FetchingClassConstOfDeprecatedClass\Foo:\nUse different constant.",
4832
13,
@@ -55,4 +39,12 @@ public function testFetchingClassConstOfDeprecatedClass(): void
5539
);
5640
}
5741

42+
public static function getAdditionalConfigFiles(): array
43+
{
44+
return [
45+
__DIR__ . '/../../../rules.neon',
46+
...parent::getAdditionalConfigFiles(),
47+
];
48+
}
49+
5850
}

0 commit comments

Comments
 (0)