diff --git a/src/Rules/Methods/OverridingMethodRule.php b/src/Rules/Methods/OverridingMethodRule.php index e17d2164dd..4bef013bcb 100644 --- a/src/Rules/Methods/OverridingMethodRule.php +++ b/src/Rules/Methods/OverridingMethodRule.php @@ -58,16 +58,34 @@ public function processNode(Node $node, Scope $scope): array $prototype->getName() ))->nonIgnorable()->build(); } - } else { - if ($method->isPrivate()) { + } elseif ($method->isPrivate()) { + $messages[] = RuleErrorBuilder::message(sprintf( + 'Private method %s::%s() overriding protected method %s::%s() should be protected or public.', + $method->getDeclaringClass()->getName(), + $method->getName(), + $prototype->getDeclaringClass()->getName(), + $prototype->getName() + ))->nonIgnorable()->build(); + } + + if ($prototype->isStatic()) { + if (!$method->isStatic()) { $messages[] = RuleErrorBuilder::message(sprintf( - 'Private method %s::%s() overriding protected method %s::%s() should be protected or public.', + 'Non-static method %s::%s() overrides static method %s::%s().', $method->getDeclaringClass()->getName(), $method->getName(), $prototype->getDeclaringClass()->getName(), $prototype->getName() ))->nonIgnorable()->build(); } + } elseif ($method->isStatic()) { + $messages[] = RuleErrorBuilder::message(sprintf( + 'Static method %s::%s() overrides non-static method %s::%s().', + $method->getDeclaringClass()->getName(), + $method->getName(), + $prototype->getDeclaringClass()->getName(), + $prototype->getName() + ))->nonIgnorable()->build(); } return $messages; diff --git a/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php b/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php index fb6b83376d..3b0b20661a 100644 --- a/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php +++ b/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php @@ -25,19 +25,27 @@ public function testOverridingFinalMethod(): void $this->analyse([__DIR__ . '/data/overriding-final-method.php'], [ [ 'Method OverridingFinalMethod\Bar::doFoo() overrides final method OverridingFinalMethod\Foo::doFoo().', - 33, + 43, ], [ 'Private method OverridingFinalMethod\Bar::doBar() overriding public method OverridingFinalMethod\Foo::doBar() should also be public.', - 38, + 48, ], [ 'Protected method OverridingFinalMethod\Bar::doBaz() overriding public method OverridingFinalMethod\Foo::doBaz() should also be public.', - 43, + 53, ], [ 'Private method OverridingFinalMethod\Bar::doLorem() overriding protected method OverridingFinalMethod\Foo::doLorem() should be protected or public.', - 48, + 58, + ], + [ + 'Non-static method OverridingFinalMethod\Bar::doIpsum() overrides static method OverridingFinalMethod\Foo::doIpsum().', + 63, + ], + [ + 'Static method OverridingFinalMethod\Bar::doDolor() overrides non-static method OverridingFinalMethod\Foo::doDolor().', + 68, ], ]); } diff --git a/tests/PHPStan/Rules/Methods/data/overriding-final-method.php b/tests/PHPStan/Rules/Methods/data/overriding-final-method.php index 2c0e5d20b2..f527be9251 100644 --- a/tests/PHPStan/Rules/Methods/data/overriding-final-method.php +++ b/tests/PHPStan/Rules/Methods/data/overriding-final-method.php @@ -25,6 +25,16 @@ protected function doLorem() } + public static function doIpsum() + { + + } + + public function doDolor() + { + + } + } class Bar extends Foo @@ -50,4 +60,14 @@ private function doLorem() } + public function doIpsum() + { + + } + + public static function doDolor() + { + + } + }