Failed Conditions
Push — master ( 0bbe18...211e20 )
by Moesjarraf
03:53
created

Sum::resolve()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 6.6829

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 20
loc 20
ccs 11
cts 15
cp 0.7332
rs 8.8571
cc 6
eloc 10
nc 4
nop 1
crap 6.6829
1
<?php
2
3
namespace LegalThings\DataEnricher\Processor;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
8
/**
9
 * Calculate sum of the numbers in an array
10
 */
11
class Sum implements Processor
12
{
13
    use Processor\Implementation;
14
    
15
    /**
16
     * Apply processing to a single node
17
     * 
18
     * @param Node $node
19
     */
20 4 View Code Duplication
    public function applyToNode(Node $node)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22 4
        $instruction = $node->getInstruction($this);
23
        
24 4
        $list = $this->resolve($instruction);
25
        
26 4
        if (isset($list) && !is_array($list)) {
27
            throw new \Exception("Unable to apply {$this->property} processing instruction:"
28
                . " Expected an array, got a " . (is_object($list) ? get_class($list) . ' ' : '') . gettype($list));
29
        }
30
        
31 4
        $result = $this->execute((array)$list);
32 4
        $node->setResult($result);
33 4
    }
34
    
35
    /**
36
     * Resolve processing nodes in the instruction
37
     * 
38
     * @param array $list
39
     * @return array
40
     */
41 4 View Code Duplication
    public function resolve($list)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43 4
        if ($list instanceof Node) {
44 1
            $list = $list->getResult();
45 1
        }
46
        
47 4
        if (is_array($list)) {
48 4
            foreach ($list as &$item) {
49 4
                if ($item instanceof Node) {
50
                    $item = $item->getResult();
51
                }
52
53 4
                if ($item instanceof \Traversable) {
54
                    $item = iterator_to_array($item);
55
                }
56 4
            }
57 4
        }
58
        
59 4
        return $list;
60
    }
61
    
62
    /**
63
     * Calculate sum
64
     * 
65
     * @param array $list
66
     * 
67
     * @return number
68
     */
69 4
    protected function execute(array $list)
70
    {
71 4
        $sum = array_sum($list);
72
        
73 4
        return $sum;
74
    }
75
}
76
77