mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-07 20:00:50 +00:00
f3d9b9c31c
5493b6dc13
[DX] Re-use configure() in docs + remove prefixing of ValueObjectInliner (#1410)
85 lines
3.4 KiB
PHP
85 lines
3.4 KiB
PHP
<?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 RectorPrefix20211206\Webmozart\Assert\Assert;
|
|
/**
|
|
* @see \Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\ReplaceArgumentDefaultValueRectorTest
|
|
*/
|
|
final class ReplaceArgumentDefaultValueRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
public const REPLACED_ARGUMENTS = 'replaced_arguments';
|
|
/**
|
|
* @var ReplaceArgumentDefaultValue[]
|
|
*/
|
|
private $replacedArguments = [];
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\Arguments\ArgumentDefaultValueReplacer
|
|
*/
|
|
private $argumentDefaultValueReplacer;
|
|
public function __construct(\Rector\Arguments\ArgumentDefaultValueReplacer $argumentDefaultValueReplacer)
|
|
{
|
|
$this->argumentDefaultValueReplacer = $argumentDefaultValueReplacer;
|
|
}
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Replaces defined map of arguments in defined methods and their calls.', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
|
$someObject = new SomeClass;
|
|
$someObject->someMethod(SomeClass::OLD_CONSTANT);
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
$someObject = new SomeClass;
|
|
$someObject->someMethod(false);
|
|
CODE_SAMPLE
|
|
, [new \Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue('SomeClass', 'someMethod', 0, 'SomeClass::OLD_CONSTANT', \false)])]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Expr\MethodCall::class, \PhpParser\Node\Expr\StaticCall::class, \PhpParser\Node\Stmt\ClassMethod::class];
|
|
}
|
|
/**
|
|
* @param MethodCall|StaticCall|ClassMethod $node
|
|
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Stmt\ClassMethod
|
|
*/
|
|
public function refactor(\PhpParser\Node $node)
|
|
{
|
|
foreach ($this->replacedArguments as $replacedArgument) {
|
|
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($node, $replacedArgument->getObjectType())) {
|
|
continue;
|
|
}
|
|
if (!$this->isName($node->name, $replacedArgument->getMethod())) {
|
|
continue;
|
|
}
|
|
$this->argumentDefaultValueReplacer->processReplaces($node, $replacedArgument);
|
|
}
|
|
return $node;
|
|
}
|
|
/**
|
|
* @param mixed[] $configuration
|
|
*/
|
|
public function configure(array $configuration) : void
|
|
{
|
|
$replacedArguments = $configuration[self::REPLACED_ARGUMENTS] ?? $configuration;
|
|
\RectorPrefix20211206\Webmozart\Assert\Assert::allIsAOf($replacedArguments, \Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue::class);
|
|
$this->replacedArguments = $replacedArguments;
|
|
}
|
|
}
|