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\Php70\Rector\FuncCall;
|
2018-10-07 13:38:35 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2022-10-05 09:16:10 +00:00
|
|
|
use PhpParser\Node\Arg;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Name;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
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-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php70\Rector\FuncCall\CallUserMethodRector\CallUserMethodRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class CallUserMethodRector extends AbstractRector implements MinPhpVersionInterface
|
2018-10-07 13:38:35 +00:00
|
|
|
{
|
|
|
|
/**
|
2020-09-01 17:56:30 +00:00
|
|
|
* @var array<string, string>
|
2018-10-07 13:38:35 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private const OLD_TO_NEW_FUNCTIONS = ['call_user_method' => 'call_user_func', 'call_user_method_array' => 'call_user_func_array'];
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::NO_CALL_USER_METHOD;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-10-07 13:38:35 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes call_user_method()/call_user_method_array() to call_user_func()/call_user_func_array()', [new CodeSample('call_user_method($method, $obj, "arg1", "arg2");', 'call_user_func(array(&$obj, "method"), "arg1", "arg2");')]);
|
2018-10-07 13:38:35 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-07 13:38:35 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-07 13:38:35 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [FuncCall::class];
|
2018-10-07 13:38:35 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param FuncCall $node
|
2018-10-07 13:38:35 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-10-07 13:38:35 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
$oldFunctionNames = \array_keys(self::OLD_TO_NEW_FUNCTIONS);
|
|
|
|
if (!$this->isNames($node, $oldFunctionNames)) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-07 13:38:35 +00:00
|
|
|
}
|
2022-10-05 09:16:10 +00:00
|
|
|
if ($node->isFirstClassCallable()) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-18 22:09:25 +00:00
|
|
|
$newName = self::OLD_TO_NEW_FUNCTIONS[$this->getName($node)];
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->name = new Name($newName);
|
2022-10-05 09:16:10 +00:00
|
|
|
/** @var Arg[] $oldArgs */
|
2021-09-30 13:38:25 +00:00
|
|
|
$oldArgs = $node->args;
|
2018-10-15 04:36:58 +00:00
|
|
|
unset($node->args[1]);
|
2021-01-30 21:41:25 +00:00
|
|
|
$newArgs = [$this->nodeFactory->createArg([$oldArgs[1]->value, $oldArgs[0]->value])];
|
2020-09-23 20:22:35 +00:00
|
|
|
unset($oldArgs[0]);
|
|
|
|
unset($oldArgs[1]);
|
2023-08-09 12:42:27 +00:00
|
|
|
$node->args = \array_merge($newArgs, $oldArgs);
|
2018-10-15 04:36:58 +00:00
|
|
|
return $node;
|
2018-10-07 13:38:35 +00:00
|
|
|
}
|
|
|
|
}
|