Passed
Pull Request — master (#1124)
by
unknown
10:24
created
src/Console/src/Configurator/Attribute/Parser.php 2 patches
Spacing   +44 added lines, -44 removed lines patch added patch discarded remove patch
@@ -25,7 +25,7 @@  discard block
 block discarded – undo
25 25
 {
26 26
     public function __construct(
27 27
         private readonly ReaderInterface $reader = new AttributeReader()
28
-    ) {
28
+    ){
29 29
     }
30 30
 
31 31
     public function hasCommandAttribute(\ReflectionClass $reflection): bool
@@ -38,7 +38,7 @@  discard block
 block discarded – undo
38 38
     {
39 39
         $attribute = $this->reader->firstClassMetadata($reflection, AsCommand::class);
40 40
 
41
-        if ($attribute === null) {
41
+        if ($attribute === null){
42 42
             $attribute = $reflection->getAttributes(SymfonyAsCommand::class)[0]->newInstance();
43 43
         }
44 44
 
@@ -57,13 +57,13 @@  discard block
 block discarded – undo
57 57
     {
58 58
         $reflection = new \ReflectionClass($command);
59 59
 
60
-        foreach ($reflection->getProperties() as $property) {
60
+        foreach ($reflection->getProperties() as $property){
61 61
             $attribute = $this->reader->firstPropertyMetadata($property, Argument::class);
62
-            if ($attribute === null) {
62
+            if ($attribute === null){
63 63
                 continue;
64 64
             }
65 65
 
66
-            if ($input->hasArgument($attribute->name ?? $property->getName())) {
66
+            if ($input->hasArgument($attribute->name ?? $property->getName())){
67 67
                 $property->setValue(
68 68
                     $command,
69 69
                     $this->typecast($input->getArgument($attribute->name ?? $property->getName()), $property)
@@ -71,16 +71,16 @@  discard block
 block discarded – undo
71 71
             }
72 72
         }
73 73
 
74
-        foreach ($reflection->getProperties() as $property) {
74
+        foreach ($reflection->getProperties() as $property){
75 75
             $attribute = $this->reader->firstPropertyMetadata($property, Option::class);
76
-            if ($attribute === null) {
76
+            if ($attribute === null){
77 77
                 continue;
78 78
             }
79 79
 
80
-            if ($input->hasOption($attribute->name ?? $property->getName())) {
80
+            if ($input->hasOption($attribute->name ?? $property->getName())){
81 81
                 $value = $this->typecast($input->getOption($attribute->name ?? $property->getName()), $property);
82 82
 
83
-                if ($value !== null || $this->getPropertyType($property)->allowsNull()) {
83
+                if ($value !== null || $this->getPropertyType($property)->allowsNull()){
84 84
                     $property->setValue($command, $value);
85 85
                 }
86 86
             }
@@ -100,20 +100,20 @@  discard block
 block discarded – undo
100 100
                 ? $reflection->getMethod('__invoke')
101 101
                 : null);
102 102
 
103
-        if ($method === null) {
103
+        if ($method === null){
104 104
             return $reflection;
105 105
         }
106 106
 
107 107
         $parameter = null;
108
-        foreach ($method->getParameters() as $param) {
108
+        foreach ($method->getParameters() as $param){
109 109
             $attribute = $this->reader->firstParameterMetadata($param, AsInput::class);
110
-            if ($attribute !== null) {
110
+            if ($attribute !== null){
111 111
                 $parameter = $param;
112 112
                 break;
113 113
             }
114 114
         }
115 115
 
116
-        if ($parameter === null) {
116
+        if ($parameter === null){
117 117
             return $reflection;
118 118
         }
119 119
 
@@ -124,9 +124,9 @@  discard block
 block discarded – undo
124 124
     {
125 125
         $result = [];
126 126
         $arrayArgument = null;
127
-        foreach ($reflection->getProperties() as $property) {
127
+        foreach ($reflection->getProperties() as $property){
128 128
             $attribute = $this->reader->firstPropertyMetadata($property, Argument::class);
129
-            if ($attribute === null) {
129
+            if ($attribute === null){
130 130
                 continue;
131 131
             }
132 132
 
@@ -144,24 +144,24 @@  discard block
 block discarded – undo
144 144
             $argument = new InputArgument(
145 145
                 name: $attribute->name ?? $property->getName(),
146 146
                 mode: $mode,
147
-                description: (string) $attribute->description,
147
+                description: (string)$attribute->description,
148 148
                 default: $property->hasDefaultValue() ? $property->getDefaultValue() : null,
149
-                suggestedValues: $attribute->suggestedValues
149
+                suggestedValues : $attribute->suggestedValues
150 150
             );
151 151
 
152
-            if ($arrayArgument !== null && $isArray) {
152
+            if ($arrayArgument !== null && $isArray){
153 153
                 throw new ConfiguratorException('There must be only one array argument!');
154 154
             }
155 155
 
156 156
             // It must be used at the end of the argument list.
157
-            if ($isArray) {
157
+            if ($isArray){
158 158
                 $arrayArgument = $argument;
159 159
                 continue;
160 160
             }
161 161
             $result[] = $argument;
162 162
         }
163 163
 
164
-        if ($arrayArgument !== null) {
164
+        if ($arrayArgument !== null){
165 165
             $result[] = $arrayArgument;
166 166
         }
167 167
 
@@ -171,21 +171,21 @@  discard block
 block discarded – undo
171 171
     private function parseOptions(\ReflectionClass $reflection): array
172 172
     {
173 173
         $result = [];
174
-        foreach ($reflection->getProperties() as $property) {
174
+        foreach ($reflection->getProperties() as $property){
175 175
             $attribute = $this->reader->firstPropertyMetadata($property, Option::class);
176
-            if ($attribute === null) {
176
+            if ($attribute === null){
177 177
                 continue;
178 178
             }
179 179
 
180 180
             $type = $this->getPropertyType($property);
181 181
             $mode = $attribute->mode;
182 182
 
183
-            if ($mode === null) {
183
+            if ($mode === null){
184 184
                 $mode = $this->guessOptionMode($type, $property);
185 185
             }
186 186
 
187
-            if ($mode === InputOption::VALUE_NONE || $mode === InputOption::VALUE_NEGATABLE) {
188
-                if ($type->getName() !== 'bool') {
187
+            if ($mode === InputOption::VALUE_NONE || $mode === InputOption::VALUE_NEGATABLE){
188
+                if ($type->getName() !== 'bool'){
189 189
                     throw new ConfiguratorException(
190 190
                         'Options properties with mode `VALUE_NONE` or `VALUE_NEGATABLE` must be bool!'
191 191
                     );
@@ -198,9 +198,9 @@  discard block
 block discarded – undo
198 198
                 name: $attribute->name ?? $property->getName(),
199 199
                 shortcut: $attribute->shortcut,
200 200
                 mode: $mode,
201
-                description: (string) $attribute->description,
201
+                description: (string)$attribute->description,
202 202
                 default: $hasDefaultValue ? $property->getDefaultValue() : null,
203
-                suggestedValues: $attribute->suggestedValues
203
+                suggestedValues : $attribute->suggestedValues
204 204
             );
205 205
         }
206 206
 
@@ -211,37 +211,37 @@  discard block
 block discarded – undo
211 211
     {
212 212
         $type = $property->hasType() ? $property->getType() : null;
213 213
 
214
-        if (!$type instanceof \ReflectionNamedType || $value === null) {
214
+        if (!$type instanceof \ReflectionNamedType || $value === null){
215 215
             return $value;
216 216
         }
217 217
 
218
-        if (!$type->isBuiltin() && \enum_exists($type->getName())) {
219
-            try {
218
+        if (!$type->isBuiltin() && \enum_exists($type->getName())){
219
+            try{
220 220
                 /** @var class-string<\BackedEnum> $enum */
221 221
                 $enum = $type->getName();
222 222
 
223 223
                 return $enum::from($value);
224
-            } catch (\Throwable) {
224
+            }catch (\Throwable){
225 225
                 throw new ConfiguratorException(\sprintf('Wrong option value. Allowed options: `%s`.', \implode(
226 226
                     '`, `',
227
-                    \array_map(static fn (\BackedEnum $item): string => (string) $item->value, $enum::cases())
227
+                    \array_map(static fn (\BackedEnum $item): string => (string)$item->value, $enum::cases())
228 228
                 )));
229 229
             }
230 230
         }
231 231
 
232 232
         return match ($type->getName()) {
233
-            'int' => (int) $value,
234
-            'string' => (string) $value,
235
-            'bool' => (bool) $value,
236
-            'float' => (float) $value,
237
-            'array' => (array) $value,
233
+            'int' => (int)$value,
234
+            'string' => (string)$value,
235
+            'bool' => (bool)$value,
236
+            'float' => (float)$value,
237
+            'array' => (array)$value,
238 238
             default => $value
239 239
         };
240 240
     }
241 241
 
242 242
     private function getPropertyType(\ReflectionProperty $property): \ReflectionNamedType
243 243
     {
244
-        if (!$property->hasType()) {
244
+        if (!$property->hasType()){
245 245
             throw new ConfiguratorException(
246 246
                 \sprintf('Please, specify the type for the `%s` property!', $property->getName())
247 247
             );
@@ -249,23 +249,23 @@  discard block
 block discarded – undo
249 249
 
250 250
         $type = $property->getType();
251 251
 
252
-        if ($type instanceof \ReflectionIntersectionType) {
252
+        if ($type instanceof \ReflectionIntersectionType){
253 253
             throw new ConfiguratorException(\sprintf('Invalid type for the `%s` property.', $property->getName()));
254 254
         }
255 255
 
256
-        if ($type instanceof \ReflectionUnionType) {
257
-            foreach ($type->getTypes() as $type) {
258
-                if ($type instanceof \ReflectionNamedType && $type->isBuiltin()) {
256
+        if ($type instanceof \ReflectionUnionType){
257
+            foreach ($type->getTypes() as $type){
258
+                if ($type instanceof \ReflectionNamedType && $type->isBuiltin()){
259 259
                     return $type;
260 260
                 }
261 261
             }
262 262
         }
263 263
 
264
-        if ($type instanceof \ReflectionNamedType && !$type->isBuiltin() && \enum_exists($type->getName())) {
264
+        if ($type instanceof \ReflectionNamedType && !$type->isBuiltin() && \enum_exists($type->getName())){
265 265
             return $type;
266 266
         }
267 267
 
268
-        if ($type instanceof \ReflectionNamedType && $type->isBuiltin() && $type->getName() !== 'object') {
268
+        if ($type instanceof \ReflectionNamedType && $type->isBuiltin() && $type->getName() !== 'object'){
269 269
             return $type;
270 270
         }
271 271
 
Please login to merge, or discard this patch.
Braces   +67 added lines, -33 removed lines patch added patch discarded remove patch
@@ -38,7 +38,8 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 
Please login to merge, or discard this patch.