-
Notifications
You must be signed in to change notification settings - Fork 74
/
DrupalServiceRenameRector.php
82 lines (70 loc) · 2.61 KB
/
DrupalServiceRenameRector.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
<?php
declare(strict_types=1);
namespace DrupalRector\Drupal8\Rector\Deprecation;
use DrupalRector\Drupal8\Rector\ValueObject\DrupalServiceRenameConfiguration;
use PhpParser\Node;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
class DrupalServiceRenameRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var \DrupalRector\Drupal8\Rector\ValueObject\DrupalServiceRenameConfiguration[]
*/
protected array $staticArgumentRenameConfigs = [];
public function configure(array $configuration): void
{
foreach ($configuration as $value) {
if (!($value instanceof DrupalServiceRenameConfiguration)) {
throw new \InvalidArgumentException(sprintf('Each configuration item must be an instance of "%s"', DrupalServiceRenameConfiguration::class));
}
}
$this->staticArgumentRenameConfigs = $configuration;
}
public function getNodeTypes(): array
{
return [
Node\Expr\StaticCall::class,
];
}
public function refactor(Node $node)
{
if ($node instanceof Node\Expr\StaticCall) {
foreach ($this->staticArgumentRenameConfigs as $configuration) {
if ($this->getName($node->name) === 'service' && (string) $node->class === 'Drupal') {
if (count($node->args) === 1) {
/* @var Node\Arg $argument */
$argument = $node->args[0];
if ($argument->value instanceof Node\Scalar\String_ && $argument->value->value === $configuration->getDeprecatedService()) {
$node->args[0] = new Node\Arg(new Node\Scalar\String_($configuration->getNewService()));
return $node;
}
}
}
}
}
return null;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Renames the IDs in Drupal::service() calls', [
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
\Drupal::service('old')->foo();
CODE_BEFORE
,
<<<'CODE_AFTER'
\Drupal::service('bar')->foo();
CODE_AFTER
,
[
new DrupalServiceRenameConfiguration(
'old',
'bar',
),
]
),
]);
}
}