-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathFunctionToFirstArgMethodRector.php
88 lines (76 loc) · 2.47 KB
/
FunctionToFirstArgMethodRector.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
namespace DrupalRector\Drupal9\Rector\Deprecation;
use DrupalRector\Drupal9\Rector\ValueObject\FunctionToFirstArgMethodConfiguration;
use PhpParser\Node;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class FunctionToFirstArgMethodRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var FunctionToFirstArgMethodConfiguration[]
*/
private array $configuration;
/**
* {@inheritdoc}
*/
public function getNodeTypes(): array
{
return [
Node\Expr\FuncCall::class,
];
}
public function configure(array $configuration): void
{
foreach ($configuration as $value) {
if (!($value instanceof FunctionToFirstArgMethodConfiguration)) {
throw new \InvalidArgumentException(sprintf('Each configuration item must be an instance of "%s"', FunctionToFirstArgMethodConfiguration::class));
}
}
$this->configuration = $configuration;
}
/**
* {@inheritdoc}
*/
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\FuncCall);
foreach ($this->configuration as $configuration) {
if ($this->getName($node) !== $configuration->getDeprecatedFunctionName()) {
continue;
}
$args = $node->getArgs();
if (count($args) !== 1) {
continue;
}
return $this->nodeFactory->createMethodCall($args[0]->value, $configuration->getMethodName());
}
return null;
}
/**
* {@inheritdoc}
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated taxonomy_implode_tags() calls', [
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$url = taxonomy_term_uri($term);
$name = taxonomy_term_title($term);
CODE_BEFORE
,
<<<'CODE_AFTER'
$url = $term->toUrl();
$name = $term->label();
CODE_AFTER
,
[
new FunctionToFirstArgMethodConfiguration('taxonomy_term_uri', 'toUrl'),
new FunctionToFirstArgMethodConfiguration('taxonomy_term_title', 'label'),
]
),
]);
}
}