Conditions | 10 |
Paths | 8 |
Total Lines | 32 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
25 | function array_merge_recursive_unique() |
||
|
|||
26 | { |
||
27 | $arrays = func_get_args(); |
||
28 | |||
29 | if (count($arrays) < 2) { |
||
30 | if ($arrays === []) { |
||
31 | return []; |
||
32 | } else { |
||
33 | return $arrays[0]; |
||
34 | } |
||
35 | } |
||
36 | |||
37 | $merged = array_shift($arrays); |
||
38 | |||
39 | foreach ($arrays as $array) { |
||
40 | foreach ($array as $key => $value) { |
||
41 | if (is_array($value) && (isset($merged[$key]) && is_array($merged[$key]))) { |
||
42 | $merged[$key] = array_merge_recursive_unique($merged[$key], $value); |
||
43 | } else { |
||
44 | if (is_numeric($key)) { |
||
45 | if (! in_array($value, $merged)) { |
||
46 | $merged[] = $value; |
||
47 | } |
||
48 | } else { |
||
49 | $merged[$key] = $value; |
||
50 | } |
||
51 | } |
||
52 | } |
||
53 | unset($key, $value); |
||
54 | } |
||
55 | |||
56 | return $merged; |
||
57 | } |
||
59 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.