Failed Conditions
Pull Request — master (#32)
by Moesjarraf
09:36
created

Reference::objectivy()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 12
ccs 0
cts 0
cp 0
rs 8.8571
cc 5
eloc 6
nc 6
nop 1
crap 30
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