|
11 | 11 | use PhpParser\Node\Stmt\Class_;
|
12 | 12 | use PhpParser\Node\Stmt\ClassMethod;
|
13 | 13 | use PhpParser\Node\Stmt\Expression;
|
| 14 | +use PhpParser\Node\Stmt\TraitUse; |
14 | 15 | use Rector\Contract\Rector\ConfigurableRectorInterface;
|
15 | 16 | use Rector\Exception\ShouldNotHappenException;
|
16 | 17 | use Rector\NodeManipulator\ClassInsertManipulator;
|
@@ -154,15 +155,36 @@ private function getOrCreateConstructorMethod(Class_ $node): ClassMethod
|
154 | 155 | {
|
155 | 156 | $constructor = $node->getMethod(MethodName::CONSTRUCT);
|
156 | 157 |
|
157 |
| - if (null === $constructor) { |
158 |
| - $constructor = $this->constructorClassMethodFactory->createConstructorClassMethod([], []); |
159 |
| - $constructor->stmts = [new Expression($this->syliusNodeFactory->createParentConstructWithParams([]))]; |
160 |
| - $this->classInsertManipulator->addAsFirstMethod($node, $constructor); |
| 158 | + if ($constructor !== null) { |
| 159 | + return $constructor; |
161 | 160 | }
|
162 | 161 |
|
| 162 | + $constructor = $this->constructorClassMethodFactory->createConstructorClassMethod([], []); |
| 163 | + $constructor->stmts = [ |
| 164 | + new Expression($this->syliusNodeFactory->createParentConstructWithParams([])) |
| 165 | + ]; |
| 166 | + |
| 167 | + $this->insertConstructorAfterTraits($node, $constructor); |
| 168 | + |
163 | 169 | return $constructor;
|
164 | 170 | }
|
165 | 171 |
|
| 172 | + private function insertConstructorAfterTraits(Class_ $node, ClassMethod $constructor): void |
| 173 | + { |
| 174 | + $traitUses = []; |
| 175 | + $others = []; |
| 176 | + |
| 177 | + foreach ($node->stmts as $stmt) { |
| 178 | + if ($stmt instanceof TraitUse) { |
| 179 | + $traitUses[] = $stmt; |
| 180 | + } else { |
| 181 | + $others[] = $stmt; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + $node->stmts = array_merge($traitUses, [$constructor], $others); |
| 186 | + } |
| 187 | + |
166 | 188 | private function isConstructMethodCallAlreadyExisting(ClassMethod $constructor, MethodCall $newMethodCall): bool
|
167 | 189 | {
|
168 | 190 | foreach ($constructor->stmts as $stmt) {
|
|
0 commit comments