forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewToMethodCallRector.php
156 lines (133 loc) · 4.34 KB
/
NewToMethodCallRector.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
namespace Rector\Transform\Rector\New_;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Type\ObjectType;
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Transform\ValueObject\NewToMethodCall;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Transform\Rector\New_\NewToMethodCallRector\NewToMethodCallRectorTest
*/
final class NewToMethodCallRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
*/
public const NEWS_TO_METHOD_CALLS = 'news_to_method_calls';
/**
* @var NewToMethodCall[]
*/
private $newsToMethodCalls = [];
/**
* @var ClassNaming
*/
private $classNaming;
public function __construct(ClassNaming $classNaming)
{
$this->classNaming = $classNaming;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replaces creating object instances with "new" keyword with factory method.', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function example() {
new MyClass($argument);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var \MyClassFactory
*/
private $myClassFactory;
public function example() {
$this->myClassFactory->create($argument);
}
}
CODE_SAMPLE
,
[
self::NEWS_TO_METHOD_CALLS => [new NewToMethodCall('MyClass', 'MyClassFactory', 'create')],
]
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [New_::class];
}
/**
* @param New_ $node
*/
public function refactor(Node $node): ?Node
{
foreach ($this->newsToMethodCalls as $newsToMethodCall) {
if (! $this->isObjectType($node, $newsToMethodCall->getNewObjectType())) {
continue;
}
$serviceObjectType = $newsToMethodCall->getServiceObjectType();
$className = $node->getAttribute(AttributeKey::CLASS_NAME);
if ($className === $serviceObjectType->getClassName()) {
continue;
}
/** @var Class_ $classNode */
$classNode = $node->getAttribute(AttributeKey::CLASS_NODE);
$propertyName = $this->getExistingFactoryPropertyName(
$classNode,
$newsToMethodCall->getServiceObjectType()
);
if ($propertyName === null) {
$serviceObjectType = $newsToMethodCall->getServiceObjectType();
$propertyName = $this->classNaming->getShortName($serviceObjectType->getClassName());
$propertyName = lcfirst($propertyName);
$this->addConstructorDependencyToClass(
$classNode,
$newsToMethodCall->getServiceObjectType(),
$propertyName
);
}
$propertyFetch = new PropertyFetch(new Variable('this'), $propertyName);
return new MethodCall($propertyFetch, $newsToMethodCall->getServiceMethod(), $node->args);
}
return $node;
}
/**
* @param array<string, NewToMethodCall[]> $configuration
*/
public function configure(array $configuration): void
{
$newsToMethodCalls = $configuration[self::NEWS_TO_METHOD_CALLS] ?? [];
Assert::allIsInstanceOf($newsToMethodCalls, NewToMethodCall::class);
$this->newsToMethodCalls = $newsToMethodCalls;
}
private function getExistingFactoryPropertyName(Class_ $class, ObjectType $factoryObjectType): ?string
{
foreach ($class->getProperties() as $property) {
if (! $this->isObjectType($property, $factoryObjectType)) {
continue;
}
return $this->getName($property);
}
return null;
}
}