Skip to content

Commit

Permalink
create a temporary var for coalescing ternaries
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Nov 5, 2021
1 parent b783468 commit ad57516
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static function analyze(
|| $root_expr instanceof PhpParser\Node\Expr\Cast
|| $root_expr instanceof PhpParser\Node\Expr\NullsafePropertyFetch
|| $root_expr instanceof PhpParser\Node\Expr\NullsafeMethodCall
|| $root_expr instanceof PhpParser\Node\Expr\Ternary
) {
$left_var_id = '$<tmp coalesce var>' . (int) $left_expr->getAttribute('startFilePos');

Expand Down
13 changes: 13 additions & 0 deletions tests/BinaryOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,19 @@ function scope(array $a): int|float {
'$b' => 'float|int',
],
],
'coalesceFilterOutNullEvenWithTernary' => [
'<?php
interface FooInterface
{
public function toString(): ?string;
}
function example(object $foo): string
{
return ($foo instanceof FooInterface ? $foo->toString() : null) ?? "Not a stringable foo";
}',
],
];
}

Expand Down

0 comments on commit ad57516

Please sign in to comment.