Completed
Push — master ( 071064...9995fc )
by Sven
10s
created

Enrich   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 50.91 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 0
cbo 3
dl 28
loc 55
ccs 22
cts 28
cp 0.7856
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C applyToNode() 28 45 11

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
 * Enriches objects in arrays by matching them
10
 */
11
class Enrich implements Processor
12
{
13
    use Processor\Implementation;
14
    
15
    /**
16
     * Apply processing to a single node
17
     * 
18
     * @param Node $node
19
     */
20 2
    public function applyToNode(Node $node)
21
    {
22 2
        $instruction = $node->getInstruction($this);
23 2
        $source = $node->getResult() ?: [];
24
        
25 2
        if (is_string($instruction->match)) {
26 1
            $match = ['extra' => $instruction->match, 'source' => $instruction->match];
27 1
        } else {
28 1
            $match = (array)$instruction->match;
29
        }
30
        
31 2
        $extraIndexed = [];
32
        
33 2 View Code Duplication
        foreach ($instruction->extra as $extra) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
34 2
            $key = \Jasny\DotKey::on($extra)->get($match['extra']);
35
            
36 2
            if (!isset($key) || isset($extraIndexed[$key])) {
37
                continue;
38
            }
39
            
40 2
            if (!is_scalar($key)) {
41
                trigger_error("Trying to match on non-scalar type", E_WARNING);
42
                continue;
43
            }
44
            
45 2
            $extraIndexed[$key] = $extra;
46 2
        }
47
        
48 2 View Code Duplication
        foreach ($source as &$item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
49 2
            $key = \Jasny\DotKey::on($item)->get($match['source']);
50
            
51 2
            if (!isset($key) || !isset($extraIndexed[$key])) {
52
                continue;
53
            }
54
            
55 2
            if (!is_scalar($key)) {
56
                trigger_error("Trying to match on non-scalar type", E_WARNING);
57
                continue;
58
            }
59
            
60 2
            $item = array_merge((array)$item, (array)$extraIndexed[$key]);
61 2
        }
62
        
63 2
        $node->setResult($source);
64 2
    }
65
}
66