Completed
Push — master ( 40fce1...025fda )
by Oleg
06:38
created

MergeAttributes::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
namespace Malezha\Menu\Support;
3
4
class MergeAttributes
5
{
6
    protected $arrays = [];
7
    
8
    /**
9
     * @param array $arrays,...
0 ignored issues
show
Bug introduced by
There is no parameter named $arrays,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
10
     * @throws \RuntimeException
11
     */
12
    public function __construct()
13
    {
14
        if (func_num_args() <= 1) {
15
            throw new \RuntimeException("Must has min two parameters.");
16
        }
17
        
18
        $this->arrays = func_get_args();
19
    }
20
    
21
    /**
22
     * @return array
23
     */
24
    public function merge()
25
    {
26
        $arrays = $this->arrays;
27
        $keys = [];
28
29
        foreach ($arrays as $array) {
30
            $keys = array_merge($keys, array_keys($array));
31
        }
32
33
        $keys = array_unique($keys);
34
35
        $merged = array_fill_keys($keys, null);
36
37
        foreach ($arrays as $array) {
38
            foreach ($keys as $key) {
39
                if (array_key_exists($key, $array)) {
40
                    $merged[$key] = $this->mergeValues($merged[$key], $array[$key]);
41
                }
42
            }
43
        }
44
45
        return $merged;
46
    }
47
    
48
    /**
49
     * @param string $valueOne
50
     * @param string $valueTwo
51
     * @return string
52
     */
53
    protected function mergeValues($valueOne, $valueTwo)
54
    {
55
        if (is_null($valueOne)) {
56
            return $valueTwo;
57
        }
58
        
59
        $valueOne = explode(' ', $valueOne);
60
        $valueTwo = explode(' ', $valueTwo);
61
62
        $merged = array_merge($valueOne, $valueTwo);
63
64
        return trim(implode(' ', $merged));
65
    }
66
}