@@ -38,7 +38,8 @@ discard block |
||
| 38 | 38 | { |
| 39 | 39 | $attribute = $this->reader->firstClassMetadata($reflection, AsCommand::class); |
| 40 | 40 | |
| 41 | - if ($attribute === null) { |
|
| 41 | + if ($attribute === null) |
|
| 42 | + { |
|
| 42 | 43 | $attribute = $reflection->getAttributes(SymfonyAsCommand::class)[0]->newInstance(); |
| 43 | 44 | } |
| 44 | 45 | |
@@ -57,13 +58,16 @@ discard block |
||
| 57 | 58 | { |
| 58 | 59 | $reflection = new \ReflectionClass($command); |
| 59 | 60 | |
| 60 | - foreach ($reflection->getProperties() as $property) { |
|
| 61 | + foreach ($reflection->getProperties() as $property) |
|
| 62 | + { |
|
| 61 | 63 | $attribute = $this->reader->firstPropertyMetadata($property, Argument::class); |
| 62 | - if ($attribute === null) { |
|
| 64 | + if ($attribute === null) |
|
| 65 | + { |
|
| 63 | 66 | continue; |
| 64 | 67 | } |
| 65 | 68 | |
| 66 | - if ($input->hasArgument($attribute->name ?? $property->getName())) { |
|
| 69 | + if ($input->hasArgument($attribute->name ?? $property->getName())) |
|
| 70 | + { |
|
| 67 | 71 | $property->setValue( |
| 68 | 72 | $command, |
| 69 | 73 | $this->typecast($input->getArgument($attribute->name ?? $property->getName()), $property) |
@@ -71,16 +75,20 @@ discard block |
||
| 71 | 75 | } |
| 72 | 76 | } |
| 73 | 77 | |
| 74 | - foreach ($reflection->getProperties() as $property) { |
|
| 78 | + foreach ($reflection->getProperties() as $property) |
|
| 79 | + { |
|
| 75 | 80 | $attribute = $this->reader->firstPropertyMetadata($property, Option::class); |
| 76 | - if ($attribute === null) { |
|
| 81 | + if ($attribute === null) |
|
| 82 | + { |
|
| 77 | 83 | continue; |
| 78 | 84 | } |
| 79 | 85 | |
| 80 | - if ($input->hasOption($attribute->name ?? $property->getName())) { |
|
| 86 | + if ($input->hasOption($attribute->name ?? $property->getName())) |
|
| 87 | + { |
|
| 81 | 88 | $value = $this->typecast($input->getOption($attribute->name ?? $property->getName()), $property); |
| 82 | 89 | |
| 83 | - if ($value !== null || $this->getPropertyType($property)->allowsNull()) { |
|
| 90 | + if ($value !== null || $this->getPropertyType($property)->allowsNull()) |
|
| 91 | + { |
|
| 84 | 92 | $property->setValue($command, $value); |
| 85 | 93 | } |
| 86 | 94 | } |
@@ -100,20 +108,24 @@ discard block |
||
| 100 | 108 | ? $reflection->getMethod('__invoke') |
| 101 | 109 | : null); |
| 102 | 110 | |
| 103 | - if ($method === null) { |
|
| 111 | + if ($method === null) |
|
| 112 | + { |
|
| 104 | 113 | return $reflection; |
| 105 | 114 | } |
| 106 | 115 | |
| 107 | 116 | $parameter = null; |
| 108 | - foreach ($method->getParameters() as $param) { |
|
| 117 | + foreach ($method->getParameters() as $param) |
|
| 118 | + { |
|
| 109 | 119 | $attribute = $this->reader->firstParameterMetadata($param, AsInput::class); |
| 110 | - if ($attribute !== null) { |
|
| 120 | + if ($attribute !== null) |
|
| 121 | + { |
|
| 111 | 122 | $parameter = $param; |
| 112 | 123 | break; |
| 113 | 124 | } |
| 114 | 125 | } |
| 115 | 126 | |
| 116 | - if ($parameter === null) { |
|
| 127 | + if ($parameter === null) |
|
| 128 | + { |
|
| 117 | 129 | return $reflection; |
| 118 | 130 | } |
| 119 | 131 | |
@@ -124,9 +136,11 @@ discard block |
||
| 124 | 136 | { |
| 125 | 137 | $result = []; |
| 126 | 138 | $arrayArgument = null; |
| 127 | - foreach ($reflection->getProperties() as $property) { |
|
| 139 | + foreach ($reflection->getProperties() as $property) |
|
| 140 | + { |
|
| 128 | 141 | $attribute = $this->reader->firstPropertyMetadata($property, Argument::class); |
| 129 | - if ($attribute === null) { |
|
| 142 | + if ($attribute === null) |
|
| 143 | + { |
|
| 130 | 144 | continue; |
| 131 | 145 | } |
| 132 | 146 | |
@@ -149,19 +163,22 @@ discard block |
||
| 149 | 163 | suggestedValues: $attribute->suggestedValues |
| 150 | 164 | ); |
| 151 | 165 | |
| 152 | - if ($arrayArgument !== null && $isArray) { |
|
| 166 | + if ($arrayArgument !== null && $isArray) |
|
| 167 | + { |
|
| 153 | 168 | throw new ConfiguratorException('There must be only one array argument!'); |
| 154 | 169 | } |
| 155 | 170 | |
| 156 | 171 | // It must be used at the end of the argument list. |
| 157 | - if ($isArray) { |
|
| 172 | + if ($isArray) |
|
| 173 | + { |
|
| 158 | 174 | $arrayArgument = $argument; |
| 159 | 175 | continue; |
| 160 | 176 | } |
| 161 | 177 | $result[] = $argument; |
| 162 | 178 | } |
| 163 | 179 | |
| 164 | - if ($arrayArgument !== null) { |
|
| 180 | + if ($arrayArgument !== null) |
|
| 181 | + { |
|
| 165 | 182 | $result[] = $arrayArgument; |
| 166 | 183 | } |
| 167 | 184 | |
@@ -171,21 +188,26 @@ discard block |
||
| 171 | 188 | private function parseOptions(\ReflectionClass $reflection): array |
| 172 | 189 | { |
| 173 | 190 | $result = []; |
| 174 | - foreach ($reflection->getProperties() as $property) { |
|
| 191 | + foreach ($reflection->getProperties() as $property) |
|
| 192 | + { |
|
| 175 | 193 | $attribute = $this->reader->firstPropertyMetadata($property, Option::class); |
| 176 | - if ($attribute === null) { |
|
| 194 | + if ($attribute === null) |
|
| 195 | + { |
|
| 177 | 196 | continue; |
| 178 | 197 | } |
| 179 | 198 | |
| 180 | 199 | $type = $this->getPropertyType($property); |
| 181 | 200 | $mode = $attribute->mode; |
| 182 | 201 | |
| 183 | - if ($mode === null) { |
|
| 202 | + if ($mode === null) |
|
| 203 | + { |
|
| 184 | 204 | $mode = $this->guessOptionMode($type, $property); |
| 185 | 205 | } |
| 186 | 206 | |
| 187 | - if ($mode === InputOption::VALUE_NONE || $mode === InputOption::VALUE_NEGATABLE) { |
|
| 188 | - if ($type->getName() !== 'bool') { |
|
| 207 | + if ($mode === InputOption::VALUE_NONE || $mode === InputOption::VALUE_NEGATABLE) |
|
| 208 | + { |
|
| 209 | + if ($type->getName() !== 'bool') |
|
| 210 | + { |
|
| 189 | 211 | throw new ConfiguratorException( |
| 190 | 212 | 'Options properties with mode `VALUE_NONE` or `VALUE_NEGATABLE` must be bool!' |
| 191 | 213 | ); |
@@ -211,17 +233,22 @@ discard block |
||
| 211 | 233 | { |
| 212 | 234 | $type = $property->hasType() ? $property->getType() : null; |
| 213 | 235 | |
| 214 | - if (!$type instanceof \ReflectionNamedType || $value === null) { |
|
| 236 | + if (!$type instanceof \ReflectionNamedType || $value === null) |
|
| 237 | + { |
|
| 215 | 238 | return $value; |
| 216 | 239 | } |
| 217 | 240 | |
| 218 | - if (!$type->isBuiltin() && \enum_exists($type->getName())) { |
|
| 219 | - try { |
|
| 241 | + if (!$type->isBuiltin() && \enum_exists($type->getName())) |
|
| 242 | + { |
|
| 243 | + try |
|
| 244 | + { |
|
| 220 | 245 | /** @var class-string<\BackedEnum> $enum */ |
| 221 | 246 | $enum = $type->getName(); |
| 222 | 247 | |
| 223 | 248 | return $enum::from($value); |
| 224 | - } catch (\Throwable) { |
|
| 249 | + } |
|
| 250 | + catch (\Throwable) |
|
| 251 | + { |
|
| 225 | 252 | throw new ConfiguratorException(\sprintf('Wrong option value. Allowed options: `%s`.', \implode( |
| 226 | 253 | '`, `', |
| 227 | 254 | \array_map(static fn (\BackedEnum $item): string => (string) $item->value, $enum::cases()) |
@@ -241,7 +268,8 @@ discard block |
||
| 241 | 268 | |
| 242 | 269 | private function getPropertyType(\ReflectionProperty $property): \ReflectionNamedType |
| 243 | 270 | { |
| 244 | - if (!$property->hasType()) { |
|
| 271 | + if (!$property->hasType()) |
|
| 272 | + { |
|
| 245 | 273 | throw new ConfiguratorException( |
| 246 | 274 | \sprintf('Please, specify the type for the `%s` property!', $property->getName()) |
| 247 | 275 | ); |
@@ -249,23 +277,29 @@ discard block |
||
| 249 | 277 | |
| 250 | 278 | $type = $property->getType(); |
| 251 | 279 | |
| 252 | - if ($type instanceof \ReflectionIntersectionType) { |
|
| 280 | + if ($type instanceof \ReflectionIntersectionType) |
|
| 281 | + { |
|
| 253 | 282 | throw new ConfiguratorException(\sprintf('Invalid type for the `%s` property.', $property->getName())); |
| 254 | 283 | } |
| 255 | 284 | |
| 256 | - if ($type instanceof \ReflectionUnionType) { |
|
| 257 | - foreach ($type->getTypes() as $type) { |
|
| 258 | - if ($type instanceof \ReflectionNamedType && $type->isBuiltin()) { |
|
| 285 | + if ($type instanceof \ReflectionUnionType) |
|
| 286 | + { |
|
| 287 | + foreach ($type->getTypes() as $type) |
|
| 288 | + { |
|
| 289 | + if ($type instanceof \ReflectionNamedType && $type->isBuiltin()) |
|
| 290 | + { |
|
| 259 | 291 | return $type; |
| 260 | 292 | } |
| 261 | 293 | } |
| 262 | 294 | } |
| 263 | 295 | |
| 264 | - if ($type instanceof \ReflectionNamedType && !$type->isBuiltin() && \enum_exists($type->getName())) { |
|
| 296 | + if ($type instanceof \ReflectionNamedType && !$type->isBuiltin() && \enum_exists($type->getName())) |
|
| 297 | + { |
|
| 265 | 298 | return $type; |
| 266 | 299 | } |
| 267 | 300 | |
| 268 | - if ($type instanceof \ReflectionNamedType && $type->isBuiltin() && $type->getName() !== 'object') { |
|
| 301 | + if ($type instanceof \ReflectionNamedType && $type->isBuiltin() && $type->getName() !== 'object') |
|
| 302 | + { |
|
| 269 | 303 | return $type; |
| 270 | 304 | } |
| 271 | 305 | |