2020-05-31 14:19:58 +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\Removing\Rector\FuncCall;
|
2020-05-31 14:19:58 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Removing\ValueObject\RemoveFuncCallArg;
|
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;
|
2020-05-31 14:19:58 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Removing\Rector\FuncCall\RemoveFuncCallArgRector\RemoveFuncCallArgRectorTest
|
2020-05-31 14:19:58 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveFuncCallArgRector extends AbstractRector implements ConfigurableRectorInterface
|
2020-05-31 14:19:58 +00:00
|
|
|
{
|
|
|
|
/**
|
2020-09-12 21:19:08 +00:00
|
|
|
* @var RemoveFuncCallArg[]
|
2020-05-31 14:19:58 +00:00
|
|
|
*/
|
2020-08-26 10:54:53 +00:00
|
|
|
private $removedFunctionArguments = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-05-31 14:19:58 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove argument by position by function name', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2020-05-31 14:19:58 +00:00
|
|
|
remove_last_arg(1, 2);
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-05-31 14:19:58 +00:00
|
|
|
remove_last_arg(1);
|
|
|
|
CODE_SAMPLE
|
2022-06-07 08:22:29 +00:00
|
|
|
, [new RemoveFuncCallArg('remove_last_arg', 1)])]);
|
2020-05-31 14:19:58 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-05-31 14:19:58 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-05-31 14:19:58 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [FuncCall::class];
|
2020-05-31 14:19:58 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param FuncCall $node
|
2020-05-31 14:19:58 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-05-31 14:19:58 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->name instanceof Expr) {
|
2021-02-17 14:17:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-02-28 06:43:27 +00:00
|
|
|
$hasChanged = \false;
|
2020-08-26 10:54:53 +00:00
|
|
|
foreach ($this->removedFunctionArguments as $removedFunctionArgument) {
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isName($node->name, $removedFunctionArgument->getFunction())) {
|
2020-05-31 14:19:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
foreach (\array_keys($node->args) as $position) {
|
2020-08-26 10:54:53 +00:00
|
|
|
if ($removedFunctionArgument->getArgumentPosition() !== $position) {
|
2020-05-31 14:19:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-06 21:53:15 +00:00
|
|
|
unset($node->args[$position]);
|
2022-02-28 06:43:27 +00:00
|
|
|
$hasChanged = \true;
|
2020-05-31 14:19:58 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-28 06:43:27 +00:00
|
|
|
if (!$hasChanged) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-31 14:19:58 +00:00
|
|
|
return $node;
|
|
|
|
}
|
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, RemoveFuncCallArg::class);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->removedFunctionArguments = $configuration;
|
2020-07-29 23:39:41 +00:00
|
|
|
}
|
2020-05-31 14:19:58 +00:00
|
|
|
}
|