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

SwitchChoose::applyToNode()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8.0109

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
ccs 17
cts 18
cp 0.9444
rs 5.3846
cc 8
eloc 15
nc 11
nop 1
crap 8.0109
1
<?php
2
3
namespace LegalThings\DataEnricher\Processor;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
use LegalThings\DataEnricher\Processor\Helper;
8
9
/**
10
 * Choose one of the child properties based on a property in the document
11
 */
12
class SwitchChoose implements Processor
13
{
14
    use Processor\Implementation,
15
        Helper\GetByReference
16
    {
17
        Helper\GetByReference::withSourceAndTarget insteadof Processor\Implementation;
18
    }
19
    
20
    /**
21
     * Apply processing to a single node
22
     * 
23
     * @param Node $node
24
     */
25 5
    public function applyToNode(Node $node)
26
    {
27 5
        $instruction = $node->getInstruction($this);
28
29
        // for bc
30 5
        if (is_string($instruction)) {
31 1
            $cases = $node->getResult();
32
33 1
            $result = $this->choose($instruction, $cases);
0 ignored issues
show
Bug introduced by
It seems like $cases defined by $node->getResult() on line 31 can also be of type array; however, LegalThings\DataEnricher...\SwitchChoose::choose() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
34 1
            return $node->setResult($result);
35
        }
36
        
37 4
        if (is_array($instruction) || is_object($instruction)) {
38 4
            $instruction = (object)$instruction;
39 4
        }
40
        
41 4
        if (!isset($instruction->on) || !isset($instruction->options)) {
42
            return;
43
        }
44
        
45 4
        $instruction->options = (object)$instruction->options;
46
        
47 4
        $result = isset($instruction->default) ? $instruction->default : null;
48
        
49 4
        if (isset($instruction->options->{$instruction->on})) {
50 2
            $result = $instruction->options->{$instruction->on};
51 2
        }
52
        
53 4
        $node->setResult($result);
54 4
    }
55
    
56
    /**
57
     * Choose on of the cases
58
     * 
59
     * @param string $ref    Property name of source
60
     * @param object $cases
61
     * @return mixed
62
     */
63 1
    protected function choose($ref, $cases)
64
    {
65 1
        $test = $this->getByReference($ref, $this->source, $this->target);
66 1
        return isset($cases->$test) ? $cases->$test : null;
67
    }
68
}
69