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 | 25 | public function parse() |
|
| 211 | |||
| 212 | 6 | public function getLookAheadArgument() |
|
| 216 | |||
| 217 | 6 | public function moveToNextArgument() |
|
| 221 | |||
| 222 | 25 | private function executeCommand($command, $options) |
|
| 223 | { |
||
| 224 | 25 | if ($command === '__default__' || !isset($this->commands[$command]['class'])) { |
|
| 225 | 24 | return $options; |
|
| 226 | } else { |
||
| 227 | 1 | $class = $this->commands[$command]['class']; |
|
| 228 | 1 | $object = new $class(); |
|
| 229 | 1 | unset($options['__command__']); |
|
| 230 | 1 | $object->run($options); |
|
| 231 | 1 | return $options; |
|
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | 24 | private function parseArgument($argument) |
|
| 236 | { |
||
| 237 | 24 | $success = FALSE; |
|
| 238 | 24 | if ($this->parsedOptions['__command__'] != '__default__') { |
|
| 239 | 5 | parsers\BaseParser::setLogUnknowns(false); |
|
| 240 | 5 | $success = $this->getArgumentWithCommand($argument, $this->parsedOptions['__command__']); |
|
| 241 | 5 | } |
|
| 242 | |||
| 243 | 24 | if ($success === false) { |
|
| 244 | 22 | parsers\BaseParser::setLogUnknowns(true); |
|
| 245 | 22 | $this->getArgumentWithCommand($argument, '__default__'); |
|
| 246 | 22 | } |
|
| 247 | 24 | } |
|
| 248 | |||
| 249 | 25 | private function aggregateOptions() |
|
| 261 | |||
| 262 | 25 | private function showHelp() |
|
| 277 | |||
| 278 | 25 | private function showStrictErrors($executed) |
|
| 279 | { |
||
| 280 | 25 | if ($this->strict && count($this->unknownOptions) > 0) { |
|
| 281 | 2 | foreach ($this->unknownOptions as $unknown) { |
|
| 282 | 2 | ClearIce::error("$executed: invalid option -- {$unknown}\n"); |
|
| 283 | 2 | } |
|
| 284 | |||
| 285 | 2 | if ($this->hasHelp) { |
|
| 286 | 1 | ClearIce::error("Try `$executed --help` for more information\n"); |
|
| 287 | 1 | } |
|
| 288 | 2 | ClearIce::terminate(); |
|
| 289 | 2 | } |
|
| 290 | 25 | } |
|
| 291 | |||
| 292 | 24 | private function getArgumentWithCommand($argument, $command) |
|
| 310 | |||
| 311 | 25 | private function getCommand() |
|
| 327 | |||
| 328 | 6 | private function stringCommandToArray($command) |
|
| 329 | { |
||
| 330 | 6 | if(class_exists($command)) { |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Add commands for parsing. |
||
| 353 | * This method takes many arguments with each representing a unique command. |
||
| 354 | * |
||
| 355 | * @param String |
||
| 356 | * @see ClearIce::addCommands() |
||
| 357 | */ |
||
| 358 | 7 | public function addCommands() |
|
| 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 | 28 | 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..