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 |
||
9 | class CommandInfo |
||
10 | { |
||
11 | /** |
||
12 | * @var \ReflectionMethod |
||
13 | */ |
||
14 | protected $reflection; |
||
15 | |||
16 | /** |
||
17 | * @var boolean |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $docBlockIsParsed; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $name; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $description = ''; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $help = ''; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $options = []; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $arguments = []; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $argumentDescriptions = []; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $optionDescriptions = []; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $exampleUsage = []; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $otherAnnotations = []; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $aliases = []; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $methodName; |
||
76 | |||
77 | /** |
||
78 | * Create a new CommandInfo class for a particular method of a class. |
||
79 | * |
||
80 | * @param string|mixed $classNameOrInstance The name of a class, or an |
||
81 | * instance of it. |
||
82 | * @param string $methodName The name of the method to get info about. |
||
83 | */ |
||
84 | public function __construct($classNameOrInstance, $methodName) |
||
94 | |||
95 | /** |
||
96 | * Recover the method name provided to the constructor. |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | public function getMethodName() |
||
104 | |||
105 | /** |
||
106 | * Return the list of refleaction parameters. |
||
107 | * |
||
108 | * @return ReflectionParameter[] |
||
109 | */ |
||
110 | public function getParameters() |
||
114 | |||
115 | /** |
||
116 | * Get the synopsis of the command (~first line). |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getDescription() |
||
125 | |||
126 | /** |
||
127 | * Set the command description. |
||
128 | * |
||
129 | * @param string $description The description to set. |
||
130 | */ |
||
131 | public function setDescription($description) |
||
135 | |||
136 | /** |
||
137 | * Get the help text of the command (the description) |
||
138 | */ |
||
139 | public function getHelp() |
||
144 | /** |
||
145 | * Set the help text for this command. |
||
146 | * |
||
147 | * @param string $help The help text. |
||
148 | */ |
||
149 | public function setHelp($help) |
||
153 | |||
154 | /** |
||
155 | * Return the list of aliases for this command. |
||
156 | * @return string[] |
||
157 | */ |
||
158 | public function getAliases() |
||
163 | |||
164 | /** |
||
165 | * Set aliases that can be used in place of the command's primary name. |
||
166 | * |
||
167 | * @param string|string[] $aliases |
||
168 | */ |
||
169 | public function setAliases($aliases) |
||
176 | |||
177 | /** |
||
178 | * Return the examples for this command. This is @usage instead of |
||
179 | * @example because the later is defined by the phpdoc standard to |
||
180 | * be example method calls. |
||
181 | * |
||
182 | * @return string[] |
||
183 | */ |
||
184 | public function getExampleUsages() |
||
189 | |||
190 | /** |
||
191 | * Return the primary name for this command. |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | public function getName() |
||
200 | |||
201 | /** |
||
202 | * Set the primary name for this command. |
||
203 | * |
||
204 | * @param string $name |
||
205 | */ |
||
206 | public function setName($name) |
||
210 | |||
211 | /** |
||
212 | * Examine the parameters of the method for this command, and |
||
213 | * build a list of commandline arguements for them. |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | protected function determineAgumentClassifications() |
||
232 | |||
233 | /** |
||
234 | * Return the commandline arguments for this command. The key |
||
235 | * contains the name of the argument, and the value contains its |
||
236 | * default value. Required commands have a 'null' value. |
||
237 | * |
||
238 | * @return array |
||
239 | */ |
||
240 | public function getArguments() |
||
244 | |||
245 | /** |
||
246 | * Check to see if an argument with the specified name exits. |
||
247 | * |
||
248 | * @param string $name Argument to test for. |
||
249 | * @return boolean |
||
250 | */ |
||
251 | public function hasArgument($name) |
||
255 | |||
256 | /** |
||
257 | * Set the default value for an argument. A default value of 'null' |
||
258 | * indicates that the argument is required. |
||
259 | * |
||
260 | * @param string $name Name of argument to modify. |
||
261 | * @param string $defaultValue New default value for that argument. |
||
262 | */ |
||
263 | public function setArgumentDefaultValue($name, $defaultValue) |
||
267 | |||
268 | /** |
||
269 | * Add another argument to this command. |
||
270 | * |
||
271 | * @param string $name Name of the argument. |
||
272 | * @param string $description Help text for the argument. |
||
273 | * @param string $defaultValue The default value for the argument. |
||
274 | */ |
||
275 | public function addArgument($name, $description, $defaultValue = null) |
||
285 | |||
286 | /** |
||
287 | * Examine the provided parameter, and determine whether it |
||
288 | * is a parameter that will be filled in with a positional |
||
289 | * commandline argument. |
||
290 | * |
||
291 | * @return false|null|string|array |
||
292 | */ |
||
293 | protected function getArgumentClassification($param) |
||
312 | |||
313 | /** |
||
314 | * Return the options for is command. The key is the options name, |
||
315 | * and the value is its default value. |
||
316 | * |
||
317 | * @return array |
||
318 | */ |
||
319 | public function getOptions() |
||
323 | |||
324 | /** |
||
325 | * Check to see if the specified option exists. |
||
326 | * |
||
327 | * @param string $name Name of the option to check. |
||
328 | * @return boolean |
||
329 | */ |
||
330 | public function hasOption($name) |
||
334 | |||
335 | /** |
||
336 | * Change the default value for an option. |
||
337 | * |
||
338 | * @param string $name Option name. |
||
339 | * @param string $defaultValue Option default value. |
||
340 | */ |
||
341 | public function setOptionDefaultValue($name, $defaultValue) |
||
345 | |||
346 | /** |
||
347 | * Add another option to this command. |
||
348 | * |
||
349 | * @param string $name Option name. |
||
350 | * @param string $description Option description. |
||
351 | * @param string $defaultValue Option default value. |
||
352 | */ |
||
353 | public function addOption($name, $description, $defaultValue = false) |
||
354 | { |
||
355 | if (!$this->hasOption($name) || ($defaultValue !== false)) { |
||
356 | $this->options[$name] = $defaultValue; |
||
357 | } |
||
358 | unset($this->optionDescriptions[$name]); |
||
359 | if (isset($description)) { |
||
360 | $this->optionDescriptions[$name] = $description; |
||
361 | } |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Examine the parameters of the method for this command, and determine |
||
366 | * the disposition of the options from them. |
||
367 | * |
||
368 | * @return array |
||
369 | */ |
||
370 | public function determineOptionsFromParameters() |
||
385 | |||
386 | /** |
||
387 | * Get the description of one argument. |
||
388 | * |
||
389 | * @param string $name The name of the argument. |
||
390 | * @return string |
||
391 | */ |
||
392 | public function getArgumentDescription($name) |
||
401 | |||
402 | /** |
||
403 | * Get the description of one argument. |
||
404 | * |
||
405 | * @param string $name The name of the option. |
||
406 | * @return string |
||
407 | */ |
||
408 | public function getOptionDescription($name) |
||
417 | |||
418 | /** |
||
419 | * Helper; determine if an array is associative or not. An array |
||
420 | * is not associative if its keys are numeric, and numbered sequentially |
||
421 | * from zero. All other arrays are considered to be associative. |
||
422 | * |
||
423 | * @param arrau $arr The array |
||
424 | * @return boolean |
||
425 | */ |
||
426 | protected function isAssoc($arr) |
||
433 | |||
434 | /** |
||
435 | * Get any annotations included in the docblock comment for the |
||
436 | * implementation method of this command that are not already |
||
437 | * handled by the primary methods of this class. |
||
438 | * |
||
439 | * @return array |
||
440 | */ |
||
441 | public function getAnnotations() |
||
446 | |||
447 | /** |
||
448 | * Return a specific named annotation for this command. |
||
449 | * |
||
450 | * @param string $annotation The name of the annotation. |
||
451 | * @return string |
||
452 | */ |
||
453 | public function getAnnotation($annotation) |
||
461 | |||
462 | /** |
||
463 | * Check to see if the specified annotation exists for this command. |
||
464 | * |
||
465 | * @param string $annotation The name of the annotation. |
||
466 | * @return boolean |
||
467 | */ |
||
468 | public function hasAnnotation($annotation) |
||
473 | |||
474 | /** |
||
475 | * Convert from a method name to the corresponding command name. A |
||
476 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
477 | * become 'foo:bar-baz-boz'. |
||
478 | * |
||
479 | * @param type $camel method name. |
||
480 | * @return string |
||
481 | */ |
||
482 | protected function convertName($camel) |
||
489 | |||
490 | /** |
||
491 | * Add an example usage for this command. |
||
492 | * |
||
493 | * @param string $usage An example of the command, including the command |
||
494 | * name and all of its example arguments and options. |
||
495 | * @param string $description An explanation of what the example does. |
||
496 | */ |
||
497 | public function setExampleUsage($usage, $description) |
||
501 | |||
502 | /** |
||
503 | * Parse the docBlock comment for this command, and set the |
||
504 | * fields of this class with the data thereby obtained. |
||
505 | */ |
||
506 | protected function parseDocBlock() |
||
515 | |||
516 | /** |
||
517 | * Save any tag that we do not explicitly recognize in the |
||
518 | * 'otherAnnotations' map. |
||
519 | */ |
||
520 | public function addOtherAnnotation($name, $content) |
||
524 | |||
525 | /** |
||
526 | * An option might have a name such as 'silent|s'. In this |
||
527 | * instance, we will allow the @option or @default tag to |
||
528 | * reference the option only by name (e.g. 'silent' or 's' |
||
529 | * instead of 'silent|s'). |
||
530 | */ |
||
531 | public function findMatchingOption($optionName) |
||
559 | |||
560 | /** |
||
561 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
562 | * convert the data into the last of these forms. |
||
563 | */ |
||
564 | protected static function convertListToCommaSeparated($text) |
||
568 | } |
||
569 |