Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9e96248

Browse files
Implement RestrictedPropertyUsageExtension
1 parent 3562b56 commit 9e96248

File tree

6 files changed

+126
-217
lines changed

6 files changed

+126
-217
lines changed

‎rules.neon‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ services:
2626
tags:
2727
- phpstan.restrictedMethodUsageExtension
2828

29+
-
30+
class: PHPStan\Rules\Deprecations\RestrictedDeprecatedPropertyUsageExtension
31+
tags:
32+
- phpstan.restrictedPropertyUsageExtension
33+
2934
-
3035
class: PHPStan\Rules\Deprecations\RestrictedDeprecatedClassNameUsageExtension
3136
arguments:
@@ -34,8 +39,6 @@ services:
3439
- phpstan.restrictedClassNameUsageExtension
3540

3641
rules:
37-
- PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule
38-
- PHPStan\Rules\Deprecations\AccessDeprecatedStaticPropertyRule
3942
- PHPStan\Rules\Deprecations\FetchingClassConstOfDeprecatedClassRule
4043
- PHPStan\Rules\Deprecations\FetchingDeprecatedConstRule
4144
- PHPStan\Rules\Deprecations\UsageOfDeprecatedCastRule

‎src/Rules/Deprecations/AccessDeprecatedPropertyRule.php‎

Lines changed: 0 additions & 90 deletions
This file was deleted.

‎src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php‎

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

‎tests/Rules/Deprecations/AccessDeprecatedPropertyRuleTest.php‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
22

33
namespace PHPStan\Rules\Deprecations;
44

5+
use PHPStan\Rules\RestrictedUsage\RestrictedPropertyUsageRule;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Testing\RuleTestCase;
78

89
/**
9-
* @extends RuleTestCase<AccessDeprecatedPropertyRule>
10+
* @extends RuleTestCase<RestrictedPropertyUsageRule>
1011
*/
1112
class AccessDeprecatedPropertyRuleTest extends RuleTestCase
1213
{
1314

1415
protected function getRule(): Rule
1516
{
16-
return new AccessDeprecatedPropertyRule(
17-
$this->createReflectionProvider(),
18-
new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]),
19-
);
17+
return self::getContainer()->getByType(RestrictedPropertyUsageRule::class);
2018
}
2119

2220
public function testAccessDeprecatedProperty(): void
@@ -49,4 +47,12 @@ public function testAccessDeprecatedProperty(): void
4947
);
5048
}
5149

50+
public static function getAdditionalConfigFiles(): array
51+
{
52+
return [
53+
__DIR__ . '/../../../rules.neon',
54+
...parent::getAdditionalConfigFiles(),
55+
];
56+
}
57+
5258
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /