mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-21 02:23:09 +00:00
[DeadCode] Add RemoveDeadLoopRector (#5600)
Co-authored-by: Tomas Votruba <tomas.vot@gmail.com> Co-authored-by: kaizen-ci <info@kaizen-ci.org>
This commit is contained in:
parent
c609697f14
commit
fd6f48c576
71
rules/dead-code/src/Rector/For_/RemoveDeadLoopRector.php
Normal file
71
rules/dead-code/src/Rector/For_/RemoveDeadLoopRector.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DeadCode\Rector\For_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Do_;
|
||||
use PhpParser\Node\Stmt\For_;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use PhpParser\Node\Stmt\While_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\DeadCode\Tests\Rector\For_\RemoveDeadLoopRector\RemoveDeadLoopRectorTest
|
||||
*/
|
||||
final class RemoveDeadLoopRector extends AbstractRector
|
||||
{
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Remove loop with no body',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run($values)
|
||||
{
|
||||
for ($i=1; $i<count($values); ++$i) {
|
||||
}
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run($values)
|
||||
{
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Do_::class, For_::class, Foreach_::class, While_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Do_|For_|Foreach_|While_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->stmts !== []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->removeNode($node);
|
||||
return $node;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadLoopRector\Fixture;
|
||||
|
||||
class Do_
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
do {
|
||||
|
||||
} while (++$i < 10);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadLoopRector\Fixture;
|
||||
|
||||
class Do_
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadLoopRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
public function run($values)
|
||||
{
|
||||
for ($i=1; $i<count($values); ++$i) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadLoopRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
public function run($values)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadLoopRector\Fixture;
|
||||
|
||||
class Foreach_
|
||||
{
|
||||
public function run($values)
|
||||
{
|
||||
foreach ($values as $value) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadLoopRector\Fixture;
|
||||
|
||||
class Foreach_
|
||||
{
|
||||
public function run($values)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadLoopRector\Fixture;
|
||||
|
||||
class SkipHasStmt
|
||||
{
|
||||
public function run($values)
|
||||
{
|
||||
for ($i=1; $i<count($values); ++$i) {
|
||||
echo $i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadLoopRector\Fixture;
|
||||
|
||||
class While_
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
while (++$i < 10) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadLoopRector\Fixture;
|
||||
|
||||
class While_
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\For_\RemoveDeadLoopRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\DeadCode\Rector\For_\RemoveDeadLoopRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class RemoveDeadLoopRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return RemoveDeadLoopRector::class;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user