mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 23:10:51 +00:00
[DeadCode] Remove RemoveCodeAfterReturnRector, already handled at RemoveUnreachableStatementRector (#2199)
* [DeadCode] Remove RemoveCodeAfterReturnRector, already handled at RemoveUnreachableStatementRector * clean up
This commit is contained in:
parent
3f1f632c04
commit
039181ac6c
|
@ -1,4 +1,4 @@
|
|||
# 513 Rules Overview
|
||||
# 512 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
|||
|
||||
- [Composer](#composer) (6)
|
||||
|
||||
- [DeadCode](#deadcode) (49)
|
||||
- [DeadCode](#deadcode) (48)
|
||||
|
||||
- [DependencyInjection](#dependencyinjection) (2)
|
||||
|
||||
|
@ -2787,25 +2787,6 @@ return static function (RectorConfig $rectorConfig): void {
|
|||
|
||||
<br>
|
||||
|
||||
### RemoveCodeAfterReturnRector
|
||||
|
||||
Remove dead code after return statement
|
||||
|
||||
- class: [`Rector\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector`](../rules/DeadCode/Rector/FunctionLike/RemoveCodeAfterReturnRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run(int $a)
|
||||
{
|
||||
return $a;
|
||||
- $a++;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### RemoveConcatAutocastRector
|
||||
|
||||
Remove (string) casting when it comes to concat, that does this by default
|
||||
|
|
|
@ -30,7 +30,6 @@ use Rector\DeadCode\Rector\For_\RemoveDeadContinueRector;
|
|||
use Rector\DeadCode\Rector\For_\RemoveDeadIfForeachForRector;
|
||||
use Rector\DeadCode\Rector\For_\RemoveDeadLoopRector;
|
||||
use Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector;
|
||||
use Rector\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector;
|
||||
use Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector;
|
||||
use Rector\DeadCode\Rector\FunctionLike\RemoveDuplicatedIfReturnRector;
|
||||
use Rector\DeadCode\Rector\FunctionLike\RemoveOverriddenValuesRector;
|
||||
|
@ -66,7 +65,6 @@ return static function (RectorConfig $rectorConfig): void {
|
|||
$rectorConfig->rule(RemoveUnusedPrivatePropertyRector::class);
|
||||
$rectorConfig->rule(RemoveUnusedPrivateClassConstantRector::class);
|
||||
$rectorConfig->rule(RemoveUnusedPrivateMethodRector::class);
|
||||
$rectorConfig->rule(RemoveCodeAfterReturnRector::class);
|
||||
$rectorConfig->rule(RemoveDeadConstructorRector::class);
|
||||
$rectorConfig->rule(RemoveDeadReturnRector::class);
|
||||
$rectorConfig->rule(RemoveDeadContinueRector::class);
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
public function run(int $a)
|
||||
{
|
||||
return $a;
|
||||
$a++;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
public function run(int $a)
|
||||
{
|
||||
return $a;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector\Fixture;
|
||||
|
||||
class KeepComment
|
||||
{
|
||||
public function returnStuff()
|
||||
{
|
||||
return 'something'; // this comment explains stuff
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector\Fixture;
|
||||
|
||||
class KeepNested
|
||||
{
|
||||
public function run(int $a)
|
||||
{
|
||||
if ($a > 5) {
|
||||
return $a;
|
||||
} else
|
||||
|
||||
$a++;
|
||||
|
||||
return $a;
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class RemoveCodeAfterReturnRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector;
|
||||
|
||||
return static function (RectorConfig $rectorConfig): void {
|
||||
$rectorConfig->rule(RemoveCodeAfterReturnRector::class);
|
||||
};
|
|
@ -1,91 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DeadCode\Rector\FunctionLike;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Nop;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector\RemoveCodeAfterReturnRectorTest
|
||||
*/
|
||||
final class RemoveCodeAfterReturnRector extends AbstractRector
|
||||
{
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Remove dead code after return statement', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run(int $a)
|
||||
{
|
||||
return $a;
|
||||
$a++;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run(int $a)
|
||||
{
|
||||
return $a;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Closure::class, ClassMethod::class, Function_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Closure|ClassMethod|Function_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->stmts === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$isDeadAfterReturn = false;
|
||||
foreach ($node->stmts as $key => $stmt) {
|
||||
if ($isDeadAfterReturn) {
|
||||
if (! isset($node->stmts[$key])) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
|
||||
// keep comment
|
||||
/** @var int $key */
|
||||
if ($node->stmts[$key] instanceof Nop) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->nodeRemover->removeStmt($node, $key);
|
||||
}
|
||||
|
||||
if ($stmt instanceof Return_) {
|
||||
$isDeadAfterReturn = true;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user