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

Sum   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 52.31 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 2
dl 34
loc 65
ccs 21
cts 27
cp 0.7778
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A applyToNode() 14 14 4
B resolve() 20 20 6
A execute() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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