Updated Rector to commit 14089145f699c64437b484209998963d3c75b0a1

14089145f6 [Rector] Skip used after TryCatch on RemoveUnusedVariableInCatchRector (#4459)
This commit is contained in:
Tomas Votruba 2023-07-09 13:58:38 +00:00
parent e24fff2c36
commit 001414980a
6 changed files with 74 additions and 24 deletions

View File

@ -5,7 +5,9 @@ namespace Rector\Php80\Rector\Catch_;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\TryCatch;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeManipulator\StmtsManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
@ -18,6 +20,15 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
*/
final class RemoveUnusedVariableInCatchRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\Core\NodeManipulator\StmtsManipulator
*/
private $stmtsManipulator;
public function __construct(StmtsManipulator $stmtsManipulator)
{
$this->stmtsManipulator = $stmtsManipulator;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Remove unused variable in catch()', [new CodeSample(<<<'CODE_SAMPLE'
@ -49,24 +60,37 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [Catch_::class];
return [StmtsAwareInterface::class];
}
/**
* @param Catch_ $node
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node) : ?Node
{
$caughtVar = $node->var;
if (!$caughtVar instanceof Variable) {
if ($node->stmts === null) {
return null;
}
/** @var string $variableName */
$variableName = $this->getName($caughtVar);
$isVariableUsed = (bool) $this->betterNodeFinder->findVariableOfName($node->stmts, $variableName);
if ($isVariableUsed) {
return null;
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;
}
}
$node->var = null;
return $node;
}
public function provideMinPhpVersion() : int

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '68d092c547af462f8e036a6d06bd343e15103901';
public const PACKAGE_VERSION = '14089145f699c64437b484209998963d3c75b0a1';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-07-09 13:46:55';
public const RELEASE_DATE = '2023-07-09 20:54:40';
/**
* @var int
*/

View File

@ -7,7 +7,9 @@ use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
final class StmtsManipulator
{
@ -16,14 +18,20 @@ final class StmtsManipulator
* @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
*/
private $simpleCallableNodeTraverser;
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
/**
* @readonly
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
*/
private $nodeComparator;
public function __construct(SimpleCallableNodeTraverser $simpleCallableNodeTraverser, NodeComparator $nodeComparator)
public function __construct(SimpleCallableNodeTraverser $simpleCallableNodeTraverser, BetterNodeFinder $betterNodeFinder, NodeComparator $nodeComparator)
{
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
$this->betterNodeFinder = $betterNodeFinder;
$this->nodeComparator = $nodeComparator;
}
/**
@ -56,4 +64,22 @@ final class StmtsManipulator
});
return $stmts;
}
public function isVariableUsedInNextStmt(StmtsAwareInterface $stmtsAware, int $jumpToKey, string $variableName) : bool
{
if ($stmtsAware->stmts === null) {
return \false;
}
\end($stmtsAware->stmts);
$totalKeys = \key($stmtsAware->stmts);
for ($key = $jumpToKey; $key <= $totalKeys; ++$key) {
if (!isset($stmtsAware->stmts[$key])) {
continue;
}
$isVariableUsed = (bool) $this->betterNodeFinder->findVariableOfName($stmtsAware->stmts[$key], $variableName);
if ($isVariableUsed) {
return \true;
}
}
return \false;
}
}

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit107bf610d34d7675e946cb541511a643::getLoader();
return ComposerAutoloaderInit4416ec183070b953c5f3ccaf40dc7d20::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit107bf610d34d7675e946cb541511a643
class ComposerAutoloaderInit4416ec183070b953c5f3ccaf40dc7d20
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit107bf610d34d7675e946cb541511a643
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit107bf610d34d7675e946cb541511a643', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit4416ec183070b953c5f3ccaf40dc7d20', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit107bf610d34d7675e946cb541511a643', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit4416ec183070b953c5f3ccaf40dc7d20', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit107bf610d34d7675e946cb541511a643::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit4416ec183070b953c5f3ccaf40dc7d20::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit107bf610d34d7675e946cb541511a643::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit4416ec183070b953c5f3ccaf40dc7d20::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit107bf610d34d7675e946cb541511a643
class ComposerStaticInit4416ec183070b953c5f3ccaf40dc7d20
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3068,9 +3068,9 @@ class ComposerStaticInit107bf610d34d7675e946cb541511a643
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit107bf610d34d7675e946cb541511a643::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit107bf610d34d7675e946cb541511a643::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit107bf610d34d7675e946cb541511a643::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit4416ec183070b953c5f3ccaf40dc7d20::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit4416ec183070b953c5f3ccaf40dc7d20::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit4416ec183070b953c5f3ccaf40dc7d20::$classMap;
}, null, ClassLoader::class);
}