2020-02-14 10:25:21 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\DeadCode\Rector\TryCatch;
|
2020-02-14 10:25:21 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Stmt\Finally_;
|
|
|
|
use PhpParser\Node\Stmt\Nop;
|
|
|
|
use PhpParser\Node\Stmt\Throw_;
|
|
|
|
use PhpParser\Node\Stmt\TryCatch;
|
2023-06-05 09:32:57 +00:00
|
|
|
use PhpParser\NodeTraverser;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-02-14 10:25:21 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector\RemoveDeadTryCatchRectorTest
|
2020-02-14 10:25:21 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveDeadTryCatchRector extends AbstractRector
|
2020-02-14 10:25:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-02-14 10:25:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove dead try/catch', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-02-14 10:25:21 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
// some code
|
|
|
|
}
|
|
|
|
catch (Throwable $throwable) {
|
|
|
|
throw $throwable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-02-14 10:25:21 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-02-14 10:25:21 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-02-14 10:25:21 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-02-14 10:25:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [TryCatch::class];
|
2020-02-14 10:25:21 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param TryCatch $node
|
2023-06-05 09:32:57 +00:00
|
|
|
* @return Stmt[]|null|TryCatch|int
|
2020-02-14 10:25:21 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node)
|
2020-02-14 10:25:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$isEmptyFinallyStmts = !$node->finally instanceof Finally_ || $this->isEmpty($node->finally->stmts);
|
2022-02-23 08:20:48 +00:00
|
|
|
// not empty stmts on finally always executed
|
|
|
|
if (!$isEmptyFinallyStmts) {
|
2020-02-14 10:25:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-02-23 08:20:48 +00:00
|
|
|
if ($this->isEmpty($node->stmts)) {
|
2023-06-05 09:32:57 +00:00
|
|
|
return NodeTraverser::REMOVE_NODE;
|
2022-02-23 08:20:48 +00:00
|
|
|
}
|
|
|
|
if (\count($node->catches) !== 1) {
|
2020-02-14 10:25:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-02-23 08:20:48 +00:00
|
|
|
$onlyCatch = $node->catches[0];
|
|
|
|
if ($this->isEmpty($onlyCatch->stmts)) {
|
2020-08-28 21:08:45 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-02-14 10:25:21 +00:00
|
|
|
$onlyCatchStmt = $onlyCatch->stmts[0];
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$onlyCatchStmt instanceof Throw_) {
|
2020-02-14 10:25:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($onlyCatch->var, $onlyCatchStmt->expr)) {
|
2020-02-14 10:25:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-06 08:23:37 +00:00
|
|
|
return $node->stmts;
|
2020-02-14 10:25:21 +00:00
|
|
|
}
|
2022-02-23 08:20:48 +00:00
|
|
|
/**
|
|
|
|
* @param Stmt[] $stmts
|
|
|
|
*/
|
|
|
|
private function isEmpty(array $stmts) : bool
|
|
|
|
{
|
|
|
|
if ($stmts === []) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
if (\count($stmts) > 1) {
|
|
|
|
return \false;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return $stmts[0] instanceof Nop;
|
2022-02-23 08:20:48 +00:00
|
|
|
}
|
2020-02-14 10:25:21 +00:00
|
|
|
}
|