rector/rules/Arguments/Rector/ClassMethod/ReplaceArgumentDefaultValueRector.php

87 lines
3.1 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare (strict_types=1);
namespace Rector\Arguments\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Arguments\ArgumentDefaultValueReplacer;
use Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202306\Webmozart\Assert\Assert;
2019-09-03 09:11:45 +00:00
/**
* @api used in rector-symfony
* @see \Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\ReplaceArgumentDefaultValueRectorTest
2019-09-03 09:11:45 +00:00
*/
final class ReplaceArgumentDefaultValueRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var \Rector\Arguments\ArgumentDefaultValueReplacer
*/
private $argumentDefaultValueReplacer;
/**
* @var ReplaceArgumentDefaultValue[]
*/
private $replacedArguments = [];
public function __construct(ArgumentDefaultValueReplacer $argumentDefaultValueReplacer)
{
$this->argumentDefaultValueReplacer = $argumentDefaultValueReplacer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Replaces defined map of arguments in defined methods and their calls.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
$someObject = new SomeClass;
$someObject->someMethod(SomeClass::OLD_CONSTANT);
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$someObject = new SomeClass;
$someObject->someMethod(false);
CODE_SAMPLE
, [new ReplaceArgumentDefaultValue('SomeClass', 'someMethod', 0, 'SomeClass::OLD_CONSTANT', \false)])]);
}
2018-08-14 22:12:41 +00:00
/**
* @return array<class-string<Node>>
2018-08-14 22:12:41 +00:00
*/
public function getNodeTypes() : array
{
return [MethodCall::class, StaticCall::class, ClassMethod::class];
}
/**
* @param MethodCall|StaticCall|ClassMethod $node
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Stmt\ClassMethod|null
*/
public function refactor(Node $node)
{
$hasChanged = \false;
2020-08-25 22:21:41 +00:00
foreach ($this->replacedArguments as $replacedArgument) {
if (!$this->isName($node->name, $replacedArgument->getMethod())) {
continue;
}
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($node, $replacedArgument->getObjectType())) {
2020-08-25 22:21:41 +00:00
continue;
}
$replacedNode = $this->argumentDefaultValueReplacer->processReplaces($node, $replacedArgument);
if ($replacedNode instanceof Node) {
$hasChanged = \true;
}
}
if ($hasChanged) {
return $node;
}
return null;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
2020-07-29 23:39:41 +00:00
{
Assert::allIsAOf($configuration, ReplaceArgumentDefaultValue::class);
$this->replacedArguments = $configuration;
2020-07-29 23:39:41 +00:00
}
}