diff --git a/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php b/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php index 45348f5dca..a3863ff6c0 100644 --- a/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php +++ b/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php @@ -32,6 +32,9 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { + if ($scope->isInTrait()) { + return []; + } $method = $node->getMethodReflection(); $isFirstDeclaration = $method->getPrototype()->getDeclaringClass() === $method->getDeclaringClass(); if (!$method->isPrivate()) { diff --git a/tests/PHPStan/Rules/TooWideTypehints/data/tooWideMethodReturnType-private.php b/tests/PHPStan/Rules/TooWideTypehints/data/tooWideMethodReturnType-private.php index aa3f6259a6..e454de2229 100644 --- a/tests/PHPStan/Rules/TooWideTypehints/data/tooWideMethodReturnType-private.php +++ b/tests/PHPStan/Rules/TooWideTypehints/data/tooWideMethodReturnType-private.php @@ -92,3 +92,20 @@ private function dolor6() { } } + +trait FooTrait +{ + + private function doFoo(): ?int + { + return 1; + } + +} + +class UsesFooTrait +{ + + use FooTrait; + +}