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\Arguments\Rector\ClassMethod;
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
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;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2023-06-01 08:56:46 +00:00
|
|
|
use RectorPrefix202306\Webmozart\Assert\Assert;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2023-06-08 18:28:01 +00:00
|
|
|
* @api used in rector-symfony
|
2021-06-10 10:46:24 +00:00
|
|
|
* @see \Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\ReplaceArgumentDefaultValueRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ReplaceArgumentDefaultValueRector extends AbstractRector implements ConfigurableRectorInterface
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2021-05-17 23:17:47 +00:00
|
|
|
/**
|
|
|
|
* @var \Rector\Arguments\ArgumentDefaultValueReplacer
|
|
|
|
*/
|
|
|
|
private $argumentDefaultValueReplacer;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var ReplaceArgumentDefaultValue[]
|
|
|
|
*/
|
|
|
|
private $replacedArguments = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(ArgumentDefaultValueReplacer $argumentDefaultValueReplacer)
|
2021-05-17 23:17:47 +00:00
|
|
|
{
|
|
|
|
$this->argumentDefaultValueReplacer = $argumentDefaultValueReplacer;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Replaces defined map of arguments in defined methods and their calls.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2018-05-04 12:55:47 +00:00
|
|
|
$someObject = new SomeClass;
|
|
|
|
$someObject->someMethod(SomeClass::OLD_CONSTANT);
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-05-04 12:55:47 +00:00
|
|
|
$someObject = new SomeClass;
|
2021-07-28 12:55:48 +00:00
|
|
|
$someObject->someMethod(false);
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2022-06-07 08:22:29 +00:00
|
|
|
, [new ReplaceArgumentDefaultValue('SomeClass', 'someMethod', 0, 'SomeClass::OLD_CONSTANT', \false)])]);
|
2018-05-01 17:31:41 +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
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [MethodCall::class, StaticCall::class, ClassMethod::class];
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param MethodCall|StaticCall|ClassMethod $node
|
2022-08-11 14:46:08 +00:00
|
|
|
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Stmt\ClassMethod|null
|
2018-05-01 17:31:41 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node)
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2021-12-28 14:03:15 +00:00
|
|
|
$hasChanged = \false;
|
2020-08-25 22:21:41 +00:00
|
|
|
foreach ($this->replacedArguments as $replacedArgument) {
|
2023-03-22 14:20:22 +00:00
|
|
|
if (!$this->isName($node->name, $replacedArgument->getMethod())) {
|
2018-10-21 19:43:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-03-22 14:20:22 +00:00
|
|
|
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($node, $replacedArgument->getObjectType())) {
|
2020-08-25 22:21:41 +00:00
|
|
|
continue;
|
2018-10-21 19:43:47 +00:00
|
|
|
}
|
2021-12-28 14:03:15 +00:00
|
|
|
$replacedNode = $this->argumentDefaultValueReplacer->processReplaces($node, $replacedArgument);
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($replacedNode instanceof Node) {
|
2021-12-28 14:03:15 +00:00
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
2018-10-21 19:43:47 +00:00
|
|
|
}
|
2021-12-28 14:03:15 +00:00
|
|
|
return null;
|
2018-05-01 17:31:41 +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, ReplaceArgumentDefaultValue::class);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->replacedArguments = $configuration;
|
2020-07-29 23:39:41 +00:00
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|