| Conditions | 18 |
| Paths | 81 |
| Total Lines | 67 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 25 | public function load(): void |
||
| 26 | { |
||
| 27 | $loadedArguments = []; |
||
| 28 | $loadedOptions = []; |
||
| 29 | $argv = $_SERVER['argv']; |
||
| 30 | $argvCount = count($argv); |
||
| 31 | for ($i = 2; $i <= $argvCount; $i++) { |
||
| 32 | if (!isset($argv[$i])) { |
||
| 33 | continue; |
||
| 34 | } |
||
| 35 | if (str_contains($argv[$i], '=')) { |
||
| 36 | [$optionName, $value] = explode('=', $argv[$i], 2); |
||
| 37 | if (empty($value)) { |
||
| 38 | throw new Exception('Missing value for option: ' . $optionName); |
||
| 39 | } |
||
| 40 | if (str_starts_with($optionName, '--')) { |
||
| 41 | $loadedOptions[] = [ |
||
| 42 | 'shortName' => null, |
||
| 43 | 'longName' => substr($optionName, 2), |
||
| 44 | 'value' => $value |
||
| 45 | ]; |
||
| 46 | } elseif (str_starts_with($optionName, '-')) { |
||
| 47 | $loadedOptions[] = [ |
||
| 48 | 'shortName' => substr($optionName, 1), |
||
| 49 | 'longName' => null, |
||
| 50 | 'value' => $value |
||
| 51 | ]; |
||
| 52 | } else { |
||
| 53 | throw new KernelException('Uncovered option ' . $argv[$i]); |
||
| 54 | } |
||
| 55 | } elseif (str_starts_with($argv[$i], '--')) { |
||
| 56 | if (!isset($argv[$i+1])) { |
||
| 57 | throw new Exception('Missing value for option: ' . substr($argv[$i], 2)); |
||
| 58 | } |
||
| 59 | $loadedOptions[] = [ |
||
| 60 | 'shortName' => null, |
||
| 61 | 'longName' => substr($argv[$i], 2), |
||
| 62 | 'value' => $argv[$i+1] |
||
| 63 | ]; |
||
| 64 | unset($argv[$i+1]); |
||
| 65 | } elseif (str_starts_with($argv[$i], '-')) { |
||
| 66 | if (!isset($argv[$i+1])) { |
||
| 67 | throw new Exception('Missing value for option: ' . substr($argv[$i], 1)); |
||
| 68 | } |
||
| 69 | $loadedOptions[] = [ |
||
| 70 | 'shortName' => substr($argv[$i], 1), |
||
| 71 | 'longName' => null, |
||
| 72 | 'value' => $argv[$i+1] |
||
| 73 | ]; |
||
| 74 | unset($argv[$i+1]); |
||
| 75 | } else { |
||
| 76 | $loadedArguments[] = $argv[$i]; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | foreach ($this->arguments as $argument) { |
||
| 80 | $argument->setValue(array_shift($loadedArguments)); |
||
| 81 | if ($argument->type == Argument::REQUIRED && $argument->value == null) { |
||
| 82 | throw new Exception('Missing required argument: ' . $argument->name); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | foreach ($this->options as $option) { |
||
| 86 | $loadedOption = $this->optionExists($loadedOptions, $option->shortcut, $option->name); |
||
| 87 | if ($loadedOption !== false) { |
||
| 88 | $option->setValue($loadedOption['value']); |
||
| 89 | } |
||
| 90 | if ($option->type == Option::REQUIRED && $option->value == null) { |
||
| 91 | throw new Exception('Missing required option: ' . $option->name); |
||
| 92 | } |
||
| 164 | } |