Skip to content

Commit 036cfd3

Browse files
authored
[Php70] Handle keep right parentheses on ternary else is BinaryOp on TernaryToNullCoalescingRector (#7916)
* [Php70] Handle keep right parentheses on ternary else is BinaryOp on TernaryToNullCoalescingRector * fix
1 parent 3f583cd commit 036cfd3

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php70\Rector\Ternary\TernaryToNullCoalescingRector\Fixture;
4+
5+
class KeepRightParenthesesBinaryOp
6+
{
7+
public function run($bar, $baz, $qux)
8+
{
9+
$foo = isset($bar) ? $bar : ($baz && $qux);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\Php70\Rector\Ternary\TernaryToNullCoalescingRector\Fixture;
18+
19+
class KeepRightParenthesesBinaryOp
20+
{
21+
public function run($bar, $baz, $qux)
22+
{
23+
$foo = $bar ?? ($baz && $qux);
24+
}
25+
}
26+
27+
?>

rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\BinaryOp;
910
use PhpParser\Node\Expr\BinaryOp\Coalesce;
1011
use PhpParser\Node\Expr\BinaryOp\Identical;
1112
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
@@ -114,7 +115,7 @@ private function processTernaryWithIsset(Ternary $ternary, Isset_ $isset): ?Coal
114115
return null;
115116
}
116117

117-
if ($ternary->else instanceof Ternary && $this->isTernaryParenthesized($this->file, $ternary->cond, $ternary)) {
118+
if (($ternary->else instanceof Ternary || $ternary->else instanceof BinaryOp) && $this->isTernaryParenthesized($this->file, $ternary->cond, $ternary)) {
118119
$ternary->else->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
119120
}
120121

0 commit comments

Comments
 (0)