rector/rules/Removing/Rector/FuncCall/RemoveFuncCallArgRector.php
Tomas Votruba e9e85a1300 Updated Rector to commit a8b01f217b
a8b01f217b [automated] Re-Generate Nodes/Rectors Documentation (#619)
2021-08-08 00:26:40 +00:00

76 lines
2.8 KiB
PHP

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