Skip to content

Commit

Permalink
Too-wide return type - allow void return type in a union when the r…
Browse files Browse the repository at this point in the history
…eturned expr is originally `void`
  • Loading branch information
ondrejmirtes committed Nov 10, 2024
1 parent f2dfd5d commit 80c1df2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/TooWideTypehints/data/bug-11980-function.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/TooWideTypehints/data/bug-11980.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
}

0 comments on commit 80c1df2

Please sign in to comment.