Remove CURRENT_STATEMENT where not needed (#2149)

This commit is contained in:
Tomas Votruba 2022-04-24 18:54:56 +02:00 committed by GitHub
parent 80a04aa51f
commit 0954611a38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 77 additions and 149 deletions

View File

@ -15,10 +15,9 @@ use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use Rector\Core\PhpParser\Node\NamedVariableFactory;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Naming\Naming\VariableNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -30,7 +29,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class DowngradeThisInClosureRector extends AbstractRector
{
public function __construct(
private readonly VariableNaming $variableNaming,
private readonly NamedVariableFactory $namedVariableFactory,
private readonly ReflectionResolver $reflectionResolver
) {
}
@ -101,12 +100,11 @@ CODE_SAMPLE
return null;
}
$scope = $node->getAttribute(AttributeKey::SCOPE);
$selfVariable = new Variable($this->variableNaming->createCountedValueName('self', $scope));
$selfVariable = $this->namedVariableFactory->createVariable($node, 'self');
$expression = new Expression(new Assign($selfVariable, new Variable('this')));
$currentStmt = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
$this->nodesToAddCollector->addNodeBeforeNode($expression, $currentStmt);
$this->nodesToAddCollector->addNodeBeforeNode($expression, $node);
$this->traverseNodesWithCallable($node, function (Node $subNode) use ($selfVariable): ?Closure {
if (! $subNode instanceof Closure) {

View File

@ -11,12 +11,9 @@ use PhpParser\Node\Expr\Clone_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\NamedVariableFactory;
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;
@ -29,7 +26,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class DowngradeInstanceMethodCallRector extends AbstractRector
{
public function __construct(
private readonly VariableNaming $variableNaming
private readonly NamedVariableFactory $namedVariableFactory,
) {
}
@ -66,7 +63,7 @@ CODE_SAMPLE
return null;
}
$variable = $this->createVariable($node);
$variable = $this->namedVariableFactory->createVariable($node, 'object');
$expression = new Expression(new Assign($variable, $node->var));
$this->nodesToAddCollector->addNodeBeforeNode($expression, $node);
@ -85,16 +82,4 @@ CODE_SAMPLE
return ! $node->var instanceof Clone_;
}
private function createVariable(Node $node): Variable
{
$currentStmt = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! $currentStmt instanceof Stmt) {
throw new ShouldNotHappenException();
}
$scope = $currentStmt->getAttribute(AttributeKey::SCOPE);
return new Variable($this->variableNaming->createCountedValueName('object', $scope));
}
}

View File

@ -7,25 +7,22 @@ namespace Rector\DowngradePhp55\Rector\Foreach_;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\NamedVariableFactory;
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;
/**
* @changelog https://wiki.php.net/rfc/foreachlist
*
* @see Rector\Tests\DowngradePhp55\Rector\Foreach_\DowngradeForeachListRector\DowngradeForeachListRectorTest
* @see \Rector\Tests\DowngradePhp55\Rector\Foreach_\DowngradeForeachListRector\DowngradeForeachListRectorTest
*/
final class DowngradeForeachListRector extends AbstractRector
{
public function __construct(
private readonly VariableNaming $variableNaming
private readonly NamedVariableFactory $namedVariableFactory
) {
}
@ -69,23 +66,12 @@ CODE_SAMPLE
return null;
}
$variable = $this->createVariable($node);
$variable = $this->namedVariableFactory->createVariable($node, 'arrayItem');
$expression = new Expression(new Assign($node->valueVar, $variable));
$node->valueVar = $variable;
$node->stmts = array_merge([$expression], $node->stmts);
return $node;
}
private function createVariable(Foreach_ $foreach): Variable
{
$currentStmt = $foreach->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! $currentStmt instanceof Node) {
throw new ShouldNotHappenException();
}
$scope = $currentStmt->getAttribute(AttributeKey::SCOPE);
return new Variable($this->variableNaming->createCountedValueName('arrayItem', $scope));
}
}

View File

@ -106,17 +106,12 @@ CODE_SAMPLE
return null;
}
$currentStatement = $funcCall->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! $currentStatement instanceof Stmt) {
return null;
}
$scope = $funcCall->getAttribute(AttributeKey::SCOPE);
$variable = new Variable($this->variableNaming->createCountedValueName('result', $scope));
$this->nodesToAddCollector->addNodeBeforeNode(
new Expression(new Assign($variable, new Array_([]))),
$currentStatement
$funcCall
);
/** @var ConstFetch $constant */
@ -125,7 +120,7 @@ CODE_SAMPLE
? $this->applyArrayFilterUseKey($args, $closure, $variable)
: $this->applyArrayFilterUseBoth($args, $closure, $variable);
$this->nodesToAddCollector->addNodeBeforeNode($foreach, $currentStatement);
$this->nodesToAddCollector->addNodeBeforeNode($foreach, $funcCall);
return $variable;
}

View File

@ -20,10 +20,8 @@ use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\While_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\NamedVariableFactory;
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;
@ -40,7 +38,7 @@ final class DowngradeDirnameLevelsRector extends AbstractRector
private const DIRNAME = 'dirname';
public function __construct(
private readonly VariableNaming $variableNaming
private readonly NamedVariableFactory $namedVariableFactory,
) {
}
@ -128,14 +126,7 @@ CODE_SAMPLE
private function refactorForVariableLevels(FuncCall $funcCall): FuncCall
{
$currentStmt = $funcCall->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! $currentStmt instanceof Node) {
throw new ShouldNotHappenException();
}
$scope = $currentStmt->getAttribute(AttributeKey::SCOPE);
$funcVariable = new Variable($this->variableNaming->createCountedValueName('dirnameFunc', $scope));
$funcVariable = $this->namedVariableFactory->createVariable($funcCall, 'dirnameFunc');
$closure = $this->createClosure();
$exprAssignClosure = $this->createExprAssign($funcVariable, $closure);

View File

@ -13,7 +13,6 @@ use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -60,8 +59,6 @@ CODE_SAMPLE
return null;
}
$currentStatement = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! isset($node->args[0])) {
return null;
}
@ -90,7 +87,7 @@ CODE_SAMPLE
$funcName = new Name('ini_set');
$iniSet = new FuncCall($funcName, [new Arg($sessionKey), new Arg($option->value)]);
$this->nodesToAddCollector->addNodeBeforeNode(new Expression($iniSet), $currentStatement);
$this->nodesToAddCollector->addNodeBeforeNode(new Expression($iniSet), $node);
}
unset($node->args[0]);

View File

@ -11,10 +11,8 @@ use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\NamedVariableFactory;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\Naming\VariableNaming;
use Rector\NodeCollector\BinaryOpConditionsCollector;
use Rector\NodeCollector\BinaryOpTreeRootLocator;
use Rector\NodeTypeResolver\Node\AttributeKey;
@ -29,9 +27,9 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class DowngradeInstanceofThrowableRector extends AbstractRector
{
public function __construct(
private readonly VariableNaming $variableNaming,
private readonly BinaryOpConditionsCollector $binaryOpConditionsCollector,
private readonly BinaryOpTreeRootLocator $binaryOpTreeRootLocator,
private readonly NamedVariableFactory $namedVariableFactory,
) {
}
@ -77,25 +75,14 @@ CODE_SAMPLE
// Store the value into a temporary variable to prevent running possible side-effects twice
// when the expression is e.g. function.
$variable = $this->createVariable($node);
$variable = $this->namedVariableFactory->createVariable($node, 'throwable');
$instanceof = new Instanceof_(new Assign($variable, $node->expr), $node->class);
$exceptionFallbackCheck = $this->createFallbackCheck($variable);
return new BooleanOr($instanceof, $exceptionFallbackCheck);
}
private function createVariable(Instanceof_ $instanceof): Variable
{
$currentStmt = $instanceof->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! $currentStmt instanceof Stmt) {
throw new ShouldNotHappenException();
}
$scope = $currentStmt->getAttribute(AttributeKey::SCOPE);
return new Variable($this->variableNaming->createCountedValueName('throwable', $scope));
}
/**
* Also checks similar manual transformations.
*/

View File

@ -9,7 +9,6 @@ use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Clone_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\Naming\VariableNaming;
@ -78,11 +77,6 @@ CODE_SAMPLE
}
}
$currentStatement = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! $currentStatement instanceof Stmt) {
return null;
}
if ($isFoundCloneInAssign) {
/** @var Assign $assign */
$assign = $node->var;
@ -94,7 +88,7 @@ CODE_SAMPLE
$assign = new Assign($variable, $node->var);
}
$this->nodesToAddCollector->addNodeBeforeNode(new Expression($assign), $currentStatement);
$this->nodesToAddCollector->addNodeBeforeNode(new Expression($assign), $node);
$node->var = $variable;
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);

View File

@ -18,11 +18,9 @@ use PhpParser\Node\Param;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\PhpParser\Node\NamedVariableFactory;
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;
@ -33,7 +31,7 @@ final class DowngradeSpaceshipRector extends AbstractRector
{
public function __construct(
private readonly IfManipulator $ifManipulator,
private readonly VariableNaming $variableNaming
private readonly NamedVariableFactory $namedVariableFactory,
) {
}
@ -94,19 +92,12 @@ CODE_SAMPLE
$ternary = new Ternary($smaller, $ternaryIf, $ternaryElse);
$anonymousFunction->stmts[1] = new Return_($ternary);
$currentStatement = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! $currentStatement instanceof Node) {
throw new ShouldNotHappenException();
}
$assignVariable = $this->namedVariableFactory->createVariable($node, 'battleShipcompare');
$scope = $currentStatement->getAttribute(AttributeKey::SCOPE);
$assignExpression = $this->getAssignExpression($anonymousFunction, $assignVariable);
$this->nodesToAddCollector->addNodeBeforeNode($assignExpression, $node);
$variableAssignName = $this->variableNaming->createCountedValueName('battleShipcompare', $scope);
$variableAssign = new Variable($variableAssignName);
$assignExpression = $this->getAssignExpression($anonymousFunction, $variableAssign);
$this->nodesToAddCollector->addNodeBeforeNode($assignExpression, $currentStatement);
return new FuncCall($variableAssign, [new Arg($node->left), new Arg($node->right)]);
return new FuncCall($assignVariable, [new Arg($node->left), new Arg($node->right)]);
}
private function getAssignExpression(Closure $closure, Variable $variable): Expression

View File

@ -11,12 +11,10 @@ use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\PhpParser\Node\NamedVariableFactory;
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;
@ -28,7 +26,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class DowngradeClosureFromCallableRector extends AbstractRector
{
public function __construct(
private readonly VariableNaming $variableNaming
private readonly NamedVariableFactory $namedVariableFactory,
) {
}
@ -78,17 +76,10 @@ CODE_SAMPLE
return null;
}
$tempVariableName = $this->variableNaming->createCountedValueName(
'callable',
$node->getAttribute(AttributeKey::SCOPE)
);
$tempVariable = new Variable($tempVariableName);
$tempVariable = $this->namedVariableFactory->createVariable($node, 'callable');
$expression = new Expression(new Assign($tempVariable, $node->args[0]->value));
$currentStatement = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
$this->nodesToAddCollector->addNodeBeforeNode($expression, $currentStatement);
$this->nodesToAddCollector->addNodeBeforeNode($expression, $node);
$closure = new Closure();
$closure->uses[] = new ClosureUse($tempVariable);

View File

@ -215,8 +215,7 @@ CODE_SAMPLE
private function processInAssign(Assign $assign, FuncCall $funcCall, FuncCall $replaceEmptyStringToNull): FuncCall
{
$currentStatement = $assign->getAttribute(AttributeKey::CURRENT_STATEMENT);
$this->nodesToAddCollector->addNodeAfterNode(new Expression($replaceEmptyStringToNull), $currentStatement);
$this->nodesToAddCollector->addNodeAfterNode(new Expression($replaceEmptyStringToNull), $assign);
return $funcCall;
}

View File

@ -17,7 +17,6 @@ use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Type\ObjectType;
use Rector\Core\NodeManipulator\IfManipulator;
@ -123,16 +122,11 @@ CODE_SAMPLE
/**
* @param string[] $classConstFetchNames
*/
private function processClassConstFetches(MethodCall $methodCall, array $classConstFetchNames): ?Variable
private function processClassConstFetches(MethodCall $methodCall, array $classConstFetchNames): Variable
{
$scope = $methodCall->getAttribute(AttributeKey::SCOPE);
$reflectionClassConstants = $this->variableNaming->createCountedValueName('reflectionClassConstants', $scope);
$currentStmt = $methodCall->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! $currentStmt instanceof Stmt) {
return null;
}
$variableReflectionClassConstants = new Variable($this->variableNaming->createCountedValueName(
$reflectionClassConstants,
$scope
@ -141,12 +135,12 @@ CODE_SAMPLE
$variableReflectionClassConstants,
new MethodCall($methodCall->var, 'getReflectionConstants')
);
$this->nodesToAddCollector->addNodeBeforeNode(new Expression($assign), $currentStmt);
$this->nodesToAddCollector->addNodeBeforeNode(new Expression($assign), $methodCall);
$result = $this->variableNaming->createCountedValueName('result', $scope);
$variableResult = new Variable($result);
$assignVariableResult = new Assign($variableResult, new Array_());
$this->nodesToAddCollector->addNodeBeforeNode(new Expression($assignVariableResult), $currentStmt);
$this->nodesToAddCollector->addNodeBeforeNode(new Expression($assignVariableResult), $methodCall);
$ifs = [];
$valueVariable = new Variable('value');
@ -174,7 +168,7 @@ CODE_SAMPLE
'array_walk',
[$variableReflectionClassConstants, $closure]
);
$this->nodesToAddCollector->addNodeBeforeNode(new Expression($funcCall), $currentStmt);
$this->nodesToAddCollector->addNodeBeforeNode(new Expression($funcCall), $methodCall);
return $variableResult;
}

View File

@ -15,9 +15,8 @@ use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\NamedVariableFactory;
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;
@ -30,7 +29,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class DowngradeArbitraryExpressionsSupportRector extends AbstractRector
{
public function __construct(
private readonly VariableNaming $variableNaming
private readonly NamedVariableFactory $namedVariableFactory,
) {
}
@ -93,13 +92,7 @@ CODE_SAMPLE
$assign = $node->class;
$variable = $assign->var;
} else {
$currentStmt = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! $currentStmt instanceof Node) {
throw new ShouldNotHappenException();
}
$scope = $currentStmt->getAttribute(AttributeKey::SCOPE);
$variable = new Variable($this->variableNaming->createCountedValueName('className', $scope));
$variable = $this->namedVariableFactory->createVariable($node, 'className');
$assign = new Assign($variable, $node->class);
}

View File

@ -9,7 +9,6 @@ use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Parser\InlineCodeParser;
@ -82,18 +81,13 @@ CODE_SAMPLE
return null;
}
$currentStmt = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! $currentStmt instanceof Stmt) {
return null;
}
$scope = $node->getAttribute(AttributeKey::SCOPE);
$variable = new Variable($this->variableNaming->createCountedValueName('arrayIsList', $scope));
$function = $this->createClosure();
$expression = new Expression(new Assign($variable, $function));
$this->nodesToAddCollector->addNodeBeforeNode($expression, $currentStmt);
$this->nodesToAddCollector->addNodeBeforeNode($expression, $node);
return new FuncCall($variable, $node->args);
}

View File

@ -538,7 +538,7 @@ final class BetterNodeFinder
return $foundNode;
}
private function resolveCurrentStatement(Node $node): ?Stmt
public function resolveCurrentStatement(Node $node): ?Stmt
{
if ($node instanceof Stmt) {
return $node;

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Core\PhpParser\Node;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Naming\Naming\VariableNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class NamedVariableFactory
{
public function __construct(
private readonly VariableNaming $variableNaming,
private readonly BetterNodeFinder $betterNodeFinder,
) {
}
public function createVariable(Node $node, string $variableName): Variable
{
$currentStmt = $this->betterNodeFinder->resolveCurrentStatement($node);
if (! $currentStmt instanceof Node) {
throw new ShouldNotHappenException();
}
$scope = $currentStmt->getAttribute(AttributeKey::SCOPE);
$variableName = $this->variableNaming->createCountedValueName($variableName, $scope);
return new Variable($variableName);
}
}