Updated Rector to commit 70287f1c96f3814ceb544491d03baae3c03f8d65

70287f1c96 [Naming] Remove parent lookup on ForeachMatcher (#4293)
This commit is contained in:
Tomas Votruba 2023-06-19 19:32:10 +00:00
parent 8bb866241c
commit 296985db95
6 changed files with 54 additions and 46 deletions

View File

@ -8,7 +8,6 @@ use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Naming\ValueObject\VariableAndCallForeach;
use Rector\NodeNameResolver\NodeNameResolver;
final class ForeachMatcher
@ -23,18 +22,15 @@ final class ForeachMatcher
* @var \Rector\Naming\Matcher\CallMatcher
*/
private $callMatcher;
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
public function __construct(NodeNameResolver $nodeNameResolver, \Rector\Naming\Matcher\CallMatcher $callMatcher, BetterNodeFinder $betterNodeFinder)
public function __construct(NodeNameResolver $nodeNameResolver, \Rector\Naming\Matcher\CallMatcher $callMatcher)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->callMatcher = $callMatcher;
$this->betterNodeFinder = $betterNodeFinder;
}
public function match(Foreach_ $foreach) : ?VariableAndCallForeach
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Stmt\Function_ $functionLike
*/
public function match(Foreach_ $foreach, $functionLike) : ?VariableAndCallForeach
{
$call = $this->callMatcher->matchCall($foreach);
if ($call === null) {
@ -43,21 +39,10 @@ final class ForeachMatcher
if (!$foreach->valueVar instanceof Variable) {
return null;
}
$functionLike = $this->getFunctionLike($foreach);
if ($functionLike === null) {
return null;
}
$variableName = $this->nodeNameResolver->getName($foreach->valueVar);
if ($variableName === null) {
return null;
}
return new VariableAndCallForeach($foreach->valueVar, $call, $variableName, $functionLike);
}
/**
* @return \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure|null
*/
private function getFunctionLike(Foreach_ $foreach)
{
return $this->betterNodeFinder->findParentByTypes($foreach, [Closure::class, ClassMethod::class, Function_::class]);
}
}

View File

@ -4,7 +4,12 @@ declare (strict_types=1);
namespace Rector\Naming\Rector\Foreach_;
use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeTraverser;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\Guard\BreakingVariableRenameGuard;
use Rector\Naming\Matcher\ForeachMatcher;
@ -85,28 +90,46 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [Foreach_::class];
return [ClassMethod::class, Closure::class, Function_::class];
}
/**
* @param Foreach_ $node
* @param ClassMethod|Closure|Function_ $node
*/
public function refactor(Node $node) : ?Node
{
$variableAndCallForeach = $this->foreachMatcher->match($node);
if (!$variableAndCallForeach instanceof VariableAndCallForeach) {
if ($node->stmts === null) {
return null;
}
$expectedName = $this->expectedNameResolver->resolveForForeach($variableAndCallForeach);
if ($expectedName === null) {
$hasRenamed = \false;
$this->traverseNodesWithCallable($node->stmts, function (Node $subNode) use($node, &$hasRenamed) : ?int {
if ($subNode instanceof Class_ || $subNode instanceof Closure || $subNode instanceof Function_) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if (!$subNode instanceof Foreach_) {
return null;
}
$variableAndCallForeach = $this->foreachMatcher->match($subNode, $node);
if (!$variableAndCallForeach instanceof VariableAndCallForeach) {
return null;
}
$expectedName = $this->expectedNameResolver->resolveForForeach($variableAndCallForeach);
if ($expectedName === null) {
return null;
}
if ($this->isName($variableAndCallForeach->getVariable(), $expectedName)) {
return null;
}
if ($this->shouldSkip($variableAndCallForeach, $expectedName)) {
return null;
}
$hasChanged = $this->variableRenamer->renameVariableInFunctionLike($variableAndCallForeach->getFunctionLike(), $variableAndCallForeach->getVariableName(), $expectedName, null);
// use different variable on purpose to avoid variable re-assign back to false
// after go to other method
if ($hasChanged) {
$hasRenamed = \true;
}
return null;
}
if ($this->isName($variableAndCallForeach->getVariable(), $expectedName)) {
return null;
}
if ($this->shouldSkip($variableAndCallForeach, $expectedName)) {
return null;
}
$hasRenamed = $this->variableRenamer->renameVariableInFunctionLike($variableAndCallForeach->getFunctionLike(), $variableAndCallForeach->getVariableName(), $expectedName, null);
});
if ($hasRenamed) {
return $node;
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '253b4f6e866fbdcd7381d22ea84f17005b233bfb';
public const PACKAGE_VERSION = '70287f1c96f3814ceb544491d03baae3c03f8d65';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-06-20 01:36:03';
public const RELEASE_DATE = '2023-06-19 19:26:43';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

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

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit66e94e9e635d093c46811c8fdf3e53ef
class ComposerAutoloaderInit10b480a67eccbee700c750829c01aafa
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit66e94e9e635d093c46811c8fdf3e53ef
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit66e94e9e635d093c46811c8fdf3e53ef', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit10b480a67eccbee700c750829c01aafa', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit66e94e9e635d093c46811c8fdf3e53ef', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit10b480a67eccbee700c750829c01aafa', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit66e94e9e635d093c46811c8fdf3e53ef::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit10b480a67eccbee700c750829c01aafa::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit66e94e9e635d093c46811c8fdf3e53ef::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit10b480a67eccbee700c750829c01aafa::$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 ComposerStaticInit66e94e9e635d093c46811c8fdf3e53ef
class ComposerStaticInit10b480a67eccbee700c750829c01aafa
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3096,9 +3096,9 @@ class ComposerStaticInit66e94e9e635d093c46811c8fdf3e53ef
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit66e94e9e635d093c46811c8fdf3e53ef::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit66e94e9e635d093c46811c8fdf3e53ef::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit66e94e9e635d093c46811c8fdf3e53ef::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit10b480a67eccbee700c750829c01aafa::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit10b480a67eccbee700c750829c01aafa::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit10b480a67eccbee700c750829c01aafa::$classMap;
}, null, ClassLoader::class);
}