-
-
Notifications
You must be signed in to change notification settings - Fork 670
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1261 from rectorphp/dead-if-foreach-for
[DeadCode] Add RemoveDeadIfForeachForRector
- Loading branch information
Showing
6 changed files
with
262 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
packages/DeadCode/src/Rector/For_/RemoveDeadIfForeachForRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\Rector\For_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Scalar; | ||
use PhpParser\Node\Stmt\For_; | ||
use PhpParser\Node\Stmt\Foreach_; | ||
use PhpParser\Node\Stmt\If_; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\RectorDefinition\CodeSample; | ||
use Rector\RectorDefinition\RectorDefinition; | ||
|
||
final class RemoveDeadIfForeachForRector extends AbstractRector | ||
{ | ||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition('Remove if, foreach and for that does not do anything', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run($someObject) | ||
{ | ||
$value = 5; | ||
if ($value) { | ||
} | ||
if ($someObject->run()) { | ||
} | ||
foreach ($values as $value) { | ||
} | ||
return $value; | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run($someObject) | ||
{ | ||
$value = 5; | ||
if ($someObject->run()) { | ||
} | ||
return $value; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [For_::class, If_::class, Foreach_::class]; | ||
} | ||
|
||
/** | ||
* @param For_|If_|Foreach_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node instanceof If_) { | ||
return $this->processIf($node); | ||
} | ||
|
||
if ($node instanceof Foreach_) { | ||
return $this->processForeach($node); | ||
} | ||
|
||
if ($node instanceof For_) { | ||
if ($node->stmts) { | ||
return null; | ||
} | ||
|
||
$this->removeNode($node); | ||
|
||
return null; | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
private function processIf(If_ $if): ?If_ | ||
{ | ||
if ($if->stmts) { | ||
return null; | ||
} | ||
|
||
if ($if->else) { | ||
return null; | ||
} | ||
|
||
if ($if->elseifs) { | ||
return null; | ||
} | ||
|
||
if ($this->isNodeWithSideEffect($if->cond)) { | ||
return null; | ||
} | ||
|
||
$this->removeNode($if); | ||
|
||
return null; | ||
} | ||
|
||
private function processForeach(Foreach_ $node): ?Foreach_ | ||
{ | ||
if ($node->stmts) { | ||
return null; | ||
} | ||
|
||
if ($this->isNodeWithSideEffect($node->expr)) { | ||
return null; | ||
} | ||
|
||
$this->removeNode($node); | ||
|
||
return null; | ||
} | ||
|
||
private function isNodeWithSideEffect(Expr $expr): bool | ||
{ | ||
if ($expr instanceof Variable) { | ||
return false; | ||
} | ||
|
||
if ($expr instanceof Scalar) { | ||
return false; | ||
} | ||
|
||
if ($this->isBool($expr)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
packages/DeadCode/tests/Rector/For_/RemoveDeadIfForeachForRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadIfForeachForRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run($someObject) | ||
{ | ||
$value = 5; | ||
if ($value) { | ||
} | ||
|
||
if ($someObject->run()) { | ||
} | ||
|
||
$values = []; | ||
foreach ($values as $value) { | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
public function forMe() | ||
{ | ||
for ($i = 0; $i < 5; ++$i) { | ||
} | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadIfForeachForRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run($someObject) | ||
{ | ||
$value = 5; | ||
|
||
if ($someObject->run()) { | ||
} | ||
|
||
$values = []; | ||
|
||
return $value; | ||
} | ||
|
||
public function forMe() | ||
{ | ||
} | ||
} | ||
|
||
?> |
35 changes: 35 additions & 0 deletions
35
...eadCode/tests/Rector/For_/RemoveDeadIfForeachForRector/Fixture/side_effect_checks.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadIfForeachForRector\Fixture; | ||
|
||
class SideEffectChecks | ||
{ | ||
public function run() | ||
{ | ||
if (10) { | ||
} | ||
|
||
if (true) { | ||
} | ||
|
||
if (5 + 10) { | ||
} | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadIfForeachForRector\Fixture; | ||
|
||
class SideEffectChecks | ||
{ | ||
public function run() | ||
{ | ||
if (5 + 10) { | ||
} | ||
} | ||
} | ||
|
||
?> |
22 changes: 22 additions & 0 deletions
22
...dCode/tests/Rector/For_/RemoveDeadIfForeachForRector/RemoveDeadIfForeachForRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadIfForeachForRector; | ||
|
||
use Rector\DeadCode\Rector\For_\RemoveDeadIfForeachForRector; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class RemoveDeadIfForeachForRectorTest extends AbstractRectorTestCase | ||
{ | ||
public function test(): void | ||
{ | ||
$this->doTestFiles([ | ||
__DIR__ . '/Fixture/fixture.php.inc', | ||
__DIR__ . '/Fixture/side_effect_checks.php.inc', | ||
]); | ||
} | ||
|
||
protected function getRectorClass(): string | ||
{ | ||
return RemoveDeadIfForeachForRector::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters