From a22b36b955a2e9a3d39fe533b6c1bb5359f9c319 Mon Sep 17 00:00:00 2001 From: Thomas Gnandt Date: Fri, 17 Mar 2023 07:45:17 +0100 Subject: [PATCH] report deprecated properties accessed by self --- .../AccessDeprecatedStaticPropertyRule.php | 6 +++--- ...AccessDeprecatedStaticPropertyRuleTest.php | 16 ++++++++++++++++ .../CallToDeprecatedStaticMethodRuleTest.php | 16 ++++++++++++++++ .../access-deprecated-static-property.php | 16 ++++++++++++++++ .../data/call-to-deprecated-static-method.php | 19 +++++++++++++++++++ 5 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php b/src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php index db128cc..beb9891 100644 --- a/src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php +++ b/src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php @@ -53,7 +53,7 @@ public function processNode(Node $node, Scope $scope): array $referencedClasses = []; if ($node->class instanceof Name) { - $referencedClasses[] = (string) $node->class; + $referencedClasses[] = $scope->resolveName($node->class); } else { $classTypeResult = $this->ruleLevelHelper->findTypeToCheck( $scope, @@ -87,14 +87,14 @@ static function (Type $type) use ($propertyName): bool { return [sprintf( 'Access to deprecated static property $%s of class %s.', $propertyName, - $referencedClass + $property->getDeclaringClass()->getName() )]; } return [sprintf( "Access to deprecated static property $%s of class %s:\n%s", $propertyName, - $referencedClass, + $property->getDeclaringClass()->getName(), $description )]; } diff --git a/tests/Rules/Deprecations/AccessDeprecatedStaticPropertyRuleTest.php b/tests/Rules/Deprecations/AccessDeprecatedStaticPropertyRuleTest.php index 655013a..75e4cbe 100644 --- a/tests/Rules/Deprecations/AccessDeprecatedStaticPropertyRuleTest.php +++ b/tests/Rules/Deprecations/AccessDeprecatedStaticPropertyRuleTest.php @@ -59,6 +59,22 @@ public function testAccessDeprecatedStaticProperty(): void "Access to deprecated static property \$deprecatedWithDescription of class AccessDeprecatedStaticProperty\Foo:\nThis is probably a singleton.", 33, ], + [ + 'Access to deprecated static property $deprecatedFoo of class AccessDeprecatedStaticProperty\Foo.', + 117, + ], + [ + 'Access to deprecated static property $deprecatedOtherFoo of class AccessDeprecatedStaticProperty\Child.', + 118, + ], + [ + 'Access to deprecated static property $deprecatedFoo of class AccessDeprecatedStaticProperty\Foo.', + 119, + ], + [ + 'Access to deprecated static property $deprecatedOtherFoo of class AccessDeprecatedStaticProperty\Child.', + 120, + ], ] ); } diff --git a/tests/Rules/Deprecations/CallToDeprecatedStaticMethodRuleTest.php b/tests/Rules/Deprecations/CallToDeprecatedStaticMethodRuleTest.php index f97ff5f..ecc1393 100644 --- a/tests/Rules/Deprecations/CallToDeprecatedStaticMethodRuleTest.php +++ b/tests/Rules/Deprecations/CallToDeprecatedStaticMethodRuleTest.php @@ -67,6 +67,22 @@ public function testDeprecatedStaticMethodCall(): void 'Call to deprecated method deprecatedFoo() of class CheckDeprecatedStaticMethodCall\Foo.', 33, ], + [ + 'Call to deprecated method deprecatedFoo() of class CheckDeprecatedStaticMethodCall\Foo.', + 74, + ], + [ + 'Call to deprecated method deprecatedOtherFoo() of class CheckDeprecatedStaticMethodCall\Child.', + 75, + ], + [ + 'Call to deprecated method deprecatedFoo() of class CheckDeprecatedStaticMethodCall\Foo.', + 76, + ], + [ + 'Call to deprecated method deprecatedOtherFoo() of class CheckDeprecatedStaticMethodCall\Child.', + 77, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/access-deprecated-static-property.php b/tests/Rules/Deprecations/data/access-deprecated-static-property.php index 9d78308..b7fc136 100644 --- a/tests/Rules/Deprecations/data/access-deprecated-static-property.php +++ b/tests/Rules/Deprecations/data/access-deprecated-static-property.php @@ -104,3 +104,19 @@ public function foo() } } + +class Child extends Foo +{ + /** + * @deprecated + */ + public static $deprecatedOtherFoo; + + public static function foo() + { + self::$deprecatedFoo; + self::$deprecatedOtherFoo; + static::$deprecatedFoo; + static::$deprecatedOtherFoo; + } +} diff --git a/tests/Rules/Deprecations/data/call-to-deprecated-static-method.php b/tests/Rules/Deprecations/data/call-to-deprecated-static-method.php index 93e122c..5be90bd 100644 --- a/tests/Rules/Deprecations/data/call-to-deprecated-static-method.php +++ b/tests/Rules/Deprecations/data/call-to-deprecated-static-method.php @@ -58,3 +58,22 @@ public static function foo() } } + +class Child extends Foo +{ + /** + * @deprecated + */ + public static function deprecatedOtherFoo() + { + + } + + public static function foo() + { + self::deprecatedFoo(); + self::deprecatedOtherFoo(); + static::deprecatedFoo(); + static::deprecatedOtherFoo(); + } +}