Updated Rector to commit d031c059eb2b7b07bbf9609b4eee0b6b895a4095

d031c059eb Remove PARENT_NODE from RemoveJustPropertyFetchRector (#3937)
This commit is contained in:
Tomas Votruba 2023-05-23 14:08:54 +00:00
parent 62ef1326dc
commit 22d4bdfac5
7 changed files with 37 additions and 184 deletions

View File

@ -4,25 +4,15 @@ declare (strict_types=1);
namespace Rector\DeadCode\Rector\StmtsAwareInterface;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp\Concat;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\While_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\Type\ObjectType;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\ValueObject\PropertyFetchToVariableAssign;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\ReadWrite\NodeAnalyzer\ReadExprAnalyzer;
use Rector\ReadWrite\NodeFinder\NodeUsageFinder;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
@ -30,21 +20,6 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
*/
final class RemoveJustPropertyFetchRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\ReadWrite\NodeFinder\NodeUsageFinder
*/
private $nodeUsageFinder;
/**
* @readonly
* @var \Rector\ReadWrite\NodeAnalyzer\ReadExprAnalyzer
*/
private $readExprAnalyzer;
public function __construct(NodeUsageFinder $nodeUsageFinder, ReadExprAnalyzer $readExprAnalyzer)
{
$this->nodeUsageFinder = $nodeUsageFinder;
$this->readExprAnalyzer = $readExprAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Inline property fetch assign to a variable, that has no added value', [new CodeSample(<<<'CODE_SAMPLE'
@ -89,111 +64,38 @@ CODE_SAMPLE
if ($stmts === []) {
return null;
}
$variableUsages = [];
$currentStmtKey = null;
$variableToPropertyAssign = null;
foreach ($stmts as $key => $stmt) {
$variableToPropertyAssign = $this->matchVariableToPropertyAssign($stmt);
if (!$variableToPropertyAssign instanceof PropertyFetchToVariableAssign) {
if (!$stmt instanceof Return_) {
continue;
}
$assignPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($stmt);
if (!$stmt->expr instanceof Variable) {
continue;
}
$previousStmt = $stmts[$key - 1] ?? null;
if (!$previousStmt instanceof Expression) {
continue;
}
$propertyFetch = $this->matchVariableToPropertyFetchAssign($previousStmt, $stmt->expr);
if (!$propertyFetch instanceof PropertyFetch) {
continue;
}
$assignPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($previousStmt);
// there is a @var tag on purpose, keep the assign
if ($assignPhpDocInfo->getVarTagValueNode() instanceof VarTagValueNode) {
continue;
}
$followingStmts = \array_slice($stmts, $key + 1);
if ($this->isFollowingStatementStaticClosure($followingStmts)) {
// can not replace usages in anonymous static functions
continue;
}
if (!$this->readExprAnalyzer->isExprRead($variableToPropertyAssign->getVariable())) {
continue;
}
$variableUsages = $this->nodeUsageFinder->findVariableUsages($followingStmts, $variableToPropertyAssign->getVariable());
$currentStmtKey = $key;
break;
unset($node->stmts[$key - 1]);
$stmt->expr = $propertyFetch;
return $node;
}
// filter out variable usages that are part of nested property fetch, or change variable
$variableUsages = $this->filterOutReferencedVariableUsages($variableUsages);
if ($variableUsages === []) {
return null;
}
if ($this->isOverridenWithAssign($variableUsages)) {
return null;
}
if (!$variableToPropertyAssign instanceof PropertyFetchToVariableAssign) {
return null;
}
/** @var int $currentStmtKey */
return $this->replaceVariablesWithPropertyFetch($node, $currentStmtKey, $variableUsages, $variableToPropertyAssign->getPropertyFetch());
return null;
}
/**
* @param Stmt[] $followingStmts
*/
private function isFollowingStatementStaticClosure(array $followingStmts) : bool
private function matchVariableToPropertyFetchAssign(Expression $expression, Variable $variable) : ?PropertyFetch
{
if ($followingStmts === []) {
return \false;
}
return $followingStmts[0] instanceof Expression && $followingStmts[0]->expr instanceof Closure && $followingStmts[0]->expr->static;
}
/**
* @param Variable[] $variableUsages
*/
private function replaceVariablesWithPropertyFetch(StmtsAwareInterface $stmtsAware, int $currentStmtsKey, array $variableUsages, PropertyFetch $propertyFetch) : StmtsAwareInterface
{
// remove assign node
unset($stmtsAware->stmts[$currentStmtsKey]);
$this->traverseNodesWithCallable($stmtsAware, function (Node $node) use($variableUsages, $propertyFetch) : ?PropertyFetch {
if (!\in_array($node, $variableUsages, \true)) {
return null;
}
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof ClosureUse) {
// remove closure use which will be replaced by a property fetch
$this->removeNode($parentNode);
return null;
}
return $propertyFetch;
});
return $stmtsAware;
}
/**
* @param Variable[] $variableUsages
* @return Variable[]
*/
private function filterOutReferencedVariableUsages(array $variableUsages) : array
{
return \array_filter($variableUsages, function (Variable $variable) : bool {
$variableUsageParent = $variable->getAttribute(AttributeKey::PARENT_NODE);
if ($variableUsageParent instanceof Arg) {
$variableUsageParent = $variableUsageParent->getAttribute(AttributeKey::PARENT_NODE);
}
// skip nested property fetch, the assign is for purpose of named variable
if ($variableUsageParent instanceof PropertyFetch) {
return \false;
}
// skip, as assign can be used in a loop
$parentWhile = $this->betterNodeFinder->findParentType($variable, While_::class);
if ($parentWhile instanceof While_) {
return \false;
}
if (!$variableUsageParent instanceof FuncCall) {
return \true;
}
return !$this->isName($variableUsageParent, 'array_pop');
});
}
private function matchVariableToPropertyAssign(Stmt $stmt) : ?PropertyFetchToVariableAssign
{
if (!$stmt instanceof Expression) {
if (!$expression->expr instanceof Assign) {
return null;
}
if (!$stmt->expr instanceof Assign) {
return null;
}
$assign = $stmt->expr;
$assign = $expression->expr;
if (!$assign->expr instanceof PropertyFetch) {
return null;
}
@ -207,7 +109,10 @@ CODE_SAMPLE
if ($this->isPropertyFetchCallerNode($assign->expr)) {
return null;
}
return new PropertyFetchToVariableAssign($assign->var, $assign->expr);
if (!$this->nodeComparator->areNodesEqual($variable, $assign->var)) {
return null;
}
return $assign->expr;
}
private function isPropertyFetchCallerNode(PropertyFetch $propertyFetch) : bool
{
@ -219,21 +124,4 @@ CODE_SAMPLE
$nodeObjectType = new ObjectType('PhpParser\\Node');
return $nodeObjectType->isSuperTypeOf($propertyFetchCallerType)->yes();
}
/**
* @param Variable[] $variables
*/
private function isOverridenWithAssign(array $variables) : bool
{
foreach ($variables as $variable) {
$parentNode = $variable->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Concat && $parentNode->var === $variable) {
return \true;
}
// variable is overriden
if ($parentNode instanceof Assign && $parentNode->var === $variable) {
return \true;
}
}
return \false;
}
}

View File

@ -1,33 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\DeadCode\ValueObject;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
final class PropertyFetchToVariableAssign
{
/**
* @readonly
* @var \PhpParser\Node\Expr\Variable
*/
private $variable;
/**
* @readonly
* @var \PhpParser\Node\Expr\PropertyFetch
*/
private $propertyFetch;
public function __construct(Variable $variable, PropertyFetch $propertyFetch)
{
$this->variable = $variable;
$this->propertyFetch = $propertyFetch;
}
public function getVariable() : Variable
{
return $this->variable;
}
public function getPropertyFetch() : PropertyFetch
{
return $this->propertyFetch;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '8a74ea74d5501f5f850632c834878468eaa39b8e';
public const PACKAGE_VERSION = 'd031c059eb2b7b07bbf9609b4eee0b6b895a4095';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-23 14:55:52';
public const RELEASE_DATE = '2023-05-23 14:03:53';
/**
* @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';
return ComposerAutoloaderInit79faf09d907f46e89d9cb7b01693ec27::getLoader();
return ComposerAutoloaderInitbbae854da20153f8cf5d9bd84366c876::getLoader();

View File

@ -1634,7 +1634,6 @@ return array(
'Rector\\DeadCode\\TypeNodeAnalyzer\\MixedArrayTypeNodeAnalyzer' => $baseDir . '/rules/DeadCode/TypeNodeAnalyzer/MixedArrayTypeNodeAnalyzer.php',
'Rector\\DeadCode\\UselessIfCondBeforeForeachDetector' => $baseDir . '/rules/DeadCode/UselessIfCondBeforeForeachDetector.php',
'Rector\\DeadCode\\ValueObject\\BinaryToVersionCompareCondition' => $baseDir . '/rules/DeadCode/ValueObject/BinaryToVersionCompareCondition.php',
'Rector\\DeadCode\\ValueObject\\PropertyFetchToVariableAssign' => $baseDir . '/rules/DeadCode/ValueObject/PropertyFetchToVariableAssign.php',
'Rector\\DeadCode\\ValueObject\\TargetRemoveClassMethod' => $baseDir . '/rules/DeadCode/ValueObject/TargetRemoveClassMethod.php',
'Rector\\DeadCode\\ValueObject\\VariableAndPropertyFetchAssign' => $baseDir . '/rules/DeadCode/ValueObject/VariableAndPropertyFetchAssign.php',
'Rector\\DeadCode\\ValueObject\\VersionCompareCondition' => $baseDir . '/rules/DeadCode/ValueObject/VersionCompareCondition.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit79faf09d907f46e89d9cb7b01693ec27
class ComposerAutoloaderInitbbae854da20153f8cf5d9bd84366c876
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit79faf09d907f46e89d9cb7b01693ec27
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit79faf09d907f46e89d9cb7b01693ec27', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitbbae854da20153f8cf5d9bd84366c876', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit79faf09d907f46e89d9cb7b01693ec27', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitbbae854da20153f8cf5d9bd84366c876', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit79faf09d907f46e89d9cb7b01693ec27::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitbbae854da20153f8cf5d9bd84366c876::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit79faf09d907f46e89d9cb7b01693ec27::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInitbbae854da20153f8cf5d9bd84366c876::$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 ComposerStaticInit79faf09d907f46e89d9cb7b01693ec27
class ComposerStaticInitbbae854da20153f8cf5d9bd84366c876
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -1876,7 +1876,6 @@ class ComposerStaticInit79faf09d907f46e89d9cb7b01693ec27
'Rector\\DeadCode\\TypeNodeAnalyzer\\MixedArrayTypeNodeAnalyzer' => __DIR__ . '/../..' . '/rules/DeadCode/TypeNodeAnalyzer/MixedArrayTypeNodeAnalyzer.php',
'Rector\\DeadCode\\UselessIfCondBeforeForeachDetector' => __DIR__ . '/../..' . '/rules/DeadCode/UselessIfCondBeforeForeachDetector.php',
'Rector\\DeadCode\\ValueObject\\BinaryToVersionCompareCondition' => __DIR__ . '/../..' . '/rules/DeadCode/ValueObject/BinaryToVersionCompareCondition.php',
'Rector\\DeadCode\\ValueObject\\PropertyFetchToVariableAssign' => __DIR__ . '/../..' . '/rules/DeadCode/ValueObject/PropertyFetchToVariableAssign.php',
'Rector\\DeadCode\\ValueObject\\TargetRemoveClassMethod' => __DIR__ . '/../..' . '/rules/DeadCode/ValueObject/TargetRemoveClassMethod.php',
'Rector\\DeadCode\\ValueObject\\VariableAndPropertyFetchAssign' => __DIR__ . '/../..' . '/rules/DeadCode/ValueObject/VariableAndPropertyFetchAssign.php',
'Rector\\DeadCode\\ValueObject\\VersionCompareCondition' => __DIR__ . '/../..' . '/rules/DeadCode/ValueObject/VersionCompareCondition.php',
@ -3107,9 +3106,9 @@ class ComposerStaticInit79faf09d907f46e89d9cb7b01693ec27
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit79faf09d907f46e89d9cb7b01693ec27::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit79faf09d907f46e89d9cb7b01693ec27::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit79faf09d907f46e89d9cb7b01693ec27::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitbbae854da20153f8cf5d9bd84366c876::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitbbae854da20153f8cf5d9bd84366c876::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitbbae854da20153f8cf5d9bd84366c876::$classMap;
}, null, ClassLoader::class);
}