Skip to content

Commit fd8fe42

Browse files
committed
detect override of deprecated property
1 parent a22b36b commit fd8fe42

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

Diff for: rules.neon

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ parameters:
44
services:
55
-
66
class: PHPStan\Rules\Deprecations\DeprecatedClassHelper
7+
-
8+
class: PHPStan\Rules\Deprecations\OverrideDeprecatedPropertyRule
79

810
rules:
911
- PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule
@@ -22,3 +24,7 @@ rules:
2224
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInFunctionSignatureRule
2325
- PHPStan\Rules\Deprecations\UsageOfDeprecatedCastRule
2426
- PHPStan\Rules\Deprecations\UsageOfDeprecatedTraitRule
27+
28+
conditionalTags:
29+
PHPStan\Rules\Deprecations\OverrideDeprecatedPropertyRule:
30+
phpstan.rules.rule: %featureToggles.bleedingEdge%
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Stmt\Property;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Rules\Rule;
9+
use function sprintf;
10+
11+
/**
12+
* @implements Rule<Property>
13+
*/
14+
class OverrideDeprecatedPropertyRule implements Rule
15+
{
16+
17+
public function getNodeType(): string
18+
{
19+
return Property::class;
20+
}
21+
22+
public function processNode(Node $node, Scope $scope): array
23+
{
24+
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
25+
return [];
26+
}
27+
28+
if (!$scope->isInClass()) {
29+
return [];
30+
}
31+
32+
$class = $scope->getClassReflection();
33+
34+
$parents = $class->getParents();
35+
36+
$propertyName = (string) $node->props[0]->name;
37+
38+
foreach ($parents as $parent) {
39+
if (!$parent->hasProperty($propertyName)) {
40+
continue;
41+
}
42+
43+
$parentProperty = $parent->getProperty($propertyName, $scope);
44+
45+
if (!$parentProperty->isDeprecated()->yes()) {
46+
return [];
47+
}
48+
49+
return [sprintf(
50+
'Class %s overrides deprecated property %s of class %s.',
51+
$class->getName(),
52+
$propertyName,
53+
$parent->getName()
54+
)];
55+
}
56+
57+
return [];
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
8+
/**
9+
* @extends RuleTestCase<OverrideDeprecatedPropertyRule>
10+
*/
11+
class OverrideDeprecatedPropertyRuleTest extends RuleTestCase
12+
{
13+
14+
protected function getRule(): Rule
15+
{
16+
return new OverrideDeprecatedPropertyRule();
17+
}
18+
19+
public function testDeprecatedPropertyOverride(): void
20+
{
21+
$this->analyse(
22+
[__DIR__ . '/data/override-deprecated-property.php'],
23+
[
24+
[
25+
'Class OverrideDeprecatedProperty\Child overrides deprecated property foo of class OverrideDeprecatedProperty\Foo.',
26+
15,
27+
],
28+
]
29+
);
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace OverrideDeprecatedProperty;
4+
5+
class Foo
6+
{
7+
/**
8+
* @deprecated
9+
*/
10+
public $foo;
11+
}
12+
13+
class Child extends Foo
14+
{
15+
public $foo;
16+
}

0 commit comments

Comments
 (0)