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 | public function __construct($classNameOrInstance, $methodName) |
||
78 | { |
||
79 | $this->reflection = new \ReflectionMethod($classNameOrInstance, $methodName); |
||
80 | $this->methodName = $methodName; |
||
81 | // Set up a default name for the command from the method name. |
||
82 | // This can be overridden via @command or @name annotations. |
||
83 | $this->setDefaultName(); |
||
84 | $this->options = $this->determineOptionsFromParameters(); |
||
85 | $this->arguments = $this->determineAgumentClassifications(); |
||
86 | } |
||
87 | |||
88 | public function getMethodName() |
||
92 | |||
93 | public function getParameters() |
||
97 | |||
98 | /** |
||
99 | * Get the synopsis of the command (~first line). |
||
100 | */ |
||
101 | public function getDescription() |
||
106 | |||
107 | public function setDescription($description) |
||
111 | |||
112 | /** |
||
113 | * Get the help text of the command (the description) |
||
114 | */ |
||
115 | public function getHelp() |
||
120 | |||
121 | public function setHelp($help) |
||
125 | |||
126 | public function getAliases() |
||
131 | |||
132 | public function setAliases($aliases) |
||
139 | |||
140 | public function getExampleUsages() |
||
145 | |||
146 | public function getName() |
||
151 | |||
152 | public function setDefaultName() |
||
156 | |||
157 | public function setName($name) |
||
161 | |||
162 | protected function determineAgumentClassifications() |
||
177 | |||
178 | public function getArguments() |
||
182 | |||
183 | public function hasArgument($name) |
||
187 | |||
188 | public function setArgumentDefaultValue($name, $defaultValue) |
||
192 | |||
193 | public function addArgument($name, $description, $defaultValue = null) |
||
203 | |||
204 | /** |
||
205 | * Examine the provided parameter, and determine whether it |
||
206 | * is a parameter that will be filled in with a positional |
||
207 | * commandline argument. |
||
208 | * |
||
209 | * @return false|null|string|array |
||
210 | */ |
||
211 | protected function getArgumentClassification($param) |
||
230 | |||
231 | public function getOptions() |
||
235 | |||
236 | public function hasOption($name) |
||
240 | |||
241 | public function setOptionDefaultValue($name, $defaultValue) |
||
245 | |||
246 | public function addOption($name, $description, $defaultValue = false) |
||
256 | |||
257 | public function determineOptionsFromParameters() |
||
272 | |||
273 | public function getArgumentDescription($name) |
||
282 | |||
283 | public function getOptionDescription($name) |
||
292 | |||
293 | protected function isAssoc($arr) |
||
300 | |||
301 | public function getAnnotations() |
||
306 | |||
307 | public function getAnnotation($annotation) |
||
315 | |||
316 | public function hasAnnotation($annotation) |
||
321 | |||
322 | protected function convertName($camel) |
||
329 | |||
330 | public function setExampleUsage($usage, $description) |
||
334 | |||
335 | /** |
||
336 | * Parse the docBlock comment for this command, and set the |
||
337 | * fields of this class with the data thereby obtained. |
||
338 | */ |
||
339 | protected function parseDocBlock() |
||
348 | |||
349 | /** |
||
350 | * Save any tag that we do not explicitly recognize in the |
||
351 | * 'otherAnnotations' map. |
||
352 | */ |
||
353 | public function addOtherAnnotation($name, $content) |
||
357 | |||
358 | /** |
||
359 | * An option might have a name such as 'silent|s'. In this |
||
360 | * instance, we will allow the @option or @default tag to |
||
361 | * reference the option only by name (e.g. 'silent' or 's' |
||
362 | * instead of 'silent|s'). |
||
363 | */ |
||
364 | public function findMatchingOption($optionName) |
||
392 | /** |
||
393 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
394 | * convert the data into the last of these forms. |
||
395 | */ |
||
396 | protected static function convertListToCommaSeparated($text) |
||
400 | } |
||
401 |