diff --git a/rules.neon b/rules.neon index 9b63b38..8592640 100644 --- a/rules.neon +++ b/rules.neon @@ -17,6 +17,7 @@ rules: - PHPStan\Rules\Deprecations\InheritanceOfDeprecatedClassRule - PHPStan\Rules\Deprecations\InheritanceOfDeprecatedInterfaceRule - PHPStan\Rules\Deprecations\InstantiationOfDeprecatedClassRule + - PHPStan\Rules\Deprecations\OverrideDeprecatedPropertyRule - PHPStan\Rules\Deprecations\TypeHintDeprecatedInClassMethodSignatureRule - PHPStan\Rules\Deprecations\TypeHintDeprecatedInClosureSignatureRule - PHPStan\Rules\Deprecations\TypeHintDeprecatedInFunctionSignatureRule diff --git a/src/Rules/Deprecations/OverrideDeprecatedPropertyRule.php b/src/Rules/Deprecations/OverrideDeprecatedPropertyRule.php new file mode 100644 index 0000000..c30bc6f --- /dev/null +++ b/src/Rules/Deprecations/OverrideDeprecatedPropertyRule.php @@ -0,0 +1,60 @@ + + */ +class OverrideDeprecatedPropertyRule implements Rule +{ + + public function getNodeType(): string + { + return Property::class; + } + + public function processNode(Node $node, Scope $scope): array + { + if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + return []; + } + + if (!$scope->isInClass()) { + return []; + } + + $class = $scope->getClassReflection(); + + $parents = $class->getParents(); + + $propertyName = (string) $node->props[0]->name; + + foreach ($parents as $parent) { + if (!$parent->hasProperty($propertyName)) { + continue; + } + + $parentProperty = $parent->getProperty($propertyName, $scope); + + if (!$parentProperty->isDeprecated()->yes()) { + return []; + } + + return [sprintf( + 'Class %s overrides deprecated property %s of class %s.', + $class->getName(), + $propertyName, + $parent->getName() + )]; + } + + return []; + } + +} diff --git a/tests/Rules/Deprecations/OverrideDeprecatedPropertyRuleTest.php b/tests/Rules/Deprecations/OverrideDeprecatedPropertyRuleTest.php new file mode 100644 index 0000000..ce310f3 --- /dev/null +++ b/tests/Rules/Deprecations/OverrideDeprecatedPropertyRuleTest.php @@ -0,0 +1,32 @@ + + */ +class OverrideDeprecatedPropertyRuleTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + return new OverrideDeprecatedPropertyRule(); + } + + public function testDeprecatedMethodCall(): void + { + $this->analyse( + [__DIR__ . '/data/override-deprecated-property.php'], + [ + [ + 'Class OverrideDeprecatedProperty\Child overrides deprecated property foo of class OverrideDeprecatedProperty\Foo.', + 15, + ], + ] + ); + } + +} diff --git a/tests/Rules/Deprecations/data/override-deprecated-property.php b/tests/Rules/Deprecations/data/override-deprecated-property.php new file mode 100644 index 0000000..d3faa46 --- /dev/null +++ b/tests/Rules/Deprecations/data/override-deprecated-property.php @@ -0,0 +1,16 @@ +