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;
|
2021-05-03 19:03:16 +00:00
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Array_;
|
2019-09-25 21:08:32 +00:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2021-05-03 19:03:16 +00:00
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
2021-05-04 14:59:15 +00:00
|
|
|
use Rector\CodingStyle\NodeFactory\ArrayCallableToMethodCallFactory;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2021-05-03 21:27:20 +00:00
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
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
|
|
|
|
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://www.php.net/manual/en/function.call-user-func-array.php#117655
|
2021-05-03 19:03:16 +00:00
|
|
|
* @changelog https://3v4l.org/CBWt9
|
2019-09-25 21:08:32 +00:00
|
|
|
*
|
2021-05-04 14:59:15 +00:00
|
|
|
* @see \Rector\Tests\CodingStyle\Rector\FuncCall\CallUserFuncArrayToVariadicRector\CallUserFuncArrayToVariadicRectorTest
|
2019-09-25 21:08:32 +00:00
|
|
|
*/
|
2021-05-04 14:59:15 +00:00
|
|
|
final class CallUserFuncArrayToVariadicRector extends AbstractRector
|
2019-09-25 21:08:32 +00:00
|
|
|
{
|
2021-05-04 14:59:15 +00:00
|
|
|
/**
|
|
|
|
* @var ArrayCallableToMethodCallFactory
|
|
|
|
*/
|
|
|
|
private $arrayCallableToMethodCallFactory;
|
|
|
|
|
|
|
|
public function __construct(ArrayCallableToMethodCallFactory $arrayCallableToMethodCallFactory)
|
|
|
|
{
|
|
|
|
$this->arrayCallableToMethodCallFactory = $arrayCallableToMethodCallFactory;
|
|
|
|
}
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2019-09-25 21:08:32 +00:00
|
|
|
{
|
2021-05-04 14:59:15 +00:00
|
|
|
return new RuleDefinition('Replace call_user_func_array() 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
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-09-25 21:08:32 +00:00
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [FuncCall::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2021-05-03 21:27:20 +00:00
|
|
|
if (! $this->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_SPREAD)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-09-25 21:08:32 +00:00
|
|
|
if (! $this->isName($node, 'call_user_func_array')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:03:16 +00:00
|
|
|
$firstArgValue = $node->args[0]->value;
|
|
|
|
$secondArgValue = $node->args[1]->value;
|
|
|
|
|
|
|
|
if ($firstArgValue instanceof String_) {
|
|
|
|
$functionName = $this->valueResolver->getValue($firstArgValue);
|
|
|
|
return $this->createFuncCall($secondArgValue, $functionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// method call
|
|
|
|
if ($firstArgValue instanceof Array_) {
|
|
|
|
return $this->createMethodCall($firstArgValue, $secondArgValue);
|
2019-12-15 19:41:41 +00:00
|
|
|
}
|
2019-09-25 21:08:32 +00:00
|
|
|
|
2021-05-03 19:03:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createFuncCall(Expr $expr, string $functionName): FuncCall
|
|
|
|
{
|
2019-09-25 21:08:32 +00:00
|
|
|
$args = [];
|
2021-05-03 21:27:20 +00:00
|
|
|
$args[] = $this->createUnpackedArg($expr);
|
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
|
|
|
}
|
2021-05-03 19:03:16 +00:00
|
|
|
|
|
|
|
private function createMethodCall(Array_ $array, Expr $secondExpr): ?MethodCall
|
|
|
|
{
|
2021-05-04 14:59:15 +00:00
|
|
|
$methodCall = $this->arrayCallableToMethodCallFactory->create($array);
|
|
|
|
if (! $methodCall instanceof MethodCall) {
|
2021-05-03 19:03:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-04 14:59:15 +00:00
|
|
|
$methodCall->args[] = $this->createUnpackedArg($secondExpr);
|
|
|
|
return $methodCall;
|
2021-05-03 19:03:16 +00:00
|
|
|
}
|
2021-05-03 21:27:20 +00:00
|
|
|
|
|
|
|
private function createUnpackedArg(Expr $expr): Arg
|
|
|
|
{
|
|
|
|
return new Arg($expr, false, true);
|
|
|
|
}
|
2019-09-25 21:08:32 +00:00
|
|
|
}
|