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 $container;  | 
            ||
| 143 | |||
| 144 | 29 |     public function __construct() { | 
            |
| 147 | |||
| 148 |     public function setContainer($container) { | 
            ||
| 151 | |||
| 152 | /**  | 
            ||
| 153 | * Adds an unknown option to the list of unknown options currently held in  | 
            ||
| 154 | * the parser.  | 
            ||
| 155 | *  | 
            ||
| 156 | * @param string $unknown  | 
            ||
| 157 | */  | 
            ||
| 158 | 4 |     public function addUnknownOption($unknown) { | 
            |
| 161 | |||
| 162 | /**  | 
            ||
| 163 | * Adds a known parsed option to the list of parsed options currently held  | 
            ||
| 164 | * in the parser.  | 
            ||
| 165 | * @param string $key The option.  | 
            ||
| 166 | * @param string $value The value asigned to the option.  | 
            ||
| 167 | */  | 
            ||
| 168 | 22 |     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) { | 
            |
| 180 | |||
| 181 | /**  | 
            ||
| 182 | * Parse the command line arguments and return a structured array which  | 
            ||
| 183 | * represents the options which were interpreted by ClearIce. The array  | 
            ||
| 184 | * returned has the following structure.  | 
            ||
| 185 | *  | 
            ||
| 186 | *  | 
            ||
| 187 | * @global type $argv  | 
            ||
| 188 | * @return array  | 
            ||
| 189 | */  | 
            ||
| 190 | 27 |     public function parse() { | 
            |
| 212 | |||
| 213 | 7 |     public function getLookAheadArgument() { | 
            |
| 216 | |||
| 217 | 7 |     public function moveToNextArgument() { | 
            |
| 220 | |||
| 221 | 27 |     private function executeCommand($command, $options) { | 
            |
| 233 | |||
| 234 | 25 |     private function parseArgument($argument) { | 
            |
| 248 | |||
| 249 | 27 |     private function aggregateOptions() { | 
            |
| 260 | |||
| 261 | 27 |     private function showHelp() { | 
            |
| 275 | |||
| 276 | 27 |     private function showStrictErrors($executed) { | 
            |
| 288 | |||
| 289 | 25 |     private function getArgumentWithCommand($argument, $command) { | 
            |
| 303 | |||
| 304 | 27 |     private function getCommand() { | 
            |
| 319 | |||
| 320 | 8 |     private function stringCommandToArray($command) { | 
            |
| 341 | |||
| 342 | /**  | 
            ||
| 343 | * Add commands for parsing.  | 
            ||
| 344 | * This method takes many arguments with each representing a unique command.  | 
            ||
| 345 | *  | 
            ||
| 346 | * @param String  | 
            ||
| 347 | * @see ClearIce::addCommands()  | 
            ||
| 348 | */  | 
            ||
| 349 | 9 |     public function addCommands() { | 
            |
| 357 | |||
| 358 | /**  | 
            ||
| 359 | * Add options to be recognized.  | 
            ||
| 360 | * Options could either be strings or structured arrays. Strings define  | 
            ||
| 361 | * simple options. Structured arrays describe options in deeper details.  | 
            ||
| 362 | */  | 
            ||
| 363 | 29 |     public function addOptions() { | 
            |
| 367 | |||
| 368 | 1 |     public function addGroups() { | 
            |
| 374 | |||
| 375 | /**  | 
            ||
| 376 | * Sets whether the parser should be strict or not. A strict parser would  | 
            ||
| 377 | * terminate the application if it doesn't understand any options. A  | 
            ||
| 378 | * not-strict parser would just return the unknown options it encountered  | 
            ||
| 379 | * and expect the application to deal with it appropriately.  | 
            ||
| 380 | *  | 
            ||
| 381 | * @param boolean $strict A boolean value for the strictness state  | 
            ||
| 382 | */  | 
            ||
| 383 | 2 |     public function setStrict($strict) { | 
            |
| 386 | |||
| 387 | /**  | 
            ||
| 388 | * Adds the two automatic help options. A long one represented by --help and  | 
            ||
| 389 | * a short one represented by -h.  | 
            ||
| 390 | */  | 
            ||
| 391 | 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) { | 
            |
| 421 | |||
| 422 | /**  | 
            ||
| 423 | * Set the description text shown on top of the help text.  | 
            ||
| 424 | * @param string $description  | 
            ||
| 425 | */  | 
            ||
| 426 | 7 |     public function setDescription($description) { | 
            |
| 429 | |||
| 430 | /**  | 
            ||
| 431 | * Set the footnote text shown at the bottom of the help text.  | 
            ||
| 432 | * @param string $footnote  | 
            ||
| 433 | */  | 
            ||
| 434 | 7 |     public function setFootnote($footnote) { | 
            |
| 437 | |||
| 438 | /**  | 
            ||
| 439 | * Returns the help message as a string.  | 
            ||
| 440 | *  | 
            ||
| 441 | * @global type $argv  | 
            ||
| 442 | * @return string  | 
            ||
| 443 | */  | 
            ||
| 444 | 8 |     public function getHelpMessage($command) { | 
            |
| 455 | |||
| 456 | }  | 
            ||
| 457 | 
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