forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallUserMethodRector.php
74 lines (60 loc) · 1.92 KB
/
CallUserMethodRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
namespace Rector\Php70\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Php70\Rector\FuncCall\CallUserMethodRector\CallUserMethodRectorTest
*/
final class CallUserMethodRector extends AbstractRector
{
/**
* @var array<string, string>
*/
private const OLD_TO_NEW_FUNCTIONS = [
'call_user_method' => 'call_user_func',
'call_user_method_array' => 'call_user_func_array',
];
public function getRuleDefinition(): RuleDefinition
{
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");'
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
$oldFunctionNames = array_keys(self::OLD_TO_NEW_FUNCTIONS);
if (! $this->isNames($node, $oldFunctionNames)) {
return null;
}
$newName = self::OLD_TO_NEW_FUNCTIONS[$this->getName($node)];
$node->name = new Name($newName);
$oldArgs = $node->args;
unset($node->args[1]);
$newArgs = [$this->nodeFactory->createArg([$oldArgs[1]->value, $oldArgs[0]->value])];
unset($oldArgs[0]);
unset($oldArgs[1]);
$node->args = $this->appendArgs($newArgs, $oldArgs);
return $node;
}
}