2019-10-13 05:59:52 +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\CodingStyle\Rector\FuncCall;
|
2019-09-25 21:08:32 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Array_;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use Rector\CodingStyle\NodeFactory\ArrayCallableToMethodCallFactory;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +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
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class CallUserFuncArrayToVariadicRector extends AbstractRector implements MinPhpVersionInterface
|
2019-09-25 21:08:32 +00:00
|
|
|
{
|
2021-05-04 14:59:15 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodingStyle\NodeFactory\ArrayCallableToMethodCallFactory
|
2021-05-04 14:59:15 +00:00
|
|
|
*/
|
|
|
|
private $arrayCallableToMethodCallFactory;
|
2023-05-13 17:20:02 +00:00
|
|
|
public function __construct(ArrayCallableToMethodCallFactory $arrayCallableToMethodCallFactory)
|
2021-05-04 14:59:15 +00:00
|
|
|
{
|
|
|
|
$this->arrayCallableToMethodCallFactory = $arrayCallableToMethodCallFactory;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-09-25 21:08:32 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Replace call_user_func_array() with variadic', [new CodeSample(<<<'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
|
2021-05-09 20:15:43 +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
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
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
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-09-25 21:08:32 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [FuncCall::class];
|
2019-09-25 21:08:32 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param FuncCall $node
|
2019-09-25 21:08:32 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-09-25 21:08:32 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isName($node, 'call_user_func_array')) {
|
2019-09-25 21:08:32 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-13 17:20:02 +00:00
|
|
|
if ($node->isFirstClassCallable()) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-13 17:20:02 +00:00
|
|
|
$firstArgValue = $node->getArgs()[0]->value;
|
|
|
|
$secondArgValue = $node->getArgs()[1]->value;
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($firstArgValue instanceof String_) {
|
2021-05-03 19:03:16 +00:00
|
|
|
$functionName = $this->valueResolver->getValue($firstArgValue);
|
|
|
|
return $this->createFuncCall($secondArgValue, $functionName);
|
|
|
|
}
|
|
|
|
// method call
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($firstArgValue instanceof Array_) {
|
2021-05-03 19:03:16 +00:00
|
|
|
return $this->createMethodCall($firstArgValue, $secondArgValue);
|
2019-12-15 19:41:41 +00:00
|
|
|
}
|
2021-05-03 19:03:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-07-21 09:35:57 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::ARRAY_SPREAD;
|
2021-07-21 09:35:57 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function createFuncCall(Expr $expr, string $functionName) : FuncCall
|
2021-05-03 19:03:16 +00:00
|
|
|
{
|
2023-05-13 17:20:02 +00:00
|
|
|
$args = [$this->createUnpackedArg($expr)];
|
2021-01-30 21:41:25 +00:00
|
|
|
return $this->nodeFactory->createFuncCall($functionName, $args);
|
2019-09-25 21:08:32 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function createMethodCall(Array_ $array, Expr $secondExpr) : ?MethodCall
|
2021-05-03 19:03:16 +00:00
|
|
|
{
|
2021-05-04 14:59:15 +00:00
|
|
|
$methodCall = $this->arrayCallableToMethodCallFactory->create($array);
|
2022-06-07 08:22:29 +00:00
|
|
|
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
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function createUnpackedArg(Expr $expr) : Arg
|
2021-05-03 21:27:20 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Arg($expr, \false, \true);
|
2021-05-03 21:27:20 +00:00
|
|
|
}
|
2019-09-25 21:08:32 +00:00
|
|
|
}
|