Failed Conditions
Pull Request — master (#32)
by Moesjarraf
06:49
created

Reference   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 44
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 0
cbo 3
dl 44
loc 44
ccs 5
cts 5
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B applyToNode() 16 16 5
B objectivy() 12 12 5

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
use function JmesPath\search as jmespath_search;
8
use JmesPath\Utils;
9
10
/**
11
 * Reference JMESPath processor
12
 * @see http://jmespath.org/
13
 */
14 View Code Duplication
class Reference implements Processor
0 ignored issues
show
Duplication introduced by
This class 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...
15
{
16
    use Processor\Implementation;
17
    
18
    /**
19
     * Apply processing to a single node
20
     * 
21
     * @param Node $node
22
     */
23
    public function applyToNode(Node $node)
24
    {
25 1
        $instruction = $node->getInstruction($this);
26
        
27 1
        if (is_array($instruction) || is_object($instruction)) {
28
            $instruction = (object)$instruction;
29 1
        }
30 1
        
31 1
        $input = is_string($instruction) ? $node->getResult() : $instruction->input;
32
        $query = is_string($instruction) ? $instruction : $instruction->query;
33
        
34
        $result = jmespath_search($query, $input);
35
        static::objectivy($result);
36
        
37
        $node->setResult($result);
38
    }
39
    
40
    /**
41
     * Cast associated arrays to objects
42
     * 
43
     * @return mixed
44
     */
45
    protected static function objectivy(&$value)
46
    {
47
        if (Utils::isObject($value)) {
48
            $value = (object)$value;
49
        }
50
        
51
        if (is_array($value) || is_object($value)) {
52
            foreach ($value as &$item) {
53
                static::objectivy($item);
54
            }
55
        }
56
    }
57
}
58