Skip to content

Commit 13e2191

Browse files
committed
Fix removing elseifs that should be skipped
I accidentally put an && ! condition rather than an ||, causing elseifs with skippable exprs to get removed rather than being skipped. - Fix the conditional - Add a test for skippable exprs in elseif condition.
1 parent 2d7c59c commit 13e2191

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlocksRector\Fixture;
4+
5+
class SkipPropertyFetch
6+
{
7+
public function run($obj, $condition)
8+
{
9+
if ($condition) {
10+
echo '1';
11+
} elseif ($obj->v) {
12+
echo '2';
13+
}
14+
}
15+
}
16+
17+
?>

rules/DeadCode/Rector/If_/RemoveDeadIfBlocksRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function refactor(Node $node): int|null|If_
112112
foreach ($node->elseifs as $elseif) {
113113
$keep_elseifs = array_filter(
114114
$node->elseifs,
115-
fn ($elseif) => $elseif->stmts !== [] && ! $this->shouldSkipExpr($elseif->cond)
115+
fn ($elseif) => $elseif->stmts !== [] || $this->shouldSkipExpr($elseif->cond)
116116
);
117117
if (count($node->elseifs) !== count($keep_elseifs)) {
118118
$node->elseifs = $keep_elseifs;

0 commit comments

Comments
 (0)