-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAddArgumentToClassWithoutDefaultRector.php
More file actions
116 lines (99 loc) · 3.51 KB
/
AddArgumentToClassWithoutDefaultRector.php
File metadata and controls
116 lines (99 loc) · 3.51 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
declare(strict_types=1);
namespace Frosh\Rector\Rule\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectType;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
class AddArgumentToClassWithoutDefaultRector extends AbstractRector implements ConfigurableRectorInterface
{
public function __construct(private readonly StaticTypeMapper $staticTypeMapper)
{
}
/**
* @var AddArgumentToClassWithoutDefault[]
*/
protected array $configuration = [];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'This Rector adds new default arguments in calls of defined methods and class types.',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
$someObject = new SomeExampleClass;
$someObject->someMethod();
class MyCustomClass extends SomeExampleClass
{
public function someMethod()
{
}
}
CODE_SAMPLE,
<<<'CODE_SAMPLE'
$someObject = new SomeExampleClass;
$someObject->someMethod(true);
class MyCustomClass extends SomeExampleClass
{
public function someMethod($value)
{
}
}
CODE_SAMPLE,
[
new AddArgumentToClassWithoutDefault('SomeExampleClass', 'someMethod', 0, 'someArgument', new ObjectType('SomeType')), ],
),
],
);
}
public function getNodeTypes(): array
{
return [
Class_::class,
];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
$hasChanged = false;
foreach ($node->stmts as $method) {
if ($method instanceof ClassMethod) {
foreach ($this->configuration as $config) {
if (!$this->isObjectType($node, $config->getObjectType())) {
continue;
}
if (!$this->isName($method->name, $config->getMethod())) {
continue;
}
$param = new Param(new Variable($config->getName()));
if ($config->getType()) {
$param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($config->getType(), TypeKind::PARAM);
}
$method->params[$config->getPosition()] = $param;
$hasChanged = true;
}
}
}
if ($hasChanged) {
return $node;
}
return null;
}
/**
* @param AddArgumentToClassWithoutDefault[] $configuration
*/
public function configure(array $configuration): void
{
$this->configuration = $configuration;
}
}