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 |
||
12 | class CommandInfo |
||
13 | { |
||
14 | /** |
||
15 | * @var \ReflectionMethod |
||
16 | */ |
||
17 | protected $reflection; |
||
18 | |||
19 | /** |
||
20 | * @var boolean |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $docBlockIsParsed; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $name; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $description = ''; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $help = ''; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $tagProcessors = [ |
||
44 | 'command' => 'processCommandTag', |
||
45 | 'name' => 'processCommandTag', |
||
46 | 'param' => 'processArgumentTag', |
||
47 | 'option' => 'processOptionTag', |
||
48 | 'default' => 'processDefaultTag', |
||
49 | 'aliases' => 'processAliases', |
||
50 | 'usage' => 'processUsageTag', |
||
51 | 'description' => 'processAlternateDescriptionTag', |
||
52 | 'desc' => 'processAlternateDescriptionTag', |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $options = []; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $arguments = []; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $argumentDescriptions = []; |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $optionDescriptions = []; |
||
74 | |||
75 | /** |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $exampleUsage = []; |
||
79 | |||
80 | /** |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $otherAnnotations = []; |
||
84 | |||
85 | /** |
||
86 | * @var array |
||
87 | */ |
||
88 | protected $aliases = []; |
||
89 | |||
90 | /** |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $methodName; |
||
94 | |||
95 | public function __construct($classNameOrInstance, $methodName) |
||
105 | |||
106 | public function getMethodName() |
||
110 | |||
111 | public function getParameters() |
||
115 | |||
116 | /** |
||
117 | * Get the synopsis of the command (~first line). |
||
118 | */ |
||
119 | public function getDescription() |
||
124 | |||
125 | public function setDescription($description) |
||
129 | |||
130 | /** |
||
131 | * Get the help text of the command (the description) |
||
132 | */ |
||
133 | public function getHelp() |
||
138 | |||
139 | public function setHelp($help) |
||
143 | |||
144 | public function getAliases() |
||
149 | |||
150 | public function setAliases($aliases) |
||
157 | |||
158 | public function getExampleUsages() |
||
163 | |||
164 | public function getName() |
||
169 | |||
170 | public function setDefaultName() |
||
174 | |||
175 | protected function determineAgumentClassifications() |
||
190 | |||
191 | public function getArguments() |
||
195 | |||
196 | /** |
||
197 | * Examine the provided parameter, and determine whether it |
||
198 | * is a parameter that will be filled in with a positional |
||
199 | * commandline argument. |
||
200 | * |
||
201 | * @return false|null|string|array |
||
202 | */ |
||
203 | protected function getArgumentClassification($param) |
||
222 | |||
223 | public function getOptions() |
||
227 | |||
228 | public function determineOptionsFromParameters() |
||
243 | |||
244 | public function getArgumentDescription($name) |
||
253 | |||
254 | public function getOptionDescription($name) |
||
263 | |||
264 | protected function isAssoc($arr) |
||
271 | |||
272 | public function getAnnotations() |
||
277 | |||
278 | public function getAnnotation($annotation) |
||
286 | |||
287 | public function hasAnnotation($annotation) |
||
292 | |||
293 | protected function convertName($camel) |
||
300 | |||
301 | /** |
||
302 | * Parse the docBlock comment for this command, and set the |
||
303 | * fields of this class with the data thereby obtained. |
||
304 | */ |
||
305 | protected function parseDocBlock() |
||
326 | |||
327 | /** |
||
328 | * Save any tag that we do not explicitly recognize in the |
||
329 | * 'otherAnnotations' map. |
||
330 | */ |
||
331 | protected function processGenericTag($tag) |
||
335 | |||
336 | /** |
||
337 | * Set the name of the command from a @command or @name annotation. |
||
338 | */ |
||
339 | protected function processCommandTag($tag) |
||
346 | |||
347 | /** |
||
348 | * The @description and @desc annotations may be used in |
||
349 | * place of the synopsis (which we call 'description'). |
||
350 | * This is discouraged. |
||
351 | * |
||
352 | * @deprecated |
||
353 | */ |
||
354 | protected function processAlternateDescriptionTag($tag) |
||
358 | |||
359 | /** |
||
360 | * Store the data from a @param annotation in our argument descriptions. |
||
361 | */ |
||
362 | protected function processArgumentTag($tag) |
||
373 | |||
374 | /** |
||
375 | * Given a docblock description in the form "$variable description", |
||
376 | * return the variable name and description via the 'match' parameter. |
||
377 | */ |
||
378 | protected function pregMatchNameAndDescription($source, &$match) |
||
386 | |||
387 | /** |
||
388 | * Store the data from an @option annotation in our option descriptions. |
||
389 | */ |
||
390 | protected function processOptionTag($tag) |
||
401 | |||
402 | /** |
||
403 | * Store the data from a @default annotation in our argument or option store, |
||
404 | * as appropriate. |
||
405 | */ |
||
406 | protected function processDefaultTag($tag) |
||
419 | |||
420 | protected function interpretDefaultValue($defaultValue) |
||
436 | |||
437 | /** |
||
438 | * Process the comma-separated list of aliases |
||
439 | */ |
||
440 | protected function processAliases($tag) |
||
444 | |||
445 | /** |
||
446 | * Store the data from a @usage annotation in our example usage list. |
||
447 | */ |
||
448 | protected function processUsageTag($tag) |
||
456 | |||
457 | /** |
||
458 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
459 | * convert the data into the last of these forms. |
||
460 | */ |
||
461 | protected static function convertListToCommaSeparated($text) |
||
465 | |||
466 | /** |
||
467 | * Take a multiline description and convert it into a single |
||
468 | * long unbroken line. |
||
469 | */ |
||
470 | protected static function removeLineBreaks($text) |
||
474 | } |
||
475 |