Updated Rector to commit ffec9f0960d8f26c81b745a218b56a32e187c157

ffec9f0960 Remove parent node dependency from ReplaceEachAssignmentWithKeyCurrentRector (#3751)
This commit is contained in:
Tomas Votruba 2023-05-07 07:53:03 +00:00
parent da6f698ff7
commit f8706c58a3
5 changed files with 36 additions and 46 deletions

View File

@ -11,11 +11,10 @@ use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Stmt\While_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Collector\NodesToAddCollector;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -28,15 +27,6 @@ final class ReplaceEachAssignmentWithKeyCurrentRector extends AbstractRector imp
* @var string
*/
private const KEY = 'key';
/**
* @readonly
* @var \Rector\PostRector\Collector\NodesToAddCollector
*/
private $nodesToAddCollector;
public function __construct(NodesToAddCollector $nodesToAddCollector)
{
$this->nodesToAddCollector = $nodesToAddCollector;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::NO_EACH_OUTSIDE_LOOP;
@ -45,14 +35,17 @@ final class ReplaceEachAssignmentWithKeyCurrentRector extends AbstractRector imp
{
return new RuleDefinition('Replace each() assign outside loop', [new CodeSample(<<<'CODE_SAMPLE'
$array = ['b' => 1, 'a' => 2];
$eachedArray = each($array);
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$array = ['b' => 1, 'a' => 2];
$eachedArray[1] = current($array);
$eachedArray['value'] = current($array);
$eachedArray[0] = key($array);
$eachedArray['key'] = key($array);
next($array);
CODE_SAMPLE
)]);
@ -62,18 +55,23 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [Assign::class];
return [Expression::class];
}
/**
* @param Assign $node
* @param Expression $node
* @return Stmt[]|null
*/
public function refactor(Node $node) : ?Node
public function refactor(Node $node) : ?array
{
if ($this->shouldSkip($node)) {
if (!$node->expr instanceof Assign) {
return null;
}
$assign = $node->expr;
if ($this->shouldSkip($assign)) {
return null;
}
/** @var FuncCall $eachFuncCall */
$eachFuncCall = $node->expr;
$eachFuncCall = $assign->expr;
if (!isset($eachFuncCall->args[0])) {
return null;
}
@ -81,11 +79,8 @@ CODE_SAMPLE
return null;
}
$eachedVariable = $eachFuncCall->args[0]->value;
$assignVariable = $node->var;
$newNodes = $this->createNewNodes($assignVariable, $eachedVariable);
$this->nodesToAddCollector->addNodesAfterNode($newNodes, $node);
$this->removeNode($node);
return null;
$assignVariable = $assign->var;
return $this->createNewStmts($assignVariable, $eachedVariable);
}
private function shouldSkip(Assign $assign) : bool
{
@ -95,22 +90,17 @@ CODE_SAMPLE
if (!$this->nodeNameResolver->isName($assign->expr, 'each')) {
return \true;
}
$parentNode = $assign->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof While_) {
return \true;
}
// skip assign to List
if (!$parentNode instanceof Assign) {
return \false;
}
return $parentNode->var instanceof List_;
return $assign->var instanceof List_;
}
/**
* @return array<Assign|FuncCall>
* @return Stmt[]
*/
private function createNewNodes(Expr $assignVariable, Expr $eachedVariable) : array
private function createNewStmts(Expr $assignVariable, Expr $eachedVariable) : array
{
return [$this->createDimFetchAssignWithFuncCall($assignVariable, $eachedVariable, 1, 'current'), $this->createDimFetchAssignWithFuncCall($assignVariable, $eachedVariable, 'value', 'current'), $this->createDimFetchAssignWithFuncCall($assignVariable, $eachedVariable, 0, self::KEY), $this->createDimFetchAssignWithFuncCall($assignVariable, $eachedVariable, self::KEY, self::KEY), $this->nodeFactory->createFuncCall('next', [new Arg($eachedVariable)])];
$exprs = [$this->createDimFetchAssignWithFuncCall($assignVariable, $eachedVariable, 1, 'current'), $this->createDimFetchAssignWithFuncCall($assignVariable, $eachedVariable, 'value', 'current'), $this->createDimFetchAssignWithFuncCall($assignVariable, $eachedVariable, 0, self::KEY), $this->createDimFetchAssignWithFuncCall($assignVariable, $eachedVariable, self::KEY, self::KEY), $this->nodeFactory->createFuncCall('next', [new Arg($eachedVariable)])];
return \array_map(static function (Expr $expr) : Expression {
return new Expression($expr);
}, $exprs);
}
/**
* @param string|int $dimValue

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '14fa8c914b12bfc6a4112d77e03057dab9b90cd4';
public const PACKAGE_VERSION = 'ffec9f0960d8f26c81b745a218b56a32e187c157';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-06 22:05:52';
public const RELEASE_DATE = '2023-05-07 07:48:15';
/**
* @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 ComposerAutoloaderInitdfd4c3e736678063c35975e54c7aae84::getLoader();
return ComposerAutoloaderInit0001430b17f5b9a45d92e8cf357f64a6::getLoader();

View File

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