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 | * Create a new CommandInfo class for a particular method of a class. |
||
93 | * |
||
94 | * @param string|mixed $classNameOrInstance The name of a class, or an |
||
95 | * instance of it, or an array of cached data. |
||
96 | * @param string $methodName The name of the method to get info about. |
||
97 | * @param array $cache Cached data |
||
98 | * @deprecated Use CommandInfo::create() or CommandInfo::deserialize() |
||
99 | * instead. In the future, this constructor will be protected. |
||
100 | */ |
||
101 | public function __construct($classNameOrInstance, $methodName, $cache = []) |
||
118 | |||
119 | public static function create($classNameOrInstance, $methodName) |
||
123 | |||
124 | public static function deserialize($cache) |
||
129 | |||
130 | public function cachedFileIsModified($cache) |
||
135 | |||
136 | protected function constructFromClassAndMethod($classNameOrInstance, $methodName) |
||
145 | |||
146 | /** |
||
147 | * Recover the method name provided to the constructor. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getMethodName() |
||
155 | |||
156 | /** |
||
157 | * Return the primary name for this command. |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function getName() |
||
166 | |||
167 | /** |
||
168 | * Set the primary name for this command. |
||
169 | * |
||
170 | * @param string $name |
||
171 | */ |
||
172 | public function setName($name) |
||
177 | |||
178 | /** |
||
179 | * Return whether or not this method represents a valid command |
||
180 | * or hook. |
||
181 | */ |
||
182 | public function valid() |
||
186 | |||
187 | /** |
||
188 | * If higher-level code decides that this CommandInfo is not interesting |
||
189 | * or useful (if it is not a command method or a hook method), then |
||
190 | * we will mark it as invalid to prevent it from being created as a command. |
||
191 | * We still cache a placeholder record for invalid methods, so that we |
||
192 | * do not need to re-parse the method again later simply to determine that |
||
193 | * it is invalid. |
||
194 | */ |
||
195 | public function invalidate() |
||
199 | |||
200 | public function getReturnType() |
||
205 | |||
206 | public function setReturnType($returnType) |
||
211 | |||
212 | /** |
||
213 | * Get any annotations included in the docblock comment for the |
||
214 | * implementation method of this command that are not already |
||
215 | * handled by the primary methods of this class. |
||
216 | * |
||
217 | * @return AnnotationData |
||
218 | */ |
||
219 | public function getRawAnnotations() |
||
224 | |||
225 | /** |
||
226 | * Replace the annotation data. |
||
227 | */ |
||
228 | public function replaceRawAnnotations($annotationData) |
||
233 | |||
234 | /** |
||
235 | * Get any annotations included in the docblock comment, |
||
236 | * also including default values such as @command. We add |
||
237 | * in the default @command annotation late, and only in a |
||
238 | * copy of the annotation data because we use the existance |
||
239 | * of a @command to indicate that this CommandInfo is |
||
240 | * a command, and not a hook or anything else. |
||
241 | * |
||
242 | * @return AnnotationData |
||
243 | */ |
||
244 | public function getAnnotations() |
||
259 | |||
260 | /** |
||
261 | * Return a specific named annotation for this command as a list. |
||
262 | * |
||
263 | * @param string $name The name of the annotation. |
||
264 | * @return array|null |
||
265 | */ |
||
266 | public function getAnnotationList($name) |
||
275 | |||
276 | /** |
||
277 | * Return a specific named annotation for this command as a string. |
||
278 | * |
||
279 | * @param string $name The name of the annotation. |
||
280 | * @return string|null |
||
281 | */ |
||
282 | public function getAnnotation($name) |
||
290 | |||
291 | /** |
||
292 | * Check to see if the specified annotation exists for this command. |
||
293 | * |
||
294 | * @param string $annotation The name of the annotation. |
||
295 | * @return boolean |
||
296 | */ |
||
297 | public function hasAnnotation($annotation) |
||
302 | |||
303 | /** |
||
304 | * Save any tag that we do not explicitly recognize in the |
||
305 | * 'otherAnnotations' map. |
||
306 | */ |
||
307 | public function addAnnotation($name, $content) |
||
316 | |||
317 | /** |
||
318 | * Remove an annotation that was previoudly set. |
||
319 | */ |
||
320 | public function removeAnnotation($name) |
||
324 | |||
325 | /** |
||
326 | * Get the synopsis of the command (~first line). |
||
327 | * |
||
328 | * @return string |
||
329 | */ |
||
330 | public function getDescription() |
||
335 | |||
336 | /** |
||
337 | * Set the command description. |
||
338 | * |
||
339 | * @param string $description The description to set. |
||
340 | */ |
||
341 | public function setDescription($description) |
||
346 | |||
347 | /** |
||
348 | * Get the help text of the command (the description) |
||
349 | */ |
||
350 | public function getHelp() |
||
355 | /** |
||
356 | * Set the help text for this command. |
||
357 | * |
||
358 | * @param string $help The help text. |
||
359 | */ |
||
360 | public function setHelp($help) |
||
365 | |||
366 | /** |
||
367 | * Return the list of aliases for this command. |
||
368 | * @return string[] |
||
369 | */ |
||
370 | public function getAliases() |
||
375 | |||
376 | /** |
||
377 | * Set aliases that can be used in place of the command's primary name. |
||
378 | * |
||
379 | * @param string|string[] $aliases |
||
380 | */ |
||
381 | public function setAliases($aliases) |
||
389 | |||
390 | /** |
||
391 | * Return the examples for this command. This is @usage instead of |
||
392 | * @example because the later is defined by the phpdoc standard to |
||
393 | * be example method calls. |
||
394 | * |
||
395 | * @return string[] |
||
396 | */ |
||
397 | public function getExampleUsages() |
||
402 | |||
403 | /** |
||
404 | * Add an example usage for this command. |
||
405 | * |
||
406 | * @param string $usage An example of the command, including the command |
||
407 | * name and all of its example arguments and options. |
||
408 | * @param string $description An explanation of what the example does. |
||
409 | */ |
||
410 | public function setExampleUsage($usage, $description) |
||
415 | |||
416 | /** |
||
417 | * Overwrite all example usages |
||
418 | */ |
||
419 | public function replaceExampleUsages($usages) |
||
424 | |||
425 | /** |
||
426 | * Return the topics for this command. |
||
427 | * |
||
428 | * @return string[] |
||
429 | */ |
||
430 | public function getTopics() |
||
438 | |||
439 | /** |
||
440 | * Return the list of refleaction parameters. |
||
441 | * |
||
442 | * @return ReflectionParameter[] |
||
443 | */ |
||
444 | public function getParameters() |
||
448 | |||
449 | /** |
||
450 | * Descriptions of commandline arguements for this command. |
||
451 | * |
||
452 | * @return DefaultsWithDescriptions |
||
453 | */ |
||
454 | public function arguments() |
||
458 | |||
459 | /** |
||
460 | * Descriptions of commandline options for this command. |
||
461 | * |
||
462 | * @return DefaultsWithDescriptions |
||
463 | */ |
||
464 | public function options() |
||
468 | |||
469 | /** |
||
470 | * Get the inputOptions for the options associated with this CommandInfo |
||
471 | * object, e.g. via @option annotations, or from |
||
472 | * $options = ['someoption' => 'defaultvalue'] in the command method |
||
473 | * parameter list. |
||
474 | * |
||
475 | * @return InputOption[] |
||
476 | */ |
||
477 | public function inputOptions() |
||
484 | |||
485 | protected function createInputOptions() |
||
519 | |||
520 | /** |
||
521 | * An option might have a name such as 'silent|s'. In this |
||
522 | * instance, we will allow the @option or @default tag to |
||
523 | * reference the option only by name (e.g. 'silent' or 's' |
||
524 | * instead of 'silent|s'). |
||
525 | * |
||
526 | * @param string $optionName |
||
527 | * @return string |
||
528 | */ |
||
529 | public function findMatchingOption($optionName) |
||
541 | |||
542 | /** |
||
543 | * @param string $optionName |
||
544 | * @return string |
||
545 | */ |
||
546 | protected function findOptionAmongAlternatives($optionName) |
||
561 | |||
562 | /** |
||
563 | * @param string $optionName |
||
564 | * @return string|null |
||
565 | */ |
||
566 | protected function findExistingOption($optionName) |
||
577 | |||
578 | /** |
||
579 | * Examine the parameters of the method for this command, and |
||
580 | * build a list of commandline arguements for them. |
||
581 | * |
||
582 | * @return array |
||
583 | */ |
||
584 | protected function determineAgumentClassifications() |
||
597 | |||
598 | /** |
||
599 | * Examine the provided parameter, and determine whether it |
||
600 | * is a parameter that will be filled in with a positional |
||
601 | * commandline argument. |
||
602 | */ |
||
603 | protected function addParameterToResult($result, $param) |
||
620 | |||
621 | /** |
||
622 | * Examine the parameters of the method for this command, and determine |
||
623 | * the disposition of the options from them. |
||
624 | * |
||
625 | * @return array |
||
626 | */ |
||
627 | protected function determineOptionsFromParameters() |
||
642 | |||
643 | /** |
||
644 | * Helper; determine if an array is associative or not. An array |
||
645 | * is not associative if its keys are numeric, and numbered sequentially |
||
646 | * from zero. All other arrays are considered to be associative. |
||
647 | * |
||
648 | * @param array $arr The array |
||
649 | * @return boolean |
||
650 | */ |
||
651 | protected function isAssoc($arr) |
||
658 | |||
659 | /** |
||
660 | * Convert from a method name to the corresponding command name. A |
||
661 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
662 | * become 'foo:bar-baz-boz'. |
||
663 | * |
||
664 | * @param string $camel method name. |
||
665 | * @return string |
||
666 | */ |
||
667 | protected function convertName($camel) |
||
674 | |||
675 | /** |
||
676 | * Parse the docBlock comment for this command, and set the |
||
677 | * fields of this class with the data thereby obtained. |
||
678 | */ |
||
679 | protected function parseDocBlock() |
||
688 | |||
689 | /** |
||
690 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
691 | * convert the data into the last of these forms. |
||
692 | */ |
||
693 | protected static function convertListToCommaSeparated($text) |
||
697 | } |
||
698 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.