Merge pull request #244 from carusogabriel/use-method-call-node-factory

Use MethodCallNodeFactory by Rector
This commit is contained in:
Tomáš Votruba 2017-12-27 22:39:45 +01:00 committed by GitHub
commit f15348069d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 11 deletions

View File

@ -3,8 +3,8 @@
namespace Rector\Rector\Contrib\PhpParser;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use Rector\Node\MethodCallNodeFactory;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Rector\AbstractRector;
@ -18,9 +18,17 @@ final class UseWithAliasRector extends AbstractRector
*/
private $propertyFetchAnalyzer;
public function __construct(PropertyFetchAnalyzer $propertyFetchAnalyzer)
{
/**
* @var MethodCallNodeFactory
*/
private $methodCallNodeFactory;
public function __construct(
PropertyFetchAnalyzer $propertyFetchAnalyzer,
MethodCallNodeFactory $methodCallNodeFactory
) {
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
$this->methodCallNodeFactory = $methodCallNodeFactory;
}
public function isCandidate(Node $node): bool
@ -33,8 +41,11 @@ final class UseWithAliasRector extends AbstractRector
*/
public function refactor(Node $propertyFetchNode): ?Node
{
$getAliasMethodCall = new MethodCall($propertyFetchNode->var, 'getAlias');
$getAliasMethodCall = $this->methodCallNodeFactory->createWithVariableAndMethodName(
$propertyFetchNode->var,
'getAlias'
);
return new MethodCall($getAliasMethodCall, 'toString');
return $this->methodCallNodeFactory->createWithVariableAndMethodName($getAliasMethodCall, 'toString');
}
}

View File

@ -4,9 +4,10 @@ namespace Rector\Rector\Dynamic;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use Rector\Node\MethodCallNodeFactory;
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Rector\AbstractRector;
@ -46,17 +47,24 @@ final class PropertyToMethodRector extends AbstractRector
*/
private $nodeFactory;
/**
* @var MethodCallNodeFactory
*/
private $methodCallNodeFactory;
/**
* @param string[][][] $perClassOldToNewProperties
*/
public function __construct(
array $perClassOldToNewProperties,
PropertyFetchAnalyzer $propertyFetchAnalyzer,
NodeFactory $nodeFactory
NodeFactory $nodeFactory,
MethodCallNodeFactory $methodCallNodeFactory
) {
$this->perClassPropertyToMethods = $perClassOldToNewProperties;
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
$this->nodeFactory = $nodeFactory;
$this->methodCallNodeFactory = $methodCallNodeFactory;
}
public function isCandidate(Node $node): bool
@ -89,12 +97,22 @@ final class PropertyToMethodRector extends AbstractRector
$assignNode->expr,
]);
return new MethodCall($assignNode->var->var, $this->activeMethod, $args);
/** @var Variable $variable */
$variable = $assignNode->var->var;
return $this->methodCallNodeFactory->createWithVariableMethodNameAndArguments(
$variable,
$this->activeMethod,
$args
);
}
// getter
if ($assignNode->expr instanceof PropertyFetch) {
$assignNode->expr = new MethodCall($assignNode->expr->var, $this->activeMethod);
$assignNode->expr = $this->methodCallNodeFactory->createWithVariableAndMethodName(
$assignNode->expr->var,
$this->activeMethod
);
}
return null;

View File

@ -5,6 +5,7 @@ namespace Rector\Rector\MagicDisclosure;
use PhpParser\Node;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\MethodCall;
use Rector\Node\MethodCallNodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\NodeTypeResolver\NodeTypeResolver;
@ -48,6 +49,11 @@ final class ToStringToMethodCallRector extends AbstractRector
*/
private $nodeTypeResolver;
/**
* @var MethodCallNodeFactory
*/
private $methodCallNodeFactory;
/**
* Type to method call()
*
@ -57,12 +63,14 @@ final class ToStringToMethodCallRector extends AbstractRector
array $typeToMethodCalls,
MethodCallAnalyzer $methodCallAnalyzer,
IdentifierRenamer $identifierRenamer,
NodeTypeResolver $nodeTypeResolver
NodeTypeResolver $nodeTypeResolver,
MethodCallNodeFactory $methodCallNodeFactory
) {
$this->typeToMethodCalls = $typeToMethodCalls;
$this->methodCallAnalyzer = $methodCallAnalyzer;
$this->identifierRenamer = $identifierRenamer;
$this->nodeTypeResolver = $nodeTypeResolver;
$this->methodCallNodeFactory = $methodCallNodeFactory;
}
public function isCandidate(Node $node): bool
@ -84,7 +92,10 @@ final class ToStringToMethodCallRector extends AbstractRector
public function refactor(Node $node): ?Node
{
if ($node instanceof String_) {
return new MethodCall($node->expr, $this->activeTransformation);
return $this->methodCallNodeFactory->createWithVariableAndMethodName(
$node->expr,
$this->activeTransformation
);
}
$this->identifierRenamer->renameNode($node, $this->activeTransformation);