-
-
Notifications
You must be signed in to change notification settings - Fork 672
/
WhateverRector.php
87 lines (72 loc) · 2.16 KB
/
WhateverRector.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
<?php
declare(strict_types=1);
namespace Utils\Rector\Rector\MethodCall;
use PhpParser\Node;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Utils\Rector\Tests\Rector\MethodCall\WhateverRector\WhateverRectorTest
*/
final class WhateverRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
*/
public const CLASS_TYPE_TO_METHOD_NAME = 'class_type_to_method_name';
/**
* @var mixed[]
*/
private $classTypeToMethodName = [];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change $service->arg(...) to $service->call(...)', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(SomeClass::class)
->arg('$key', 'value');
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(SomeClass::class)
->call('configure', [[
'$key' => 'value'
]]);
}
CODE_SAMPLE
,
[self::CLASS_TYPE_TO_METHOD_NAME => ['SomeClass' => 'configure']]
)
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [\PhpParser\Node\Expr\MethodCall::class];
}
/**
* @param \PhpParser\Node\Expr\MethodCall $node
*/
public function refactor(Node $node): ?Node
{
// change the node
return $node;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
$this->classTypeToMethodName = $configuration[self::CLASS_TYPE_TO_METHOD_NAME] ?? [];
}
}