Updated Rector to commit 00015a4390

00015a4390 [DowngradePhp70] Handle increment variable assign on existing variable on DowngradeSpaceshipRector (#565)
This commit is contained in:
Tomas Votruba 2021-08-01 16:58:54 +00:00
parent 139baa203b
commit c512faabc2
7 changed files with 42 additions and 51 deletions

View File

@ -15,11 +15,11 @@ use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\Naming\VariableNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -32,9 +32,14 @@ final class DowngradeSpaceshipRector extends \Rector\Core\Rector\AbstractRector
* @var \Rector\Core\NodeManipulator\IfManipulator
*/
private $ifManipulator;
public function __construct(\Rector\Core\NodeManipulator\IfManipulator $ifManipulator)
/**
* @var \Rector\Naming\Naming\VariableNaming
*/
private $variableNaming;
public function __construct(\Rector\Core\NodeManipulator\IfManipulator $ifManipulator, \Rector\Naming\Naming\VariableNaming $variableNaming)
{
$this->ifManipulator = $ifManipulator;
$this->variableNaming = $variableNaming;
}
/**
* @return array<class-string<Node>>
@ -78,7 +83,9 @@ CODE_SAMPLE
$ternary = new \PhpParser\Node\Expr\Ternary($smaller, $ternaryIf, $ternaryElse);
$anonymousFunction->stmts[1] = new \PhpParser\Node\Stmt\Return_($ternary);
$currentStatement = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CURRENT_STATEMENT);
$variableAssign = $this->getVariableAssign($currentStatement);
$scope = $currentStatement->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
$variableAssignName = $this->variableNaming->createCountedValueName('battleShipcompare', $scope);
$variableAssign = new \PhpParser\Node\Expr\Variable($variableAssignName);
$assignExpression = $this->getAssignExpression($anonymousFunction, $variableAssign);
$this->addNodeBeforeNode($assignExpression, $currentStatement);
return new \PhpParser\Node\Expr\FuncCall($variableAssign, [new \PhpParser\Node\Arg($node->left), new \PhpParser\Node\Arg($node->right)]);
@ -87,18 +94,4 @@ CODE_SAMPLE
{
return new \PhpParser\Node\Stmt\Expression(new \PhpParser\Node\Expr\Assign($variable, $closure));
}
private function getVariableAssign(\PhpParser\Node\Stmt $stmt, string $variableName = 'battleShipcompare') : \PhpParser\Node\Expr\Variable
{
$variable = new \PhpParser\Node\Expr\Variable($variableName);
$isFoundPrevious = (bool) $this->betterNodeFinder->findFirstPreviousOfNode($stmt, function (\PhpParser\Node $node) use($variable) : bool {
return $node instanceof \PhpParser\Node\Expr\Variable && $this->nodeComparator->areNodesEqual($node, $variable);
});
$count = 0;
if ($isFoundPrevious) {
++$count;
$variableName .= (string) $count;
return $this->getVariableAssign($stmt, $variableName);
}
return $variable;
}
}

View File

@ -16,6 +16,7 @@ use PhpParser\Node\Stmt\Foreach_;
use Rector\CodeQuality\NodeAnalyzer\ForeachAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\ExpectedNameResolver\InflectorSingularResolver;
use Rector\Naming\Naming\VariableNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -32,10 +33,15 @@ final class DowngradeKeysInListRector extends \Rector\Core\Rector\AbstractRector
* @var \Rector\CodeQuality\NodeAnalyzer\ForeachAnalyzer
*/
private $foreachAnalyzer;
public function __construct(\Rector\Naming\ExpectedNameResolver\InflectorSingularResolver $inflectorSingularResolver, \Rector\CodeQuality\NodeAnalyzer\ForeachAnalyzer $foreachAnalyzer)
/**
* @var \Rector\Naming\Naming\VariableNaming
*/
private $variableNaming;
public function __construct(\Rector\Naming\ExpectedNameResolver\InflectorSingularResolver $inflectorSingularResolver, \Rector\CodeQuality\NodeAnalyzer\ForeachAnalyzer $foreachAnalyzer, \Rector\Naming\Naming\VariableNaming $variableNaming)
{
$this->inflectorSingularResolver = $inflectorSingularResolver;
$this->foreachAnalyzer = $foreachAnalyzer;
$this->variableNaming = $variableNaming;
}
/**
* @return array<class-string<Node>>
@ -99,7 +105,9 @@ CODE_SAMPLE
return $node;
}
if ($parent instanceof \PhpParser\Node\Stmt\Foreach_) {
$newValueVar = $this->getNewValueVar($parent);
$defaultValueVar = $this->inflectorSingularResolver->resolve((string) $this->getName($parent->expr));
$scope = $parent->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
$newValueVar = $this->variableNaming->createCountedValueName($defaultValueVar, $scope);
$parent->valueVar = new \PhpParser\Node\Expr\Variable($newValueVar);
$stmts = $parent->stmts;
if ($stmts === []) {
@ -142,20 +150,10 @@ CODE_SAMPLE
}
private function getExpressionFromForeachValue(\PhpParser\Node\Stmt\Foreach_ $foreach, \PhpParser\Node\Expr\ArrayItem $arrayItem) : \PhpParser\Node\Stmt\Expression
{
$newValueVar = $this->getNewValueVar($foreach);
$defaultValueVar = $this->inflectorSingularResolver->resolve((string) $this->getName($foreach->expr));
$scope = $foreach->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
$newValueVar = $this->variableNaming->createCountedValueName($defaultValueVar, $scope);
$assign = new \PhpParser\Node\Expr\Assign($arrayItem->value, new \PhpParser\Node\Expr\ArrayDimFetch(new \PhpParser\Node\Expr\Variable($newValueVar), $arrayItem->key));
return new \PhpParser\Node\Stmt\Expression($assign);
}
private function getNewValueVar(\PhpParser\Node\Stmt\Foreach_ $foreach, ?string $newValueVar = null) : string
{
if ($newValueVar === null) {
$newValueVar = $this->inflectorSingularResolver->resolve((string) $this->getName($foreach->expr));
}
$count = 0;
if ($this->foreachAnalyzer->isValueVarUsed($foreach, $newValueVar)) {
$newValueVar .= (string) ++$count;
return $this->getNewValueVar($foreach, $newValueVar);
}
return $newValueVar;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '5ad522ddaa3cecb343da84fd2184ef6ed24f1e60';
public const PACKAGE_VERSION = '00015a4390ff1ee82930374b286d1053569d9580';
/**
* @var string
*/
public const RELEASE_DATE = '2021-08-01 18:46:52';
public const RELEASE_DATE = '2021-08-01 18:48:04';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20210801\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit7eb8d6a73ea613b143f2738aa31f3d47::getLoader();
return ComposerAutoloaderInit1ed3c3c61a460fb7b361c0abc2259dce::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit7eb8d6a73ea613b143f2738aa31f3d47
class ComposerAutoloaderInit1ed3c3c61a460fb7b361c0abc2259dce
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit7eb8d6a73ea613b143f2738aa31f3d47
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit7eb8d6a73ea613b143f2738aa31f3d47', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit1ed3c3c61a460fb7b361c0abc2259dce', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit7eb8d6a73ea613b143f2738aa31f3d47', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit1ed3c3c61a460fb7b361c0abc2259dce', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit7eb8d6a73ea613b143f2738aa31f3d47::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit1ed3c3c61a460fb7b361c0abc2259dce::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,19 +42,19 @@ class ComposerAutoloaderInit7eb8d6a73ea613b143f2738aa31f3d47
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit7eb8d6a73ea613b143f2738aa31f3d47::$files;
$includeFiles = Composer\Autoload\ComposerStaticInit1ed3c3c61a460fb7b361c0abc2259dce::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire7eb8d6a73ea613b143f2738aa31f3d47($fileIdentifier, $file);
composerRequire1ed3c3c61a460fb7b361c0abc2259dce($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequire7eb8d6a73ea613b143f2738aa31f3d47($fileIdentifier, $file)
function composerRequire1ed3c3c61a460fb7b361c0abc2259dce($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit7eb8d6a73ea613b143f2738aa31f3d47
class ComposerStaticInit1ed3c3c61a460fb7b361c0abc2259dce
{
public static $files = array (
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
@ -3850,9 +3850,9 @@ class ComposerStaticInit7eb8d6a73ea613b143f2738aa31f3d47
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit7eb8d6a73ea613b143f2738aa31f3d47::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit7eb8d6a73ea613b143f2738aa31f3d47::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit7eb8d6a73ea613b143f2738aa31f3d47::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit1ed3c3c61a460fb7b361c0abc2259dce::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit1ed3c3c61a460fb7b361c0abc2259dce::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit1ed3c3c61a460fb7b361c0abc2259dce::$classMap;
}, null, ClassLoader::class);
}

View File

@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20210801\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit7eb8d6a73ea613b143f2738aa31f3d47', false) && !interface_exists('ComposerAutoloaderInit7eb8d6a73ea613b143f2738aa31f3d47', false) && !trait_exists('ComposerAutoloaderInit7eb8d6a73ea613b143f2738aa31f3d47', false)) {
spl_autoload_call('RectorPrefix20210801\ComposerAutoloaderInit7eb8d6a73ea613b143f2738aa31f3d47');
if (!class_exists('ComposerAutoloaderInit1ed3c3c61a460fb7b361c0abc2259dce', false) && !interface_exists('ComposerAutoloaderInit1ed3c3c61a460fb7b361c0abc2259dce', false) && !trait_exists('ComposerAutoloaderInit1ed3c3c61a460fb7b361c0abc2259dce', false)) {
spl_autoload_call('RectorPrefix20210801\ComposerAutoloaderInit1ed3c3c61a460fb7b361c0abc2259dce');
}
if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) {
spl_autoload_call('RectorPrefix20210801\Doctrine\Inflector\Inflector');
@ -3308,9 +3308,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20210801\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire7eb8d6a73ea613b143f2738aa31f3d47')) {
function composerRequire7eb8d6a73ea613b143f2738aa31f3d47() {
return \RectorPrefix20210801\composerRequire7eb8d6a73ea613b143f2738aa31f3d47(...func_get_args());
if (!function_exists('composerRequire1ed3c3c61a460fb7b361c0abc2259dce')) {
function composerRequire1ed3c3c61a460fb7b361c0abc2259dce() {
return \RectorPrefix20210801\composerRequire1ed3c3c61a460fb7b361c0abc2259dce(...func_get_args());
}
}
if (!function_exists('parseArgs')) {