Updated Rector to commit 18d8df639c4376c2784a585eb5deafeda971597e

18d8df639c Refactor away PARENT_NODE from AssignArrayToStringRector (#3983)
This commit is contained in:
Tomas Votruba 2023-05-27 09:26:50 +00:00
parent 2774b5f9dd
commit eb19a0084e
8 changed files with 149 additions and 122 deletions

View File

@ -8,14 +8,17 @@ use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property;
use Rector\Core\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -26,6 +29,15 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
*/
final class AssignArrayToStringRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\Core\PhpParser\NodeFinder\PropertyFetchFinder
*/
private $propertyFetchFinder;
public function __construct(PropertyFetchFinder $propertyFetchFinder)
{
$this->propertyFetchFinder = $propertyFetchFinder;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::NO_ASSIGN_ARRAY_TO_STRING;
@ -47,61 +59,17 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [Assign::class, Property::class];
return [Assign::class, Class_::class];
}
/**
* @param Assign|Property $node
* @param Assign|Class_ $node
*/
public function refactor(Node $node) : ?Node
{
$defaultExpr = $this->resolveDefaultValueExpr($node);
if (!$defaultExpr instanceof Expr) {
return null;
if ($node instanceof Class_) {
return $this->refactorClass($node);
}
if (!$this->isEmptyString($defaultExpr)) {
return null;
}
$assignedVar = $this->resolveAssignedVar($node);
// 1. variable!
$shouldRetype = \false;
/** @var array<Variable|PropertyFetch|StaticPropertyFetch> $exprUsages */
$exprUsages = $this->betterNodeFinder->findSameNamedExprs($assignedVar);
// detect if is part of variable assign?
foreach ($exprUsages as $exprUsage) {
$parentNode = $exprUsage->getAttribute(AttributeKey::PARENT_NODE);
if (!$parentNode instanceof ArrayDimFetch) {
continue;
}
$firstAssign = $this->betterNodeFinder->findParentType($parentNode, Assign::class);
if (!$firstAssign instanceof Assign) {
continue;
}
// skip explicit assigns
if ($parentNode->dim instanceof Expr) {
continue;
}
$shouldRetype = \true;
break;
}
if (!$shouldRetype) {
return null;
}
if ($node instanceof Property) {
$node->props[0]->default = new Array_();
return $node;
}
$node->expr = new Array_();
return $node;
}
/**
* @param \PhpParser\Node\Expr\Assign|\PhpParser\Node\Stmt\Property $node
*/
private function resolveDefaultValueExpr($node) : ?Expr
{
if ($node instanceof Property) {
return $node->props[0]->default;
}
return $node->expr;
return $this->refactorAssign($node);
}
private function isEmptyString(Expr $expr) : bool
{
@ -110,15 +78,97 @@ CODE_SAMPLE
}
return $expr->value === '';
}
/**
* @param \PhpParser\Node\Expr\Assign|\PhpParser\Node\Stmt\Property $node
* @return \PhpParser\Node\Expr|\PhpParser\Node\Stmt\Property
*/
private function resolveAssignedVar($node)
private function refactorClass(Class_ $class) : ?Class_
{
if ($node instanceof Assign) {
return $node->var;
$hasChanged = \false;
foreach ($class->getProperties() as $property) {
if (!$this->hasPropertyDefaultEmptyString($property)) {
continue;
}
$arrayDimFetches = $this->propertyFetchFinder->findLocalPropertyArrayDimFetchesAssignsByName($class, $property);
foreach ($arrayDimFetches as $arrayDimFetch) {
if ($arrayDimFetch->dim instanceof Expr) {
continue;
}
$property->props[0]->default = new Array_();
$hasChanged = \true;
}
}
return $node;
if ($hasChanged) {
return $class;
}
return null;
}
private function hasPropertyDefaultEmptyString(Property $property) : bool
{
$defaultExpr = $property->props[0]->default;
if (!$defaultExpr instanceof Expr) {
return \false;
}
return $this->isEmptyString($defaultExpr);
}
/**
* @return ArrayDimFetch[]
*/
private function findSameNamedVariableAssigns(Variable $variable) : array
{
// assign of empty string to something
$scopeStmt = $this->findParentScope($variable);
if (!$scopeStmt instanceof Stmt) {
return [];
}
$variableName = $this->nodeNameResolver->getName($variable);
if ($variableName === null) {
return [];
}
$assignedArrayDimFetches = [];
$this->traverseNodesWithCallable($scopeStmt, function (Node $node) use($variableName, &$assignedArrayDimFetches) {
if (!$node instanceof Assign) {
return null;
}
if (!$node->var instanceof ArrayDimFetch) {
return null;
}
$arrayDimFetch = $node->var;
if (!$arrayDimFetch->var instanceof Variable) {
return null;
}
if (!$this->isName($arrayDimFetch->var, $variableName)) {
return null;
}
$assignedArrayDimFetches[] = $arrayDimFetch;
});
return $assignedArrayDimFetches;
}
/**
* @return Function_|ClassMethod|Class_|Namespace_|null
*/
private function findParentScope(Variable $variable)
{
return $this->betterNodeFinder->findParentByTypes($variable, [Function_::class, ClassMethod::class, Class_::class, Namespace_::class]);
}
private function refactorAssign(Assign $assign) : ?Assign
{
if (!$this->isEmptyString($assign->expr)) {
return null;
}
if (!$assign->var instanceof Variable) {
return null;
}
$variableAssignArrayDimFetches = $this->findSameNamedVariableAssigns($assign->var);
$shouldRetype = \false;
// detect if is part of variable assign?
foreach ($variableAssignArrayDimFetches as $variableAssignArrayDimFetch) {
if ($variableAssignArrayDimFetch->dim instanceof Expr) {
continue;
}
$shouldRetype = \true;
break;
}
if (!$shouldRetype) {
return null;
}
$assign->expr = new Array_();
return $assign;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'c70a5b8fdbbb3306603c67a972265872eaa3da2b';
public const PACKAGE_VERSION = '18d8df639c4376c2784a585eb5deafeda971597e';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-27 08:22:59';
public const RELEASE_DATE = '2023-05-27 09:22:43';
/**
* @var int
*/

View File

@ -15,7 +15,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v35';
private const CACHE_KEY = 'v36';
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface|null
*/

View File

@ -8,7 +8,6 @@ use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
@ -24,7 +23,6 @@ use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\While_;
@ -355,46 +353,6 @@ final class BetterNodeFinder
}
return null;
}
/**
* @api
* @return Expr[]
* @param \PhpParser\Node\Expr|\PhpParser\Node\Expr\Variable|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\StaticPropertyFetch $expr
*/
public function findSameNamedExprs($expr) : array
{
// assign of empty string to something
$scopeNode = $this->findParentScope($expr);
if (!$scopeNode instanceof Node) {
return [];
}
if ($expr instanceof Variable) {
$exprName = $this->nodeNameResolver->getName($expr);
if ($exprName === null) {
return [];
}
/** @var Variable[] $variables */
$variables = $this->find($scopeNode, function (Node $node) use($exprName) : bool {
return $node instanceof Variable && $this->nodeNameResolver->isName($node, $exprName);
});
return $variables;
}
if ($expr instanceof Property) {
$singleProperty = $expr->props[0];
$exprName = $this->nodeNameResolver->getName($singleProperty->name);
} elseif ($expr instanceof StaticPropertyFetch || $expr instanceof PropertyFetch) {
$exprName = $this->nodeNameResolver->getName($expr->name);
} else {
return [];
}
if ($exprName === null) {
return [];
}
/** @var PropertyFetch[]|StaticPropertyFetch[] $propertyFetches */
$propertyFetches = $this->find($scopeNode, function (Node $node) use($exprName) : bool {
return ($node instanceof PropertyFetch || $node instanceof StaticPropertyFetch) && $this->nodeNameResolver->isName($node->name, $exprName);
});
return $propertyFetches;
}
/**
* @template T of Node
* @param array<class-string<T>>|class-string<T> $types
@ -679,11 +637,4 @@ final class BetterNodeFinder
return $node instanceof $type && $this->nodeNameResolver->isName($node, $name);
});
}
/**
* @return Closure|Function_|ClassMethod|Class_|Namespace_|null
*/
private function findParentScope(Node $node)
{
return $this->findParentByTypes($node, [Closure::class, Function_::class, ClassMethod::class, Class_::class, Namespace_::class]);
}
}

View File

@ -3,6 +3,8 @@
declare (strict_types=1);
namespace Rector\Core\PhpParser\NodeFinder;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Param;
@ -94,6 +96,30 @@ final class PropertyFetchFinder
}
return $foundPropertyFetches;
}
/**
* @return ArrayDimFetch[]
*/
public function findLocalPropertyArrayDimFetchesAssignsByName(Class_ $class, Property $property) : array
{
$propertyName = $this->nodeNameResolver->getName($property);
/** @var Assign[] $assigns */
$assigns = $this->betterNodeFinder->findInstanceOf($class, Assign::class);
$propertyArrayDimFetches = [];
foreach ($assigns as $assign) {
if (!$assign->var instanceof ArrayDimFetch) {
continue;
}
$dimFetchVar = $assign->var;
if (!$dimFetchVar->var instanceof PropertyFetch && !$dimFetchVar->var instanceof StaticPropertyFetch) {
continue;
}
if (!$this->propertyFetchAnalyzer->isLocalPropertyFetchName($dimFetchVar->var, $propertyName)) {
continue;
}
$propertyArrayDimFetches[] = $dimFetchVar;
}
return $propertyArrayDimFetches;
}
/**
* @param Stmt[] $stmts
* @return PropertyFetch[]|StaticPropertyFetch[]

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitd752502b836e291d63dd9c849fbdf742::getLoader();
return ComposerAutoloaderInitb3b0da5ec15dc443a3b012145b50fbab::getLoader();

View File

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