| Conditions | 9 |
| Paths | 72 |
| Total Lines | 59 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 2 | 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 |
||
| 44 | protected function getConvertedOptions(array $options_map): array |
||
| 45 | { |
||
| 46 | $argv = $this->getGlobalArgV(); |
||
| 47 | $just_longs = array_filter( |
||
| 48 | $options_map, |
||
| 49 | /** |
||
| 50 | * @param mixed $k |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | static fn($k): bool => is_int($k), |
||
| 54 | ARRAY_FILTER_USE_KEY |
||
| 55 | ); |
||
| 56 | $options_map = array_filter( |
||
| 57 | $options_map, |
||
| 58 | /** |
||
| 59 | * @param mixed $k |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | static fn($k): bool => is_string($k), |
||
| 63 | ARRAY_FILTER_USE_KEY |
||
| 64 | ); |
||
| 65 | $short_options = join('', array_keys($options_map)); |
||
| 66 | $long_options = array_merge(array_values($options_map), $just_longs); |
||
| 67 | $args = $this->getOpt($short_options, $long_options); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Map words to characters to only return word arguments |
||
| 71 | * @var array<string, string> $short_to_long_map |
||
| 72 | */ |
||
| 73 | $short_to_long_map = []; |
||
| 74 | foreach ($options_map as $short => $long) { |
||
| 75 | $short_to_long_map[trim($short, ':')] = trim($long, ':'); |
||
| 76 | } |
||
| 77 | foreach ($short_to_long_map as $short => $long) { |
||
| 78 | if (isset($args[$short])) { |
||
| 79 | $args[$long] = $args[$short]; |
||
| 80 | unset($args[$short]); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | // Flip no-value option present to a true value |
||
| 85 | // A no-value option that is present multiple times converted to a number |
||
| 86 | foreach ($args as $key => $value) { |
||
| 87 | if ($value === false) { |
||
| 88 | $args[$key] = true; |
||
| 89 | } elseif (is_array($value)) { |
||
| 90 | $args[$key] = count($value); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | // Non present values get false value |
||
| 95 | foreach (array_values($long_options) as $long) { |
||
| 96 | $long = trim($long, ':'); |
||
| 97 | if (array_key_exists($long, $args) === false) { |
||
| 98 | $args[$long] = false; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | return [$argv[0], array_pop($argv), $args]; |
||
| 103 | } |
||
| 257 |