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 | 28 | 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 | 22 | 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 | 26 | public function parse() |
|
190 | { |
||
191 | 26 | global $argv; |
|
192 | 26 | $this->arguments = $argv; |
|
193 | 26 | $executed = array_shift($this->arguments); |
|
194 | 26 | $this->command = $this->getCommand(); |
|
195 | |||
196 | 26 | $this->parsedOptions = $this->options->getDefaults(); |
|
197 | 26 | $this->parsedOptions['__command__'] = $this->command; |
|
198 | 26 | $this->longOptionParser = new parsers\LongOptionParser($this, $this->options->getMap()); |
|
199 | 26 | $this->shortOptionParser = new parsers\ShortOptionParser($this, $this->options->getMap()); |
|
200 | |||
201 | 26 | for ($this->argumentPointer = 0; $this->argumentPointer < count($this->arguments); $this->argumentPointer++) { |
|
202 | 25 | $this->parseArgument($this->arguments[$this->argumentPointer]); |
|
203 | 25 | } |
|
204 | |||
205 | 26 | $this->showStrictErrors($executed); |
|
206 | 26 | $this->aggregateOptions(); |
|
207 | 26 | $this->showHelp(); |
|
208 | |||
209 | 26 | return $this->executeCommand($this->command, $this->parsedOptions); |
|
210 | } |
||
211 | |||
212 | 7 | public function getLookAheadArgument() |
|
213 | { |
||
214 | 7 | return $this->arguments[$this->argumentPointer + 1]; |
|
215 | } |
||
216 | |||
217 | 7 | public function moveToNextArgument() |
|
218 | { |
||
219 | 7 | $this->argumentPointer++; |
|
220 | 7 | } |
|
221 | |||
222 | 26 | private function executeCommand($command, $options) |
|
223 | { |
||
224 | 26 | if ($command === '__default__' || !isset($this->commands[$command]['class'])) { |
|
225 | 24 | return $options; |
|
226 | } else { |
||
227 | 2 | $class = $this->commands[$command]['class']; |
|
228 | 2 | $object = new $class(); |
|
229 | 2 | unset($options['__command__']); |
|
230 | 2 | $object->run($options); |
|
231 | 2 | return $options; |
|
232 | } |
||
233 | } |
||
234 | |||
235 | 25 | private function parseArgument($argument) |
|
236 | { |
||
237 | 25 | $success = false; |
|
238 | // Try to parse the argument for a command |
||
239 | 25 | if ($this->parsedOptions['__command__'] != '__default__') { |
|
240 | 6 | parsers\BaseParser::setLogUnknowns(false); |
|
241 | 6 | $success = $this->getArgumentWithCommand($argument, $this->parsedOptions['__command__']); |
|
242 | 6 | } |
|
243 | |||
244 | // If not succesful get argument for the __default__ command. |
||
245 | 25 | if ($success === false) { |
|
246 | 22 | parsers\BaseParser::setLogUnknowns(true); |
|
247 | 22 | $this->getArgumentWithCommand($argument, '__default__'); |
|
248 | 22 | } |
|
249 | 25 | } |
|
250 | |||
251 | 26 | private function aggregateOptions() |
|
252 | { |
||
253 | 26 | if (count($this->standAlones)) |
|
254 | 26 | $this->parsedOptions['stand_alones'] = $this->standAlones; |
|
255 | 26 | if (count($this->unknownOptions)) |
|
256 | 26 | $this->parsedOptions['unknowns'] = $this->unknownOptions; |
|
257 | |||
258 | // Hide the __default__ command from the outside world |
||
259 | 26 | if ($this->parsedOptions['__command__'] == '__default__') { |
|
260 | 20 | unset($this->parsedOptions['__command__']); |
|
261 | 20 | } |
|
262 | 26 | } |
|
263 | |||
264 | 26 | private function showHelp() |
|
265 | { |
||
266 | 26 | if (isset($this->parsedOptions['help'])) { |
|
267 | 3 | ClearIce::output($this->getHelpMessage( |
|
268 | 3 | isset($this->parsedOptions['__command__']) ? |
|
269 | 3 | $this->parsedOptions['__command__'] : null |
|
270 | 3 | ) |
|
271 | 3 | ); |
|
272 | 3 | ClearIce::terminate(); |
|
273 | 3 | } |
|
274 | 26 | if ($this->command == 'help') { |
|
275 | 2 | ClearIce::output($this->getHelpMessage($this->standAlones[0])); |
|
276 | 2 | ClearIce::terminate(); |
|
277 | 2 | } |
|
278 | 26 | } |
|
279 | |||
280 | 26 | private function showStrictErrors($executed) |
|
281 | { |
||
282 | 26 | if ($this->strict && count($this->unknownOptions) > 0) { |
|
283 | 2 | foreach ($this->unknownOptions as $unknown) { |
|
284 | 2 | ClearIce::error("$executed: invalid option -- {$unknown}\n"); |
|
285 | 2 | } |
|
286 | |||
287 | 2 | if ($this->hasHelp) { |
|
288 | 1 | ClearIce::error("Try `$executed --help` for more information\n"); |
|
289 | 1 | } |
|
290 | 2 | ClearIce::terminate(); |
|
291 | 2 | } |
|
292 | 26 | } |
|
293 | |||
294 | 25 | private function getArgumentWithCommand($argument, $command) |
|
295 | { |
||
296 | 25 | $return = true; |
|
297 | 25 | if (preg_match('/^(--)(?<option>[a-zA-z][0-9a-zA-Z-_\.]*)(=)(?<value>.*)/i', $argument, $matches)) { |
|
298 | 7 | $return = $this->longOptionParser->parse($matches['option'], $matches['value'], $command); |
|
299 | 25 | } else if (preg_match('/^(--)(?<option>[a-zA-z][0-9a-zA-Z-_\.]*)/i', $argument, $matches)) { |
|
300 | 14 | $return = $this->longOptionParser->parse($matches['option'], true, $command); |
|
301 | 25 | } else if (preg_match('/^(-)(?<option>[a-zA-z0-9](.*))/i', $argument, $matches)) { |
|
302 | 13 | $this->shortOptionParser->parse($matches['option'], $command); |
|
303 | 13 | $return = true; |
|
304 | 13 | } else { |
|
305 | 5 | $this->standAlones[] = $argument; |
|
306 | } |
||
307 | 25 | return $return; |
|
308 | } |
||
309 | |||
310 | 26 | private function getCommand() |
|
326 | |||
327 | 7 | private function stringCommandToArray($command) |
|
328 | { |
||
329 | 7 | if(class_exists($command)) { |
|
330 | try{ |
||
331 | 1 | $className = $command; |
|
332 | 1 | $method = new \ReflectionMethod($className, 'getCommandOptions'); |
|
333 | 1 | $command = $method->invoke(null); |
|
334 | 1 | $command['class'] = $className; |
|
335 | 1 | if(is_array($command['options'])) { |
|
336 | 1 | foreach($command['options'] as $option) { |
|
337 | 1 | $option['command'] = $command['command']; |
|
338 | 1 | ClearIce::addOptions($option); |
|
339 | 1 | } |
|
340 | 1 | } |
|
341 | 1 | return $command; |
|
342 | } catch (\ReflectionException $e) { |
||
343 | // Do nothing |
||
344 | } |
||
345 | } |
||
346 | return [ |
||
347 | 6 | 'help' => '', |
|
348 | 'command' => $command |
||
349 | 6 | ]; |
|
350 | } |
||
351 | |||
352 | /** |
||
353 | * Add commands for parsing. |
||
354 | * This method takes many arguments with each representing a unique command. |
||
355 | * |
||
356 | * @param String |
||
357 | * @see ClearIce::addCommands() |
||
358 | */ |
||
359 | 8 | public function addCommands() |
|
360 | { |
||
361 | 8 | foreach (func_get_args() as $command) { |
|
362 | 8 | if (is_string($command)) { |
|
363 | 7 | $command = $this->stringCommandToArray($command); |
|
364 | 7 | } |
|
365 | 8 | $this->commands[$command['command']] = $command; |
|
366 | 8 | } |
|
367 | 8 | } |
|
368 | |||
369 | /** |
||
370 | * Add options to be recognized. |
||
371 | * Options could either be strings or structured arrays. Strings define |
||
372 | * simple options. Structured arrays describe options in deeper details. |
||
373 | */ |
||
374 | 29 | public function addOptions() |
|
379 | |||
380 | 1 | public function addGroups() |
|
387 | |||
388 | /** |
||
389 | * Sets whether the parser should be strict or not. A strict parser would |
||
390 | * terminate the application if it doesn't understand any options. A |
||
391 | * not-strict parser would just return the unknown options it encountered |
||
392 | * and expect the application to deal with it appropriately. |
||
393 | * |
||
394 | * @param boolean $strict A boolean value for the strictness state |
||
395 | */ |
||
396 | 2 | public function setStrict($strict) |
|
400 | |||
401 | /** |
||
402 | * Adds the two automatic help options. A long one represented by --help and |
||
403 | * a short one represented by -h. |
||
404 | */ |
||
405 | 8 | public function addHelp() |
|
428 | |||
429 | /** |
||
430 | * Set the usage text which forms part of the help text. |
||
431 | * @param string|array $usage |
||
432 | */ |
||
433 | 7 | public function setUsage($usage) |
|
437 | |||
438 | /** |
||
439 | * Set the description text shown on top of the help text. |
||
440 | * @param string $description |
||
441 | */ |
||
442 | 7 | public function setDescription($description) |
|
446 | |||
447 | /** |
||
448 | * Set the footnote text shown at the bottom of the help text. |
||
449 | * @param string $footnote |
||
450 | */ |
||
451 | 7 | public function setFootnote($footnote) |
|
455 | |||
456 | /** |
||
457 | * Returns the help message as a string. |
||
458 | * |
||
459 | * @global type $argv |
||
460 | * @return string |
||
461 | */ |
||
462 | 8 | public function getHelpMessage($command) |
|
474 | |||
475 | private function fillCommand($command) |
||
479 | } |
||
480 |
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..