| Conditions | 15 |
| Paths | 153 |
| Total Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 27 |
| CRAP Score | 15.225 |
| 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 declare(strict_types=1); |
||
| 10 | function options_merge(array $base, array $options): array |
||
| 11 | { |
||
| 12 | 5 | $merge = true; |
|
| 13 | 5 | foreach ($base as $key => $value) { |
|
| 14 | 2 | if (is_numeric($key)) { |
|
| 15 | 2 | $merge = false; |
|
| 16 | } |
||
| 17 | } |
||
| 18 | 5 | foreach ($options as $name => $option) { |
|
| 19 | 4 | if (is_numeric($name)) { |
|
| 20 | 4 | $merge = false; |
|
| 21 | } |
||
| 22 | } |
||
| 23 | |||
| 24 | 5 | if ($merge === false) { |
|
| 25 | 2 | $new = []; |
|
| 26 | |||
| 27 | 2 | foreach ($base as $key => $value) { |
|
| 28 | 2 | if (in_array($value, $new, true)) { |
|
| 29 | continue; |
||
| 30 | } |
||
| 31 | 2 | $new[] = $value; |
|
| 32 | } |
||
| 33 | 2 | foreach ($options as $name => $option) { |
|
| 34 | 2 | if (in_array($option, $new, true)) { |
|
| 35 | 1 | continue; |
|
| 36 | } |
||
| 37 | 1 | $new[] = $option; |
|
| 38 | } |
||
| 39 | |||
| 40 | 2 | return $new; |
|
| 41 | } |
||
| 42 | |||
| 43 | 5 | foreach ($base as $key => $value) { |
|
| 44 | 2 | if (!isset($options[$key])) { |
|
| 45 | continue; |
||
| 46 | } |
||
| 47 | |||
| 48 | 2 | $option = $options[$key]; |
|
| 49 | 2 | unset($options[$key]); |
|
| 50 | |||
| 51 | 2 | if (is_array($value) && is_array($option)) { |
|
| 52 | 2 | $base[$key] = options_merge($value, $option); |
|
| 53 | 2 | continue; |
|
| 54 | } |
||
| 55 | |||
| 56 | $base[$key] = $option; |
||
| 57 | } |
||
| 58 | |||
| 59 | 5 | foreach ($options as $name => $option) { |
|
| 60 | 4 | $base[$name] = $option; |
|
| 61 | } |
||
| 62 | |||
| 63 | 5 | return $base; |
|
| 64 | } |
||
| 65 |