2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-09-25 21:08:32 +00:00
|
|
|
|
|
|
|
namespace Rector\CodingStyle\Rector\FuncCall;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-25 21:08:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://www.php.net/manual/en/function.call-user-func-array.php#117655
|
|
|
|
*
|
|
|
|
* @see \Rector\CodingStyle\Tests\Rector\FuncCall\CallUserFuncCallToVariadicRector\CallUserFuncCallToVariadicRectorTest
|
|
|
|
*/
|
|
|
|
final class CallUserFuncCallToVariadicRector extends AbstractRector
|
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2019-09-25 21:08:32 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition('Replace call_user_func_call with variadic', [
|
2019-09-25 21:08:32 +00:00
|
|
|
new CodeSample(
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2019-09-25 21:08:32 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
call_user_func_array('some_function', $items);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2019-09-25 21:08:32 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2019-09-25 21:08:32 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
some_function(...$items);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2019-09-25 21:08:32 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [FuncCall::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if (! $this->isName($node, 'call_user_func_array')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-30 23:20:05 +00:00
|
|
|
$functionName = $this->valueResolver->getValue($node->args[0]->value);
|
2019-12-15 19:41:41 +00:00
|
|
|
if (! is_string($functionName)) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-09-25 21:08:32 +00:00
|
|
|
|
|
|
|
$args = [];
|
|
|
|
$args[] = new Arg($node->args[1]->value, false, true);
|
2019-12-15 19:41:41 +00:00
|
|
|
|
2021-01-30 21:41:25 +00:00
|
|
|
return $this->nodeFactory->createFuncCall($functionName, $args);
|
2019-09-25 21:08:32 +00:00
|
|
|
}
|
|
|
|
}
|