diff --git a/src/Rules/Methods/ParentMethodHelper.php b/src/Rules/Methods/ParentMethodHelper.php index f9930d9586..8ff7c40603 100644 --- a/src/Rules/Methods/ParentMethodHelper.php +++ b/src/Rules/Methods/ParentMethodHelper.php @@ -53,6 +53,12 @@ public function collectParentMethods(string $methodName, ClassReflection $class) continue; } + // Skip traits that inherited the method from a sub-trait + // The actual declaring trait will be processed separately + if ($methodReflection->getBetterReflection()->getDeclaringClass()->getName() !== $trait->getName()) { + continue; + } + $declaringTrait = $trait->getNativeMethod($methodName)->getDeclaringClass(); $parentMethods[] = [ $this->phpClassReflectionExtension->createUserlandMethodReflection( diff --git a/tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php b/tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php index faa0a3080b..87319aa44d 100644 --- a/tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php +++ b/tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php @@ -558,4 +558,11 @@ public function testBug12073(): void $this->analyse([__DIR__ . '/data/bug-12073.php'], []); } + public function testBug14320(): void + { + $this->reportMaybes = true; + $this->reportStatic = true; + $this->analyse([__DIR__ . '/data/bug-14320.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/bug-14320.php b/tests/PHPStan/Rules/Methods/data/bug-14320.php new file mode 100644 index 0000000000..1ad7075391 --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-14320.php @@ -0,0 +1,33 @@ + $data + * @return array + */ + abstract protected function myFunction(array $data): array; +} + +trait MyFirstTrait +{ + use MyTrait; +} + +abstract class MyAbstractClass +{ + use MyFirstTrait; + + /** + * @param array $data + * @return array + */ + protected function myFunction(array $data): array + { + return [ + 'hello' => 'bug', + ]; + } +}