Complex classes like ArgumentParser 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 ArgumentParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class ArgumentParser |
||
38 | { |
||
39 | /** |
||
40 | * An array of all the options that are available to the parser. Unlike the |
||
41 | * ClearIce::$optionsMap parameter, this paramter just lists all the options |
||
42 | * and their parameters. Any option added through the ArgumentParser::addOptions() |
||
43 | * parameter is just appended to this array. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | private $options = []; |
||
48 | |||
49 | /** |
||
50 | * Specifies whether the parser should be strict about errors or not. |
||
51 | * |
||
52 | * @var boolean |
||
53 | */ |
||
54 | private $strict = false; |
||
55 | |||
56 | /** |
||
57 | * A flag raised when the parser already has the automatic help option |
||
58 | * added. This is used to prevent multiple help options. |
||
59 | * |
||
60 | * @var boolean |
||
61 | */ |
||
62 | private $hasHelp; |
||
63 | |||
64 | /** |
||
65 | * The usage instructions for the application displayed as part of the |
||
66 | * automatically generated help message. This message is usually printed |
||
67 | * after the description. |
||
68 | * |
||
69 | * @var array|string |
||
70 | */ |
||
71 | private $usage; |
||
72 | |||
73 | /** |
||
74 | * The description displayed on top of the help message just after the |
||
75 | * usage instructions. |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | private $description; |
||
80 | |||
81 | /** |
||
82 | * A footnote displayed at the bottom of the help message. |
||
83 | * |
||
84 | * @var string |
||
85 | */ |
||
86 | private $footnote; |
||
87 | |||
88 | /** |
||
89 | * An array of all the commands that the script can work with. |
||
90 | * @var array |
||
91 | */ |
||
92 | private $commands = []; |
||
93 | |||
94 | private $groups = []; |
||
95 | |||
96 | /** |
||
97 | * The current command being run. |
||
98 | * @var string |
||
99 | */ |
||
100 | private $command; |
||
101 | |||
102 | /** |
||
103 | * Holds all the options that have already been parsed and recognized. |
||
104 | * @var array |
||
105 | */ |
||
106 | private $parsedOptions = []; |
||
107 | |||
108 | /** |
||
109 | * Holds all the options that have been parsed but are unknown. |
||
110 | * @var array |
||
111 | */ |
||
112 | private $unknownOptions = []; |
||
113 | |||
114 | /** |
||
115 | * Options that are standing alone. |
||
116 | * @var array |
||
117 | */ |
||
118 | private $standAlones = []; |
||
119 | |||
120 | /** |
||
121 | * An instance of the long option parser used for the parsing of long options |
||
122 | * which are preceed with a double dash "--". |
||
123 | * @var \clearice\parsers\LongOptionParser |
||
124 | */ |
||
125 | private $longOptionParser; |
||
126 | |||
127 | /** |
||
128 | * An instance of the short option parser used for the parsing of short optoins |
||
129 | * which are preceeded with a single dash "-". |
||
130 | * @var \clearice\parsers\ShortOptionParser |
||
131 | */ |
||
132 | private $shortOptionParser; |
||
133 | |||
134 | /** |
||
135 | * The arguments that were passed through the command line to the script or |
||
136 | * application. |
||
137 | * |
||
138 | * @var array |
||
139 | */ |
||
140 | private $arguments = []; |
||
141 | private $argumentPointer; |
||
142 | |||
143 | 27 | public function __construct() |
|
147 | |||
148 | /** |
||
149 | * Adds an unknown option to the list of unknown options currently held in |
||
150 | * the parser. |
||
151 | * |
||
152 | * @param string $unknown |
||
153 | */ |
||
154 | 4 | public function addUnknownOption($unknown) |
|
158 | |||
159 | /** |
||
160 | * Adds a known parsed option to the list of parsed options currently held |
||
161 | * in the parser. |
||
162 | * @param string $key The option. |
||
163 | * @param string $value The value asigned to the option. |
||
164 | */ |
||
165 | 21 | public function addParsedOption($key, $value) |
|
169 | |||
170 | /** |
||
171 | * Adds a new value of a multi option. |
||
172 | * @param string $key The option. |
||
173 | * @param string $value The value to be appended to the list. |
||
174 | */ |
||
175 | 1 | public function addParsedMultiOption($key, $value) |
|
179 | |||
180 | /** |
||
181 | * Parse the command line arguments and return a structured array which |
||
182 | * represents the options which were interpreted by ClearIce. The array |
||
183 | * returned has the following structure. |
||
184 | * |
||
185 | * |
||
186 | * @global type $argv |
||
187 | * @return array |
||
188 | */ |
||
189 | 24 | public function parse() |
|
210 | |||
211 | 6 | public function getLookAheadArgument() |
|
215 | |||
216 | 6 | public function moveToNextArgument() |
|
220 | |||
221 | 24 | private function executeCommand($command, $options) |
|
233 | |||
234 | 24 | private function parseArgument($argument) |
|
247 | |||
248 | 24 | private function aggregateOptions() |
|
260 | |||
261 | 24 | private function showHelp() |
|
276 | |||
277 | 24 | private function showStrictErrors($executed) |
|
290 | |||
291 | 24 | private function getArgumentWithCommand($argument, $command) |
|
309 | |||
310 | 24 | private function getCommand() |
|
326 | |||
327 | 6 | private function stringCommandToArray($command) |
|
334 | |||
335 | /** |
||
336 | * Add commands for parsing. |
||
337 | * This method can take as many commands as possible. |
||
338 | * |
||
339 | * @param String |
||
340 | * @see ClearIce::addCommands() |
||
341 | */ |
||
342 | 7 | public function addCommands() |
|
352 | |||
353 | /** |
||
354 | * Add options to be recognized. |
||
355 | * Options could either be strings or |
||
356 | * structured arrays. Strings only define simple options. Structured arrays |
||
357 | * describe options in deeper details. |
||
358 | */ |
||
359 | 27 | public function addOptions() |
|
364 | |||
365 | 1 | public function addGroups() |
|
372 | |||
373 | /** |
||
374 | * Sets whether the parser should be strict or not. A strict parser would |
||
375 | * terminate the application if it doesn't understand any options. A |
||
376 | * not-strict parser would just return the unknown options it encountered |
||
377 | * and expect the application to deal with it appropriately. |
||
378 | * |
||
379 | * @param boolean $strict A boolean value for the strictness state |
||
380 | */ |
||
381 | 2 | public function setStrict($strict) |
|
385 | |||
386 | /** |
||
387 | * Adds the two automatic help options. A long one represented by --help and |
||
388 | * a short one represented by -h. |
||
389 | */ |
||
390 | 8 | public function addHelp() |
|
413 | |||
414 | /** |
||
415 | * Set the usage text which forms part of the help text. |
||
416 | * @param string|array $usage |
||
417 | */ |
||
418 | 7 | public function setUsage($usage) |
|
422 | |||
423 | /** |
||
424 | * Set the description text shown on top of the help text. |
||
425 | * @param string $description |
||
426 | */ |
||
427 | 7 | public function setDescription($description) |
|
431 | |||
432 | /** |
||
433 | * Set the footnote text shown at the bottom of the help text. |
||
434 | * @param string $footnote |
||
435 | */ |
||
436 | 7 | public function setFootnote($footnote) |
|
440 | |||
441 | /** |
||
442 | * Returns the help message as a string. |
||
443 | * |
||
444 | * @global type $argv |
||
445 | * @return string |
||
446 | */ |
||
447 | 8 | public function getHelpMessage($command) |
|
459 | |||
460 | 7 | private function fillCommand($command) |
|
464 | } |
||
465 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..