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