From e0eb85028d55ebab32be614631639c142b37daa6 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 7 Dec 2023 10:09:32 +0100 Subject: [PATCH] TooWideMethodReturnTypehintRule - never report in a trait --- .../TooWideMethodReturnTypehintRule.php | 3 +++ .../data/tooWideMethodReturnType-private.php | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) 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; + +}