2018-05-01 17:31:41 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
2018-08-01 18:29:40 +00:00
|
|
|
namespace Rector\Rector\Argument;
|
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;
|
2018-10-21 19:43:47 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2018-08-01 19:52:44 +00:00
|
|
|
use Rector\RectorDefinition\ConfiguredCodeSample;
|
2018-05-01 17:31:41 +00:00
|
|
|
use Rector\RectorDefinition\RectorDefinition;
|
|
|
|
|
2018-10-21 19:43:47 +00:00
|
|
|
final class ArgumentRemoverRector extends AbstractRector
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
|
|
|
/**
|
2018-10-21 19:43:47 +00:00
|
|
|
* @var mixed[]
|
2018-05-01 17:31:41 +00:00
|
|
|
*/
|
2018-10-21 19:43:47 +00:00
|
|
|
private $positionsByMethodNameByClassType = [];
|
2018-05-01 17:31:41 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-21 19:43:47 +00:00
|
|
|
* @param mixed[] $positionsByMethodNameByClassType
|
2018-05-01 17:31:41 +00:00
|
|
|
*/
|
2019-04-26 13:32:24 +00:00
|
|
|
public function __construct(array $positionsByMethodNameByClassType = [])
|
2018-07-18 12:56:04 +00:00
|
|
|
{
|
2018-10-21 19:43:47 +00:00
|
|
|
$this->positionsByMethodNameByClassType = $positionsByMethodNameByClassType;
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition(
|
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(
|
2018-05-04 12:56:35 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
$someObject = new SomeClass;
|
|
|
|
$someObject->someMethod(true);
|
|
|
|
CODE_SAMPLE
|
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
$someObject = new SomeClass;
|
|
|
|
$someObject->someMethod();'
|
|
|
|
CODE_SAMPLE
|
2018-08-01 19:52:44 +00:00
|
|
|
,
|
|
|
|
[
|
2018-10-21 19:43:47 +00:00
|
|
|
'ExampleClass' => [
|
|
|
|
'someMethod' => [
|
|
|
|
0 => [
|
|
|
|
'value' => '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
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
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
|
|
|
{
|
2018-10-21 19:43:47 +00:00
|
|
|
foreach ($this->positionsByMethodNameByClassType as $type => $positionByMethodName) {
|
|
|
|
if (! $this->isType($node, $type)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2018-10-21 19:43:47 +00:00
|
|
|
foreach ($positionByMethodName as $methodName => $positions) {
|
|
|
|
if (! $this->isName($node, $methodName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($positions as $position => $match) {
|
|
|
|
$this->processPosition($node, $position, $match);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-21 19:43:47 +00:00
|
|
|
* @param ClassMethod|StaticCall|MethodCall $node
|
|
|
|
* @param mixed[]|null $match
|
2018-05-01 17:31:41 +00:00
|
|
|
*/
|
2018-10-21 19:43:47 +00:00
|
|
|
private function processPosition(Node $node, int $position, ?array $match): void
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2018-10-21 19:43:47 +00:00
|
|
|
if ($match === null) {
|
|
|
|
if ($node instanceof MethodCall || $node instanceof StaticCall) {
|
|
|
|
unset($node->args[$position]);
|
2018-12-16 23:00:16 +00:00
|
|
|
} else {
|
2018-10-21 19:43:47 +00:00
|
|
|
unset($node->params[$position]);
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 19:43:47 +00:00
|
|
|
if ($match) {
|
2019-03-08 23:53:12 +00:00
|
|
|
if (isset($match['name'])) {
|
|
|
|
$this->removeByName($node, $position, $match['name']);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-21 19:43:47 +00:00
|
|
|
// only argument specific value can be removed
|
|
|
|
if ($node instanceof ClassMethod || ! isset($node->args[$position])) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-17 20:48:31 +00:00
|
|
|
|
2018-10-21 19:43:47 +00:00
|
|
|
if ($this->isArgumentValueMatch($node->args[$position], $match)) {
|
|
|
|
unset($node->args[$position]);
|
2018-07-17 20:48:31 +00:00
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-17 20:48:31 +00:00
|
|
|
|
2018-07-17 20:52:46 +00:00
|
|
|
/**
|
2018-10-21 19:43:47 +00:00
|
|
|
* @param mixed[] $values
|
2018-07-17 20:52:46 +00:00
|
|
|
*/
|
2019-02-22 17:25:31 +00:00
|
|
|
private function isArgumentValueMatch(Arg $arg, array $values): bool
|
2018-07-17 20:48:31 +00:00
|
|
|
{
|
2019-02-22 17:25:31 +00:00
|
|
|
$nodeValue = $this->getValue($arg->value);
|
2018-07-17 20:48:31 +00:00
|
|
|
|
2018-10-22 02:02:53 +00:00
|
|
|
return in_array($nodeValue, $values, true);
|
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)) {
|
|
|
|
unset($node->args[$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
|
|
|
}
|
|
|
|
|
2019-03-08 23:53:54 +00:00
|
|
|
if ($node instanceof ClassMethod) {
|
|
|
|
if (isset($node->params[$position]) && $this->isName($node->params[$position], $name)) {
|
2019-03-08 23:53:12 +00:00
|
|
|
unset($node->params[$position]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|