Skip to content

Commit

Permalink
[DeadCode] Register more Stmt Nodes on RemoveUnreachableStatementRect…
Browse files Browse the repository at this point in the history
…or (#2140)

* [DeadCode] Register more Stmt Nodes on RemoveUnreachableStatementRector

* add try

* final touch: exclude ArrowFunction FunctionLike as only 1 stmt

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 23, 2022
1 parent 365b289 commit 469f0ee
Show file tree
Hide file tree
Showing 9 changed files with 334 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

class SomeCase
{
public function run()
{
switch ($a) {
case 'a':
return 'A';
break;
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

class SomeCase
{
public function run()
{
switch ($a) {
case 'a':
return 'A';
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;
use RuntimeException;

class SomeCatch
{
public function run()
{
try {

} catch (Exception $e) {
throw new RuntimeException($e->getMesssage());
echo 'test';
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;
use RuntimeException;

class SomeCatch
{
public function run()
{
try {

} catch (Exception $e) {
throw new RuntimeException($e->getMesssage());
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;

class SomeDo
{
public function run()
{
$i = 1;
do {
throw new Exception();
echo 'test';
} while (++$i < 10);
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;

class SomeDo
{
public function run()
{
$i = 1;
do {
throw new Exception();
} while (++$i < 10);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;

class SomeElseIf
{
public function run()
{
if (rand(0, 1)) {

} elseif (rand(0, 1)) {
throw new Exception();
echo 'test';
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;

class SomeElseIf
{
public function run()
{
if (rand(0, 1)) {

} elseif (rand(0, 1)) {
throw new Exception();
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;
use RuntimeException;

class SomeFinally
{
public function run()
{
try {

} finally {
throw new RuntimeException();
echo 'test';
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;
use RuntimeException;

class SomeFinally
{
public function run()
{
try {

} finally {
throw new RuntimeException();
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;

class SomeFor
{
public function run()
{
for ($i = 1; $i<10;++$i) {
throw new Exception();
echo 'test';
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;

class SomeFor
{
public function run()
{
for ($i = 1; $i<10;++$i) {
throw new Exception();
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use RuntimeException;

class SomeTry
{
public function run()
{
try {
throw new RuntimeException();
echo 'test';
} finally {

}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use RuntimeException;

class SomeTry
{
public function run()
{
try {
throw new RuntimeException();
} finally {

}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;

class SomeWhile
{
public function run()
{
while (++$i < 10) {
throw new Exception();
echo 'test';
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;

use Exception;

class SomeWhile
{
public function run()
{
while (++$i < 10) {
throw new Exception();
}
}
}

?>
29 changes: 26 additions & 3 deletions rules/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@
namespace Rector\DeadCode\Rector\Stmt;

use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Exit_;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Case_;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Finally_;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\Node\Stmt\TryCatch;
use PhpParser\Node\Stmt\While_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -63,11 +71,26 @@ public function run()
*/
public function getNodeTypes(): array
{
return [Foreach_::class, FunctionLike::class, Else_::class, If_::class];
return [
For_::class,
Foreach_::class,
Do_::class,
While_::class,
ClassMethod::class,
Function_::class,
Closure::class,
If_::class,
ElseIf_::class,
Else_::class,
Case_::class,
TryCatch::class,
Catch_::class,
Finally_::class,
];
}

/**
* @param Foreach_|FunctionLike|Else_|If_ $node
* @param For_|Foreach_|Do_|While_|ClassMethod|Function_|Closure|If_|ElseIf_|Else_|Case_|TryCatch|Catch_|Finally_ $node
*/
public function refactor(Node $node): ?Node
{
Expand Down

0 comments on commit 469f0ee

Please sign in to comment.