mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-08 01:23:15 +02:00
Updated Rector to commit cfc88dc019bc47deb44280e14940af355dfd1e81
cfc88dc019
[DX] Check only for current scope in RemoveUnusedVariableInCatchRector, as… magic exception use is bad practise and should be avoided (#4449)
This commit is contained in:
parent
2d4dfa7604
commit
bc1a862545
@ -8,7 +8,6 @@ use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\Catch_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
@ -19,15 +18,6 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
*/
|
||||
final class RemoveUnusedVariableInCatchRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer
|
||||
*/
|
||||
private $exprUsedInNodeAnalyzer;
|
||||
public function __construct(ExprUsedInNodeAnalyzer $exprUsedInNodeAnalyzer)
|
||||
{
|
||||
$this->exprUsedInNodeAnalyzer = $exprUsedInNodeAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Remove unused variable in catch()', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
@ -70,10 +60,10 @@ CODE_SAMPLE
|
||||
if (!$caughtVar instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
if ($this->isVariableUsedInStmts($node->stmts, $caughtVar)) {
|
||||
return null;
|
||||
}
|
||||
if ($this->isVariableUsedNext($node, $caughtVar)) {
|
||||
/** @var string $variableName */
|
||||
$variableName = $this->getName($caughtVar);
|
||||
$isVariableUsed = (bool) $this->betterNodeFinder->findVariableOfName($node->stmts, $variableName);
|
||||
if ($isVariableUsed) {
|
||||
return null;
|
||||
}
|
||||
$node->var = null;
|
||||
@ -83,19 +73,4 @@ CODE_SAMPLE
|
||||
{
|
||||
return PhpVersionFeature::NON_CAPTURING_CATCH;
|
||||
}
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
*/
|
||||
private function isVariableUsedInStmts(array $nodes, Variable $variable) : bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirst($nodes, function (Node $node) use($variable) : bool {
|
||||
return $this->exprUsedInNodeAnalyzer->isUsed($node, $variable);
|
||||
});
|
||||
}
|
||||
private function isVariableUsedNext(Catch_ $catch, Variable $variable) : bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirstNext($catch, function (Node $node) use($variable) : bool {
|
||||
return $this->exprUsedInNodeAnalyzer->isUsed($node, $variable);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,6 @@ use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Core\NodeAnalyzer\ScopeAnalyzer;
|
||||
use Rector\Core\Provider\CurrentFileProvider;
|
||||
use Rector\Core\ValueObject\Application\File;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;
|
||||
/**
|
||||
* In case of changed node, we need to re-traverse the PHPStan Scope to make all the new nodes aware of what is going on.
|
||||
@ -69,17 +68,7 @@ final class ChangedNodeScopeRefresher
|
||||
}
|
||||
$mutatingScope = $this->scopeAnalyzer->resolveScope($node, $filePath, $mutatingScope);
|
||||
if (!$mutatingScope instanceof MutatingScope) {
|
||||
/**
|
||||
* @var Node $parentNode
|
||||
*
|
||||
* $parentNode is always a Node when $mutatingScope is null, as checked in previous
|
||||
*
|
||||
* $this->scopeAnalyzer->resolveScope()
|
||||
*
|
||||
* which verify if no parent and no scope, it resolve Scope from File
|
||||
*/
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
$errorMessage = \sprintf('Node "%s" with parent of "%s" is missing scope required for scope refresh.', \get_class($node), \get_class($parentNode));
|
||||
$errorMessage = \sprintf('Node "%s" with is missing scope required for scope refresh', \get_class($node));
|
||||
throw new ShouldNotHappenException($errorMessage);
|
||||
}
|
||||
// note from flight: when we traverse ClassMethod, the scope must be already in Class_, otherwise it crashes
|
||||
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'ce03029084e1d8c860de15ee6fcfef8f6f35dbdb';
|
||||
public const PACKAGE_VERSION = 'cfc88dc019bc47deb44280e14940af355dfd1e81';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-07-09 10:31:17';
|
||||
public const RELEASE_DATE = '2023-07-09 10:37:21';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
@ -175,6 +175,8 @@ final class BetterNodeFinder
|
||||
return $this->nodeFinder->findFirst($nodes, $filter);
|
||||
}
|
||||
/**
|
||||
* @deprecated Use node traversing instead
|
||||
*
|
||||
* @param callable(Node $node): bool $filter
|
||||
*/
|
||||
public function findFirstNext(Node $node, callable $filter) : ?Node
|
||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitedfb2f18c84b12f92d11c1ea4357e517::getLoader();
|
||||
return ComposerAutoloaderInit978c3c65a70434a42ecc983d56681bb6::getLoader();
|
||||
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitedfb2f18c84b12f92d11c1ea4357e517
|
||||
class ComposerAutoloaderInit978c3c65a70434a42ecc983d56681bb6
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,17 +22,17 @@ class ComposerAutoloaderInitedfb2f18c84b12f92d11c1ea4357e517
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitedfb2f18c84b12f92d11c1ea4357e517', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit978c3c65a70434a42ecc983d56681bb6', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitedfb2f18c84b12f92d11c1ea4357e517', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit978c3c65a70434a42ecc983d56681bb6', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitedfb2f18c84b12f92d11c1ea4357e517::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit978c3c65a70434a42ecc983d56681bb6::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitedfb2f18c84b12f92d11c1ea4357e517::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit978c3c65a70434a42ecc983d56681bb6::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
8
vendor/composer/autoload_static.php
vendored
8
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitedfb2f18c84b12f92d11c1ea4357e517
|
||||
class ComposerStaticInit978c3c65a70434a42ecc983d56681bb6
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
@ -3070,9 +3070,9 @@ class ComposerStaticInitedfb2f18c84b12f92d11c1ea4357e517
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitedfb2f18c84b12f92d11c1ea4357e517::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitedfb2f18c84b12f92d11c1ea4357e517::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitedfb2f18c84b12f92d11c1ea4357e517::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit978c3c65a70434a42ecc983d56681bb6::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit978c3c65a70434a42ecc983d56681bb6::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit978c3c65a70434a42ecc983d56681bb6::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user