Skip to content

Commit 5735370

Browse files
committed
[CodeQuality] Handle with assign on SimplifyIfElseToTernaryRector
1 parent b1899f4 commit 5735370

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector\Fixture;
4+
5+
class WithAssign
6+
{
7+
public function run()
8+
{
9+
if ($a = $this->methodX()) {
10+
$this->out = $a;
11+
} else {
12+
$this->out = $this->methodY();
13+
}
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector\Fixture;
22+
23+
class WithAssign
24+
{
25+
public function run()
26+
{
27+
$this->out = ($a = $this->methodX()) ? $a : $this->methodY();
28+
}
29+
}
30+
31+
?>

rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function refactor(Node $node): ?Node
125125
return null;
126126
}
127127

128-
if ($ternary->cond instanceof BinaryOp) {
128+
if ($ternary->cond instanceof BinaryOp || $ternary->cond instanceof Assign) {
129129
$ternary->cond->setAttribute(AttributeKey::ORIGINAL_NODE, null);
130130
}
131131

0 commit comments

Comments
 (0)