rector/rules/Removing/Rector/FuncCall/RemoveFuncCallArgRector.php

76 lines
2.8 KiB
PHP
Raw Normal View History

2020-05-31 14:19:58 +00:00
<?php
declare (strict_types=1);
namespace Rector\Removing\Rector\FuncCall;
2020-05-31 14:19:58 +00:00
use PhpParser\Node;
use PhpParser\Node\Expr;
2020-05-31 14:19:58 +00:00
use PhpParser\Node\Expr\FuncCall;
2020-07-29 23:39:41 +00:00
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
2020-05-31 14:19:58 +00:00
use Rector\Core\Rector\AbstractRector;
use Rector\Removing\ValueObject\RemoveFuncCallArg;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix20211128\Webmozart\Assert\Assert;
2020-05-31 14:19:58 +00:00
/**
* @see \Rector\Tests\Removing\Rector\FuncCall\RemoveFuncCallArgRector\RemoveFuncCallArgRectorTest
2020-05-31 14:19:58 +00:00
*/
final class RemoveFuncCallArgRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
2020-05-31 14:19:58 +00:00
{
/**
2020-07-29 23:39:41 +00:00
* @var string
2020-05-31 14:19:58 +00:00
*/
public const REMOVED_FUNCTION_ARGUMENTS = 'removed_function_arguments';
2020-05-31 14:19:58 +00:00
/**
* @var RemoveFuncCallArg[]
2020-05-31 14:19:58 +00:00
*/
private $removedFunctionArguments = [];
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2020-05-31 14:19:58 +00:00
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove argument by position by function name', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
2020-05-31 14:19:58 +00:00
remove_last_arg(1, 2);
CODE_SAMPLE
, <<<'CODE_SAMPLE'
2020-05-31 14:19:58 +00:00
remove_last_arg(1);
CODE_SAMPLE
, [self::REMOVED_FUNCTION_ARGUMENTS => [new \Rector\Removing\ValueObject\RemoveFuncCallArg('remove_last_arg', 1)]])]);
2020-05-31 14:19:58 +00:00
}
/**
* @return array<class-string<Node>>
2020-05-31 14:19:58 +00:00
*/
public function getNodeTypes() : array
2020-05-31 14:19:58 +00:00
{
return [\PhpParser\Node\Expr\FuncCall::class];
2020-05-31 14:19:58 +00:00
}
/**
* @param FuncCall $node
2020-05-31 14:19:58 +00:00
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
2020-05-31 14:19:58 +00:00
{
if ($node->name instanceof \PhpParser\Node\Expr) {
return null;
}
foreach ($this->removedFunctionArguments as $removedFunctionArgument) {
if (!$this->isName($node->name, $removedFunctionArgument->getFunction())) {
2020-05-31 14:19:58 +00:00
continue;
}
foreach (\array_keys($node->args) as $position) {
if ($removedFunctionArgument->getArgumentPosition() !== $position) {
2020-05-31 14:19:58 +00:00
continue;
}
$this->nodeRemover->removeArg($node, $position);
2020-05-31 14:19:58 +00:00
}
}
return $node;
}
/**
* @param array<string, RemoveFuncCallArg[]> $configuration
*/
public function configure(array $configuration) : void
2020-07-29 23:39:41 +00:00
{
$removedFunctionArguments = $configuration[self::REMOVED_FUNCTION_ARGUMENTS] ?? ($configuration ?: []);
\RectorPrefix20211128\Webmozart\Assert\Assert::allIsInstanceOf($removedFunctionArguments, \Rector\Removing\ValueObject\RemoveFuncCallArg::class);
$this->removedFunctionArguments = $removedFunctionArguments;
2020-07-29 23:39:41 +00:00
}
2020-05-31 14:19:58 +00:00
}