diff --git a/src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php b/src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php index b0b11a10ae..3b52a47aad 100644 --- a/src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php +++ b/src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php @@ -56,6 +56,10 @@ public function processNode(Node $node, Scope $scope): array $returnTypes[] = $returnStatement->getScope()->getType($returnNode->expr); } + if (!$statementResult->isAlwaysTerminating()) { + $returnTypes[] = new VoidType(); + } + $returnType = TypeCombinator::union(...$returnTypes); $messages = []; diff --git a/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php b/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php index b73fa09641..92084ea3a0 100644 --- a/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php +++ b/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php @@ -82,6 +82,10 @@ public function processNode(Node $node, Scope $scope): array $returnTypes[] = $returnStatement->getScope()->getType($returnNode->expr); } + if (!$statementResult->isAlwaysTerminating()) { + $returnTypes[] = new VoidType(); + } + $returnType = TypeCombinator::union(...$returnTypes); if ( !$method->isPrivate() diff --git a/tests/PHPStan/Rules/TooWideTypehints/data/bug-11980-function.php b/tests/PHPStan/Rules/TooWideTypehints/data/bug-11980-function.php index 0db93a1cfb..aaafea889d 100644 --- a/tests/PHPStan/Rules/TooWideTypehints/data/bug-11980-function.php +++ b/tests/PHPStan/Rules/TooWideTypehints/data/bug-11980-function.php @@ -46,4 +46,25 @@ function process2($tokens, $stackPtr) // Not a stand-alone statement. return $end; } + + return 1; +} + +/** @return int|void */ +function process3( int $code ) { + + if ( $code === \T_CLASS ) { + return process_class( $code ); + } + + process_function( $code ); +} + +/** @return int */ +function process_class(int $code) { + return $code; +} + +/** @return void */ +function process_function(int $code) { } diff --git a/tests/PHPStan/Rules/TooWideTypehints/data/bug-11980.php b/tests/PHPStan/Rules/TooWideTypehints/data/bug-11980.php index 99ab186f62..d45bb08576 100644 --- a/tests/PHPStan/Rules/TooWideTypehints/data/bug-11980.php +++ b/tests/PHPStan/Rules/TooWideTypehints/data/bug-11980.php @@ -49,5 +49,26 @@ public function process2($tokens, $stackPtr) // Not a stand-alone statement. return $end; } + + return 1; + } + + /** @return int|void */ + public function process3( int $code ) { + + if ( $code === \T_CLASS ) { + return $this->process_class( $code ); + } + + $this->process_function( $code ); + } + + /** @return int */ + public function process_class(int $code) { + return $code; + } + + /** @return void */ + public function process_function(int $code) { } }