2020-06-03 07:23:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Php80\Rector\Catch_;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Stmt\Catch_;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-06-03 07:23:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://wiki.php.net/rfc/non-capturing_catches
|
|
|
|
*
|
|
|
|
* @see \Rector\Php80\Tests\Rector\Catch_\RemoveUnusedVariableInCatchRector\RemoveUnusedVariableInCatchRectorTest
|
|
|
|
*/
|
|
|
|
final class RemoveUnusedVariableInCatchRector extends AbstractRector
|
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2020-06-03 07:23:39 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition('Remove unused variable in catch()', [
|
2020-06-03 07:23:39 +00:00
|
|
|
new CodeSample(
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2020-06-03 07:23:39 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
} catch (Throwable $notUsedThrowable) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-06-03 07:23:39 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2020-06-03 07:23:39 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
} catch (Throwable) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-06-03 07:23:39 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-06-03 07:23:39 +00:00
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [Catch_::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Catch_ $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
$caughtVar = $node->var;
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $caughtVar instanceof Variable) {
|
2020-06-03 07:23:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-25 00:22:45 +00:00
|
|
|
if ($this->isVariableUsed($node->stmts, $caughtVar)) {
|
2020-06-03 07:23:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$node->var = null;
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Node[] $nodes
|
|
|
|
*/
|
|
|
|
private function isVariableUsed(array $nodes, Variable $variable): bool
|
|
|
|
{
|
2020-09-01 17:56:30 +00:00
|
|
|
return (bool) $this->betterNodeFinder->findFirst($nodes, function (Node $node) use ($variable): bool {
|
2020-06-03 07:23:39 +00:00
|
|
|
if (! $node instanceof Variable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:01:23 +00:00
|
|
|
return $this->nodeComparator->areNodesEqual($node, $variable);
|
2020-06-03 07:23:39 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|