| Total Complexity | 40 |
| Total Lines | 320 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Parser 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.
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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 59 | abstract class Parser |
||
| 60 | { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The last seen variadic option name |
||
| 64 | * @var string|null |
||
| 65 | */ |
||
| 66 | protected ?string $lastVariadic = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The list of options |
||
| 70 | * @var array<Option> |
||
| 71 | */ |
||
| 72 | protected array $options = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The list of arguments |
||
| 76 | * @var array<Argument> |
||
| 77 | */ |
||
| 78 | protected array $arguments = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Parsed values indexed by option name |
||
| 82 | * @var array<string|int, mixed> |
||
| 83 | */ |
||
| 84 | protected array $values = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The version |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected string $version = ''; |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * Parse the command line argument. |
||
| 95 | * @param array<int, string> $argv |
||
| 96 | * @return $this |
||
| 97 | */ |
||
| 98 | public function parse(array $argv): self |
||
| 99 | { |
||
| 100 | //The first item is ignored. |
||
| 101 | array_shift($argv); |
||
| 102 | |||
| 103 | $arguments = Helper::normalizeArguments($argv); |
||
| 104 | |||
| 105 | $count = count($arguments); |
||
| 106 | $literal = false; |
||
| 107 | |||
| 108 | for ($i = 0; $i < $count; $i++) { |
||
| 109 | list($arg, $nextArg) = [ |
||
| 110 | $arguments[$i], |
||
| 111 | isset($arguments[$i + 1]) ? $arguments[$i + 1] : null |
||
| 112 | ]; |
||
| 113 | |||
| 114 | if ($arg === '--') { |
||
| 115 | $literal = true; |
||
| 116 | } elseif ($arg[0] !== '-' || $literal) { |
||
| 117 | $this->parseArgument($arg); |
||
| 118 | } else { |
||
| 119 | $i += (int) $this->parseOptions($arg, $nextArg); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | $this->validate(); |
||
| 124 | |||
| 125 | return $this; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Return all options. |
||
| 130 | * @return array<Option> |
||
| 131 | */ |
||
| 132 | public function options(): array |
||
| 133 | { |
||
| 134 | return $this->options; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Return all arguments. |
||
| 139 | * @return array<Argument> |
||
| 140 | */ |
||
| 141 | public function arguments(): array |
||
| 142 | { |
||
| 143 | return $this->arguments; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the command arguments i.e which is not an option. |
||
| 148 | * @return array<string> |
||
| 149 | */ |
||
| 150 | public function args(): array |
||
| 151 | { |
||
| 152 | return array_diff_key($this->values, $this->options); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get values indexed by camel case attribute name. |
||
| 157 | * @param bool $withDefaults |
||
| 158 | * @return array<string|int, mixed> |
||
| 159 | */ |
||
| 160 | public function values(bool $withDefaults = true): array |
||
| 161 | { |
||
| 162 | $values = $this->values; |
||
| 163 | $values['version'] = $this->version; |
||
| 164 | |||
| 165 | if (!$withDefaults) { |
||
| 166 | unset($values['help'], $values['version'], $values['verbosity']); |
||
| 167 | } |
||
| 168 | |||
| 169 | return $values; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Handle Unknown option |
||
| 174 | * @param string $arg is option name |
||
| 175 | * @param string|null $value is option value |
||
| 176 | * @return mixed If true it will indicate that value has been eaten. |
||
| 177 | */ |
||
| 178 | abstract protected function handleUnknown(string $arg, $value = null); |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Emit the event with value. |
||
| 182 | * @param string $event is option name |
||
| 183 | * @param mixed $value is option value |
||
| 184 | * @return mixed |
||
| 185 | */ |
||
| 186 | abstract protected function emit(string $event, $value = null); |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Parse single argument. |
||
| 190 | * @param string $arg |
||
| 191 | * @return mixed |
||
| 192 | */ |
||
| 193 | protected function parseArgument(string $arg) |
||
| 194 | { |
||
| 195 | if ($this->lastVariadic) { |
||
| 196 | return $this->set($this->lastVariadic, $arg, true); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** @var Argument|false $argument */ |
||
| 200 | $argument = reset($this->arguments); |
||
| 201 | if ($argument === false) { |
||
| 202 | return $this->set(null, $arg, false); |
||
| 203 | } |
||
| 204 | |||
| 205 | $this->setValue($argument, $arg); |
||
| 206 | |||
| 207 | // Otherwise we will always collect same arguments again! |
||
| 208 | if (!$argument->isVariadic()) { |
||
| 209 | array_shift($this->arguments); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Parse an option, emit its event and set value |
||
| 215 | * @param string $arg |
||
| 216 | * @param string|null $nextArg |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | protected function parseOptions(string $arg, ?string $nextArg = null): bool |
||
| 220 | { |
||
| 221 | $value = null; |
||
| 222 | if ($nextArg !== null && substr($nextArg, 0, 1) !== '-') { |
||
| 223 | $value = $nextArg; |
||
| 224 | } |
||
| 225 | |||
| 226 | $option = $this->getOptionForArgument($arg); |
||
| 227 | if ($option === null) { |
||
| 228 | return $this->handleUnknown($arg, $value); |
||
| 229 | } |
||
| 230 | |||
| 231 | $this->lastVariadic = $option->isVariadic() |
||
| 232 | ? $option->getAttributeName() |
||
| 233 | : null; |
||
| 234 | |||
| 235 | return $this->emit($option->getAttributeName(), $value) === false |
||
| 236 | ? false |
||
| 237 | : $this->setValue($option, $value); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get matching option by argument (name) or null. |
||
| 242 | * @param string $arg |
||
| 243 | * @return Option|null |
||
| 244 | */ |
||
| 245 | protected function getOptionForArgument(string $arg): ?Option |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Set a raw value. |
||
| 258 | * @param mixed $key |
||
| 259 | * @param mixed $value |
||
| 260 | * @param bool $isVariadic |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | protected function set($key, $value, bool $isVariadic = false): bool |
||
| 264 | { |
||
| 265 | if ($key === null) { |
||
| 266 | $this->values[] = $value; |
||
| 267 | } elseif ($isVariadic) { |
||
| 268 | $this->values[$key] = array_merge($this->values[$key], (array) $value); |
||
| 269 | } else { |
||
| 270 | $this->values[$key] = $value; |
||
| 271 | } |
||
| 272 | |||
| 273 | return !in_array($value, [true, false, null], true); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Sets value of an option. |
||
| 278 | * @param Parameter $parameter |
||
| 279 | * @param string|null $value |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | protected function setValue(Parameter $parameter, ?string $value = null): bool |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Validate if all required arguments/options have proper values. |
||
| 292 | * @return void |
||
| 293 | */ |
||
| 294 | protected function validate(): void |
||
| 295 | { |
||
| 296 | /** @var array<Parameter> $missingItems */ |
||
| 297 | $missingItems = array_filter( |
||
| 298 | $this->options + $this->arguments, |
||
| 299 | function ($item) { |
||
| 300 | /** @var Parameter $item */ |
||
| 301 | return $item->isRequired() && in_array( |
||
| 302 | $this->values[$item->getAttributeName()], |
||
| 303 | [null, []] |
||
| 304 | ); |
||
| 305 | } |
||
| 306 | ); |
||
| 307 | |||
| 308 | foreach ($missingItems as $item) { |
||
| 309 | list($name, $label) = [$item->getName(), 'Argument']; |
||
| 310 | if ($item instanceof Option) { |
||
| 311 | list($name, $label) = [$item->getLong(), 'Option']; |
||
| 312 | } |
||
| 313 | |||
| 314 | throw new RuntimeException(sprintf( |
||
| 315 | '%s "%s" is required', |
||
| 316 | $label, |
||
| 317 | $name |
||
| 318 | )); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Register a new argument/option. |
||
| 324 | * @param Parameter $parameter |
||
| 325 | * @return void |
||
| 326 | */ |
||
| 327 | protected function register(Parameter $parameter): void |
||
| 328 | { |
||
| 329 | $this->checkDuplicate($parameter); |
||
| 330 | |||
| 331 | $name = $parameter->getAttributeName(); |
||
| 332 | |||
| 333 | if ($parameter instanceof Option) { |
||
| 334 | $this->options[$name] = $parameter; |
||
| 335 | } elseif ($parameter instanceof Argument) { |
||
| 336 | $this->arguments[$name] = $parameter; |
||
| 337 | } |
||
| 338 | |||
| 339 | $this->set($name, $parameter->getDefault()); |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Remove a registered argument/option. |
||
| 344 | * @param string $name |
||
| 345 | * @return void |
||
| 346 | */ |
||
| 347 | protected function unregister(string $name): void |
||
| 348 | { |
||
| 349 | unset( |
||
| 350 | $this->values[$name], |
||
| 351 | $this->options[$name], |
||
| 352 | $this->arguments[$name] |
||
| 353 | ); |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Check if either argument/option with given name is registered. |
||
| 358 | * @param string $name |
||
| 359 | * @return bool |
||
| 360 | */ |
||
| 361 | protected function isRegistered(string $name): bool |
||
| 362 | { |
||
| 363 | return array_key_exists($name, $this->values); |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * What if the given name is already registered. |
||
| 368 | * @param Parameter $parameter |
||
| 369 | * @return void |
||
| 370 | */ |
||
| 371 | protected function checkDuplicate(Parameter $parameter): void |
||
| 379 | )); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | } |
||
| 383 |