Skip to content

Commit 0532a89

Browse files
authored
[Php70] Keep inner ternary parentheses on TernaryToNullCoalescingRector (#8921)
* [Php70] Keep inner ternary parentheses on TernaryToNullCoalescingRector * [Php70] Keep inner ternary parentheses on TernaryToNullCoalescingRector
1 parent 42cc5ad commit 0532a89

2 files changed

Lines changed: 58 additions & 0 deletions

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+
final class KeepInnerTernaryParenthesesInConcat
6+
{
7+
public function run($a, $b)
8+
{
9+
return isset($a) ? $a : ('R' . ($b == 1? '' : 'ector'));
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\Php70\Rector\Ternary\TernaryToNullCoalescingRector\Fixture;
18+
19+
final class KeepInnerTernaryParenthesesInConcat
20+
{
21+
public function run($a, $b)
22+
{
23+
return $a ?? ('R' . ($b == 1? '' : 'ector'));
24+
}
25+
}
26+
27+
?>

‎src/PhpParser/Printer/BetterStandardPrinter.php‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,15 +544,46 @@ private function wrapBinaryOpWithBrackets(Node $node): void
544544

545545
if ($node->left instanceof BinaryOp &&
546546
$node->left->getAttribute(AttributeKey::ORIGINAL_NODE) instanceof Node) {
547+
$this->keepTernaryOperandParentheses($node->left);
547548
$node->left->setAttribute(AttributeKey::ORIGINAL_NODE, null);
548549
}
549550

550551
if ($node->right instanceof BinaryOp &&
551552
$node->right->getAttribute(AttributeKey::ORIGINAL_NODE) instanceof Node) {
553+
$this->keepTernaryOperandParentheses($node->right);
552554
$node->right->setAttribute(AttributeKey::ORIGINAL_NODE, null);
553555
}
554556
}
555557

558+
/**
559+
* The binary op is about to be reprinted, while its unchanged ternary operand is still format-preserved,
560+
* which prints it without its original parentheses
561+
*
562+
* @see https://github.com/rectorphp/rector-src/issues/8910
563+
*/
564+
private function keepTernaryOperandParentheses(BinaryOp $binaryOp): void
565+
{
566+
$originalBinaryOp = $binaryOp->getAttribute(AttributeKey::ORIGINAL_NODE);
567+
if (! $originalBinaryOp instanceof BinaryOp || ! $this->origTokens instanceof TokenStream) {
568+
return;
569+
}
570+
571+
foreach ([$binaryOp->left, $binaryOp->right] as $key => $operand) {
572+
if (! $operand instanceof Ternary) {
573+
continue;
574+
}
575+
576+
$originalOperand = $key === 0 ? $originalBinaryOp->left : $originalBinaryOp->right;
577+
if ($operand->getAttribute(AttributeKey::ORIGINAL_NODE) !== $originalOperand) {
578+
continue;
579+
}
580+
581+
if ($this->origTokens->haveParens($operand->getStartTokenPos(), $operand->getEndTokenPos())) {
582+
$operand->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
583+
}
584+
}
585+
}
586+
556587
/**
557588
* @see https://github.com/rectorphp/rector/issues/9800
558589
*/

0 commit comments

Comments
 (0)