Updated Rector to commit c050b66f9b9812f8f7735673e651deaccded0071

c050b66f9b Refactor PARENT_NODE away from ListEachRector (#3946)
This commit is contained in:
Tomas Votruba 2023-05-24 10:04:51 +00:00
parent 13cb0fa921
commit 1f89bb9561
5 changed files with 32 additions and 39 deletions

View File

@ -13,8 +13,6 @@ use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeManipulator\AssignManipulator; use Rector\Core\NodeManipulator\AssignManipulator;
use Rector\Core\Rector\AbstractRector; use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature; use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Collector\NodesToAddCollector;
use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -30,15 +28,9 @@ final class ListEachRector extends AbstractRector implements MinPhpVersionInterf
* @var \Rector\Core\NodeManipulator\AssignManipulator * @var \Rector\Core\NodeManipulator\AssignManipulator
*/ */
private $assignManipulator; private $assignManipulator;
/** public function __construct(AssignManipulator $assignManipulator)
* @readonly
* @var \Rector\PostRector\Collector\NodesToAddCollector
*/
private $nodesToAddCollector;
public function __construct(AssignManipulator $assignManipulator, NodesToAddCollector $nodesToAddCollector)
{ {
$this->assignManipulator = $assignManipulator; $this->assignManipulator = $assignManipulator;
$this->nodesToAddCollector = $nodesToAddCollector;
} }
public function provideMinPhpVersion() : int public function provideMinPhpVersion() : int
{ {
@ -61,20 +53,24 @@ CODE_SAMPLE
*/ */
public function getNodeTypes() : array public function getNodeTypes() : array
{ {
return [Assign::class]; return [Expression::class];
} }
/** /**
* @param Assign $node * @param Expression $node
*/ */
public function refactor(Node $node) : ?Node public function refactor(Node $node)
{ {
if ($this->shouldSkip($node)) { if (!$node->expr instanceof Assign) {
return null;
}
$assign = $node->expr;
if ($this->shouldSkipAssign($assign)) {
return null; return null;
} }
/** @var List_ $listNode */ /** @var List_ $listNode */
$listNode = $node->var; $listNode = $assign->var;
/** @var FuncCall $eachFuncCall */ /** @var FuncCall $eachFuncCall */
$eachFuncCall = $node->expr; $eachFuncCall = $assign->expr;
// only key: list($key, ) = each($values); // only key: list($key, ) = each($values);
if ($listNode->items[0] instanceof ArrayItem && !$listNode->items[1] instanceof ArrayItem) { if ($listNode->items[0] instanceof ArrayItem && !$listNode->items[1] instanceof ArrayItem) {
$keyFuncCall = $this->nodeFactory->createFuncCall('key', $eachFuncCall->args); $keyFuncCall = $this->nodeFactory->createFuncCall('key', $eachFuncCall->args);
@ -83,10 +79,11 @@ CODE_SAMPLE
// only value: list(, $value) = each($values); // only value: list(, $value) = each($values);
if ($listNode->items[1] instanceof ArrayItem && !$listNode->items[0] instanceof ArrayItem) { if ($listNode->items[1] instanceof ArrayItem && !$listNode->items[0] instanceof ArrayItem) {
$nextFuncCall = $this->nodeFactory->createFuncCall('next', $eachFuncCall->args); $nextFuncCall = $this->nodeFactory->createFuncCall('next', $eachFuncCall->args);
$this->nodesToAddCollector->addNodeAfterNode($nextFuncCall, $node); // $this->nodesToAddCollector->addNodeAfterNode($nextFuncCall, $assign);
$currentFuncCall = $this->nodeFactory->createFuncCall('current', $eachFuncCall->args); $currentFuncCall = $this->nodeFactory->createFuncCall('current', $eachFuncCall->args);
$secondArrayItem = $listNode->items[1]; $secondArrayItem = $listNode->items[1];
return new Assign($secondArrayItem->value, $currentFuncCall); $currentAssign = new Assign($secondArrayItem->value, $currentFuncCall);
return [new Expression($currentAssign), new Expression($nextFuncCall)];
} }
// both: list($key, $value) = each($values); // both: list($key, $value) = each($values);
$currentFuncCall = $this->nodeFactory->createFuncCall('current', $eachFuncCall->args); $currentFuncCall = $this->nodeFactory->createFuncCall('current', $eachFuncCall->args);
@ -94,27 +91,23 @@ CODE_SAMPLE
if (!$secondArrayItem instanceof ArrayItem) { if (!$secondArrayItem instanceof ArrayItem) {
throw new ShouldNotHappenException(); throw new ShouldNotHappenException();
} }
$assign = new Assign($secondArrayItem->value, $currentFuncCall); $currentAssign = new Assign($secondArrayItem->value, $currentFuncCall);
$this->nodesToAddCollector->addNodeAfterNode($assign, $node); // $this->nodesToAddCollector->addNodeAfterNode($assign, $assign);
$nextFuncCall = $this->nodeFactory->createFuncCall('next', $eachFuncCall->args); $nextFuncCall = $this->nodeFactory->createFuncCall('next', $eachFuncCall->args);
$this->nodesToAddCollector->addNodeAfterNode($nextFuncCall, $node); // $this->nodesToAddCollector->addNodeAfterNode($nextFuncCall, $node);
$keyFuncCall = $this->nodeFactory->createFuncCall('key', $eachFuncCall->args); $keyFuncCall = $this->nodeFactory->createFuncCall('key', $eachFuncCall->args);
$firstArrayItem = $listNode->items[0]; $firstArrayItem = $listNode->items[0];
if (!$firstArrayItem instanceof ArrayItem) { if (!$firstArrayItem instanceof ArrayItem) {
throw new ShouldNotHappenException(); throw new ShouldNotHappenException();
} }
return new Assign($firstArrayItem->value, $keyFuncCall); $keyAssign = new Assign($firstArrayItem->value, $keyFuncCall);
return [new Expression($keyAssign), new Expression($currentAssign), new Expression($nextFuncCall)];
} }
private function shouldSkip(Assign $assign) : bool private function shouldSkipAssign(Assign $assign) : bool
{ {
if (!$this->assignManipulator->isListToEachAssign($assign)) { if (!$this->assignManipulator->isListToEachAssign($assign)) {
return \true; return \true;
} }
// assign should be top level, e.g. not in a while loop
$parentNode = $assign->getAttribute(AttributeKey::PARENT_NODE);
if (!$parentNode instanceof Expression) {
return \true;
}
/** @var List_ $listNode */ /** @var List_ $listNode */
$listNode = $assign->var; $listNode = $assign->var;
if (\count($listNode->items) !== 2) { if (\count($listNode->items) !== 2) {

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api * @api
* @var string * @var string
*/ */
public const PACKAGE_VERSION = '279126145e995c4d0b39a60bd6fc5fff5799199e'; public const PACKAGE_VERSION = 'c050b66f9b9812f8f7735673e651deaccded0071';
/** /**
* @api * @api
* @var string * @var string
*/ */
public const RELEASE_DATE = '2023-05-24 09:38:51'; public const RELEASE_DATE = '2023-05-24 10:00:13';
/** /**
* @var int * @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'; require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit68fa97b402c8b0e8b73786dcfe58719b::getLoader(); return ComposerAutoloaderInit6665049c1bf4f1bfd9e1d88569d19ca5::getLoader();

View File

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

View File

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