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;
|
2023-07-20 15:30:52 +00:00
|
|
|
use PhpParser\Node\Expr\New_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use Rector\Arguments\ArgumentDefaultValueReplacer;
|
|
|
|
use Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\MethodName;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2024-03-01 20:02:28 +00:00
|
|
|
use RectorPrefix202403\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
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-17 23:17:47 +00:00
|
|
|
* @var \Rector\Arguments\ArgumentDefaultValueReplacer
|
|
|
|
*/
|
|
|
|
private $argumentDefaultValueReplacer;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var ReplaceArgumentDefaultValue[]
|
|
|
|
*/
|
2023-07-20 15:30:52 +00:00
|
|
|
private $replaceArgumentDefaultValues = [];
|
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
|
|
|
{
|
2023-07-20 15:30:52 +00:00
|
|
|
return [MethodCall::class, StaticCall::class, ClassMethod::class, New_::class];
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-07-20 15:30:52 +00:00
|
|
|
* @param MethodCall|StaticCall|ClassMethod|New_ $node
|
|
|
|
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\New_|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;
|
2023-07-20 15:30:52 +00:00
|
|
|
if ($node instanceof New_) {
|
|
|
|
return $this->refactorNew($node);
|
|
|
|
}
|
2023-09-09 18:18:18 +00:00
|
|
|
$nodeName = $this->getName($node->name);
|
|
|
|
if ($nodeName === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-07-20 15:30:52 +00:00
|
|
|
foreach ($this->replaceArgumentDefaultValues as $replaceArgumentDefaultValue) {
|
2023-09-09 18:18:18 +00:00
|
|
|
if (!$this->nodeNameResolver->isStringName($nodeName, $replaceArgumentDefaultValue->getMethod())) {
|
2018-10-21 19:43:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-07-20 15:30:52 +00:00
|
|
|
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($node, $replaceArgumentDefaultValue->getObjectType())) {
|
2020-08-25 22:21:41 +00:00
|
|
|
continue;
|
2018-10-21 19:43:47 +00:00
|
|
|
}
|
2023-07-20 15:30:52 +00:00
|
|
|
$replacedNode = $this->argumentDefaultValueReplacer->processReplaces($node, $replaceArgumentDefaultValue);
|
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);
|
2023-07-20 15:30:52 +00:00
|
|
|
$this->replaceArgumentDefaultValues = $configuration;
|
|
|
|
}
|
|
|
|
private function refactorNew(New_ $new) : ?New_
|
|
|
|
{
|
|
|
|
foreach ($this->replaceArgumentDefaultValues as $replaceArgumentDefaultValue) {
|
|
|
|
if ($replaceArgumentDefaultValue->getMethod() !== MethodName::CONSTRUCT) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$this->isObjectType($new, $replaceArgumentDefaultValue->getObjectType())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return $this->argumentDefaultValueReplacer->processReplaces($new, $replaceArgumentDefaultValue);
|
|
|
|
}
|
|
|
|
return null;
|
2020-07-29 23:39:41 +00:00
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|