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 = 4; |
||
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 setInjectedClasses($injectedClasses) |
||
222 | |||
223 | public function setReturnType($returnType) |
||
228 | |||
229 | /** |
||
230 | * Get any annotations included in the docblock comment for the |
||
231 | * implementation method of this command that are not already |
||
232 | * handled by the primary methods of this class. |
||
233 | * |
||
234 | * @return AnnotationData |
||
235 | */ |
||
236 | public function getRawAnnotations() |
||
241 | |||
242 | /** |
||
243 | * Replace the annotation data. |
||
244 | */ |
||
245 | public function replaceRawAnnotations($annotationData) |
||
250 | |||
251 | /** |
||
252 | * Get any annotations included in the docblock comment, |
||
253 | * also including default values such as @command. We add |
||
254 | * in the default @command annotation late, and only in a |
||
255 | * copy of the annotation data because we use the existance |
||
256 | * of a @command to indicate that this CommandInfo is |
||
257 | * a command, and not a hook or anything else. |
||
258 | * |
||
259 | * @return AnnotationData |
||
260 | */ |
||
261 | public function getAnnotations() |
||
276 | |||
277 | /** |
||
278 | * Return a specific named annotation for this command as a list. |
||
279 | * |
||
280 | * @param string $name The name of the annotation. |
||
281 | * @return array|null |
||
282 | */ |
||
283 | public function getAnnotationList($name) |
||
292 | |||
293 | /** |
||
294 | * Return a specific named annotation for this command as a string. |
||
295 | * |
||
296 | * @param string $name The name of the annotation. |
||
297 | * @return string|null |
||
298 | */ |
||
299 | public function getAnnotation($name) |
||
307 | |||
308 | /** |
||
309 | * Check to see if the specified annotation exists for this command. |
||
310 | * |
||
311 | * @param string $annotation The name of the annotation. |
||
312 | * @return boolean |
||
313 | */ |
||
314 | public function hasAnnotation($annotation) |
||
319 | |||
320 | /** |
||
321 | * Save any tag that we do not explicitly recognize in the |
||
322 | * 'otherAnnotations' map. |
||
323 | */ |
||
324 | public function addAnnotation($name, $content) |
||
333 | |||
334 | /** |
||
335 | * Remove an annotation that was previoudly set. |
||
336 | */ |
||
337 | public function removeAnnotation($name) |
||
341 | |||
342 | /** |
||
343 | * Get the synopsis of the command (~first line). |
||
344 | * |
||
345 | * @return string |
||
346 | */ |
||
347 | public function getDescription() |
||
352 | |||
353 | /** |
||
354 | * Set the command description. |
||
355 | * |
||
356 | * @param string $description The description to set. |
||
357 | */ |
||
358 | public function setDescription($description) |
||
363 | |||
364 | /** |
||
365 | * Get the help text of the command (the description) |
||
366 | */ |
||
367 | public function getHelp() |
||
372 | /** |
||
373 | * Set the help text for this command. |
||
374 | * |
||
375 | * @param string $help The help text. |
||
376 | */ |
||
377 | public function setHelp($help) |
||
382 | |||
383 | /** |
||
384 | * Return the list of aliases for this command. |
||
385 | * @return string[] |
||
386 | */ |
||
387 | public function getAliases() |
||
392 | |||
393 | /** |
||
394 | * Set aliases that can be used in place of the command's primary name. |
||
395 | * |
||
396 | * @param string|string[] $aliases |
||
397 | */ |
||
398 | public function setAliases($aliases) |
||
406 | |||
407 | /** |
||
408 | * Get hidden status for the command. |
||
409 | * @return bool |
||
410 | */ |
||
411 | public function getHidden() |
||
416 | |||
417 | /** |
||
418 | * Set hidden status. List command omits hidden commands. |
||
419 | * |
||
420 | * @param bool $hidden |
||
421 | */ |
||
422 | public function setHidden($hidden) |
||
427 | |||
428 | /** |
||
429 | * Return the examples for this command. This is @usage instead of |
||
430 | * @example because the later is defined by the phpdoc standard to |
||
431 | * be example method calls. |
||
432 | * |
||
433 | * @return string[] |
||
434 | */ |
||
435 | public function getExampleUsages() |
||
440 | |||
441 | /** |
||
442 | * Add an example usage for this command. |
||
443 | * |
||
444 | * @param string $usage An example of the command, including the command |
||
445 | * name and all of its example arguments and options. |
||
446 | * @param string $description An explanation of what the example does. |
||
447 | */ |
||
448 | public function setExampleUsage($usage, $description) |
||
453 | |||
454 | /** |
||
455 | * Overwrite all example usages |
||
456 | */ |
||
457 | public function replaceExampleUsages($usages) |
||
462 | |||
463 | /** |
||
464 | * Return the topics for this command. |
||
465 | * |
||
466 | * @return string[] |
||
467 | */ |
||
468 | public function getTopics() |
||
476 | |||
477 | /** |
||
478 | * Return the list of refleaction parameters. |
||
479 | * |
||
480 | * @return ReflectionParameter[] |
||
481 | */ |
||
482 | public function getParameters() |
||
486 | |||
487 | /** |
||
488 | * Descriptions of commandline arguements for this command. |
||
489 | * |
||
490 | * @return DefaultsWithDescriptions |
||
491 | */ |
||
492 | public function arguments() |
||
496 | |||
497 | /** |
||
498 | * Descriptions of commandline options for this command. |
||
499 | * |
||
500 | * @return DefaultsWithDescriptions |
||
501 | */ |
||
502 | public function options() |
||
506 | |||
507 | /** |
||
508 | * Get the inputOptions for the options associated with this CommandInfo |
||
509 | * object, e.g. via @option annotations, or from |
||
510 | * $options = ['someoption' => 'defaultvalue'] in the command method |
||
511 | * parameter list. |
||
512 | * |
||
513 | * @return InputOption[] |
||
514 | */ |
||
515 | public function inputOptions() |
||
522 | |||
523 | protected function addImplicitNoOptions() |
||
536 | |||
537 | protected function createInputOptions() |
||
581 | |||
582 | /** |
||
583 | * An option might have a name such as 'silent|s'. In this |
||
584 | * instance, we will allow the @option or @default tag to |
||
585 | * reference the option only by name (e.g. 'silent' or 's' |
||
586 | * instead of 'silent|s'). |
||
587 | * |
||
588 | * @param string $optionName |
||
589 | * @return string |
||
590 | */ |
||
591 | public function findMatchingOption($optionName) |
||
603 | |||
604 | /** |
||
605 | * @param string $optionName |
||
606 | * @return string |
||
607 | */ |
||
608 | protected function findOptionAmongAlternatives($optionName) |
||
623 | |||
624 | /** |
||
625 | * @param string $optionName |
||
626 | * @return string|null |
||
627 | */ |
||
628 | protected function findExistingOption($optionName) |
||
639 | |||
640 | /** |
||
641 | * Examine the parameters of the method for this command, and |
||
642 | * build a list of commandline arguements for them. |
||
643 | * |
||
644 | * @return array |
||
645 | */ |
||
646 | protected function determineAgumentClassifications() |
||
664 | |||
665 | /** |
||
666 | * Examine the provided parameter, and determine whether it |
||
667 | * is a parameter that will be filled in with a positional |
||
668 | * commandline argument. |
||
669 | */ |
||
670 | protected function addParameterToResult($result, $param) |
||
687 | |||
688 | /** |
||
689 | * Examine the parameters of the method for this command, and determine |
||
690 | * the disposition of the options from them. |
||
691 | * |
||
692 | * @return array |
||
693 | */ |
||
694 | protected function determineOptionsFromParameters() |
||
709 | |||
710 | /** |
||
711 | * Determine if the last argument contains $options. |
||
712 | * |
||
713 | * Two forms indicate options: |
||
714 | * - $options = [] |
||
715 | * - $options = ['flag' => 'default-value'] |
||
716 | * |
||
717 | * Any other form, including `array $foo`, is not options. |
||
718 | */ |
||
719 | protected function lastParameterIsOptionsArray() |
||
731 | |||
732 | /** |
||
733 | * Helper; determine if an array is associative or not. An array |
||
734 | * is not associative if its keys are numeric, and numbered sequentially |
||
735 | * from zero. All other arrays are considered to be associative. |
||
736 | * |
||
737 | * @param array $arr The array |
||
738 | * @return boolean |
||
739 | */ |
||
740 | protected function isAssoc($arr) |
||
747 | |||
748 | /** |
||
749 | * Convert from a method name to the corresponding command name. A |
||
750 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
751 | * become 'foo:bar-baz-boz'. |
||
752 | * |
||
753 | * @param string $camel method name. |
||
754 | * @return string |
||
755 | */ |
||
756 | protected function convertName($camel) |
||
763 | |||
764 | /** |
||
765 | * Parse the docBlock comment for this command, and set the |
||
766 | * fields of this class with the data thereby obtained. |
||
767 | */ |
||
768 | protected function parseDocBlock() |
||
777 | |||
778 | /** |
||
779 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
780 | * convert the data into the last of these forms. |
||
781 | */ |
||
782 | protected static function convertListToCommaSeparated($text) |
||
786 | } |
||
787 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.