2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Transform\Rector\MethodCall;
|
2017-09-18 12:50:26 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
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;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2023-02-02 09:24:12 +00:00
|
|
|
use RectorPrefix202302\Webmozart\Assert\Assert;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Transform\Rector\MethodCall\MethodCallToAnotherMethodCallWithArgumentsRector\MethodCallToAnotherMethodCallWithArgumentsRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class MethodCallToAnotherMethodCallWithArgumentsRector extends AbstractRector implements ConfigurableRectorInterface
|
2017-09-18 12:50:26 +00:00
|
|
|
{
|
|
|
|
/**
|
2020-09-12 21:19:08 +00:00
|
|
|
* @var MethodCallToAnotherMethodCallWithArguments[]
|
2018-06-06 11:52:48 +00:00
|
|
|
*/
|
2020-08-26 10:54:53 +00:00
|
|
|
private $methodCallRenamesWithAddedArguments = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-04-07 22:57:40 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Turns old method call with specific types to new one with arguments', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2018-06-06 11:52:48 +00:00
|
|
|
$serviceDefinition = new Nette\DI\ServiceDefinition;
|
|
|
|
$serviceDefinition->setInject();
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-06-06 11:52:48 +00:00
|
|
|
$serviceDefinition = new Nette\DI\ServiceDefinition;
|
|
|
|
$serviceDefinition->addTag('inject');
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2022-06-07 09:46:15 +00:00
|
|
|
, [new MethodCallToAnotherMethodCallWithArguments('Nette\\DI\\ServiceDefinition', 'setInject', 'addTag', ['inject'])])]);
|
2018-04-07 22:57:40 +00:00
|
|
|
}
|
2018-08-14 22:12:41 +00:00
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-08-14 22:12:41 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2017-09-18 12:50:26 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [MethodCall::class];
|
2017-09-18 12:50:26 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param MethodCall $node
|
2017-09-18 12:50:26 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2017-09-18 12:50:26 +00:00
|
|
|
{
|
2021-03-05 10:55:40 +00:00
|
|
|
foreach ($this->methodCallRenamesWithAddedArguments as $methodCallRenameWithAddedArgument) {
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isObjectType($node->var, $methodCallRenameWithAddedArgument->getObjectType())) {
|
2018-10-14 10:13:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isName($node->name, $methodCallRenameWithAddedArgument->getOldMethod())) {
|
2020-08-26 10:54:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->name = new Identifier($methodCallRenameWithAddedArgument->getNewMethod());
|
2021-03-05 10:55:40 +00:00
|
|
|
$node->args = $this->nodeFactory->createArgs($methodCallRenameWithAddedArgument->getNewArguments());
|
2020-08-26 10:54:53 +00:00
|
|
|
return $node;
|
2018-10-14 10:13:24 +00:00
|
|
|
}
|
2020-07-28 15:15:30 +00:00
|
|
|
return null;
|
2017-09-18 12:50:26 +00:00
|
|
|
}
|
2021-05-02 10:46:55 +00:00
|
|
|
/**
|
2021-11-28 17:01:20 +00:00
|
|
|
* @param mixed[] $configuration
|
2021-05-02 10:46:55 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2020-07-29 23:39:41 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allIsAOf($configuration, MethodCallToAnotherMethodCallWithArguments::class);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->methodCallRenamesWithAddedArguments = $configuration;
|
2020-07-29 23:39:41 +00:00
|
|
|
}
|
2017-09-18 12:50:26 +00:00
|
|
|
}
|