Skip to content

Commit

Permalink
Merge pull request #366 from seo-ai/bugfix/ternaries
Browse files Browse the repository at this point in the history
Fix shorthand ternaries trying to pass null as a Node instance
  • Loading branch information
romalytvynenko authored May 6, 2024
2 parents 2b60c21 + d0f1342 commit 7d87f52
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Infer/Scope/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getType(Node $node): Type

if ($node instanceof Node\Expr\Ternary) {
return Union::wrap([
$this->getType($node->if),
$this->getType($node->if ?? $node->cond),
$this->getType($node->else),
]);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Infer/Scope/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ function getStatementTypeForScopeTest(string $statement, array $extensions = [])
})->with([
['unknown() ? 1 : null', 'int(1)|null'],
['unknown() ? 1 : 1', 'int(1)'],
['unknown() ?: 1', 'unknown|int(1)'],
['(int) unknown() ?: 1', 'int|int(1)'],
['1 ?: 1', 'int(1)'],
['unknown() ? 1 : unknown()', 'int(1)|unknown'],
['unknown() ? unknown() : unknown()', 'unknown'],
['unknown() ?: unknown()', 'unknown'],
['unknown() ?: true ?: 1', 'unknown|boolean(true)|int(1)'],
['unknown() ?: unknown() ?: unknown()', 'unknown'],
]);

0 comments on commit 7d87f52

Please sign in to comment.