Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php70\Rector\Ternary\TernaryToNullCoalescingRector\Fixture;

class KeepRightParenthesesBinaryOp
{
public function run($bar, $baz, $qux)
{
$foo = isset($bar) ? $bar : ($baz && $qux);
}
}

?>
-----
<?php

namespace Rector\Tests\Php70\Rector\Ternary\TernaryToNullCoalescingRector\Fixture;

class KeepRightParenthesesBinaryOp
{
public function run($bar, $baz, $qux)
{
$foo = $bar ?? ($baz && $qux);
}
}

?>
3 changes: 2 additions & 1 deletion rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
Expand Down Expand Up @@ -114,7 +115,7 @@ private function processTernaryWithIsset(Ternary $ternary, Isset_ $isset): ?Coal
return null;
}

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

Expand Down