diff --git a/src/Rules/TooWideTypehints/TooWideArrowFunctionReturnTypehintRule.php b/src/Rules/TooWideTypehints/TooWideArrowFunctionReturnTypehintRule.php index e9883482d8..cf064a1c87 100644 --- a/src/Rules/TooWideTypehints/TooWideArrowFunctionReturnTypehintRule.php +++ b/src/Rules/TooWideTypehints/TooWideArrowFunctionReturnTypehintRule.php @@ -50,7 +50,7 @@ public function processNode(Node $node, Scope $scope): array $messages[] = RuleErrorBuilder::message(sprintf( 'Anonymous function never returns %s so it can be removed from the return typehint.', - $type->describe(VerbosityLevel::typeOnly()) + $type->describe(VerbosityLevel::getRecommendedLevelByType($type)) ))->build(); } diff --git a/src/Rules/TooWideTypehints/TooWideClosureReturnTypehintRule.php b/src/Rules/TooWideTypehints/TooWideClosureReturnTypehintRule.php index 9277287e8f..0bcb7fa810 100644 --- a/src/Rules/TooWideTypehints/TooWideClosureReturnTypehintRule.php +++ b/src/Rules/TooWideTypehints/TooWideClosureReturnTypehintRule.php @@ -72,7 +72,7 @@ public function processNode(Node $node, Scope $scope): array $messages[] = RuleErrorBuilder::message(sprintf( 'Anonymous function never returns %s so it can be removed from the return typehint.', - $type->describe(VerbosityLevel::typeOnly()) + $type->describe(VerbosityLevel::getRecommendedLevelByType($type)) ))->build(); } diff --git a/src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php b/src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php index 747685bc9e..827d6d9490 100644 --- a/src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php +++ b/src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php @@ -70,7 +70,7 @@ public function processNode(Node $node, Scope $scope): array $messages[] = RuleErrorBuilder::message(sprintf( 'Function %s() never returns %s so it can be removed from the return typehint.', $function->getName(), - $type->describe(VerbosityLevel::typeOnly()) + $type->describe(VerbosityLevel::getRecommendedLevelByType($type)) ))->build(); } diff --git a/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php b/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php index 107b06af13..5b1210ff9b 100644 --- a/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php +++ b/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php @@ -96,7 +96,7 @@ public function processNode(Node $node, Scope $scope): array 'Method %s::%s() never returns %s so it can be removed from the return typehint.', $method->getDeclaringClass()->getDisplayName(), $method->getName(), - $type->describe(VerbosityLevel::typeOnly()) + $type->describe(VerbosityLevel::getRecommendedLevelByType($type)) ))->build(); } diff --git a/tests/PHPStan/Rules/TooWideTypehints/TooWideMethodReturnTypehintRuleTest.php b/tests/PHPStan/Rules/TooWideTypehints/TooWideMethodReturnTypehintRuleTest.php index 7744509e76..6ed2a9cf44 100644 --- a/tests/PHPStan/Rules/TooWideTypehints/TooWideMethodReturnTypehintRuleTest.php +++ b/tests/PHPStan/Rules/TooWideTypehints/TooWideMethodReturnTypehintRuleTest.php @@ -66,4 +66,14 @@ public function testPublicProtectedWithInheritance(): void ]); } + public function testBug5095(): void + { + $this->analyse([__DIR__ . '/data/bug-5095.php'], [ + [ + 'Method Bug5095\Parser::unaryOperatorFor() never returns \'not\' so it can be removed from the return typehint.', + 21, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/TooWideTypehints/data/bug-5095.php b/tests/PHPStan/Rules/TooWideTypehints/data/bug-5095.php new file mode 100644 index 0000000000..8c6781fbc4 --- /dev/null +++ b/tests/PHPStan/Rules/TooWideTypehints/data/bug-5095.php @@ -0,0 +1,37 @@ +