2020-06-03 07:23:39 +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\Php80\Rector\Catch_;
|
2020-06-03 07:23:39 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
2023-07-09 13:58:38 +00:00
|
|
|
use PhpParser\Node\Stmt\TryCatch;
|
|
|
|
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
|
|
|
|
use Rector\Core\NodeManipulator\StmtsManipulator;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-06-03 07:23:39 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://wiki.php.net/rfc/non-capturing_catches
|
2020-06-03 07:23:39 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector\RemoveUnusedVariableInCatchRectorTest
|
2020-06-03 07:23:39 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveUnusedVariableInCatchRector extends AbstractRector implements MinPhpVersionInterface
|
2020-06-03 07:23:39 +00:00
|
|
|
{
|
2023-07-09 13:58:38 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Core\NodeManipulator\StmtsManipulator
|
|
|
|
*/
|
|
|
|
private $stmtsManipulator;
|
|
|
|
public function __construct(StmtsManipulator $stmtsManipulator)
|
|
|
|
{
|
|
|
|
$this->stmtsManipulator = $stmtsManipulator;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-06-03 07:23:39 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove unused variable in catch()', [new CodeSample(<<<'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
|
2021-05-09 20:15:43 +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
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
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
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-06-03 07:23:39 +00:00
|
|
|
{
|
2023-07-09 13:58:38 +00:00
|
|
|
return [StmtsAwareInterface::class];
|
2020-06-03 07:23:39 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-07-09 13:58:38 +00:00
|
|
|
* @param StmtsAwareInterface $node
|
2020-06-03 07:23:39 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-06-03 07:23:39 +00:00
|
|
|
{
|
2023-07-09 13:58:38 +00:00
|
|
|
if ($node->stmts === null) {
|
2020-06-03 07:23:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-07-12 07:23:56 +00:00
|
|
|
$hasChanged = \false;
|
2023-07-09 13:58:38 +00:00
|
|
|
foreach ($node->stmts as $key => $stmt) {
|
|
|
|
if (!$stmt instanceof TryCatch) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($stmt->catches as $catch) {
|
|
|
|
$caughtVar = $catch->var;
|
|
|
|
if (!$caughtVar instanceof Variable) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/** @var string $variableName */
|
|
|
|
$variableName = $this->getName($caughtVar);
|
|
|
|
$isVariableUsed = (bool) $this->betterNodeFinder->findVariableOfName($catch->stmts, $variableName);
|
|
|
|
if ($isVariableUsed) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, $variableName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$catch->var = null;
|
2023-07-12 07:23:56 +00:00
|
|
|
$hasChanged = \true;
|
2023-07-09 13:58:38 +00:00
|
|
|
}
|
2020-06-03 07:23:39 +00:00
|
|
|
}
|
2023-07-12 07:23:56 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
return null;
|
2020-06-03 07:23:39 +00:00
|
|
|
}
|
2021-09-21 11:18:03 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::NON_CAPTURING_CATCH;
|
2021-09-21 11:18:03 +00:00
|
|
|
}
|
2020-06-03 07:23:39 +00:00
|
|
|
}
|