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 | * @var \ReflectionMethod |
||
22 | */ |
||
23 | protected $reflection; |
||
24 | |||
25 | /** |
||
26 | * @var boolean |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $docBlockIsParsed; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $name; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $description = ''; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $help = ''; |
||
45 | |||
46 | /** |
||
47 | * @var DefaultsWithDescriptions |
||
48 | */ |
||
49 | protected $options; |
||
50 | |||
51 | /** |
||
52 | * @var DefaultsWithDescriptions |
||
53 | */ |
||
54 | protected $arguments; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $exampleUsage = []; |
||
60 | |||
61 | /** |
||
62 | * @var AnnotationData |
||
63 | */ |
||
64 | protected $otherAnnotations; |
||
65 | |||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $aliases = []; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $methodName; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $returnType; |
||
80 | |||
81 | /** |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $optionParamName; |
||
85 | |||
86 | /** |
||
87 | * Create a new CommandInfo class for a particular method of a class. |
||
88 | * |
||
89 | * @param string|mixed $classNameOrInstance The name of a class, or an |
||
90 | * instance of it. |
||
91 | * @param string $methodName The name of the method to get info about. |
||
92 | */ |
||
93 | public function __construct($classNameOrInstance, $methodName) |
||
109 | |||
110 | /** |
||
111 | * Recover the method name provided to the constructor. |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | public function getMethodName() |
||
119 | |||
120 | /** |
||
121 | * Return the primary name for this command. |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getName() |
||
130 | |||
131 | /** |
||
132 | * Set the primary name for this command. |
||
133 | * |
||
134 | * @param string $name |
||
135 | */ |
||
136 | public function setName($name) |
||
140 | |||
141 | public function getReturnType() |
||
146 | |||
147 | public function setReturnType($returnType) |
||
151 | |||
152 | /** |
||
153 | * Get any annotations included in the docblock comment for the |
||
154 | * implementation method of this command that are not already |
||
155 | * handled by the primary methods of this class. |
||
156 | * |
||
157 | * @return AnnotationData |
||
158 | */ |
||
159 | public function getRawAnnotations() |
||
160 | { |
||
161 | $this->parseDocBlock(); |
||
162 | return $this->otherAnnotations; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Get any annotations included in the docblock comment, |
||
167 | * also including default values such as @command. We add |
||
168 | * in the default @command annotation late, and only in a |
||
169 | * copy of the annotation data because we use the existance |
||
170 | * of a @command to indicate that this CommandInfo is |
||
171 | * a command, and not a hook or anything else. |
||
172 | * |
||
173 | * @return AnnotationData |
||
174 | */ |
||
175 | public function getAnnotations() |
||
176 | { |
||
177 | return new AnnotationData( |
||
178 | $this->getRawAnnotations()->getArrayCopy() + |
||
179 | [ |
||
180 | 'command' => $this->getName(), |
||
181 | ] |
||
182 | ); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Return a specific named annotation for this command. |
||
187 | * |
||
188 | * @param string $annotation The name of the annotation. |
||
189 | * @return string |
||
190 | */ |
||
191 | public function getAnnotation($annotation) |
||
199 | |||
200 | /** |
||
201 | * Check to see if the specified annotation exists for this command. |
||
202 | * |
||
203 | * @param string $annotation The name of the annotation. |
||
204 | * @return boolean |
||
205 | */ |
||
206 | public function hasAnnotation($annotation) |
||
211 | |||
212 | /** |
||
213 | * Save any tag that we do not explicitly recognize in the |
||
214 | * 'otherAnnotations' map. |
||
215 | */ |
||
216 | public function addAnnotation($name, $content) |
||
220 | |||
221 | /** |
||
222 | * Remove an annotation that was previoudly set. |
||
223 | */ |
||
224 | public function removeAnnotation($name) |
||
228 | |||
229 | /** |
||
230 | * Get the synopsis of the command (~first line). |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | public function getDescription() |
||
239 | |||
240 | /** |
||
241 | * Set the command description. |
||
242 | * |
||
243 | * @param string $description The description to set. |
||
244 | */ |
||
245 | public function setDescription($description) |
||
249 | |||
250 | /** |
||
251 | * Get the help text of the command (the description) |
||
252 | */ |
||
253 | public function getHelp() |
||
258 | /** |
||
259 | * Set the help text for this command. |
||
260 | * |
||
261 | * @param string $help The help text. |
||
262 | */ |
||
263 | public function setHelp($help) |
||
267 | |||
268 | /** |
||
269 | * Return the list of aliases for this command. |
||
270 | * @return string[] |
||
271 | */ |
||
272 | public function getAliases() |
||
277 | |||
278 | /** |
||
279 | * Set aliases that can be used in place of the command's primary name. |
||
280 | * |
||
281 | * @param string|string[] $aliases |
||
282 | */ |
||
283 | public function setAliases($aliases) |
||
290 | |||
291 | /** |
||
292 | * Return the examples for this command. This is @usage instead of |
||
293 | * @example because the later is defined by the phpdoc standard to |
||
294 | * be example method calls. |
||
295 | * |
||
296 | * @return string[] |
||
297 | */ |
||
298 | public function getExampleUsages() |
||
303 | |||
304 | /** |
||
305 | * Add an example usage for this command. |
||
306 | * |
||
307 | * @param string $usage An example of the command, including the command |
||
308 | * name and all of its example arguments and options. |
||
309 | * @param string $description An explanation of what the example does. |
||
310 | */ |
||
311 | public function setExampleUsage($usage, $description) |
||
315 | |||
316 | /** |
||
317 | * Return the list of refleaction parameters. |
||
318 | * |
||
319 | * @return ReflectionParameter[] |
||
320 | */ |
||
321 | public function getParameters() |
||
325 | |||
326 | /** |
||
327 | * Descriptions of commandline arguements for this command. |
||
328 | * |
||
329 | * @return DefaultsWithDescriptions |
||
330 | */ |
||
331 | public function arguments() |
||
335 | |||
336 | /** |
||
337 | * Descriptions of commandline options for this command. |
||
338 | * |
||
339 | * @return DefaultsWithDescriptions |
||
340 | */ |
||
341 | public function options() |
||
345 | |||
346 | /** |
||
347 | * Return the name of the last parameter if it holds the options. |
||
348 | */ |
||
349 | public function optionParamName() |
||
353 | |||
354 | /** |
||
355 | * Get the inputOptions for the options associated with this CommandInfo |
||
356 | * object, e.g. via @option annotations, or from |
||
357 | * $options = ['someoption' => 'defaultvalue'] in the command method |
||
358 | * parameter list. |
||
359 | * |
||
360 | * @return InputOption[] |
||
361 | */ |
||
362 | public function inputOptions() |
||
387 | |||
388 | /** |
||
389 | * An option might have a name such as 'silent|s'. In this |
||
390 | * instance, we will allow the @option or @default tag to |
||
391 | * reference the option only by name (e.g. 'silent' or 's' |
||
392 | * instead of 'silent|s'). |
||
393 | * |
||
394 | * @param string $optionName |
||
395 | * @return string |
||
396 | */ |
||
397 | public function findMatchingOption($optionName) |
||
409 | |||
410 | /** |
||
411 | * @param string $optionName |
||
412 | * @return string |
||
413 | */ |
||
414 | protected function findOptionAmongAlternatives($optionName) |
||
429 | |||
430 | /** |
||
431 | * @param string $optionName |
||
432 | * @return string|null |
||
433 | */ |
||
434 | protected function findExistingOption($optionName) |
||
445 | |||
446 | /** |
||
447 | * Examine the parameters of the method for this command, and |
||
448 | * build a list of commandline arguements for them. |
||
449 | * |
||
450 | * @return array |
||
451 | */ |
||
452 | protected function determineAgumentClassifications() |
||
465 | |||
466 | /** |
||
467 | * Examine the provided parameter, and determine whether it |
||
468 | * is a parameter that will be filled in with a positional |
||
469 | * commandline argument. |
||
470 | */ |
||
471 | protected function addParameterToResult($result, $param) |
||
488 | |||
489 | /** |
||
490 | * Examine the parameters of the method for this command, and determine |
||
491 | * the disposition of the options from them. |
||
492 | * |
||
493 | * @return array |
||
494 | */ |
||
495 | protected function determineOptionsFromParameters() |
||
510 | |||
511 | protected function lastParameterName() |
||
520 | |||
521 | /** |
||
522 | * Helper; determine if an array is associative or not. An array |
||
523 | * is not associative if its keys are numeric, and numbered sequentially |
||
524 | * from zero. All other arrays are considered to be associative. |
||
525 | * |
||
526 | * @param arrau $arr The array |
||
527 | * @return boolean |
||
528 | */ |
||
529 | protected function isAssoc($arr) |
||
536 | |||
537 | /** |
||
538 | * Convert from a method name to the corresponding command name. A |
||
539 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
540 | * become 'foo:bar-baz-boz'. |
||
541 | * |
||
542 | * @param string $camel method name. |
||
543 | * @return string |
||
544 | */ |
||
545 | protected function convertName($camel) |
||
552 | |||
553 | /** |
||
554 | * Parse the docBlock comment for this command, and set the |
||
555 | * fields of this class with the data thereby obtained. |
||
556 | */ |
||
557 | protected function parseDocBlock() |
||
566 | |||
567 | /** |
||
568 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
569 | * convert the data into the last of these forms. |
||
570 | */ |
||
571 | protected static function convertListToCommaSeparated($text) |
||
575 | } |
||
576 |