[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:
Abdul Malik Ikhsan 2021-02-18 20:18:34 +07:00 committed by GitHub
parent c609697f14
commit fd6f48c576
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 226 additions and 0 deletions

View 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;
}
}

View File

@ -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()
{
}
}
?>

View File

@ -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)
{
}
}
?>

View File

@ -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)
{
}
}
?>

View File

@ -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;
}
}
}
?>

View File

@ -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()
{
}
}
?>

View File

@ -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;
}
}