mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-21 09:42:45 +01:00
decouple MethodCallNodeFactory calls
This commit is contained in:
parent
140c75ee9b
commit
b271ec7e3e
@ -5,6 +5,7 @@ namespace Rector\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Identifier;
|
||||
@ -37,6 +38,10 @@ final class MethodCallNodeFactory
|
||||
*/
|
||||
public function createMethodCallWithVariable(Expr $exprNode, string $methodName): MethodCall
|
||||
{
|
||||
if ($exprNode instanceof PropertyFetch) {
|
||||
$exprNode = $this->nodeFactory->clonePropertyFetch($exprNode);
|
||||
}
|
||||
|
||||
$methodCallNode = new MethodCall($exprNode, $methodName);
|
||||
$exprNode->setAttribute(Attribute::PARENT_NODE, $methodCallNode);
|
||||
|
||||
|
@ -13,7 +13,6 @@ use PhpParser\Node\Expr\ArrayItem;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
@ -113,27 +112,6 @@ final class NodeFactory
|
||||
return new ClassConstFetch($nameNode, 'class');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates "$method->call();"
|
||||
*/
|
||||
public function createMethodCall(string $variableName, string $methodName): MethodCall
|
||||
{
|
||||
$variableNode = $this->createVariable($variableName);
|
||||
|
||||
return new MethodCall($variableNode, $methodName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates "$method->call();" from existing variable
|
||||
*/
|
||||
public function createMethodCallWithVariable(Expr $exprNode, string $methodName): MethodCall
|
||||
{
|
||||
$methodCallNode = new MethodCall($exprNode, $methodName);
|
||||
$exprNode->setAttribute(Attribute::PARENT_NODE, $methodCallNode);
|
||||
|
||||
return $methodCallNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates "use \SomeTrait;"
|
||||
*/
|
||||
@ -238,34 +216,6 @@ final class NodeFactory
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Arg[] $arguments
|
||||
*/
|
||||
public function createMethodCallWithArguments(
|
||||
string $variableName,
|
||||
string $methodName,
|
||||
array $arguments
|
||||
): MethodCall {
|
||||
$methodCallNode = $this->createMethodCall($variableName, $methodName);
|
||||
$methodCallNode->args = $arguments;
|
||||
|
||||
return $methodCallNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $arguments
|
||||
*/
|
||||
public function createMethodCallWithVariableAndArguments(
|
||||
Variable $variableNode,
|
||||
string $method,
|
||||
array $arguments
|
||||
): MethodCall {
|
||||
$methodCall = $this->createMethodCallWithVariable($variableNode, $method);
|
||||
$methodCall->args = $this->createArgs($arguments);
|
||||
|
||||
return $methodCall;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates:
|
||||
* - $variable->property['key'];
|
||||
|
@ -5,6 +5,7 @@ namespace Rector\Rector\Contrib\Nette\Application;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use Rector\Node\MethodCallNodeFactory;
|
||||
use Rector\Node\NodeFactory;
|
||||
use Rector\NodeAnalyzer\MethodCallAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
@ -28,10 +29,19 @@ final class TemplateMagicInvokeFilterCallRector extends AbstractRector
|
||||
*/
|
||||
private $nodeFactory;
|
||||
|
||||
public function __construct(MethodCallAnalyzer $methodCallAnalyzer, NodeFactory $nodeFactory)
|
||||
{
|
||||
/**
|
||||
* @var MethodCallNodeFactory
|
||||
*/
|
||||
private $methodCallNodeFactory;
|
||||
|
||||
public function __construct(
|
||||
MethodCallAnalyzer $methodCallAnalyzer,
|
||||
NodeFactory $nodeFactory,
|
||||
MethodCallNodeFactory $methodCallNodeFactory
|
||||
) {
|
||||
$this->methodCallAnalyzer = $methodCallAnalyzer;
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->methodCallNodeFactory = $methodCallNodeFactory;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
@ -46,9 +56,10 @@ final class TemplateMagicInvokeFilterCallRector extends AbstractRector
|
||||
{
|
||||
$this->changeToInvokeFilterMethodCall($methodCallNode);
|
||||
|
||||
$propertyFetchNode = $this->nodeFactory->clonePropertyFetch($methodCallNode->var);
|
||||
|
||||
$methodCallNode->var = $this->nodeFactory->createMethodCallWithVariable($propertyFetchNode, 'getLatte');
|
||||
$methodCallNode->var = $this->methodCallNodeFactory->createMethodCallWithVariable(
|
||||
$methodCallNode->var,
|
||||
'getLatte'
|
||||
);
|
||||
|
||||
return $methodCallNode;
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ namespace Rector\Rector\Contrib\Nette\Application;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use Rector\Node\NodeFactory;
|
||||
use Rector\Node\MethodCallNodeFactory;
|
||||
use Rector\NodeAnalyzer\MethodCallAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
@ -24,14 +24,14 @@ final class TemplateRegisterHelperRector extends AbstractRector
|
||||
private $methodCallAnalyzer;
|
||||
|
||||
/**
|
||||
* @var NodeFactory
|
||||
* @var MethodCallNodeFactory
|
||||
*/
|
||||
private $nodeFactory;
|
||||
private $methodCallNodeFactory;
|
||||
|
||||
public function __construct(MethodCallAnalyzer $methodCallAnalyzer, NodeFactory $nodeFactory)
|
||||
public function __construct(MethodCallAnalyzer $methodCallAnalyzer, MethodCallNodeFactory $methodCallNodeFactory)
|
||||
{
|
||||
$this->methodCallAnalyzer = $methodCallAnalyzer;
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->methodCallNodeFactory = $methodCallNodeFactory;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
@ -50,9 +50,10 @@ final class TemplateRegisterHelperRector extends AbstractRector
|
||||
{
|
||||
$methodCallNode->name = new Identifier('addFilter');
|
||||
|
||||
$propertyFetchNode = $this->nodeFactory->clonePropertyFetch($methodCallNode->var);
|
||||
|
||||
$methodCallNode->var = $this->nodeFactory->createMethodCallWithVariable($propertyFetchNode, 'getLatte');
|
||||
$methodCallNode->var = $this->methodCallNodeFactory->createMethodCallWithVariable(
|
||||
$methodCallNode->var,
|
||||
'getLatte'
|
||||
);
|
||||
|
||||
return $methodCallNode;
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ namespace Rector\Rector\Contrib\Nette\Forms;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use Rector\Node\NodeFactory;
|
||||
use Rector\Node\MethodCallNodeFactory;
|
||||
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
@ -23,14 +23,16 @@ final class ChoiceDefaultValueRector extends AbstractRector
|
||||
private $propertyFetchAnalyzer;
|
||||
|
||||
/**
|
||||
* @var NodeFactory
|
||||
* @var MethodCallNodeFactory
|
||||
*/
|
||||
private $nodeFactory;
|
||||
private $methodCallNodeFactory;
|
||||
|
||||
public function __construct(PropertyFetchAnalyzer $propertyFetchAnalyzer, NodeFactory $nodeFactory)
|
||||
{
|
||||
public function __construct(
|
||||
PropertyFetchAnalyzer $propertyFetchAnalyzer,
|
||||
MethodCallNodeFactory $methodCallNodeFactory
|
||||
) {
|
||||
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->methodCallNodeFactory = $methodCallNodeFactory;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
@ -53,7 +55,7 @@ final class ChoiceDefaultValueRector extends AbstractRector
|
||||
{
|
||||
$propertyNode = $assignNode->var->var;
|
||||
|
||||
return $this->nodeFactory->createMethodCallWithVariableAndArguments(
|
||||
return $this->methodCallNodeFactory->createMethodCallWithVariableAndArguments(
|
||||
$propertyNode,
|
||||
'checkDefaultValue',
|
||||
[$assignNode->expr]
|
||||
|
@ -6,7 +6,7 @@ use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use Rector\Node\Attribute;
|
||||
use Rector\Node\NodeFactory;
|
||||
use Rector\Node\MethodCallNodeFactory;
|
||||
use Rector\NodeAnalyzer\MethodCallAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
@ -21,14 +21,14 @@ final class ExceptionRector extends AbstractRector
|
||||
private $methodCallAnalyzer;
|
||||
|
||||
/**
|
||||
* @var NodeFactory
|
||||
* @var MethodCallNodeFactory
|
||||
*/
|
||||
private $nodeFactory;
|
||||
private $methodCallNodeFactory;
|
||||
|
||||
public function __construct(MethodCallAnalyzer $methodCallAnalyzer, NodeFactory $nodeFactory)
|
||||
public function __construct(MethodCallAnalyzer $methodCallAnalyzer, MethodCallNodeFactory $methodCallNodeFactory)
|
||||
{
|
||||
$this->methodCallAnalyzer = $methodCallAnalyzer;
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->methodCallNodeFactory = $methodCallNodeFactory;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
@ -56,7 +56,7 @@ final class ExceptionRector extends AbstractRector
|
||||
$secondArgument = $methodCallNode->args[1];
|
||||
unset($methodCallNode->args[1]);
|
||||
|
||||
$expectExceptionMessageMethodCall = $this->nodeFactory->createMethodCallWithArguments(
|
||||
$expectExceptionMessageMethodCall = $this->methodCallNodeFactory->createMethodCallWithArguments(
|
||||
'this',
|
||||
'expectExceptionMessage',
|
||||
[$secondArgument]
|
||||
|
@ -6,7 +6,7 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use Rector\Node\Attribute;
|
||||
use Rector\Node\NodeFactory;
|
||||
use Rector\Node\MethodCallNodeFactory;
|
||||
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
@ -47,14 +47,16 @@ final class IdentifierRector extends AbstractRector
|
||||
];
|
||||
|
||||
/**
|
||||
* @var NodeFactory
|
||||
* @var MethodCallNodeFactory
|
||||
*/
|
||||
private $nodeFactory;
|
||||
private $methodCallNodeFactory;
|
||||
|
||||
public function __construct(PropertyFetchAnalyzer $propertyFetchAnalyzer, NodeFactory $nodeFactory)
|
||||
{
|
||||
public function __construct(
|
||||
PropertyFetchAnalyzer $propertyFetchAnalyzer,
|
||||
MethodCallNodeFactory $methodCallNodeFactory
|
||||
) {
|
||||
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->methodCallNodeFactory = $methodCallNodeFactory;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
@ -81,7 +83,7 @@ final class IdentifierRector extends AbstractRector
|
||||
return $propertyFetchNode;
|
||||
}
|
||||
|
||||
return $this->nodeFactory->createMethodCallWithVariable($propertyFetchNode, 'toString');
|
||||
return $this->methodCallNodeFactory->createMethodCallWithVariable($propertyFetchNode, 'toString');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -6,7 +6,7 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use Rector\Node\Attribute;
|
||||
use Rector\Node\NodeFactory;
|
||||
use Rector\Node\MethodCallNodeFactory;
|
||||
use Rector\NodeAnalyzer\MethodCallAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
@ -19,20 +19,20 @@ use Rector\Rector\AbstractRector;
|
||||
*/
|
||||
final class FormIsValidRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var NodeFactory
|
||||
*/
|
||||
private $nodeFactory;
|
||||
|
||||
/**
|
||||
* @var MethodCallAnalyzer
|
||||
*/
|
||||
private $methodCallAnalyzer;
|
||||
|
||||
public function __construct(NodeFactory $nodeFactory, MethodCallAnalyzer $methodCallAnalyzer)
|
||||
/**
|
||||
* @var MethodCallNodeFactory
|
||||
*/
|
||||
private $methodCallNodeFactory;
|
||||
|
||||
public function __construct(MethodCallNodeFactory $methodCallNodeFactory, MethodCallAnalyzer $methodCallAnalyzer)
|
||||
{
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->methodCallAnalyzer = $methodCallAnalyzer;
|
||||
$this->methodCallNodeFactory = $methodCallNodeFactory;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
@ -60,8 +60,8 @@ final class FormIsValidRector extends AbstractRector
|
||||
$variableName = $node->var->name;
|
||||
|
||||
return new BooleanAnd(
|
||||
$this->nodeFactory->createMethodCall($variableName, 'isSubmitted'),
|
||||
$this->nodeFactory->createMethodCall($variableName, 'isValid')
|
||||
$this->methodCallNodeFactory->createMethodCall($variableName, 'isSubmitted'),
|
||||
$this->methodCallNodeFactory->createMethodCall($variableName, 'isValid')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use Rector\Node\NodeFactory;
|
||||
use Rector\Node\MethodCallNodeFactory;
|
||||
use Rector\NodeAnalyzer\ExpressionAnalyzer;
|
||||
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
@ -35,11 +35,6 @@ final class GetAndSetToMethodCallRector extends AbstractRector
|
||||
*/
|
||||
private $propertyFetchAnalyzer;
|
||||
|
||||
/**
|
||||
* @var NodeFactory
|
||||
*/
|
||||
private $nodeFactory;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
@ -50,6 +45,11 @@ final class GetAndSetToMethodCallRector extends AbstractRector
|
||||
*/
|
||||
private $expressionAnalyzer;
|
||||
|
||||
/**
|
||||
* @var MethodCallNodeFactory
|
||||
*/
|
||||
private $methodCallNodeFactory;
|
||||
|
||||
/**
|
||||
* Type to method call()
|
||||
*
|
||||
@ -58,13 +58,13 @@ final class GetAndSetToMethodCallRector extends AbstractRector
|
||||
public function __construct(
|
||||
array $typeToMethodCalls,
|
||||
PropertyFetchAnalyzer $propertyFetchAnalyzer,
|
||||
NodeFactory $nodeFactory,
|
||||
MethodCallNodeFactory $methodCallNodeFactory,
|
||||
ExpressionAnalyzer $expressionAnalyzer
|
||||
) {
|
||||
$this->typeToMethodCalls = $typeToMethodCalls;
|
||||
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->expressionAnalyzer = $expressionAnalyzer;
|
||||
$this->methodCallNodeFactory = $methodCallNodeFactory;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
@ -115,7 +115,7 @@ final class GetAndSetToMethodCallRector extends AbstractRector
|
||||
): MethodCall {
|
||||
$value = $propertyFetchNode->name->name;
|
||||
|
||||
return $this->nodeFactory->createMethodCallWithVariableAndArguments(
|
||||
return $this->methodCallNodeFactory->createMethodCallWithVariableAndArguments(
|
||||
$propertyFetchNode->var,
|
||||
$method,
|
||||
[$value]
|
||||
@ -129,7 +129,7 @@ final class GetAndSetToMethodCallRector extends AbstractRector
|
||||
|
||||
$key = $propertyFetchNode->name->name;
|
||||
|
||||
return $this->nodeFactory->createMethodCallWithVariableAndArguments(
|
||||
return $this->methodCallNodeFactory->createMethodCallWithVariableAndArguments(
|
||||
$propertyFetchNode->var,
|
||||
$method,
|
||||
[$key, $assignNode->expr]
|
||||
|
@ -8,7 +8,7 @@ use PhpParser\Node\Expr\Isset_;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Unset_;
|
||||
use Rector\Node\Attribute;
|
||||
use Rector\Node\NodeFactory;
|
||||
use Rector\Node\MethodCallNodeFactory;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
/**
|
||||
@ -29,25 +29,25 @@ final class UnsetAndIssetToMethodCallRector extends AbstractRector
|
||||
*/
|
||||
private $typeToMethodCalls = [];
|
||||
|
||||
/**
|
||||
* @var NodeFactory
|
||||
*/
|
||||
private $nodeFactory;
|
||||
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
private $activeTransformation = [];
|
||||
|
||||
/**
|
||||
* @var MethodCallNodeFactory
|
||||
*/
|
||||
private $methodCallNodeFactory;
|
||||
|
||||
/**
|
||||
* Type to method call()
|
||||
*
|
||||
* @param string[] $typeToMethodCalls
|
||||
*/
|
||||
public function __construct(array $typeToMethodCalls, NodeFactory $nodeFactory)
|
||||
public function __construct(array $typeToMethodCalls, MethodCallNodeFactory $methodCallNodeFactory)
|
||||
{
|
||||
$this->typeToMethodCalls = $typeToMethodCalls;
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->methodCallNodeFactory = $methodCallNodeFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -88,7 +88,7 @@ final class UnsetAndIssetToMethodCallRector extends AbstractRector
|
||||
$variableNode = $issetOrUnsetNode->vars[0]->var;
|
||||
$key = $issetOrUnsetNode->vars[0]->dim;
|
||||
|
||||
$methodCall = $this->nodeFactory->createMethodCallWithVariableAndArguments(
|
||||
$methodCall = $this->methodCallNodeFactory->createMethodCallWithVariableAndArguments(
|
||||
$variableNode,
|
||||
$method,
|
||||
[$key]
|
||||
|
Loading…
x
Reference in New Issue
Block a user