Skip to content

Commit 9dccc8a

Browse files
[CodeQuality] Handle with assign on SimplifyIfElseToTernaryRector (#7951)
* [CodeQuality] Handle with assign on SimplifyIfElseToTernaryRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 4fb2a68 commit 9dccc8a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed
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)