2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2021-01-29 08:08:55 +00:00
|
|
|
namespace Rector\Removing\Rector\ClassMethod;
|
2018-05-01 17:31:41 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
2018-07-17 20:48:31 +00:00
|
|
|
use PhpParser\Node\Arg;
|
2018-05-01 17:31:41 +00:00
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2020-07-29 23:39:41 +00:00
|
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2021-01-29 10:38:59 +00:00
|
|
|
use Rector\Removing\ValueObject\ArgumentRemover;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-08-25 19:05:58 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Removing\Rector\ClassMethod\ArgumentRemoverRector\ArgumentRemoverRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2020-07-29 23:39:41 +00:00
|
|
|
final class ArgumentRemoverRector extends AbstractRector implements ConfigurableRectorInterface
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
|
|
|
/**
|
2020-07-29 23:39:41 +00:00
|
|
|
* @var string
|
2018-05-01 17:31:41 +00:00
|
|
|
*/
|
2020-08-25 19:05:58 +00:00
|
|
|
public const REMOVED_ARGUMENTS = 'removed_arguments';
|
2018-05-01 17:31:41 +00:00
|
|
|
|
|
|
|
/**
|
2020-09-12 21:19:08 +00:00
|
|
|
* @var ArgumentRemover[]
|
2018-05-01 17:31:41 +00:00
|
|
|
*/
|
2020-08-25 19:05:58 +00:00
|
|
|
private $removedArguments = [];
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
2018-08-01 13:34:55 +00:00
|
|
|
'Removes defined arguments in defined methods and their calls.',
|
2018-05-01 17:31:41 +00:00
|
|
|
[
|
2018-08-01 19:52:44 +00:00
|
|
|
new ConfiguredCodeSample(
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2018-05-04 12:56:35 +00:00
|
|
|
$someObject = new SomeClass;
|
|
|
|
$someObject->someMethod(true);
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-05-04 12:56:35 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2018-05-04 12:56:35 +00:00
|
|
|
$someObject = new SomeClass;
|
|
|
|
$someObject->someMethod();'
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-08-01 19:52:44 +00:00
|
|
|
,
|
|
|
|
[
|
2020-09-12 21:19:08 +00:00
|
|
|
self::REMOVED_ARGUMENTS => [new ArgumentRemover('ExampleClass', 'someMethod', 0, 'true')],
|
2018-08-01 19:52:44 +00:00
|
|
|
]
|
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
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2018-08-14 21:33:39 +00:00
|
|
|
return [MethodCall::class, StaticCall::class, ClassMethod::class];
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param MethodCall|StaticCall|ClassMethod $node
|
|
|
|
*/
|
2018-08-14 22:12:41 +00:00
|
|
|
public function refactor(Node $node): ?Node
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2020-08-25 19:05:58 +00:00
|
|
|
foreach ($this->removedArguments as $removedArgument) {
|
2021-02-08 23:42:47 +00:00
|
|
|
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
|
|
|
|
$node,
|
2021-02-27 02:13:22 +00:00
|
|
|
$removedArgument->getObjectType()
|
2021-02-08 23:42:47 +00:00
|
|
|
)) {
|
2018-10-21 19:43:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2020-08-25 19:05:58 +00:00
|
|
|
if (! $this->isName($node->name, $removedArgument->getMethod())) {
|
|
|
|
continue;
|
2018-10-21 19:43:47 +00:00
|
|
|
}
|
2020-08-25 19:05:58 +00:00
|
|
|
|
|
|
|
$this->processPosition($node, $removedArgument);
|
2018-10-21 19:43:47 +00:00
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:39:41 +00:00
|
|
|
public function configure(array $configuration): void
|
|
|
|
{
|
2020-08-25 19:05:58 +00:00
|
|
|
$removedArguments = $configuration[self::REMOVED_ARGUMENTS] ?? [];
|
2020-09-12 21:19:08 +00:00
|
|
|
Assert::allIsInstanceOf($removedArguments, ArgumentRemover::class);
|
2020-08-25 19:05:58 +00:00
|
|
|
$this->removedArguments = $removedArguments;
|
2020-07-29 23:39:41 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 17:31:41 +00:00
|
|
|
/**
|
2018-10-21 19:43:47 +00:00
|
|
|
* @param ClassMethod|StaticCall|MethodCall $node
|
2018-05-01 17:31:41 +00:00
|
|
|
*/
|
2020-09-12 21:19:08 +00:00
|
|
|
private function processPosition(Node $node, ArgumentRemover $argumentRemover): void
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2020-09-12 21:19:08 +00:00
|
|
|
if ($argumentRemover->getValue() === null) {
|
2018-10-21 19:43:47 +00:00
|
|
|
if ($node instanceof MethodCall || $node instanceof StaticCall) {
|
2021-02-09 03:55:21 +00:00
|
|
|
$this->nodeRemover->removeArg($node, $argumentRemover->getPosition());
|
2018-12-16 23:00:16 +00:00
|
|
|
} else {
|
2021-02-09 03:55:21 +00:00
|
|
|
$this->nodeRemover->removeParam($node, $argumentRemover->getPosition());
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
2020-08-25 19:05:58 +00:00
|
|
|
|
|
|
|
return;
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 21:19:08 +00:00
|
|
|
$match = $argumentRemover->getValue();
|
2020-08-25 19:05:58 +00:00
|
|
|
if (isset($match['name'])) {
|
2020-09-12 21:19:08 +00:00
|
|
|
$this->removeByName($node, $argumentRemover->getPosition(), $match['name']);
|
2020-08-25 19:05:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-02-09 03:55:21 +00:00
|
|
|
|
2020-08-25 19:05:58 +00:00
|
|
|
// only argument specific value can be removed
|
2020-12-24 16:28:56 +00:00
|
|
|
if ($node instanceof ClassMethod) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-09 03:55:21 +00:00
|
|
|
|
2020-12-24 16:28:56 +00:00
|
|
|
if (! isset($node->args[$argumentRemover->getPosition()])) {
|
2020-08-25 19:05:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-17 20:48:31 +00:00
|
|
|
|
2020-09-12 21:19:08 +00:00
|
|
|
if ($this->isArgumentValueMatch($node->args[$argumentRemover->getPosition()], $match)) {
|
2021-02-09 03:55:21 +00:00
|
|
|
$this->nodeRemover->removeArg($node, $argumentRemover->getPosition());
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-17 20:48:31 +00:00
|
|
|
|
2019-03-08 23:53:12 +00:00
|
|
|
/**
|
|
|
|
* @param ClassMethod|StaticCall|MethodCall $node
|
|
|
|
*/
|
|
|
|
private function removeByName(Node $node, int $position, string $name): void
|
|
|
|
{
|
|
|
|
if ($node instanceof MethodCall || $node instanceof StaticCall) {
|
2019-03-08 23:53:54 +00:00
|
|
|
if (isset($node->args[$position]) && $this->isName($node->args[$position], $name)) {
|
2021-02-09 01:52:35 +00:00
|
|
|
$this->nodeRemover->removeArg($node, $position);
|
2019-03-08 23:53:12 +00:00
|
|
|
}
|
2019-03-08 23:53:54 +00:00
|
|
|
|
|
|
|
return;
|
2019-03-08 23:53:12 +00:00
|
|
|
}
|
2021-02-09 01:52:35 +00:00
|
|
|
|
2021-02-08 23:42:47 +00:00
|
|
|
if (! (isset($node->params[$position]) && $this->isName($node->params[$position], $name))) {
|
2019-03-08 23:53:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-02-09 01:52:35 +00:00
|
|
|
|
|
|
|
$this->nodeRemover->removeParam($node, $position);
|
2019-03-08 23:53:12 +00:00
|
|
|
}
|
2019-10-30 09:49:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed[] $values
|
|
|
|
*/
|
|
|
|
private function isArgumentValueMatch(Arg $arg, array $values): bool
|
|
|
|
{
|
2021-01-30 23:20:05 +00:00
|
|
|
$nodeValue = $this->valueResolver->getValue($arg->value);
|
2019-10-30 09:49:07 +00:00
|
|
|
return in_array($nodeValue, $values, true);
|
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|