Skip to content

Commit 91bfe39

Browse files
committed
Updated Rector to commit 936a5e4c3b04a0c1d1ec2ffbd7536616508880de
rectorphp/rector-src@936a5e4 [DeadCode] Add RemoveDeadIfBlocksRector (#7528)
1 parent f8b90ee commit 91bfe39

File tree

5 files changed

+143
-2
lines changed

5 files changed

+143
-2
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\DeadCode\Rector\If_;
5+
6+
use PhpParser\Node;
7+
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
8+
use PhpParser\Node\Stmt\Else_;
9+
use PhpParser\Node\Stmt\If_;
10+
use PhpParser\NodeVisitor;
11+
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
12+
use Rector\EarlyReturn\NodeTransformer\ConditionInverter;
13+
use Rector\Rector\AbstractRector;
14+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
15+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
16+
/**
17+
* @see \Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\RemoveDeadIfBlockRectorTest
18+
*/
19+
final class RemoveDeadIfBlockRector extends AbstractRector
20+
{
21+
/**
22+
* @readonly
23+
*/
24+
private ConditionInverter $conditionInverter;
25+
/**
26+
* @readonly
27+
*/
28+
private SideEffectNodeDetector $sideEffectNodeDetector;
29+
public function __construct(ConditionInverter $conditionInverter, SideEffectNodeDetector $sideEffectNodeDetector)
30+
{
31+
$this->conditionInverter = $conditionInverter;
32+
$this->sideEffectNodeDetector = $sideEffectNodeDetector;
33+
}
34+
public function getRuleDefinition(): RuleDefinition
35+
{
36+
return new RuleDefinition('Remove if, elseif, and else blocks that do not do anything', [new CodeSample(<<<'CODE_SAMPLE'
37+
class SomeClass
38+
{
39+
public function run($value, $differentValue)
40+
{
41+
if ($value) {
42+
} elseif ($differentValue) {
43+
} else {
44+
}
45+
46+
if ($differentValue) {
47+
echo 'different';
48+
} elseif ($value) {
49+
} else {
50+
}
51+
52+
if ($differentValue) {
53+
} elseif ($value) {
54+
echo 'value';
55+
} else {
56+
}
57+
58+
return $differentValue;
59+
}
60+
}
61+
CODE_SAMPLE
62+
, <<<'CODE_SAMPLE'
63+
class SomeClass
64+
{
65+
public function run($value, $differentValue)
66+
{
67+
if ($differentValue) {
68+
echo 'different';
69+
}
70+
71+
if (!$differentValue && $value) {
72+
echo 'value';
73+
}
74+
75+
return $differentValue;
76+
}
77+
}
78+
CODE_SAMPLE
79+
)]);
80+
}
81+
/**
82+
* @return array<class-string<Node>>
83+
*/
84+
public function getNodeTypes(): array
85+
{
86+
return [If_::class];
87+
}
88+
/**
89+
* @param If_ $node
90+
* @return NodeVisitor::REMOVE_NODE|null|If_
91+
*/
92+
public function refactor(Node $node)
93+
{
94+
if ($node->else instanceof Else_ && $node->else->stmts === []) {
95+
$node->else = null;
96+
return $this->refactor($node) ?? $node;
97+
}
98+
if ($node->elseifs !== []) {
99+
foreach ($node->elseifs as $elseif) {
100+
$keep_elseifs = array_filter($node->elseifs, fn($elseif) => $elseif->stmts !== [] || $this->sideEffectNodeDetector->detect($elseif->cond));
101+
if (count($node->elseifs) !== count($keep_elseifs)) {
102+
$node->elseifs = $keep_elseifs;
103+
return $this->refactor($node) ?? $node;
104+
}
105+
}
106+
}
107+
if ($node->stmts !== []) {
108+
return null;
109+
}
110+
// Skip commented blocks because we can't know
111+
// if the comment will make sense after merging.
112+
if ($node->getComments() !== []) {
113+
return null;
114+
}
115+
if ($this->sideEffectNodeDetector->detect($node->cond)) {
116+
return null;
117+
}
118+
// When the if body is blank but it has an elseif,
119+
// merge the negated if condition with the elseif condition
120+
if ($node->elseifs !== []) {
121+
$firstElseIf = $node->elseifs[0];
122+
$cond = new BooleanAnd($this->conditionInverter->createInvertedCondition($node->cond), $firstElseIf->cond);
123+
$if = new If_($cond, ['stmts' => $firstElseIf->stmts]);
124+
if (count($node->elseifs) > 1) {
125+
$if->elseifs = \array_slice($node->elseifs, 1);
126+
}
127+
return $this->refactor($if) ?? $if;
128+
}
129+
if ($node->else instanceof Else_) {
130+
$node->cond = $this->conditionInverter->createInvertedCondition($node->cond);
131+
$node->stmts = $node->else->stmts;
132+
$node->else = null;
133+
return $node;
134+
}
135+
return NodeVisitor::REMOVE_NODE;
136+
}
137+
}

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '679e314c916c0c61ef41ca1e40d39694c0ec10b6';
22+
public const PACKAGE_VERSION = '936a5e4c3b04a0c1d1ec2ffbd7536616508880de';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2026-01-13 15:08:44';
27+
public const RELEASE_DATE = '2026-01-13 15:13:06';
2828
/**
2929
* @var int
3030
*/

src/Config/Level/DeadCodeLevel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector;
4141
use Rector\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector;
4242
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
43+
use Rector\DeadCode\Rector\If_\RemoveDeadIfBlockRector;
4344
use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector;
4445
use Rector\DeadCode\Rector\If_\RemoveTypedPropertyDeadInstanceOfRector;
4546
use Rector\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector;
@@ -121,6 +122,7 @@ final class DeadCodeLevel
121122
RemoveDeadInstanceOfRector::class,
122123
RemoveDeadCatchRector::class,
123124
RemoveDeadTryCatchRector::class,
125+
RemoveDeadIfBlockRector::class,
124126
RemoveDeadIfForeachForRector::class,
125127
RemoveConditionExactReturnRector::class,
126128
RemoveDeadStmtRector::class,

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,7 @@
14241424
'Rector\\DeadCode\\Rector\\FunctionLike\\RemoveDeadReturnRector' => $baseDir . '/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php',
14251425
'Rector\\DeadCode\\Rector\\If_\\ReduceAlwaysFalseIfOrRector' => $baseDir . '/rules/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector.php',
14261426
'Rector\\DeadCode\\Rector\\If_\\RemoveAlwaysTrueIfConditionRector' => $baseDir . '/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php',
1427+
'Rector\\DeadCode\\Rector\\If_\\RemoveDeadIfBlockRector' => $baseDir . '/rules/DeadCode/Rector/If_/RemoveDeadIfBlockRector.php',
14271428
'Rector\\DeadCode\\Rector\\If_\\RemoveDeadInstanceOfRector' => $baseDir . '/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php',
14281429
'Rector\\DeadCode\\Rector\\If_\\RemoveTypedPropertyDeadInstanceOfRector' => $baseDir . '/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php',
14291430
'Rector\\DeadCode\\Rector\\If_\\RemoveUnusedNonEmptyArrayBeforeForeachRector' => $baseDir . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php',

vendor/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,7 @@ class ComposerStaticInit7001fb5f7b9b8dbb950c02836c7af51c
16841684
'Rector\\DeadCode\\Rector\\FunctionLike\\RemoveDeadReturnRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php',
16851685
'Rector\\DeadCode\\Rector\\If_\\ReduceAlwaysFalseIfOrRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector.php',
16861686
'Rector\\DeadCode\\Rector\\If_\\RemoveAlwaysTrueIfConditionRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php',
1687+
'Rector\\DeadCode\\Rector\\If_\\RemoveDeadIfBlockRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/RemoveDeadIfBlockRector.php',
16871688
'Rector\\DeadCode\\Rector\\If_\\RemoveDeadInstanceOfRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php',
16881689
'Rector\\DeadCode\\Rector\\If_\\RemoveTypedPropertyDeadInstanceOfRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php',
16891690
'Rector\\DeadCode\\Rector\\If_\\RemoveUnusedNonEmptyArrayBeforeForeachRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php',

0 commit comments

Comments
 (0)