Failed Conditions
Push — master ( da580a...04820f )
by Moesjarraf
03:10
created

Merge::merge()   D

Complexity

Conditions 10
Paths 21

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 42
ccs 26
cts 26
cp 1
rs 4.8196
cc 10
eloc 23
nc 21
nop 1
crap 10

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace LegalThings\DataEnricher\Processor;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
8
/**
9
 * Merge multiple object into one
10
 */
11
class Merge implements Processor
12
{
13
    use Processor\Implementation;
14
    
15
    /**
16
     * Apply processing to a single node
17
     * 
18
     * @param Node $node
19
     */
20 11
    public function applyToNode(Node $node)
21
    {
22 11
        $instruction = $node->getInstruction($this);
23
        
24 11
        $list = $this->resolve($instruction);
25
        
26 11
        if (isset($list) && !is_array($list)) {
27 1
            throw new \Exception("Unable to apply {$this->property} processing instruction:"
28 1
                . " Expected an array, got a " . (is_object($list) ? get_class($list) . ' ' : '') . gettype($list));
29
        }
30
        
31 10
        $result = $this->merge((array)$list);
32 9
        $node->setResult($result);
33 9
    }
34
    
35
    /**
36
     * Resolve processing nodes in the instruction
37
     * 
38
     * @param array $list
39
     * @return array
40
     */
41 11
    public function resolve($list)
42
    {
43 11
        if ($list instanceof Node) {
44 1
            $list = $list->getResult();
45 1
        }
46
        
47 11
        if (is_array($list)) {
48 10
            foreach ($list as &$item) {
49 9
                if ($item instanceof Node) {
50 1
                    $item = $item->getResult();
51 1
                }
52
53 9
                if ($item instanceof \Traversable) {
54 1
                    $item = iterator_to_array($item);
55 1
                }
56 10
            }
57 10
        }
58
        
59 11
        return $list;
60
    }
61
    
62
    /**
63
     * Merge properties of an object
64
     * 
65
     * @param array $list
66
     * @return \stdClass|array|string
67
     */
68 10
    protected function merge(array $list)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
69
    {
70 10
        if (empty($list)) {
71 1
            return null;
72
        }
73
        
74 9
        $scalar = [];
75
        
76 9
        foreach ($list as $key => &$item) {
77 9
            if (!isset($item)) {
78 2
                unset($list[$key]);
79 2
                continue;
80
            }
81
            
82 8
            if (is_object($item)) {
83 3
                $item = get_object_vars($item);
84 3
            }
85
            
86 8
            $scalar[] = is_scalar($item);
87 9
        }
88
89 9
        if (count(array_unique($scalar)) > 1) {
90 1
            throw new \Exception("Unable to apply {$this->property} processing instruction:"
91 1
                . " Mixture of scalar and non-scalar values");
92
        }
93
        
94 8
        if (count($scalar) > 0 && $scalar[0]) {
95 2
            $result = join('', $list);
96 2
        } else {
97 6
            if(empty($list)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
98 1
                return null;
99
            }
100 5
            $result = call_user_func_array('array_merge', $list);
101
            
102
            // Is associative array
103 5
            if (array_keys($result) !== array_keys(array_keys($result))) {
104 4
                $result = (object)$result;
105 4
            }
106
        }
107
        
108 7
        return $result;
109
    }
110
}
111
112