Complex classes like CommandInfo 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 CommandInfo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class CommandInfo |
||
19 | { |
||
20 | /** |
||
21 | * Serialization schema version. Incremented every time the serialization schema changes. |
||
22 | */ |
||
23 | const SERIALIZATION_SCHEMA_VERSION = 3; |
||
24 | |||
25 | /** |
||
26 | * @var \ReflectionMethod |
||
27 | */ |
||
28 | protected $reflection; |
||
29 | |||
30 | /** |
||
31 | * @var boolean |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $docBlockIsParsed = false; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $name; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $description = ''; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $help = ''; |
||
50 | |||
51 | /** |
||
52 | * @var DefaultsWithDescriptions |
||
53 | */ |
||
54 | protected $options; |
||
55 | |||
56 | /** |
||
57 | * @var DefaultsWithDescriptions |
||
58 | */ |
||
59 | protected $arguments; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $exampleUsage = []; |
||
65 | |||
66 | /** |
||
67 | * @var AnnotationData |
||
68 | */ |
||
69 | protected $otherAnnotations; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $aliases = []; |
||
75 | |||
76 | /** |
||
77 | * @var InputOption[] |
||
78 | */ |
||
79 | protected $inputOptions; |
||
80 | |||
81 | /** |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $methodName; |
||
85 | |||
86 | /** |
||
87 | * @var string |
||
88 | */ |
||
89 | protected $returnType; |
||
90 | |||
91 | /** |
||
92 | * @var string[] |
||
93 | */ |
||
94 | protected $injectedClasses = []; |
||
95 | |||
96 | /** |
||
97 | * Create a new CommandInfo class for a particular method of a class. |
||
98 | * |
||
99 | * @param string|mixed $classNameOrInstance The name of a class, or an |
||
100 | * instance of it, or an array of cached data. |
||
101 | * @param string $methodName The name of the method to get info about. |
||
102 | * @param array $cache Cached data |
||
103 | * @deprecated Use CommandInfo::create() or CommandInfo::deserialize() |
||
104 | * instead. In the future, this constructor will be protected. |
||
105 | */ |
||
106 | public function __construct($classNameOrInstance, $methodName, $cache = []) |
||
123 | |||
124 | public static function create($classNameOrInstance, $methodName) |
||
128 | |||
129 | public static function deserialize($cache) |
||
134 | |||
135 | public function cachedFileIsModified($cache) |
||
140 | |||
141 | protected function constructFromClassAndMethod($classNameOrInstance, $methodName) |
||
150 | |||
151 | /** |
||
152 | * Recover the method name provided to the constructor. |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | public function getMethodName() |
||
160 | |||
161 | /** |
||
162 | * Return the primary name for this command. |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | public function getName() |
||
171 | |||
172 | /** |
||
173 | * Set the primary name for this command. |
||
174 | * |
||
175 | * @param string $name |
||
176 | */ |
||
177 | public function setName($name) |
||
182 | |||
183 | /** |
||
184 | * Return whether or not this method represents a valid command |
||
185 | * or hook. |
||
186 | */ |
||
187 | public function valid() |
||
191 | |||
192 | /** |
||
193 | * If higher-level code decides that this CommandInfo is not interesting |
||
194 | * or useful (if it is not a command method or a hook method), then |
||
195 | * we will mark it as invalid to prevent it from being created as a command. |
||
196 | * We still cache a placeholder record for invalid methods, so that we |
||
197 | * do not need to re-parse the method again later simply to determine that |
||
198 | * it is invalid. |
||
199 | */ |
||
200 | public function invalidate() |
||
204 | |||
205 | public function getReturnType() |
||
210 | |||
211 | public function getInjectedClasses() |
||
216 | |||
217 | public function setReturnType($returnType) |
||
222 | |||
223 | /** |
||
224 | * Get any annotations included in the docblock comment for the |
||
225 | * implementation method of this command that are not already |
||
226 | * handled by the primary methods of this class. |
||
227 | * |
||
228 | * @return AnnotationData |
||
229 | */ |
||
230 | public function getRawAnnotations() |
||
235 | |||
236 | /** |
||
237 | * Replace the annotation data. |
||
238 | */ |
||
239 | public function replaceRawAnnotations($annotationData) |
||
244 | |||
245 | /** |
||
246 | * Get any annotations included in the docblock comment, |
||
247 | * also including default values such as @command. We add |
||
248 | * in the default @command annotation late, and only in a |
||
249 | * copy of the annotation data because we use the existance |
||
250 | * of a @command to indicate that this CommandInfo is |
||
251 | * a command, and not a hook or anything else. |
||
252 | * |
||
253 | * @return AnnotationData |
||
254 | */ |
||
255 | public function getAnnotations() |
||
270 | |||
271 | /** |
||
272 | * Return a specific named annotation for this command as a list. |
||
273 | * |
||
274 | * @param string $name The name of the annotation. |
||
275 | * @return array|null |
||
276 | */ |
||
277 | public function getAnnotationList($name) |
||
286 | |||
287 | /** |
||
288 | * Return a specific named annotation for this command as a string. |
||
289 | * |
||
290 | * @param string $name The name of the annotation. |
||
291 | * @return string|null |
||
292 | */ |
||
293 | public function getAnnotation($name) |
||
301 | |||
302 | /** |
||
303 | * Check to see if the specified annotation exists for this command. |
||
304 | * |
||
305 | * @param string $annotation The name of the annotation. |
||
306 | * @return boolean |
||
307 | */ |
||
308 | public function hasAnnotation($annotation) |
||
313 | |||
314 | /** |
||
315 | * Save any tag that we do not explicitly recognize in the |
||
316 | * 'otherAnnotations' map. |
||
317 | */ |
||
318 | public function addAnnotation($name, $content) |
||
327 | |||
328 | /** |
||
329 | * Remove an annotation that was previoudly set. |
||
330 | */ |
||
331 | public function removeAnnotation($name) |
||
335 | |||
336 | /** |
||
337 | * Get the synopsis of the command (~first line). |
||
338 | * |
||
339 | * @return string |
||
340 | */ |
||
341 | public function getDescription() |
||
346 | |||
347 | /** |
||
348 | * Set the command description. |
||
349 | * |
||
350 | * @param string $description The description to set. |
||
351 | */ |
||
352 | public function setDescription($description) |
||
357 | |||
358 | /** |
||
359 | * Get the help text of the command (the description) |
||
360 | */ |
||
361 | public function getHelp() |
||
366 | /** |
||
367 | * Set the help text for this command. |
||
368 | * |
||
369 | * @param string $help The help text. |
||
370 | */ |
||
371 | public function setHelp($help) |
||
376 | |||
377 | /** |
||
378 | * Return the list of aliases for this command. |
||
379 | * @return string[] |
||
380 | */ |
||
381 | public function getAliases() |
||
386 | |||
387 | /** |
||
388 | * Set aliases that can be used in place of the command's primary name. |
||
389 | * |
||
390 | * @param string|string[] $aliases |
||
391 | */ |
||
392 | public function setAliases($aliases) |
||
400 | |||
401 | /** |
||
402 | * Get hidden status for the command. |
||
403 | * @return bool |
||
404 | */ |
||
405 | public function getHidden() |
||
410 | |||
411 | /** |
||
412 | * Set hidden status. List command omits hidden commands. |
||
413 | * |
||
414 | * @param bool $hidden |
||
415 | */ |
||
416 | public function setHidden($hidden) |
||
421 | |||
422 | /** |
||
423 | * Return the examples for this command. This is @usage instead of |
||
424 | * @example because the later is defined by the phpdoc standard to |
||
425 | * be example method calls. |
||
426 | * |
||
427 | * @return string[] |
||
428 | */ |
||
429 | public function getExampleUsages() |
||
434 | |||
435 | /** |
||
436 | * Add an example usage for this command. |
||
437 | * |
||
438 | * @param string $usage An example of the command, including the command |
||
439 | * name and all of its example arguments and options. |
||
440 | * @param string $description An explanation of what the example does. |
||
441 | */ |
||
442 | public function setExampleUsage($usage, $description) |
||
447 | |||
448 | /** |
||
449 | * Overwrite all example usages |
||
450 | */ |
||
451 | public function replaceExampleUsages($usages) |
||
456 | |||
457 | /** |
||
458 | * Return the topics for this command. |
||
459 | * |
||
460 | * @return string[] |
||
461 | */ |
||
462 | public function getTopics() |
||
470 | |||
471 | /** |
||
472 | * Return the list of refleaction parameters. |
||
473 | * |
||
474 | * @return ReflectionParameter[] |
||
475 | */ |
||
476 | public function getParameters() |
||
480 | |||
481 | /** |
||
482 | * Descriptions of commandline arguements for this command. |
||
483 | * |
||
484 | * @return DefaultsWithDescriptions |
||
485 | */ |
||
486 | public function arguments() |
||
490 | |||
491 | /** |
||
492 | * Descriptions of commandline options for this command. |
||
493 | * |
||
494 | * @return DefaultsWithDescriptions |
||
495 | */ |
||
496 | public function options() |
||
500 | |||
501 | /** |
||
502 | * Get the inputOptions for the options associated with this CommandInfo |
||
503 | * object, e.g. via @option annotations, or from |
||
504 | * $options = ['someoption' => 'defaultvalue'] in the command method |
||
505 | * parameter list. |
||
506 | * |
||
507 | * @return InputOption[] |
||
508 | */ |
||
509 | public function inputOptions() |
||
516 | |||
517 | protected function addImplicitNoOptions() |
||
530 | |||
531 | protected function createInputOptions() |
||
575 | |||
576 | /** |
||
577 | * An option might have a name such as 'silent|s'. In this |
||
578 | * instance, we will allow the @option or @default tag to |
||
579 | * reference the option only by name (e.g. 'silent' or 's' |
||
580 | * instead of 'silent|s'). |
||
581 | * |
||
582 | * @param string $optionName |
||
583 | * @return string |
||
584 | */ |
||
585 | public function findMatchingOption($optionName) |
||
597 | |||
598 | /** |
||
599 | * @param string $optionName |
||
600 | * @return string |
||
601 | */ |
||
602 | protected function findOptionAmongAlternatives($optionName) |
||
617 | |||
618 | /** |
||
619 | * @param string $optionName |
||
620 | * @return string|null |
||
621 | */ |
||
622 | protected function findExistingOption($optionName) |
||
633 | |||
634 | /** |
||
635 | * Examine the parameters of the method for this command, and |
||
636 | * build a list of commandline arguements for them. |
||
637 | * |
||
638 | * @return array |
||
639 | */ |
||
640 | protected function determineAgumentClassifications() |
||
658 | |||
659 | /** |
||
660 | * Examine the provided parameter, and determine whether it |
||
661 | * is a parameter that will be filled in with a positional |
||
662 | * commandline argument. |
||
663 | */ |
||
664 | protected function addParameterToResult($result, $param) |
||
681 | |||
682 | /** |
||
683 | * Examine the parameters of the method for this command, and determine |
||
684 | * the disposition of the options from them. |
||
685 | * |
||
686 | * @return array |
||
687 | */ |
||
688 | protected function determineOptionsFromParameters() |
||
703 | |||
704 | /** |
||
705 | * Determine if the last argument contains $options. |
||
706 | * |
||
707 | * Two forms indicate options: |
||
708 | * - $options = [] |
||
709 | * - $options = ['flag' => 'default-value'] |
||
710 | * |
||
711 | * Any other form, including `array $foo`, is not options. |
||
712 | */ |
||
713 | protected function lastParameterIsOptionsArray() |
||
725 | |||
726 | /** |
||
727 | * Helper; determine if an array is associative or not. An array |
||
728 | * is not associative if its keys are numeric, and numbered sequentially |
||
729 | * from zero. All other arrays are considered to be associative. |
||
730 | * |
||
731 | * @param array $arr The array |
||
732 | * @return boolean |
||
733 | */ |
||
734 | protected function isAssoc($arr) |
||
741 | |||
742 | /** |
||
743 | * Convert from a method name to the corresponding command name. A |
||
744 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
745 | * become 'foo:bar-baz-boz'. |
||
746 | * |
||
747 | * @param string $camel method name. |
||
748 | * @return string |
||
749 | */ |
||
750 | protected function convertName($camel) |
||
757 | |||
758 | /** |
||
759 | * Parse the docBlock comment for this command, and set the |
||
760 | * fields of this class with the data thereby obtained. |
||
761 | */ |
||
762 | protected function parseDocBlock() |
||
771 | |||
772 | /** |
||
773 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
774 | * convert the data into the last of these forms. |
||
775 | */ |
||
776 | protected static function convertListToCommaSeparated($text) |
||
780 | } |
||
781 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.