@@ -16,19 +16,29 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | public function leaveNode(Node $node): void |
| 18 | 18 | { |
| 19 | - if ($node instanceof Node\Stmt\ClassMethod && $node->name->name === '__construct') { |
|
| 20 | - foreach ($node->params as $param) { |
|
| 21 | - if ($param->type instanceof Node\NullableType) { |
|
| 22 | - if ($param->type->type instanceof Node\Identifier) { |
|
| 19 | + if ($node instanceof Node\Stmt\ClassMethod && $node->name->name === '__construct') |
|
| 20 | + { |
|
| 21 | + foreach ($node->params as $param) |
|
| 22 | + { |
|
| 23 | + if ($param->type instanceof Node\NullableType) |
|
| 24 | + { |
|
| 25 | + if ($param->type->type instanceof Node\Identifier) |
|
| 26 | + { |
|
| 23 | 27 | $type = $param->type->type->name; |
| 24 | - } else { |
|
| 28 | + } |
|
| 29 | + else |
|
| 30 | + { |
|
| 25 | 31 | $type = implode('\\', $param->type->type->getParts()); |
| 26 | 32 | } |
| 27 | 33 | |
| 28 | 34 | $type = "?$type"; |
| 29 | - } elseif ($param->type instanceof Node\Name) { |
|
| 35 | + } |
|
| 36 | + elseif ($param->type instanceof Node\Name) |
|
| 37 | + { |
|
| 30 | 38 | $type = implode('\\', $param->type->getParts()); |
| 31 | - } else { |
|
| 39 | + } |
|
| 40 | + else |
|
| 41 | + { |
|
| 32 | 42 | $type = $param->type->name ?? null; |
| 33 | 43 | } |
| 34 | 44 | |
@@ -16,8 +16,10 @@ |
||
| 16 | 16 | |
| 17 | 17 | public function enterNode(Node $node): void |
| 18 | 18 | { |
| 19 | - if ($node instanceof Node\Stmt\Use_) { |
|
| 20 | - foreach ($node->uses as $use) { |
|
| 19 | + if ($node instanceof Node\Stmt\Use_) |
|
| 20 | + { |
|
| 21 | + foreach ($node->uses as $use) |
|
| 22 | + { |
|
| 21 | 23 | $this->imports[] = [ |
| 22 | 24 | 'name' => \implode('\\', $use->name->getParts()), |
| 23 | 25 | 'alias' => $use->alias->name ?? null, |
@@ -15,11 +15,13 @@ |
||
| 15 | 15 | |
| 16 | 16 | public function enterNode(Node $node): ?int |
| 17 | 17 | { |
| 18 | - if ($node instanceof Node\Stmt\Namespace_) { |
|
| 18 | + if ($node instanceof Node\Stmt\Namespace_) |
|
| 19 | + { |
|
| 19 | 20 | $this->namespace = \implode('\\', $node->name->getParts()); |
| 20 | 21 | } |
| 21 | 22 | |
| 22 | - if ($node instanceof Node\Stmt\Class_) { |
|
| 23 | + if ($node instanceof Node\Stmt\Class_) |
|
| 24 | + { |
|
| 23 | 25 | $this->class = $node->name->name; |
| 24 | 26 | |
| 25 | 27 | return NodeTraverser::STOP_TRAVERSAL; |
@@ -17,12 +17,15 @@ discard block |
||
| 17 | 17 | { |
| 18 | 18 | public function leaveNode(Node $node): int|Node|null |
| 19 | 19 | { |
| 20 | - if (!$node instanceof Node\Stmt\TraitUse) { |
|
| 20 | + if (!$node instanceof Node\Stmt\TraitUse) |
|
| 21 | + { |
|
| 21 | 22 | return null; |
| 22 | 23 | } |
| 23 | 24 | |
| 24 | - foreach ($node->traits as $index => $use) { |
|
| 25 | - if ($use instanceof Node\Name) { |
|
| 25 | + foreach ($node->traits as $index => $use) |
|
| 26 | + { |
|
| 27 | + if ($use instanceof Node\Name) |
|
| 28 | + { |
|
| 26 | 29 | $name = $this->trimSlashes(\implode('\\', $use->getParts())); |
| 27 | 30 | if ( |
| 28 | 31 | \in_array($name, [ |
@@ -36,7 +39,8 @@ discard block |
||
| 36 | 39 | } |
| 37 | 40 | |
| 38 | 41 | $node->traits = \array_values($node->traits); |
| 39 | - if (empty($node->traits)) { |
|
| 42 | + if (empty($node->traits)) |
|
| 43 | + { |
|
| 40 | 44 | return NodeTraverser::REMOVE_NODE; |
| 41 | 45 | } |
| 42 | 46 | |
@@ -20,20 +20,26 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | public function __construct( |
| 22 | 22 | private readonly ClassNode $node, |
| 23 | - ) {} |
|
| 23 | + ) { |
|
| 24 | +} |
|
| 24 | 25 | |
| 25 | 26 | public function leaveNode(Node $node): ?Node |
| 26 | 27 | { |
| 27 | - if (!$node instanceof Node\Stmt\Namespace_) { |
|
| 28 | + if (!$node instanceof Node\Stmt\Namespace_) |
|
| 29 | + { |
|
| 28 | 30 | return null; |
| 29 | 31 | } |
| 30 | 32 | |
| 31 | 33 | $imported = []; |
| 32 | - if (!$this->node->hasConstructor && $this->node->constructorParams) { |
|
| 33 | - foreach ($this->node->constructorParams as $param) { |
|
| 34 | - if (!empty($param->type) && $param->type->fullName) { |
|
| 34 | + if (!$this->node->hasConstructor && $this->node->constructorParams) |
|
| 35 | + { |
|
| 36 | + foreach ($this->node->constructorParams as $param) |
|
| 37 | + { |
|
| 38 | + if (!empty($param->type) && $param->type->fullName) |
|
| 39 | + { |
|
| 35 | 40 | $import = [$param->type->fullName, $param->type->alias]; |
| 36 | - if (\in_array($import, $imported, true)) { |
|
| 41 | + if (\in_array($import, $imported, true)) |
|
| 42 | + { |
|
| 37 | 43 | continue; |
| 38 | 44 | } |
| 39 | 45 | |
@@ -43,9 +49,11 @@ discard block |
||
| 43 | 49 | } |
| 44 | 50 | } |
| 45 | 51 | |
| 46 | - foreach ($this->node->dependencies as $dependency) { |
|
| 52 | + foreach ($this->node->dependencies as $dependency) |
|
| 53 | + { |
|
| 47 | 54 | $import = [$dependency->type->fullName, $dependency->type->alias]; |
| 48 | - if (\in_array($import, $imported, true)) { |
|
| 55 | + if (\in_array($import, $imported, true)) |
|
| 56 | + { |
|
| 49 | 57 | continue; |
| 50 | 58 | } |
| 51 | 59 | |
@@ -68,8 +76,10 @@ discard block |
||
| 68 | 76 | |
| 69 | 77 | private function definePlacementID(Node\Stmt\Namespace_ $node): int |
| 70 | 78 | { |
| 71 | - foreach ($node->stmts as $index => $child) { |
|
| 72 | - if ($child instanceof Node\Stmt\Class_) { |
|
| 79 | + foreach ($node->stmts as $index => $child) |
|
| 80 | + { |
|
| 81 | + if ($child instanceof Node\Stmt\Class_) |
|
| 82 | + { |
|
| 73 | 83 | return $index; |
| 74 | 84 | } |
| 75 | 85 | } |
@@ -86,13 +96,17 @@ discard block |
||
| 86 | 96 | { |
| 87 | 97 | $uses = $this->getExistingUseParts($stmts); |
| 88 | 98 | |
| 89 | - foreach ($nodes as $i => $node) { |
|
| 90 | - if (!$node instanceof Node\Stmt\Use_) { |
|
| 99 | + foreach ($nodes as $i => $node) |
|
| 100 | + { |
|
| 101 | + if (!$node instanceof Node\Stmt\Use_) |
|
| 102 | + { |
|
| 91 | 103 | continue; |
| 92 | 104 | } |
| 93 | 105 | |
| 94 | - foreach ($node->uses as $use) { |
|
| 95 | - if (\in_array($use->name->getParts(), $uses, true)) { |
|
| 106 | + foreach ($node->uses as $use) |
|
| 107 | + { |
|
| 108 | + if (\in_array($use->name->getParts(), $uses, true)) |
|
| 109 | + { |
|
| 96 | 110 | unset($nodes[$i]); |
| 97 | 111 | } |
| 98 | 112 | } |
@@ -111,12 +125,15 @@ discard block |
||
| 111 | 125 | private function getExistingUseParts(array $stmts): array |
| 112 | 126 | { |
| 113 | 127 | $uses = []; |
| 114 | - foreach ($stmts as $stmt) { |
|
| 115 | - if (!$stmt instanceof Node\Stmt\Use_) { |
|
| 128 | + foreach ($stmts as $stmt) |
|
| 129 | + { |
|
| 130 | + if (!$stmt instanceof Node\Stmt\Use_) |
|
| 131 | + { |
|
| 116 | 132 | continue; |
| 117 | 133 | } |
| 118 | 134 | |
| 119 | - foreach ($stmt->uses as $use) { |
|
| 135 | + foreach ($stmt->uses as $use) |
|
| 136 | + { |
|
| 120 | 137 | $uses[] = $use->name->getParts(); |
| 121 | 138 | } |
| 122 | 139 | } |
@@ -127,7 +144,8 @@ discard block |
||
| 127 | 144 | private function buildUse(string $type, ?string $alias = null): Node\Stmt\Use_ |
| 128 | 145 | { |
| 129 | 146 | $b = new Use_(new Node\Name($type), Node\Stmt\Use_::TYPE_NORMAL); |
| 130 | - if (!empty($alias)) { |
|
| 147 | + if (!empty($alias)) |
|
| 148 | + { |
|
| 131 | 149 | $b->as($alias); |
| 132 | 150 | } |
| 133 | 151 | |
@@ -17,17 +17,21 @@ discard block |
||
| 17 | 17 | { |
| 18 | 18 | public function __construct( |
| 19 | 19 | private readonly ClassNode $definition, |
| 20 | - ) {} |
|
| 20 | + ) { |
|
| 21 | +} |
|
| 21 | 22 | |
| 22 | 23 | public function leaveNode(Node $node): int|Node|null |
| 23 | 24 | { |
| 24 | - if (!$node instanceof Node\Stmt\Class_) { |
|
| 25 | + if (!$node instanceof Node\Stmt\Class_) |
|
| 26 | + { |
|
| 25 | 27 | return null; |
| 26 | 28 | } |
| 27 | 29 | |
| 28 | 30 | $constructor = $this->getConstructor($node); |
| 29 | - if ($constructor === null) { |
|
| 30 | - if ($this->definition->dependencies === []) { |
|
| 31 | + if ($constructor === null) |
|
| 32 | + { |
|
| 33 | + if ($this->definition->dependencies === []) |
|
| 34 | + { |
|
| 31 | 35 | return $node; |
| 32 | 36 | } |
| 33 | 37 | |
@@ -38,7 +42,8 @@ discard block |
||
| 38 | 42 | |
| 39 | 43 | $this->addDependencies($constructor); |
| 40 | 44 | |
| 41 | - if (!$this->definition->hasConstructor && $this->definition->constructorParams) { |
|
| 45 | + if (!$this->definition->hasConstructor && $this->definition->constructorParams) |
|
| 46 | + { |
|
| 42 | 47 | $this->addParentConstructorCall($constructor); |
| 43 | 48 | } |
| 44 | 49 | |
@@ -50,7 +55,8 @@ discard block |
||
| 50 | 55 | */ |
| 51 | 56 | private function addDependencies(Node\Stmt\ClassMethod $constructor): void |
| 52 | 57 | { |
| 53 | - foreach ($this->definition->dependencies as $dependency) { |
|
| 58 | + foreach ($this->definition->dependencies as $dependency) |
|
| 59 | + { |
|
| 54 | 60 | \array_unshift($constructor->params, $this->buildConstructorParam($dependency)); |
| 55 | 61 | } |
| 56 | 62 | } |
@@ -67,34 +73,41 @@ discard block |
||
| 67 | 73 | private function addParentConstructorCall(Node\Stmt\ClassMethod $constructor): void |
| 68 | 74 | { |
| 69 | 75 | $parentConstructorDependencies = []; |
| 70 | - foreach ($this->definition->constructorParams as $param) { |
|
| 76 | + foreach ($this->definition->constructorParams as $param) |
|
| 77 | + { |
|
| 71 | 78 | $parentConstructorDependencies[] = new Node\Arg(new Node\Expr\Variable($param->name)); |
| 72 | 79 | |
| 73 | 80 | $cp = new Param($param->name); |
| 74 | - if (!empty($param->type)) { |
|
| 81 | + if (!empty($param->type)) |
|
| 82 | + { |
|
| 75 | 83 | $type = $this->getParamType($param); |
| 76 | - if ($param->nullable) { |
|
| 84 | + if ($param->nullable) |
|
| 85 | + { |
|
| 77 | 86 | $type = \sprintf('?%s', $type); |
| 78 | 87 | } |
| 79 | 88 | |
| 80 | 89 | $cp->setType(new Node\Name($type)); |
| 81 | 90 | } |
| 82 | 91 | |
| 83 | - if ($param->byRef) { |
|
| 92 | + if ($param->byRef) |
|
| 93 | + { |
|
| 84 | 94 | $cp->makeByRef(); |
| 85 | 95 | } |
| 86 | 96 | |
| 87 | - if ($param->isVariadic) { |
|
| 97 | + if ($param->isVariadic) |
|
| 98 | + { |
|
| 88 | 99 | $cp->makeVariadic(); |
| 89 | 100 | } |
| 90 | 101 | |
| 91 | - if ($param->hasDefault) { |
|
| 102 | + if ($param->hasDefault) |
|
| 103 | + { |
|
| 92 | 104 | $cp->setDefault($param->default); |
| 93 | 105 | } |
| 94 | 106 | $constructor->params[] = $cp->getNode(); |
| 95 | 107 | } |
| 96 | 108 | |
| 97 | - if ($parentConstructorDependencies !== []) { |
|
| 109 | + if ($parentConstructorDependencies !== []) |
|
| 110 | + { |
|
| 98 | 111 | \array_unshift( |
| 99 | 112 | $constructor->stmts, |
| 100 | 113 | new Node\Stmt\Expression( |
@@ -115,8 +128,10 @@ discard block |
||
| 115 | 128 | |
| 116 | 129 | private function getPropertyType(Dependency $dependency): string |
| 117 | 130 | { |
| 118 | - foreach ($this->definition->getStmts() as $stmt) { |
|
| 119 | - if ($stmt->name === $dependency->type->fullName && $stmt->alias) { |
|
| 131 | + foreach ($this->definition->getStmts() as $stmt) |
|
| 132 | + { |
|
| 133 | + if ($stmt->name === $dependency->type->fullName && $stmt->alias) |
|
| 134 | + { |
|
| 120 | 135 | return $stmt->alias; |
| 121 | 136 | } |
| 122 | 137 | } |
@@ -126,13 +141,16 @@ discard block |
||
| 126 | 141 | |
| 127 | 142 | private function getParamType(ClassNode\ConstructorParam $param): string |
| 128 | 143 | { |
| 129 | - foreach ($this->definition->getStmts() as $stmt) { |
|
| 130 | - if ($stmt->name === $param->type->fullName && $stmt->alias) { |
|
| 144 | + foreach ($this->definition->getStmts() as $stmt) |
|
| 145 | + { |
|
| 146 | + if ($stmt->name === $param->type->fullName && $stmt->alias) |
|
| 147 | + { |
|
| 131 | 148 | return $stmt->alias; |
| 132 | 149 | } |
| 133 | 150 | } |
| 134 | 151 | |
| 135 | - if ($param->type->alias) { |
|
| 152 | + if ($param->type->alias) |
|
| 153 | + { |
|
| 136 | 154 | return $param->type->alias; |
| 137 | 155 | } |
| 138 | 156 | |