Skip to content

Commit a3ffe76

Browse files
authored
[CodeQuality] Skip Assign Op on ternary on SimplifyUselessVariableRector (#6913)
1 parent c9189a4 commit a3ffe76

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector\Fixture;
4+
5+
class SkipTernary
6+
{
7+
public function run(?string $maybe, string $part): string
8+
{
9+
$address = $maybe;
10+
$address .= $address ? ' - ' . $part : $part;
11+
12+
return $address;
13+
}
14+
}

rules/CodeQuality/Rector/FunctionLike/SimplifyUselessVariableRector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node;
99
use PhpParser\Node\Expr\Assign;
1010
use PhpParser\Node\Expr\AssignOp;
11+
use PhpParser\Node\Expr\Ternary;
1112
use PhpParser\Node\Expr\Variable;
1213
use PhpParser\Node\Stmt;
1314
use PhpParser\Node\Stmt\Expression;
@@ -193,6 +194,10 @@ private function shouldSkipStmt(Return_ $return, Stmt $previousStmt): bool
193194
return true;
194195
}
195196

197+
if ($previousNode instanceof AssignOp && $previousNode->expr instanceof Ternary) {
198+
return true;
199+
}
200+
196201
$variable = $return->expr;
197202
// is the same variable
198203
if (! $this->nodeComparator->areNodesEqual($previousNode->var, $variable)) {

0 commit comments

Comments
 (0)