Skip to content

Commit a5d3011

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

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

rules.neon

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ rules:
1717
- PHPStan\Rules\Deprecations\InheritanceOfDeprecatedClassRule
1818
- PHPStan\Rules\Deprecations\InheritanceOfDeprecatedInterfaceRule
1919
- PHPStan\Rules\Deprecations\InstantiationOfDeprecatedClassRule
20+
- PHPStan\Rules\Deprecations\OverrideDeprecatedPropertyRule
2021
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInClassMethodSignatureRule
2122
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInClosureSignatureRule
2223
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInFunctionSignatureRule
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 testDeprecatedMethodCall(): 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)