mirror of https://github.com/rectorphp/rector.git
70 lines
2.6 KiB
PHP
70 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\Transform\Rector\MethodCall;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
use PhpParser\Node\Identifier;
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\Transform\ValueObject\MethodCallToAnotherMethodCallWithArguments;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
use RectorPrefix202302\Webmozart\Assert\Assert;
|
|
/**
|
|
* @see \Rector\Tests\Transform\Rector\MethodCall\MethodCallToAnotherMethodCallWithArgumentsRector\MethodCallToAnotherMethodCallWithArgumentsRectorTest
|
|
*/
|
|
final class MethodCallToAnotherMethodCallWithArgumentsRector extends AbstractRector implements ConfigurableRectorInterface
|
|
{
|
|
/**
|
|
* @var MethodCallToAnotherMethodCallWithArguments[]
|
|
*/
|
|
private $methodCallRenamesWithAddedArguments = [];
|
|
public function getRuleDefinition() : RuleDefinition
|
|
{
|
|
return new RuleDefinition('Turns old method call with specific types to new one with arguments', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
|
$serviceDefinition = new Nette\DI\ServiceDefinition;
|
|
$serviceDefinition->setInject();
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
$serviceDefinition = new Nette\DI\ServiceDefinition;
|
|
$serviceDefinition->addTag('inject');
|
|
CODE_SAMPLE
|
|
, [new MethodCallToAnotherMethodCallWithArguments('Nette\\DI\\ServiceDefinition', 'setInject', 'addTag', ['inject'])])]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [MethodCall::class];
|
|
}
|
|
/**
|
|
* @param MethodCall $node
|
|
*/
|
|
public function refactor(Node $node) : ?Node
|
|
{
|
|
foreach ($this->methodCallRenamesWithAddedArguments as $methodCallRenameWithAddedArgument) {
|
|
if (!$this->isObjectType($node->var, $methodCallRenameWithAddedArgument->getObjectType())) {
|
|
continue;
|
|
}
|
|
if (!$this->isName($node->name, $methodCallRenameWithAddedArgument->getOldMethod())) {
|
|
continue;
|
|
}
|
|
$node->name = new Identifier($methodCallRenameWithAddedArgument->getNewMethod());
|
|
$node->args = $this->nodeFactory->createArgs($methodCallRenameWithAddedArgument->getNewArguments());
|
|
return $node;
|
|
}
|
|
return null;
|
|
}
|
|
/**
|
|
* @param mixed[] $configuration
|
|
*/
|
|
public function configure(array $configuration) : void
|
|
{
|
|
Assert::allIsAOf($configuration, MethodCallToAnotherMethodCallWithArguments::class);
|
|
$this->methodCallRenamesWithAddedArguments = $configuration;
|
|
}
|
|
}
|