Skip to content

Commit

Permalink
Merge pull request #6825 from orklah/tmp-var-coalesce
Browse files Browse the repository at this point in the history
create a tmp fake var for ternaries inside coalesce
  • Loading branch information
orklah authored Nov 5, 2021
2 parents b783468 + ad57516 commit e6c2313
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 e6c2313

Please sign in to comment.