mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-15 23:52:23 +00:00
Updated Rector to commit 7b3612414201f08bb451d15ea3e1b94e4b61a0a8
7b36124142
Fix ChangeReadOnlyVariableWithDefaultValueToConstantRector for dupicated variable override (#3830)
This commit is contained in:
parent
2ef8a4b237
commit
6a4afec13c
|
@ -3,9 +3,9 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor;
|
||||
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
|
|
|
@ -29,6 +29,10 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
*/
|
||||
final class ChangeReadOnlyVariableWithDefaultValueToConstantRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $addedConstantNames = [];
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeManipulator\ClassMethodAssignManipulator
|
||||
|
@ -105,78 +109,19 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
$readOnlyVariableAssigns = $this->collectReadOnlyVariableAssigns($node);
|
||||
$readOnlyVariableAssigns = $this->filterOutUniqueNames($readOnlyVariableAssigns);
|
||||
if ($readOnlyVariableAssigns === []) {
|
||||
return null;
|
||||
}
|
||||
foreach ($readOnlyVariableAssigns as $readOnlyVariableAssign) {
|
||||
$classMethod = $this->betterNodeFinder->findParentType($readOnlyVariableAssign, ClassMethod::class);
|
||||
if (!$classMethod instanceof ClassMethod) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
$methodName = $this->getName($classMethod);
|
||||
$classMethod = $node->getMethod($methodName);
|
||||
if (!$classMethod instanceof ClassMethod) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
$this->refactorClassMethod($classMethod, $node, $readOnlyVariableAssigns);
|
||||
$this->addedConstantNames = [];
|
||||
foreach ($node->getMethods() as $classMethod) {
|
||||
$readOnlyVariableAssignScalarVariables = $this->classMethodAssignManipulator->collectReadyOnlyAssignScalarVariables($classMethod, $node);
|
||||
$this->refactorClassMethod($classMethod, $node, $readOnlyVariableAssignScalarVariables);
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
/**
|
||||
* @return Assign[]
|
||||
*/
|
||||
private function collectReadOnlyVariableAssigns(Class_ $class) : array
|
||||
{
|
||||
$readOnlyVariables = [];
|
||||
foreach ($class->getMethods() as $classMethod) {
|
||||
if ($this->isFoundByRefParam($classMethod)) {
|
||||
return [];
|
||||
}
|
||||
$readOnlyVariableAssignScalarVariables = $this->classMethodAssignManipulator->collectReadyOnlyAssignScalarVariables($classMethod, $this->file);
|
||||
$readOnlyVariables = \array_merge($readOnlyVariables, $readOnlyVariableAssignScalarVariables);
|
||||
}
|
||||
return $readOnlyVariables;
|
||||
}
|
||||
/**
|
||||
* @param Assign[] $assigns
|
||||
* @return Assign[]
|
||||
*/
|
||||
private function filterOutUniqueNames(array $assigns) : array
|
||||
{
|
||||
$assignsByName = $this->collectAssignsByName($assigns);
|
||||
$assignsWithUniqueName = [];
|
||||
foreach ($assignsByName as $assigns) {
|
||||
$count = \count($assigns);
|
||||
if ($count > 1) {
|
||||
continue;
|
||||
}
|
||||
$assignsWithUniqueName = \array_merge($assignsWithUniqueName, $assigns);
|
||||
}
|
||||
return $assignsWithUniqueName;
|
||||
}
|
||||
/**
|
||||
* @param Assign[] $assigns
|
||||
* @return array<string, Assign[]>
|
||||
*/
|
||||
private function collectAssignsByName(array $assigns) : array
|
||||
{
|
||||
$assignsByName = [];
|
||||
foreach ($assigns as $assign) {
|
||||
/** @var string $variableName */
|
||||
$variableName = $this->getName($assign->var);
|
||||
$assignsByName[$variableName][] = $assign;
|
||||
}
|
||||
return $assignsByName;
|
||||
}
|
||||
/**
|
||||
* @param Assign[] $readOnlyVariableAssigns
|
||||
*/
|
||||
private function refactorClassMethod(ClassMethod $classMethod, Class_ $class, array $readOnlyVariableAssigns) : void
|
||||
{
|
||||
foreach ($readOnlyVariableAssigns as $readOnlyVariableAssign) {
|
||||
/** @var Variable|ClassConstFetch $variable */
|
||||
$variable = $readOnlyVariableAssign->var;
|
||||
// already overridden
|
||||
if (!$variable instanceof Variable) {
|
||||
|
@ -184,17 +129,23 @@ CODE_SAMPLE
|
|||
}
|
||||
$variableName = $this->getName($variable);
|
||||
if ($variableName === null) {
|
||||
throw new ShouldNotHappenException();
|
||||
continue;
|
||||
}
|
||||
foreach ($classMethod->getParams() as $param) {
|
||||
if ($this->nodeNameResolver->isName($param->var, $variableName)) {
|
||||
continue 2;
|
||||
}
|
||||
if ($this->isVariableParamName($classMethod, $variableName)) {
|
||||
continue;
|
||||
}
|
||||
$parentAssign = $readOnlyVariableAssign->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (!$parentAssign instanceof Expression) {
|
||||
continue;
|
||||
}
|
||||
$constantName = $this->constantNaming->createFromVariable($variable);
|
||||
if ($constantName === null) {
|
||||
continue;
|
||||
}
|
||||
if (\in_array($constantName, $this->addedConstantNames, \true)) {
|
||||
continue;
|
||||
}
|
||||
$this->addedConstantNames[] = $constantName;
|
||||
$this->removeNode($readOnlyVariableAssign);
|
||||
$classConst = $this->createPrivateClassConst($variable, $readOnlyVariableAssign->expr);
|
||||
// replace $variable usage in the code with constant
|
||||
|
@ -202,16 +153,6 @@ CODE_SAMPLE
|
|||
$this->replaceVariableWithClassConstFetch($classMethod, $variableName, $classConst);
|
||||
}
|
||||
}
|
||||
private function isFoundByRefParam(ClassMethod $classMethod) : bool
|
||||
{
|
||||
$params = $classMethod->getParams();
|
||||
foreach ($params as $param) {
|
||||
if ($param->byRef) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
private function createPrivateClassConst(Variable $variable, Expr $expr) : ClassConst
|
||||
{
|
||||
$constantName = $this->constantNaming->createFromVariable($variable);
|
||||
|
@ -243,4 +184,13 @@ CODE_SAMPLE
|
|||
return new ClassConstFetch(new Name('self'), new Identifier($constantName));
|
||||
});
|
||||
}
|
||||
private function isVariableParamName(ClassMethod $classMethod, string $variableName) : bool
|
||||
{
|
||||
foreach ($classMethod->getParams() as $param) {
|
||||
if ($this->nodeNameResolver->isName($param->var, $variableName)) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '39ae81f5b6477247c4345d8496ef368ff359aaa2';
|
||||
public const PACKAGE_VERSION = '7b3612414201f08bb451d15ea3e1b94e4b61a0a8';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-05-13 20:21:32';
|
||||
public const RELEASE_DATE = '2023-05-14 00:05:02';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -15,6 +15,7 @@ use PhpParser\Node\Expr\Variable;
|
|||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Scalar\Encapsed;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
|
@ -26,7 +27,6 @@ use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
|||
use Rector\Core\PhpParser\Node\NodeFactory;
|
||||
use Rector\Core\Reflection\ReflectionResolver;
|
||||
use Rector\Core\Util\ArrayChecker;
|
||||
use Rector\Core\ValueObject\Application\File;
|
||||
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNextNodeAnalyzer;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
@ -96,9 +96,9 @@ final class ClassMethodAssignManipulator
|
|||
/**
|
||||
* @return Assign[]
|
||||
*/
|
||||
public function collectReadyOnlyAssignScalarVariables(ClassMethod $classMethod, File $file) : array
|
||||
public function collectReadyOnlyAssignScalarVariables(ClassMethod $classMethod, Class_ $class) : array
|
||||
{
|
||||
$assignsOfScalarOrArrayToVariable = $this->variableManipulator->collectScalarOrArrayAssignsOfVariable($classMethod);
|
||||
$assignsOfScalarOrArrayToVariable = $this->variableManipulator->collectScalarOrArrayAssignsOfVariable($classMethod, $class);
|
||||
// filter out [$value] = $array, array destructing
|
||||
$readOnlyVariableAssigns = $this->arrayDestructVariableFilter->filterOut($assignsOfScalarOrArrayToVariable, $classMethod);
|
||||
$readOnlyVariableAssigns = $this->filterOutReferencedVariables($readOnlyVariableAssigns, $classMethod);
|
||||
|
|
|
@ -75,15 +75,11 @@ final class VariableManipulator
|
|||
/**
|
||||
* @return Assign[]
|
||||
*/
|
||||
public function collectScalarOrArrayAssignsOfVariable(ClassMethod $classMethod) : array
|
||||
public function collectScalarOrArrayAssignsOfVariable(ClassMethod $classMethod, Class_ $class) : array
|
||||
{
|
||||
$currentClass = $this->betterNodeFinder->findParentType($classMethod, Class_::class);
|
||||
if (!$currentClass instanceof Class_) {
|
||||
return [];
|
||||
}
|
||||
$currentClassName = (string) $this->nodeNameResolver->getName($currentClass);
|
||||
$currentClassName = (string) $this->nodeNameResolver->getName($class);
|
||||
$assignsOfArrayToVariable = [];
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $classMethod->getStmts(), function (Node $node) use(&$assignsOfArrayToVariable, $currentClass, $currentClassName) {
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $classMethod->getStmts(), function (Node $node) use(&$assignsOfArrayToVariable, $class, $currentClassName) {
|
||||
if (!$node instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
|
@ -102,7 +98,7 @@ final class VariableManipulator
|
|||
if ($node->expr instanceof ConstFetch) {
|
||||
return null;
|
||||
}
|
||||
if ($node->expr instanceof ClassConstFetch && $this->isOutsideClass($node->expr, $currentClass, $currentClassName)) {
|
||||
if ($node->expr instanceof ClassConstFetch && $this->isOutsideClass($node->expr, $class, $currentClassName)) {
|
||||
return null;
|
||||
}
|
||||
$assignsOfArrayToVariable[] = $node;
|
||||
|
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
|
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInite70c973c2f02191b2c8e2afc48ac0f81::getLoader();
|
||||
return ComposerAutoloaderInitb0b9fa68b690d5f6696269dfaac4d7e6::getLoader();
|
||||
|
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInite70c973c2f02191b2c8e2afc48ac0f81
|
||||
class ComposerAutoloaderInitb0b9fa68b690d5f6696269dfaac4d7e6
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,17 +22,17 @@ class ComposerAutoloaderInite70c973c2f02191b2c8e2afc48ac0f81
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInite70c973c2f02191b2c8e2afc48ac0f81', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitb0b9fa68b690d5f6696269dfaac4d7e6', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInite70c973c2f02191b2c8e2afc48ac0f81', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitb0b9fa68b690d5f6696269dfaac4d7e6', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInite70c973c2f02191b2c8e2afc48ac0f81::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitb0b9fa68b690d5f6696269dfaac4d7e6::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInite70c973c2f02191b2c8e2afc48ac0f81::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitb0b9fa68b690d5f6696269dfaac4d7e6::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
8
vendor/composer/autoload_static.php
vendored
8
vendor/composer/autoload_static.php
vendored
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInite70c973c2f02191b2c8e2afc48ac0f81
|
||||
class ComposerStaticInitb0b9fa68b690d5f6696269dfaac4d7e6
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
|
@ -3111,9 +3111,9 @@ class ComposerStaticInite70c973c2f02191b2c8e2afc48ac0f81
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInite70c973c2f02191b2c8e2afc48ac0f81::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInite70c973c2f02191b2c8e2afc48ac0f81::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInite70c973c2f02191b2c8e2afc48ac0f81::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitb0b9fa68b690d5f6696269dfaac4d7e6::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitb0b9fa68b690d5f6696269dfaac4d7e6::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitb0b9fa68b690d5f6696269dfaac4d7e6::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user