mirror of https://github.com/rectorphp/rector.git
Updated Rector to commit 28412a49f7bd52094196e659ec0c68f9642c08b1
28412a49f7
[Naming] Skip used on next foreach on RenameForeachValueVariableToMatchExprVariableRector (#4469)
This commit is contained in:
parent
2d46a8afb9
commit
474e8a76ec
|
@ -6,7 +6,9 @@ namespace Rector\Naming\Rector\Foreach_;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
|
||||
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
|
||||
use Rector\Core\NodeManipulator\StmtsManipulator;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Naming\ExpectedNameResolver\InflectorSingularResolver;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
@ -26,10 +28,16 @@ final class RenameForeachValueVariableToMatchExprVariableRector extends Abstract
|
|||
* @var \Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer
|
||||
*/
|
||||
private $propertyFetchAnalyzer;
|
||||
public function __construct(InflectorSingularResolver $inflectorSingularResolver, PropertyFetchAnalyzer $propertyFetchAnalyzer)
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeManipulator\StmtsManipulator
|
||||
*/
|
||||
private $stmtsManipulator;
|
||||
public function __construct(InflectorSingularResolver $inflectorSingularResolver, PropertyFetchAnalyzer $propertyFetchAnalyzer, StmtsManipulator $stmtsManipulator)
|
||||
{
|
||||
$this->inflectorSingularResolver = $inflectorSingularResolver;
|
||||
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
|
||||
$this->stmtsManipulator = $stmtsManipulator;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
|
@ -64,42 +72,62 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Foreach_::class];
|
||||
return [StmtsAwareInterface::class];
|
||||
}
|
||||
/**
|
||||
* @param Foreach_ $node
|
||||
* @param StmtsAwareInterface $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
$isPropertyFetch = $this->propertyFetchAnalyzer->isLocalPropertyFetch($node->expr);
|
||||
if (!$node->expr instanceof Variable && !$isPropertyFetch) {
|
||||
if ($node->stmts === null) {
|
||||
return null;
|
||||
}
|
||||
$exprName = $this->getName($node->expr);
|
||||
if ($exprName === null) {
|
||||
return null;
|
||||
$hasChanged = \false;
|
||||
foreach ($node->stmts as $key => $stmt) {
|
||||
if (!$stmt instanceof Foreach_) {
|
||||
continue;
|
||||
}
|
||||
$isPropertyFetch = $this->propertyFetchAnalyzer->isLocalPropertyFetch($stmt->expr);
|
||||
if (!$stmt->expr instanceof Variable && !$isPropertyFetch) {
|
||||
continue;
|
||||
}
|
||||
$exprName = $this->getName($stmt->expr);
|
||||
if ($exprName === null) {
|
||||
continue;
|
||||
}
|
||||
if ($stmt->keyVar instanceof Node) {
|
||||
continue;
|
||||
}
|
||||
$valueVarName = $this->getName($stmt->valueVar);
|
||||
if ($valueVarName === null) {
|
||||
continue;
|
||||
}
|
||||
$singularValueVarName = $this->inflectorSingularResolver->resolve($exprName);
|
||||
if ($singularValueVarName === $exprName) {
|
||||
continue;
|
||||
}
|
||||
if ($singularValueVarName === $valueVarName) {
|
||||
continue;
|
||||
}
|
||||
$alreadyUsedVariable = $this->betterNodeFinder->findVariableOfName($stmt->stmts, $singularValueVarName);
|
||||
if ($alreadyUsedVariable instanceof Variable) {
|
||||
continue;
|
||||
}
|
||||
if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, $singularValueVarName)) {
|
||||
continue;
|
||||
}
|
||||
if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, $valueVarName)) {
|
||||
continue;
|
||||
}
|
||||
$this->processRename($stmt, $valueVarName, $singularValueVarName);
|
||||
$hasChanged = \true;
|
||||
}
|
||||
if ($node->keyVar instanceof Node) {
|
||||
return null;
|
||||
if ($hasChanged) {
|
||||
return $node;
|
||||
}
|
||||
$valueVarName = $this->getName($node->valueVar);
|
||||
if ($valueVarName === null) {
|
||||
return null;
|
||||
}
|
||||
$singularValueVarName = $this->inflectorSingularResolver->resolve($exprName);
|
||||
if ($singularValueVarName === $exprName) {
|
||||
return null;
|
||||
}
|
||||
if ($singularValueVarName === $valueVarName) {
|
||||
return null;
|
||||
}
|
||||
$alreadyUsedVariable = $this->betterNodeFinder->findVariableOfName($node->stmts, $singularValueVarName);
|
||||
if ($alreadyUsedVariable instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
return $this->processRename($node, $valueVarName, $singularValueVarName);
|
||||
return null;
|
||||
}
|
||||
private function processRename(Foreach_ $foreach, string $valueVarName, string $singularValueVarName) : Foreach_
|
||||
private function processRename(Foreach_ $foreach, string $valueVarName, string $singularValueVarName) : void
|
||||
{
|
||||
$foreach->valueVar = new Variable($singularValueVarName);
|
||||
$this->traverseNodesWithCallable($foreach->stmts, function (Node $node) use($singularValueVarName, $valueVarName) : ?Variable {
|
||||
|
@ -111,6 +139,5 @@ CODE_SAMPLE
|
|||
}
|
||||
return new Variable($singularValueVarName);
|
||||
});
|
||||
return $foreach;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '5b64258e7c9628d80fa3247a698c646def55dca4';
|
||||
public const PACKAGE_VERSION = '28412a49f7bd52094196e659ec0c68f9642c08b1';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-07-10 16:18:57';
|
||||
public const RELEASE_DATE = '2023-07-10 10:51:33';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit730acd082dbcead102e4118e2e4c898f::getLoader();
|
||||
return ComposerAutoloaderInit51eee47c780002ca3aceb5afceaedc45::getLoader();
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit730acd082dbcead102e4118e2e4c898f
|
||||
class ComposerAutoloaderInit51eee47c780002ca3aceb5afceaedc45
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,17 +22,17 @@ class ComposerAutoloaderInit730acd082dbcead102e4118e2e4c898f
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit730acd082dbcead102e4118e2e4c898f', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit51eee47c780002ca3aceb5afceaedc45', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit730acd082dbcead102e4118e2e4c898f', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit51eee47c780002ca3aceb5afceaedc45', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit730acd082dbcead102e4118e2e4c898f::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit51eee47c780002ca3aceb5afceaedc45::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit730acd082dbcead102e4118e2e4c898f::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit51eee47c780002ca3aceb5afceaedc45::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit730acd082dbcead102e4118e2e4c898f
|
||||
class ComposerStaticInit51eee47c780002ca3aceb5afceaedc45
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
|
@ -3067,9 +3067,9 @@ class ComposerStaticInit730acd082dbcead102e4118e2e4c898f
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit730acd082dbcead102e4118e2e4c898f::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit730acd082dbcead102e4118e2e4c898f::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit730acd082dbcead102e4118e2e4c898f::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit51eee47c780002ca3aceb5afceaedc45::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit51eee47c780002ca3aceb5afceaedc45::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit51eee47c780002ca3aceb5afceaedc45::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue