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 | * @global type $argv |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | 24 | public function parse() |
|
| 217 | |||
| 218 | 6 | public function getLookAheadArgument() |
|
| 222 | |||
| 223 | 6 | public function moveToNextArgument() |
|
| 227 | |||
| 228 | 23 | private function parseArgument($argument) |
|
| 243 | |||
| 244 | 24 | private function aggregateOptions() |
|
| 258 | |||
| 259 | 24 | private function showHelp() |
|
| 272 | |||
| 273 | 24 | private function showStrictErrors($executed) |
|
| 284 | |||
| 285 | 23 | private function getArgumentWithCommand($argument, $command) |
|
| 300 | |||
| 301 | 24 | private function getCommand() |
|
| 317 | |||
| 318 | 6 | private function stringCommandToArray($command) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Add commands for parsing. |
||
| 325 | * This method takes many arguments with each representing a unique command. |
||
| 326 | * |
||
| 327 | * @param String |
||
| 328 | * @see ClearIce::addCommands() |
||
| 329 | */ |
||
| 330 | 6 | public function addCommands($commands) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Add options to be recognized. |
||
| 342 | * Options could either be strings or structured arrays. Strings define |
||
| 343 | * simple options. Structured arrays describe options in deeper details. |
||
| 344 | */ |
||
| 345 | 27 | public function addOptions($options) |
|
| 349 | |||
| 350 | 1 | public function addGroups($groups) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Sets whether the parser should be strict or not. A strict parser would |
||
| 359 | * terminate the application if it doesn't understand any options. A |
||
| 360 | * not-strict parser would just return the unknown options it encountered |
||
| 361 | * and expect the application to deal with it appropriately. |
||
| 362 | * |
||
| 363 | * @param boolean $strict A boolean value for the strictness state |
||
| 364 | */ |
||
| 365 | 2 | public function setStrict($strict) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Adds the two automatic help options. A long one represented by --help and |
||
| 372 | * a short one represented by -h. |
||
| 373 | */ |
||
| 374 | 8 | public function addHelp() |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Set the usage text which forms part of the help text. |
||
| 392 | * @param string|array $usage |
||
| 393 | */ |
||
| 394 | 7 | public function setUsage($usage) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Set the description text shown on top of the help text. |
||
| 401 | * @param string $description |
||
| 402 | */ |
||
| 403 | 7 | public function setDescription($description) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Set the footnote text shown at the bottom of the help text. |
||
| 410 | * @param string $footnote |
||
| 411 | */ |
||
| 412 | 7 | public function setFootnote($footnote) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Returns the help message as a string. |
||
| 419 | * |
||
| 420 | * @global type $argv |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | 8 | public function getHelpMessage($command = null) |
|
| 435 | |||
| 436 | 3 | public function getIO() : ConsoleIO |
|
| 440 | |||
| 441 | } |
||
| 442 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state