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 | /** |
||
| 41 | * An array of all the options that are available to the parser. Unlike the |
||
| 42 | * ClearIce::$optionsMap parameter, this paramter just lists all the options |
||
| 43 | * and their parameters. Any option added through the ArgumentParser::addOptions() |
||
| 44 | * parameter is just appended to this array. |
||
| 45 | * |
||
| 46 | * @var \clearice\Options |
||
| 47 | */ |
||
| 48 | private $options = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Specifies whether the parser should be strict about errors or not. |
||
| 52 | * |
||
| 53 | * @var boolean |
||
| 54 | */ |
||
| 55 | private $strict = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * A flag raised when the parser already has the automatic help option |
||
| 59 | * added. This is used to prevent multiple help options. |
||
| 60 | * |
||
| 61 | * @var boolean |
||
| 62 | */ |
||
| 63 | private $hasHelp; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The usage instructions for the application displayed as part of the |
||
| 67 | * automatically generated help message. This message is usually printed |
||
| 68 | * after the description. |
||
| 69 | * |
||
| 70 | * @var array|string |
||
| 71 | */ |
||
| 72 | private $usage; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The description displayed on top of the help message just after the |
||
| 76 | * usage instructions. |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | private $description; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * A footnote displayed at the bottom of the help message. |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | private $footnote; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * An array of all the commands that the script can work with. |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $commands = []; |
||
| 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 | private $io; |
||
| 143 | |||
| 144 | 27 | public function __construct(ConsoleIO $io) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Adds an unknown option to the list of unknown options currently held in |
||
| 152 | * the parser. |
||
| 153 | * |
||
| 154 | * @param string $unknown |
||
| 155 | */ |
||
| 156 | 4 | public function addUnknownOption($unknown) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Adds a known parsed option to the list of parsed options currently held |
||
| 163 | * in the parser. |
||
| 164 | * @param string $key The option. |
||
| 165 | * @param string $value The value asigned to the option. |
||
| 166 | */ |
||
| 167 | 20 | public function addParsedOption($key, $value) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Adds a new value of a multi option. |
||
| 174 | * @param string $key The option. |
||
| 175 | * @param string $value The value to be appended to the list. |
||
| 176 | */ |
||
| 177 | 1 | public function addParsedMultiOption($key, $value) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Parse the command line arguments and return a structured array which |
||
| 184 | * represents the options which were interpreted by ClearIce. The array |
||
| 185 | * returned has the following structure. |
||
| 186 | * |
||
| 187 | * |
||
| 188 | * @param array $arguments |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | 24 | public function parse(array $arguments) : array |
|
| 216 | |||
| 217 | 6 | public function getLookAheadArgument() |
|
| 221 | |||
| 222 | 6 | public function moveToNextArgument() |
|
| 226 | |||
| 227 | 23 | private function parseArgument($argument) |
|
| 242 | |||
| 243 | 24 | private function aggregateOptions() |
|
| 257 | |||
| 258 | 24 | private function showHelp($called) |
|
| 267 | |||
| 268 | 24 | private function showStrictErrors($executed) |
|
| 279 | |||
| 280 | 23 | private function getArgumentWithCommand($argument, $command) |
|
| 295 | |||
| 296 | 24 | private function getCommand() |
|
| 312 | |||
| 313 | 6 | private function stringCommandToArray($command) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Add commands for parsing. |
||
| 320 | * This method takes many arguments with each representing a unique command. |
||
| 321 | * |
||
| 322 | * @param String |
||
| 323 | * @see ClearIce::addCommands() |
||
| 324 | */ |
||
| 325 | 6 | public function addCommands($commands) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Add options to be recognized. |
||
| 337 | * Options could either be strings or structured arrays. Strings define |
||
| 338 | * simple options. Structured arrays describe options in deeper details. |
||
| 339 | */ |
||
| 340 | 27 | public function addOptions($options) |
|
| 344 | |||
| 345 | 1 | public function addGroups($groups) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Sets whether the parser should be strict or not. A strict parser would |
||
| 354 | * terminate the application if it doesn't understand any options. A |
||
| 355 | * not-strict parser would just return the unknown options it encountered |
||
| 356 | * and expect the application to deal with it appropriately. |
||
| 357 | * |
||
| 358 | * @param boolean $strict A boolean value for the strictness state |
||
| 359 | */ |
||
| 360 | 2 | public function setStrict($strict) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Adds the two automatic help options. A long one represented by --help and |
||
| 367 | * a short one represented by -h. |
||
| 368 | */ |
||
| 369 | 8 | public function addHelp($called = '') |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Set the usage text which forms part of the help text. |
||
| 385 | * @param string|array $usage |
||
| 386 | */ |
||
| 387 | 7 | public function setUsage($usage) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Set the description text shown on top of the help text. |
||
| 394 | * @param string $description |
||
| 395 | */ |
||
| 396 | 7 | public function setDescription($description) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Set the footnote text shown at the bottom of the help text. |
||
| 403 | * @param string $footnote |
||
| 404 | */ |
||
| 405 | 7 | public function setFootnote($footnote) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Returns the help message as a string. |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | 8 | public function getHelpMessage($called, $command = null) |
|
| 426 | |||
| 427 | 3 | public function getIO() : ConsoleIO |
|
| 431 | |||
| 432 | } |
||
| 433 |