Skip to content

Commit 9dc9fdf

Browse files
committed
Fix
1 parent 668171d commit 9dc9fdf

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

rules-tests/Php70/Rector/Ternary/TernaryToNullCoalescingRector/Fixture/keep_right_parentheses.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Rector\Tests\Php70\Rector\Ternary\TernaryToNullCoalescingRector\Fixtur
44

55
class KeepRightParentheses
66
{
7-
public function run()
7+
public function run($port, $scheme)
88
{
99
$port = isset($port) ? $port : (($scheme == 'https') ? '443' : '80');
1010
}
@@ -18,7 +18,7 @@ namespace Rector\Tests\Php70\Rector\Ternary\TernaryToNullCoalescingRector\Fixtur
1818

1919
class KeepRightParentheses
2020
{
21-
public function run()
21+
public function run($port, $scheme)
2222
{
2323
$port = $port ?? (($scheme == 'https') ? '443' : '80');
2424
}

rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
1212
use PhpParser\Node\Expr\Isset_;
1313
use PhpParser\Node\Expr\Ternary;
14+
use Rector\NodeTypeResolver\Node\AttributeKey;
1415
use Rector\PhpParser\Node\Value\ValueResolver;
1516
use Rector\Rector\AbstractRector;
17+
use Rector\ValueObject\Application\File;
1618
use Rector\ValueObject\PhpVersionFeature;
1719
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
1820
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -112,9 +114,43 @@ private function processTernaryWithIsset(Ternary $ternary, Isset_ $isset): ?Coal
112114
return null;
113115
}
114116

117+
if ($ternary->else instanceof Ternary && $this->isTernaryParenthesized($this->file, $ternary->cond, $ternary)) {
118+
$ternary->else->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
119+
}
120+
115121
return new Coalesce($ternary->if, $ternary->else);
116122
}
117123

124+
private function isTernaryParenthesized(File $file, Expr $expr, Ternary $ternary): bool
125+
{
126+
$oldTokens = $file->getOldTokens();
127+
$endTokenPost = $ternary->getEndTokenPos();
128+
129+
if (isset($oldTokens[$endTokenPost]) && (string) $oldTokens[$endTokenPost] === ')') {
130+
$startTokenPos = $ternary->else->getStartTokenPos();
131+
$previousEndTokenPost = $expr->getEndTokenPos();
132+
133+
while ($startTokenPos > $previousEndTokenPost) {
134+
--$startTokenPos;
135+
136+
if (! isset($oldTokens[$startTokenPos])) {
137+
return false;
138+
}
139+
140+
// handle space before open parentheses
141+
if (trim((string) $oldTokens[$startTokenPos]) === '') {
142+
continue;
143+
}
144+
145+
return (string) $oldTokens[$startTokenPos] === '(';
146+
}
147+
148+
return false;
149+
}
150+
151+
return false;
152+
}
153+
118154
private function isNullMatch(Expr $possibleNullExpr, Expr $firstNode, Expr $secondNode): bool
119155
{
120156
if (! $this->valueResolver->isNull($possibleNullExpr)) {

0 commit comments

Comments
 (0)