Skip to content

Commit 930e7f5

Browse files
authored
[DeadCode] Handle crash on multi elseif on RemoveDeadIfBlockRector (#7946)
1 parent 92104b0 commit 930e7f5

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;
4+
5+
class MultiElseIfWithBinaryOp
6+
{
7+
public function run($value, $flag)
8+
{
9+
if ($flag) {
10+
} elseif ($value === 2) {
11+
} elseif ($value === 3 || $value === 4) {
12+
echo "matched";
13+
}
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;
22+
23+
class MultiElseIfWithBinaryOp
24+
{
25+
public function run($value, $flag)
26+
{
27+
if (!$flag && ($value === 3 || $value === 4)) {
28+
echo "matched";
29+
}
30+
}
31+
}
32+
33+
?>

rules/DeadCode/Rector/If_/RemoveDeadIfBlockRector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ public function refactor(Node $node): int|null|If_
102102
}
103103

104104
foreach ($node->elseifs as $elseif) {
105-
$keep_elseifs = array_filter(
105+
$keep_elseifs = array_values(array_filter(
106106
$node->elseifs,
107107
fn (ElseIf_ $elseif): bool => $elseif->stmts !== [] || $this->sideEffectNodeDetector->detect(
108108
$elseif->cond
109109
)
110-
);
110+
));
111111
if (count($node->elseifs) !== count($keep_elseifs)) {
112112
$node->elseifs = $keep_elseifs;
113113
return $this->refactor($node) ?? $node;

0 commit comments

Comments
 (0)