@@ -21,7 +21,8 @@ |
||
| 21 | 21 | |
| 22 | 22 | public function setUp(): void |
| 23 | 23 | { |
| 24 | - if ((string)ini_get('zend.assertions') === 1) { |
|
| 24 | + if ((string)ini_get('zend.assertions') === 1) |
|
| 25 | + { |
|
| 25 | 26 | ini_set('zend.assertions', 0); |
| 26 | 27 | } |
| 27 | 28 | } |
@@ -51,8 +51,10 @@ discard block |
||
| 51 | 51 | */ |
| 52 | 52 | public function injectDependencies(string $code, ClassNode $node, bool $removeTrait = false): string |
| 53 | 53 | { |
| 54 | - if (empty($node->dependencies)) { |
|
| 55 | - if ($removeTrait) { |
|
| 54 | + if (empty($node->dependencies)) |
|
| 55 | + { |
|
| 56 | + if ($removeTrait) |
|
| 57 | + { |
|
| 56 | 58 | $tr = new NodeTraverser(); |
| 57 | 59 | $tr->addVisitor(new RemoveUse()); |
| 58 | 60 | $tr->addVisitor(new RemoveTrait()); |
@@ -66,7 +68,8 @@ discard block |
||
| 66 | 68 | $tr = new NodeTraverser(); |
| 67 | 69 | $tr->addVisitor(new AddUse($node)); |
| 68 | 70 | |
| 69 | - if ($removeTrait) { |
|
| 71 | + if ($removeTrait) |
|
| 72 | + { |
|
| 70 | 73 | $tr->addVisitor(new RemoveUse()); |
| 71 | 74 | $tr->addVisitor(new RemoveTrait()); |
| 72 | 75 | } |
@@ -43,22 +43,27 @@ |
||
| 43 | 43 | $this->requested[$node->name->name] = $node->name->name; |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - if ($node instanceof Node\Stmt\Property) { |
|
| 47 | - foreach ($node->props as $prop) { |
|
| 48 | - if ($prop instanceof Node\Stmt\PropertyProperty) { |
|
| 46 | + if ($node instanceof Node\Stmt\Property) |
|
| 47 | + { |
|
| 48 | + foreach ($node->props as $prop) |
|
| 49 | + { |
|
| 50 | + if ($prop instanceof Node\Stmt\PropertyProperty) |
|
| 51 | + { |
|
| 49 | 52 | $this->properties[$prop->name->name] = $prop->name->name; |
| 50 | 53 | } |
| 51 | 54 | } |
| 52 | 55 | } |
| 53 | 56 | |
| 54 | - if ($this->isPromotedProperty($node)) { |
|
| 57 | + if ($this->isPromotedProperty($node)) |
|
| 58 | + { |
|
| 55 | 59 | $this->properties[$node->var->name] = $node->var->name; |
| 56 | 60 | } |
| 57 | 61 | } |
| 58 | 62 | |
| 59 | 63 | private function isPromotedProperty(Node $node): bool |
| 60 | 64 | { |
| 61 | - if (!$node instanceof Node\Param) { |
|
| 65 | + if (!$node instanceof Node\Param) |
|
| 66 | + { |
|
| 62 | 67 | return false; |
| 63 | 68 | } |
| 64 | 69 | |
@@ -25,13 +25,15 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | public function leaveNode(Node $node): int|Node|null |
| 27 | 27 | { |
| 28 | - if (!$node instanceof Node\Stmt\Class_) { |
|
| 28 | + if (!$node instanceof Node\Stmt\Class_) |
|
| 29 | + { |
|
| 29 | 30 | return null; |
| 30 | 31 | } |
| 31 | 32 | |
| 32 | 33 | $constructor = $this->getConstructorAttribute($node); |
| 33 | 34 | $this->addDependencies($constructor); |
| 34 | - if (!$this->definition->hasConstructor && $this->definition->constructorParams) { |
|
| 35 | + if (!$this->definition->hasConstructor && $this->definition->constructorParams) |
|
| 36 | + { |
|
| 35 | 37 | $this->addParentConstructorCall($constructor); |
| 36 | 38 | } |
| 37 | 39 | |
@@ -43,7 +45,8 @@ discard block |
||
| 43 | 45 | */ |
| 44 | 46 | private function addDependencies(Node\Stmt\ClassMethod $constructor): void |
| 45 | 47 | { |
| 46 | - foreach ($this->definition->dependencies as $dependency) { |
|
| 48 | + foreach ($this->definition->dependencies as $dependency) |
|
| 49 | + { |
|
| 47 | 50 | \array_unshift($constructor->params, $this->buildConstructorParam($dependency)); |
| 48 | 51 | } |
| 49 | 52 | } |
@@ -60,34 +63,41 @@ discard block |
||
| 60 | 63 | private function addParentConstructorCall(Node\Stmt\ClassMethod $constructor): void |
| 61 | 64 | { |
| 62 | 65 | $parentConstructorDependencies = []; |
| 63 | - foreach ($this->definition->constructorParams as $param) { |
|
| 66 | + foreach ($this->definition->constructorParams as $param) |
|
| 67 | + { |
|
| 64 | 68 | $parentConstructorDependencies[] = new Node\Arg(new Node\Expr\Variable($param->name)); |
| 65 | 69 | |
| 66 | 70 | $cp = new Param($param->name); |
| 67 | - if (!empty($param->type)) { |
|
| 71 | + if (!empty($param->type)) |
|
| 72 | + { |
|
| 68 | 73 | $type = $this->getParamType($param); |
| 69 | - if ($param->nullable) { |
|
| 74 | + if ($param->nullable) |
|
| 75 | + { |
|
| 70 | 76 | $type = \sprintf('?%s', $type); |
| 71 | 77 | } |
| 72 | 78 | |
| 73 | 79 | $cp->setType(new Node\Name($type)); |
| 74 | 80 | } |
| 75 | 81 | |
| 76 | - if ($param->byRef) { |
|
| 82 | + if ($param->byRef) |
|
| 83 | + { |
|
| 77 | 84 | $cp->makeByRef(); |
| 78 | 85 | } |
| 79 | 86 | |
| 80 | - if ($param->isVariadic) { |
|
| 87 | + if ($param->isVariadic) |
|
| 88 | + { |
|
| 81 | 89 | $cp->makeVariadic(); |
| 82 | 90 | } |
| 83 | 91 | |
| 84 | - if ($param->hasDefault) { |
|
| 92 | + if ($param->hasDefault) |
|
| 93 | + { |
|
| 85 | 94 | $cp->setDefault($param->default); |
| 86 | 95 | } |
| 87 | 96 | $constructor->params[] = $cp->getNode(); |
| 88 | 97 | } |
| 89 | 98 | |
| 90 | - if ($parentConstructorDependencies !== []) { |
|
| 99 | + if ($parentConstructorDependencies !== []) |
|
| 100 | + { |
|
| 91 | 101 | \array_unshift( |
| 92 | 102 | $constructor->stmts, |
| 93 | 103 | new Node\Stmt\Expression( |
@@ -115,18 +125,23 @@ discard block |
||
| 115 | 125 | |
| 116 | 126 | $params = []; |
| 117 | 127 | |
| 118 | - foreach ($this->definition->dependencies as $dependency) { |
|
| 128 | + foreach ($this->definition->dependencies as $dependency) |
|
| 129 | + { |
|
| 119 | 130 | $params[] = new Annotation\Line( |
| 120 | 131 | \sprintf('%s $%s', $this->getPropertyType($dependency), $dependency->var), |
| 121 | 132 | 'param' |
| 122 | 133 | ); |
| 123 | 134 | } |
| 124 | 135 | |
| 125 | - if (!$this->definition->hasConstructor) { |
|
| 126 | - foreach ($this->definition->constructorParams as $param) { |
|
| 127 | - if (!empty($param->type)) { |
|
| 136 | + if (!$this->definition->hasConstructor) |
|
| 137 | + { |
|
| 138 | + foreach ($this->definition->constructorParams as $param) |
|
| 139 | + { |
|
| 140 | + if (!empty($param->type)) |
|
| 141 | + { |
|
| 128 | 142 | $type = $this->getParamType($param); |
| 129 | - if ($param->nullable) { |
|
| 143 | + if ($param->nullable) |
|
| 144 | + { |
|
| 130 | 145 | $type = \sprintf('%s|null', $type); |
| 131 | 146 | } |
| 132 | 147 | |
@@ -134,7 +149,9 @@ discard block |
||
| 134 | 149 | \sprintf($param->isVariadic ? '%s ...$%s' : '%s $%s', $type, $param->name), |
| 135 | 150 | 'param' |
| 136 | 151 | ); |
| 137 | - } else { |
|
| 152 | + } |
|
| 153 | + else |
|
| 154 | + { |
|
| 138 | 155 | $params[] = new Annotation\Line( |
| 139 | 156 | \sprintf('$%s', $param->name), |
| 140 | 157 | 'param' |
@@ -145,12 +162,14 @@ discard block |
||
| 145 | 162 | |
| 146 | 163 | $placementID = 0; |
| 147 | 164 | $previous = null; |
| 148 | - foreach ($an->lines as $index => $line) { |
|
| 165 | + foreach ($an->lines as $index => $line) |
|
| 166 | + { |
|
| 149 | 167 | // always next node |
| 150 | 168 | $placementID = $index + 1; |
| 151 | 169 | |
| 152 | 170 | // inject before this parameters |
| 153 | - if ($line->is(['throws', 'return'])) { |
|
| 171 | + if ($line->is(['throws', 'return'])) |
|
| 172 | + { |
|
| 154 | 173 | // insert before given node |
| 155 | 174 | $placementID--; |
| 156 | 175 | break; |
@@ -159,7 +178,8 @@ discard block |
||
| 159 | 178 | $previous = $line; |
| 160 | 179 | } |
| 161 | 180 | |
| 162 | - if ($previous !== null && !$previous->isEmpty()) { |
|
| 181 | + if ($previous !== null && !$previous->isEmpty()) |
|
| 182 | + { |
|
| 163 | 183 | $placementID++; |
| 164 | 184 | } |
| 165 | 185 | |
@@ -170,8 +190,10 @@ discard block |
||
| 170 | 190 | |
| 171 | 191 | private function getPropertyType(Dependency $dependency): string |
| 172 | 192 | { |
| 173 | - foreach ($this->definition->getStmts() as $stmt) { |
|
| 174 | - if ($stmt->name === $dependency->type->fullName && $stmt->alias) { |
|
| 193 | + foreach ($this->definition->getStmts() as $stmt) |
|
| 194 | + { |
|
| 195 | + if ($stmt->name === $dependency->type->fullName && $stmt->alias) |
|
| 196 | + { |
|
| 175 | 197 | return $stmt->alias; |
| 176 | 198 | } |
| 177 | 199 | } |
@@ -181,13 +203,16 @@ discard block |
||
| 181 | 203 | |
| 182 | 204 | private function getParamType(ClassNode\ConstructorParam $param): string |
| 183 | 205 | { |
| 184 | - foreach ($this->definition->getStmts() as $stmt) { |
|
| 185 | - if ($stmt->name === $param->type->fullName && $stmt->alias) { |
|
| 206 | + foreach ($this->definition->getStmts() as $stmt) |
|
| 207 | + { |
|
| 208 | + if ($stmt->name === $param->type->fullName && $stmt->alias) |
|
| 209 | + { |
|
| 186 | 210 | return $stmt->alias; |
| 187 | 211 | } |
| 188 | 212 | } |
| 189 | 213 | |
| 190 | - if ($param->type->alias) { |
|
| 214 | + if ($param->type->alias) |
|
| 215 | + { |
|
| 191 | 216 | return $param->type->alias; |
| 192 | 217 | } |
| 193 | 218 | |