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

Enrich::applyToNode()   C

Complexity

Conditions 11
Paths 64

Size

Total Lines 45
Code Lines 25

Duplication

Lines 28
Ratio 62.22 %

Code Coverage

Tests 22
CRAP Score 12.1908

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 28
loc 45
ccs 22
cts 28
cp 0.7856
rs 5.2653
cc 11
eloc 25
nc 64
nop 1
crap 12.1908

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
 * 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