mirror of https://github.com/rectorphp/rector.git
[DeadCode] Add RemoveDeadTryCatchRector
This commit is contained in:
parent
d4a32b4e1b
commit
af90cba019
|
@ -32,4 +32,4 @@ services:
|
|||
Rector\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector: null
|
||||
Rector\PHPUnit\Rector\ClassMethod\RemoveEmptyTestMethodRector: null
|
||||
Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector: null
|
||||
Rector\dead-code\Rector\TryCatch\RemoveDeadTryCatchRector: null
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# All 449 Rectors Overview
|
||||
# All 450 Rectors Overview
|
||||
|
||||
- [Projects](#projects)
|
||||
- [General](#general)
|
||||
|
@ -2464,6 +2464,30 @@ Removes dead code statements
|
|||
|
||||
<br>
|
||||
|
||||
### `RemoveDeadTryCatchRector`
|
||||
|
||||
- class: `Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector`
|
||||
|
||||
Remove dead try/catch
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- try {
|
||||
- // some code
|
||||
- }
|
||||
- catch (Throwable $throwable) {
|
||||
- throw $throwable;
|
||||
- }
|
||||
+ // some code
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `RemoveDeadZeroAndOneOperationRector`
|
||||
|
||||
- class: `Rector\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector`
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DeadCode\Rector\TryCatch;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Catch_;
|
||||
use PhpParser\Node\Stmt\Throw_;
|
||||
use PhpParser\Node\Stmt\TryCatch;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\DeadCode\Tests\Rector\TryCatch\RemoveDeadTryCatchRector\RemoveDeadTryCatchRectorTest
|
||||
*/
|
||||
final class RemoveDeadTryCatchRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Remove dead try/catch', [
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
try {
|
||||
// some code
|
||||
}
|
||||
catch (Throwable $throwable) {
|
||||
throw $throwable;
|
||||
}
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
// some code
|
||||
}
|
||||
}
|
||||
PHP
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [TryCatch::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TryCatch $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (count($node->catches) !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Catch_ $onlyCatch */
|
||||
$onlyCatch = $node->catches[0];
|
||||
if (count($onlyCatch->stmts) !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$onlyCatchStmt = $onlyCatch->stmts[0];
|
||||
if (! $onlyCatchStmt instanceof Throw_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->areNamesEqual($onlyCatch->var, $onlyCatchStmt->expr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($node->stmts as $tryStmt) {
|
||||
$this->addNodeAfterNode($tryStmt, $node);
|
||||
}
|
||||
|
||||
$this->removeNode($node);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\TryCatch\RemoveDeadTryCatchRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
try {
|
||||
// some code
|
||||
}
|
||||
catch (Throwable $throwable) {
|
||||
throw $throwable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\TryCatch\RemoveDeadTryCatchRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
// some code
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\TryCatch\RemoveDeadTryCatchRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector;
|
||||
|
||||
final class RemoveDeadTryCatchRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(string $file): void
|
||||
{
|
||||
$this->doTestFile($file);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return RemoveDeadTryCatchRector::class;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue