diff --git a/src/Infer/Scope/Scope.php b/src/Infer/Scope/Scope.php index 5b0ac5ad..13eb86ed 100644 --- a/src/Infer/Scope/Scope.php +++ b/src/Infer/Scope/Scope.php @@ -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), ]); } diff --git a/tests/Infer/Scope/ScopeTest.php b/tests/Infer/Scope/ScopeTest.php index 4ca84d3b..4e7e74bf 100644 --- a/tests/Infer/Scope/ScopeTest.php +++ b/tests/Infer/Scope/ScopeTest.php @@ -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'], ]);