| Conditions | 14 |
| Paths | 68 |
| Total Lines | 51 |
| Code Lines | 25 |
| Lines | 12 |
| Ratio | 23.53 % |
| Changes | 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 |
||
| 75 | public function parse(array $argv) |
||
| 76 | { |
||
| 77 | $argc = count($argv); |
||
| 78 | $optionValues = []; |
||
| 79 | |||
| 80 | for ($i = 1; $i < $argc; ++$i) { |
||
| 81 | if (0 === strpos($argv[$i], '--')) { |
||
| 82 | // it is an option selector |
||
| 83 | $p = substr($argv[$i], 2); // strip the dashes |
||
|
|
|||
| 84 | $pO = []; |
||
| 85 | while ($i + 1 < $argc && false === strpos($argv[$i + 1], '--')) { |
||
| 86 | $pO[] = $argv[++$i]; |
||
| 87 | } |
||
| 88 | if (1 === count($pO)) { |
||
| 89 | $optionValues[$p] = $pO[0]; |
||
| 90 | } else { |
||
| 91 | $optionValues[$p] = $pO; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | // --help is special |
||
| 97 | if (array_key_exists('help', $optionValues)) { |
||
| 98 | return new Config(['help' => true]); |
||
| 99 | } |
||
| 100 | |||
| 101 | // check if any of the required keys is missing |
||
| 102 | foreach (array_keys($this->optionList) as $opt) { |
||
| 103 | View Code Duplication | if ($this->optionList[$opt][2]) { |
|
| 104 | // required |
||
| 105 | if (!array_key_exists($opt, $optionValues)) { |
||
| 106 | throw new CliException(sprintf('missing required parameter "--%s"', $opt)); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | // check if any of the options that require a value has no value |
||
| 112 | foreach (array_keys($this->optionList) as $opt) { |
||
| 113 | if ($this->optionList[$opt][1]) { |
||
| 114 | // check if it is actually there |
||
| 115 | View Code Duplication | if (array_key_exists($opt, $optionValues)) { |
|
| 116 | // must have value |
||
| 117 | if (0 === count($optionValues[$opt])) { |
||
| 118 | throw new CliException(sprintf('missing required parameter value for option "--%s"', $opt)); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | return new Config($optionValues); |
||
| 125 | } |
||
| 126 | } |
||
| 127 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.