@@ -37,7 +37,8 @@ discard block |
||
| 37 | 37 | { |
| 38 | 38 | $attribute = $this->reader->firstClassMetadata($reflection, AsCommand::class); |
| 39 | 39 | |
| 40 | - if ($attribute === null) { |
|
| 40 | + if ($attribute === null) |
|
| 41 | + { |
|
| 41 | 42 | $attribute = $reflection->getAttributes(SymfonyAsCommand::class)[0]->newInstance(); |
| 42 | 43 | } |
| 43 | 44 | |
@@ -54,13 +55,16 @@ discard block |
||
| 54 | 55 | { |
| 55 | 56 | $reflection = new \ReflectionClass($command); |
| 56 | 57 | |
| 57 | - foreach ($reflection->getProperties() as $property) { |
|
| 58 | + foreach ($reflection->getProperties() as $property) |
|
| 59 | + { |
|
| 58 | 60 | $attribute = $this->reader->firstPropertyMetadata($property, Argument::class); |
| 59 | - if ($attribute === null) { |
|
| 61 | + if ($attribute === null) |
|
| 62 | + { |
|
| 60 | 63 | continue; |
| 61 | 64 | } |
| 62 | 65 | |
| 63 | - if ($input->hasArgument($attribute->name ?? $property->getName())) { |
|
| 66 | + if ($input->hasArgument($attribute->name ?? $property->getName())) |
|
| 67 | + { |
|
| 64 | 68 | $property->setValue( |
| 65 | 69 | $command, |
| 66 | 70 | $this->typecast($input->getArgument($attribute->name ?? $property->getName()), $property) |
@@ -68,16 +72,20 @@ discard block |
||
| 68 | 72 | } |
| 69 | 73 | } |
| 70 | 74 | |
| 71 | - foreach ($reflection->getProperties() as $property) { |
|
| 75 | + foreach ($reflection->getProperties() as $property) |
|
| 76 | + { |
|
| 72 | 77 | $attribute = $this->reader->firstPropertyMetadata($property, Option::class); |
| 73 | - if ($attribute === null) { |
|
| 78 | + if ($attribute === null) |
|
| 79 | + { |
|
| 74 | 80 | continue; |
| 75 | 81 | } |
| 76 | 82 | |
| 77 | - if ($input->hasOption($attribute->name ?? $property->getName())) { |
|
| 83 | + if ($input->hasOption($attribute->name ?? $property->getName())) |
|
| 84 | + { |
|
| 78 | 85 | $value = $this->typecast($input->getOption($attribute->name ?? $property->getName()), $property); |
| 79 | 86 | |
| 80 | - if ($value !== null || $this->getPropertyType($property)->allowsNull()) { |
|
| 87 | + if ($value !== null || $this->getPropertyType($property)->allowsNull()) |
|
| 88 | + { |
|
| 81 | 89 | $property->setValue($command, $value); |
| 82 | 90 | } |
| 83 | 91 | } |
@@ -88,9 +96,11 @@ discard block |
||
| 88 | 96 | { |
| 89 | 97 | $result = []; |
| 90 | 98 | $arrayArgument = null; |
| 91 | - foreach ($reflection->getProperties() as $property) { |
|
| 99 | + foreach ($reflection->getProperties() as $property) |
|
| 100 | + { |
|
| 92 | 101 | $attribute = $this->reader->firstPropertyMetadata($property, Argument::class); |
| 93 | - if ($attribute === null) { |
|
| 102 | + if ($attribute === null) |
|
| 103 | + { |
|
| 94 | 104 | continue; |
| 95 | 105 | } |
| 96 | 106 | |
@@ -113,19 +123,22 @@ discard block |
||
| 113 | 123 | suggestedValues: $attribute->suggestedValues |
| 114 | 124 | ); |
| 115 | 125 | |
| 116 | - if ($arrayArgument !== null && $isArray) { |
|
| 126 | + if ($arrayArgument !== null && $isArray) |
|
| 127 | + { |
|
| 117 | 128 | throw new ConfiguratorException('There must be only one array argument!'); |
| 118 | 129 | } |
| 119 | 130 | |
| 120 | 131 | // It must be used at the end of the argument list. |
| 121 | - if ($isArray) { |
|
| 132 | + if ($isArray) |
|
| 133 | + { |
|
| 122 | 134 | $arrayArgument = $argument; |
| 123 | 135 | continue; |
| 124 | 136 | } |
| 125 | 137 | $result[] = $argument; |
| 126 | 138 | } |
| 127 | 139 | |
| 128 | - if ($arrayArgument !== null) { |
|
| 140 | + if ($arrayArgument !== null) |
|
| 141 | + { |
|
| 129 | 142 | $result[] = $arrayArgument; |
| 130 | 143 | } |
| 131 | 144 | |
@@ -135,21 +148,26 @@ discard block |
||
| 135 | 148 | private function parseOptions(\ReflectionClass $reflection): array |
| 136 | 149 | { |
| 137 | 150 | $result = []; |
| 138 | - foreach ($reflection->getProperties() as $property) { |
|
| 151 | + foreach ($reflection->getProperties() as $property) |
|
| 152 | + { |
|
| 139 | 153 | $attribute = $this->reader->firstPropertyMetadata($property, Option::class); |
| 140 | - if ($attribute === null) { |
|
| 154 | + if ($attribute === null) |
|
| 155 | + { |
|
| 141 | 156 | continue; |
| 142 | 157 | } |
| 143 | 158 | |
| 144 | 159 | $type = $this->getPropertyType($property); |
| 145 | 160 | $mode = $attribute->mode; |
| 146 | 161 | |
| 147 | - if ($mode === null) { |
|
| 162 | + if ($mode === null) |
|
| 163 | + { |
|
| 148 | 164 | $mode = $this->guessOptionMode($type, $property); |
| 149 | 165 | } |
| 150 | 166 | |
| 151 | - if ($mode === InputOption::VALUE_NONE || $mode === InputOption::VALUE_NEGATABLE) { |
|
| 152 | - if ($type->getName() !== 'bool') { |
|
| 167 | + if ($mode === InputOption::VALUE_NONE || $mode === InputOption::VALUE_NEGATABLE) |
|
| 168 | + { |
|
| 169 | + if ($type->getName() !== 'bool') |
|
| 170 | + { |
|
| 153 | 171 | throw new ConfiguratorException( |
| 154 | 172 | 'Options properties with mode `VALUE_NONE` or `VALUE_NEGATABLE` must be bool!' |
| 155 | 173 | ); |
@@ -175,17 +193,22 @@ discard block |
||
| 175 | 193 | { |
| 176 | 194 | $type = $property->hasType() ? $property->getType() : null; |
| 177 | 195 | |
| 178 | - if (!$type instanceof \ReflectionNamedType || $value === null) { |
|
| 196 | + if (!$type instanceof \ReflectionNamedType || $value === null) |
|
| 197 | + { |
|
| 179 | 198 | return $value; |
| 180 | 199 | } |
| 181 | 200 | |
| 182 | - if (!$type->isBuiltin() && \enum_exists($type->getName())) { |
|
| 183 | - try { |
|
| 201 | + if (!$type->isBuiltin() && \enum_exists($type->getName())) |
|
| 202 | + { |
|
| 203 | + try |
|
| 204 | + { |
|
| 184 | 205 | /** @var class-string<\BackedEnum> $enum */ |
| 185 | 206 | $enum = $type->getName(); |
| 186 | 207 | |
| 187 | 208 | return $enum::from($value); |
| 188 | - } catch (\Throwable) { |
|
| 209 | + } |
|
| 210 | + catch (\Throwable) |
|
| 211 | + { |
|
| 189 | 212 | throw new ConfiguratorException(\sprintf('Wrong option value. Allowed options: `%s`.', \implode( |
| 190 | 213 | '`, `', |
| 191 | 214 | \array_map(static fn (\BackedEnum $item): string => (string) $item->value, $enum::cases()) |
@@ -205,7 +228,8 @@ discard block |
||
| 205 | 228 | |
| 206 | 229 | private function getPropertyType(\ReflectionProperty $property): \ReflectionNamedType |
| 207 | 230 | { |
| 208 | - if (!$property->hasType()) { |
|
| 231 | + if (!$property->hasType()) |
|
| 232 | + { |
|
| 209 | 233 | throw new ConfiguratorException( |
| 210 | 234 | \sprintf('Please, specify the type for the `%s` property!', $property->getName()) |
| 211 | 235 | ); |
@@ -213,23 +237,29 @@ discard block |
||
| 213 | 237 | |
| 214 | 238 | $type = $property->getType(); |
| 215 | 239 | |
| 216 | - if ($type instanceof \ReflectionIntersectionType) { |
|
| 240 | + if ($type instanceof \ReflectionIntersectionType) |
|
| 241 | + { |
|
| 217 | 242 | throw new ConfiguratorException(\sprintf('Invalid type for the `%s` property.', $property->getName())); |
| 218 | 243 | } |
| 219 | 244 | |
| 220 | - if ($type instanceof \ReflectionUnionType) { |
|
| 221 | - foreach ($type->getTypes() as $type) { |
|
| 222 | - if ($type instanceof \ReflectionNamedType && $type->isBuiltin()) { |
|
| 245 | + if ($type instanceof \ReflectionUnionType) |
|
| 246 | + { |
|
| 247 | + foreach ($type->getTypes() as $type) |
|
| 248 | + { |
|
| 249 | + if ($type instanceof \ReflectionNamedType && $type->isBuiltin()) |
|
| 250 | + { |
|
| 223 | 251 | return $type; |
| 224 | 252 | } |
| 225 | 253 | } |
| 226 | 254 | } |
| 227 | 255 | |
| 228 | - if (!$type->isBuiltin() && \enum_exists($type->getName())) { |
|
| 256 | + if (!$type->isBuiltin() && \enum_exists($type->getName())) |
|
| 257 | + { |
|
| 229 | 258 | return $type; |
| 230 | 259 | } |
| 231 | 260 | |
| 232 | - if ($type instanceof \ReflectionNamedType && $type->isBuiltin() && $type->getName() !== 'object') { |
|
| 261 | + if ($type instanceof \ReflectionNamedType && $type->isBuiltin() && $type->getName() !== 'object') |
|
| 262 | + { |
|
| 233 | 263 | return $type; |
| 234 | 264 | } |
| 235 | 265 | |
@@ -37,7 +37,8 @@ discard block |
||
| 37 | 37 | ->method('getArgument') |
| 38 | 38 | ->willReturnOnConsecutiveCalls(5, 'foo', ['foo', 'bar'], 0.5, true); |
| 39 | 39 | |
| 40 | - $command = new #[AsCommand('foo')] class extends Command { |
|
| 40 | + $command = new #[AsCommand('foo')] class extends Command |
|
| 41 | + { |
|
| 41 | 42 | #[Argument] |
| 42 | 43 | public int $intVal; |
| 43 | 44 | |
@@ -76,7 +77,8 @@ discard block |
||
| 76 | 77 | ->expects($this->never()) |
| 77 | 78 | ->method('getArgument'); |
| 78 | 79 | |
| 79 | - $command = new #[AsCommand('foo')] class extends Command { |
|
| 80 | + $command = new #[AsCommand('foo')] class extends Command |
|
| 81 | + { |
|
| 80 | 82 | #[Argument] |
| 81 | 83 | public int $arg; |
| 82 | 84 | }; |
@@ -99,7 +101,8 @@ discard block |
||
| 99 | 101 | ->method('getOption') |
| 100 | 102 | ->willReturnOnConsecutiveCalls(5, 'foo', ['foo', 'bar'], 0.5, true, true); |
| 101 | 103 | |
| 102 | - $command = new #[AsCommand('foo')] class extends Command { |
|
| 104 | + $command = new #[AsCommand('foo')] class extends Command |
|
| 105 | + { |
|
| 103 | 106 | #[Option(mode: InputOption::VALUE_REQUIRED)] |
| 104 | 107 | public int $intVal; |
| 105 | 108 | |
@@ -142,7 +145,8 @@ discard block |
||
| 142 | 145 | ->method('getOption') |
| 143 | 146 | ->willReturnOnConsecutiveCalls(1, null, 0); |
| 144 | 147 | |
| 145 | - $command = new #[AsCommand('foo')] class extends Command { |
|
| 148 | + $command = new #[AsCommand('foo')] class extends Command |
|
| 149 | + { |
|
| 146 | 150 | #[Option(mode: InputOption::VALUE_REQUIRED)] |
| 147 | 151 | public Status $required; |
| 148 | 152 | |
@@ -166,7 +170,8 @@ discard block |
||
| 166 | 170 | $input->expects($this->once())->method('hasOption')->willReturn(true); |
| 167 | 171 | $input->expects($this->once())->method('getOption')->willReturn('foo'); |
| 168 | 172 | |
| 169 | - $command = new #[AsCommand('foo')] class extends Command { |
|
| 173 | + $command = new #[AsCommand('foo')] class extends Command |
|
| 174 | + { |
|
| 170 | 175 | #[Option] |
| 171 | 176 | public Status $status; |
| 172 | 177 | }; |
@@ -188,7 +193,8 @@ discard block |
||
| 188 | 193 | ->expects($this->never()) |
| 189 | 194 | ->method('getOption'); |
| 190 | 195 | |
| 191 | - $command = new #[AsCommand('foo')] class extends Command { |
|
| 196 | + $command = new #[AsCommand('foo')] class extends Command |
|
| 197 | + { |
|
| 192 | 198 | #[Option(mode: InputOption::VALUE_REQUIRED)] |
| 193 | 199 | public int $option; |
| 194 | 200 | }; |
@@ -212,7 +218,8 @@ discard block |
||
| 212 | 218 | ->method('getOption') |
| 213 | 219 | ->willReturn(null); |
| 214 | 220 | |
| 215 | - $command = new #[AsCommand('foo')] class extends Command { |
|
| 221 | + $command = new #[AsCommand('foo')] class extends Command |
|
| 222 | + { |
|
| 216 | 223 | #[Option(mode: InputOption::VALUE_REQUIRED)] |
| 217 | 224 | public int $option; |
| 218 | 225 | }; |
@@ -235,7 +242,8 @@ discard block |
||
| 235 | 242 | ->method('getOption') |
| 236 | 243 | ->willReturn(null); |
| 237 | 244 | |
| 238 | - $command = new #[AsCommand('foo')] class extends Command { |
|
| 245 | + $command = new #[AsCommand('foo')] class extends Command |
|
| 246 | + { |
|
| 239 | 247 | #[Option(mode: InputOption::VALUE_REQUIRED)] |
| 240 | 248 | public ?int $intVal; |
| 241 | 249 | }; |