| 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 |
||
| 56 | public function parse(array $argv) |
||
| 57 | { |
||
| 58 | $argc = count($argv); |
||
| 59 | $optionValues = []; |
||
| 60 | |||
| 61 | for ($i = 1; $i < $argc; ++$i) { |
||
| 62 | if (0 === strpos($argv[$i], '--')) { |
||
| 63 | // it is an option selector |
||
| 64 | $p = substr($argv[$i], 2); // strip the dashes |
||
| 65 | $pO = []; |
||
| 66 | while ($i + 1 < $argc && false === strpos($argv[$i + 1], '--')) { |
||
| 67 | $pO[] = $argv[++$i]; |
||
| 68 | } |
||
| 69 | if (1 === count($pO)) { |
||
| 70 | $optionValues[$p] = $pO[0]; |
||
| 71 | } else { |
||
| 72 | $optionValues[$p] = $pO; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | // --help is special |
||
| 78 | if (array_key_exists('help', $optionValues)) { |
||
| 79 | return new Config(['help' => true]); |
||
| 80 | } |
||
| 81 | |||
| 82 | // check if any of the required keys is missing |
||
| 83 | foreach (array_keys($this->optionList) as $opt) { |
||
| 84 | View Code Duplication | if ($this->optionList[$opt][2]) { |
|
|
|
|||
| 85 | // required |
||
| 86 | if (!array_key_exists($opt, $optionValues)) { |
||
| 87 | throw new CliException(sprintf('missing required parameter "--%s"', $opt)); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | // check if any of the options that require a value has no value |
||
| 93 | foreach (array_keys($this->optionList) as $opt) { |
||
| 94 | if ($this->optionList[$opt][1]) { |
||
| 95 | // check if it is actually there |
||
| 96 | View Code Duplication | if (array_key_exists($opt, $optionValues)) { |
|
| 97 | // must have value |
||
| 98 | if (0 === count($optionValues[$opt])) { |
||
| 99 | throw new CliException(sprintf('missing required parameter value for option "--%s"', $opt)); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | return new Config($optionValues); |
||
| 106 | } |
||
| 107 | } |
||
| 108 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.