Total Complexity | 43 |
Total Lines | 431 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like BaseMakeActionCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseMakeActionCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
62 | abstract class BaseMakeActionCommand extends MakeCommand |
||
63 | { |
||
64 | /** |
||
65 | * The form parameter class name |
||
66 | * @var class-string |
||
|
|||
67 | */ |
||
68 | protected string $paramClass; |
||
69 | |||
70 | /** |
||
71 | * The form validation class name |
||
72 | * @var class-string |
||
73 | */ |
||
74 | protected string $validatorClass; |
||
75 | |||
76 | /** |
||
77 | * The entity class name |
||
78 | * @var class-string |
||
79 | */ |
||
80 | protected string $entityClass; |
||
81 | |||
82 | /** |
||
83 | * The repository class name |
||
84 | * @var class-string |
||
85 | */ |
||
86 | protected string $repositoryClass; |
||
87 | |||
88 | /** |
||
89 | * Create new instance |
||
90 | * @param Application $application |
||
91 | * @param Filesystem $filesystem |
||
92 | */ |
||
93 | public function __construct( |
||
94 | Application $application, |
||
95 | Filesystem $filesystem |
||
96 | ) { |
||
97 | parent::__construct($application, $filesystem); |
||
98 | |||
99 | $this->addOption( |
||
100 | '-c|--fields', |
||
101 | 'The entity fields. Example field1:param1,field2:param2,field3', |
||
102 | null, |
||
103 | false |
||
104 | ); |
||
105 | |||
106 | $this->addOption( |
||
107 | '-i|--fields-unique', |
||
108 | 'The entity unique fields. Example field1:param1,field2:param2,field3', |
||
109 | null, |
||
110 | false |
||
111 | ); |
||
112 | |||
113 | $this->addOption( |
||
114 | '-o|--fields-order', |
||
115 | 'The entity orders fields. Example field1:ASC,field2:DESC,field3', |
||
116 | null, |
||
117 | false |
||
118 | ); |
||
119 | |||
120 | $this->addOption( |
||
121 | '-t|--template-prefix', |
||
122 | 'The template prefix', |
||
123 | null, |
||
124 | false |
||
125 | ); |
||
126 | |||
127 | $this->addOption( |
||
128 | '-r|--route-prefix', |
||
129 | 'The route name prefix', |
||
130 | null, |
||
131 | false |
||
132 | ); |
||
133 | |||
134 | $this->addOption( |
||
135 | '-e|--message-not-found', |
||
136 | 'The entity not found error message', |
||
137 | 'This record doesn\'t exist', |
||
138 | false |
||
139 | ); |
||
140 | |||
141 | $this->addOption( |
||
142 | '-l|--message-duplicate', |
||
143 | 'The entity duplicate error message', |
||
144 | 'This record already exist', |
||
145 | false |
||
146 | ); |
||
147 | |||
148 | $this->addOption( |
||
149 | '-a|--message-create', |
||
150 | 'The entity successfully create message', |
||
151 | 'Data successfully created', |
||
152 | false |
||
153 | ); |
||
154 | |||
155 | $this->addOption( |
||
156 | '-u|--message-update', |
||
157 | 'The entity successfully update message', |
||
158 | 'Data successfully updated', |
||
159 | false |
||
160 | ); |
||
161 | |||
162 | $this->addOption( |
||
163 | '-d|--message-delete', |
||
164 | 'The entity successfully delete message', |
||
165 | 'Data successfully deleted', |
||
166 | false |
||
167 | ); |
||
168 | |||
169 | $this->addOption( |
||
170 | '-p|--message-process-error', |
||
171 | 'The entity processing error message', |
||
172 | 'Data processing error', |
||
173 | false |
||
174 | ); |
||
175 | |||
176 | $this->addOption( |
||
177 | '-j|--config', |
||
178 | 'Use JSON config file for options', |
||
179 | null, |
||
180 | false |
||
181 | ); |
||
182 | |||
183 | $this->addOption( |
||
184 | '-b|--entity-context-key', |
||
185 | 'The entity context key name', |
||
186 | 'entity', |
||
187 | false |
||
188 | ); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function interact(Reader $reader, Writer $writer): void |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Record class properties |
||
206 | * @return void |
||
207 | */ |
||
208 | protected function recordProperties(): void |
||
232 | ]; |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Record the resource classes |
||
240 | * @return void |
||
241 | */ |
||
242 | protected function recordResourceClasses(): void |
||
243 | { |
||
244 | $io = $this->io(); |
||
245 | |||
246 | $paramClass = $io->prompt('Enter the form parameter full class name', null); |
||
247 | while (class_exists($paramClass) === false) { |
||
248 | $paramClass = $io->prompt('Class does not exists, please enter the form parameter full class name', null); |
||
249 | } |
||
250 | $this->paramClass = $paramClass; |
||
251 | |||
252 | $validatorClass = $io->prompt('Enter the form validator full class name', null); |
||
253 | while (class_exists($validatorClass) === false) { |
||
254 | $validatorClass = $io->prompt( |
||
255 | 'Class does not exists, please enter the form validator full class name', |
||
256 | null |
||
257 | ); |
||
258 | } |
||
259 | $this->validatorClass = $validatorClass; |
||
260 | |||
261 | $entityClass = $io->prompt('Enter the entity full class name', null); |
||
262 | while (class_exists($entityClass) === false) { |
||
263 | $entityClass = $io->prompt('Class does not exists, please enter the entity full class name', null); |
||
264 | } |
||
265 | $this->entityClass = $entityClass; |
||
266 | |||
267 | $repositoryClass = $io->prompt('Enter the repository full class name', null); |
||
268 | while (class_exists($repositoryClass) === false) { |
||
269 | $repositoryClass = $io->prompt('Class does not exists, please enter the repository full class name', null); |
||
270 | } |
||
271 | $this->repositoryClass = $repositoryClass; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Add new property |
||
276 | * @param class-string $value |
||
277 | * @param string|null $name |
||
278 | * @return $this |
||
279 | */ |
||
280 | protected function addProperty(string $value, ?string $name = null): self |
||
281 | { |
||
282 | $shortClass = $this->getClassBaseName($value); |
||
283 | if ($name === null) { |
||
284 | $name = Str::camel($shortClass, true); |
||
285 | } |
||
286 | |||
287 | //replace "interface", "abstract" |
||
288 | $nameClean = str_ireplace(['interface', 'abstract'], '', $name); |
||
289 | |||
290 | $this->properties[$value] = [ |
||
291 | 'name' => $nameClean, |
||
292 | 'short' => $shortClass, |
||
293 | ]; |
||
294 | |||
295 | return $this; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Return the property name |
||
300 | * @param class-string $value |
||
301 | * @return string |
||
302 | */ |
||
303 | protected function getPropertyName(string $value): string |
||
304 | { |
||
305 | if (!isset($this->properties[$value])) { |
||
306 | return ''; |
||
307 | } |
||
308 | |||
309 | return $this->properties[$value]['name']; |
||
310 | } |
||
311 | |||
312 | |||
313 | /** |
||
314 | * Return the route prefix |
||
315 | * @return string |
||
316 | */ |
||
317 | protected function getTemplatePrefix(): string |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Return the entity context key |
||
330 | * @param bool $isKey |
||
331 | * @return string |
||
332 | */ |
||
333 | protected function getEntityContextKey(bool $isKey = true): string |
||
334 | { |
||
335 | $key = (string) $this->getOptionValue('entityContextKey'); |
||
336 | if (!empty($key)) { |
||
337 | if ($isKey) { |
||
338 | $key = Str::snake($key, '_'); |
||
339 | } else { |
||
340 | $key = Str::camel($key, true); |
||
341 | } |
||
342 | } |
||
343 | |||
344 | return $key; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Return the route prefix |
||
349 | * @return string |
||
350 | */ |
||
351 | protected function getRoutePrefix(): string |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Return the route name |
||
364 | * @param string $value |
||
365 | * @return string |
||
366 | */ |
||
367 | protected function getRouteName(string $value): string |
||
368 | { |
||
369 | $routePrefix = $this->getRoutePrefix(); |
||
370 | return sprintf('%s_%s', $routePrefix, $value); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Return the form parameter method name of the given name |
||
375 | * @param string $field |
||
376 | * @return string |
||
377 | */ |
||
378 | protected function getFormParamMethodName(string $field): string |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Return the message |
||
385 | * @param string $option |
||
386 | * @return string|null |
||
387 | */ |
||
388 | protected function getMessage(string $option): ?string |
||
389 | { |
||
390 | $message = (string) $this->getOptionValue($option); |
||
391 | if (!empty($message)) { |
||
392 | $message = addslashes($message); |
||
393 | } |
||
394 | |||
395 | return $message; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Return the template for form parameter entity field |
||
400 | * @param string $field |
||
401 | * @param string $param |
||
402 | * @param bool $isLast |
||
403 | * @return string |
||
404 | */ |
||
405 | protected function getFormParamEntityFieldTemplate( |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Format option fields |
||
420 | * @param string $values |
||
421 | * @return array<string, string> |
||
422 | */ |
||
423 | protected function formatFields(string $values): array |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * Format fields |
||
444 | * @param array<string, string> $fields |
||
445 | * @param bool $orderField |
||
446 | * @return string |
||
447 | */ |
||
448 | protected function formatFieldStr(array $fields, bool $orderField = false): string |
||
449 | { |
||
450 | $result = ''; |
||
451 | foreach ($fields as $field => $param) { |
||
452 | if ($orderField) { |
||
453 | $param = Str::upper($param); |
||
454 | $order = 'ASC'; |
||
455 | if ($param === 'DESC') { |
||
456 | $order = 'DESC'; |
||
457 | } |
||
458 | |||
459 | if ($order === 'ASC') { |
||
460 | $result .= sprintf('\'%s\', ', $field); |
||
461 | } else { |
||
462 | $result .= sprintf('\'%s\' => \'DESC\', ', $field); |
||
463 | } |
||
464 | } else { |
||
465 | if ($field === $param) { |
||
466 | $result .= sprintf('\'%s\', ', $field); |
||
467 | } else { |
||
468 | $result .= sprintf('\'%s\' => \'%s\', ', $field, $param); |
||
469 | } |
||
470 | } |
||
471 | } |
||
472 | |||
473 | return rtrim($result, ', '); |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Load JSON configuration file if exist |
||
478 | * @return void |
||
479 | */ |
||
480 | protected function loadConfig(): void |
||
493 | } |
||
494 | } |
||
495 | } |
||
496 | } |
||
497 | } |
||
498 |