Failed Conditions
Push — master ( 04820f...7dec56 )
by Moesjarraf
03:10
created

SwitchChoose::applyToNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
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 1
    public function applyToNode(Node $node)
26
    {
27 1
        $ref = $node->getInstruction($this);
28 1
        $cases = $node->getResult();
29
        
30 1
        $result = $this->choose($ref, $cases);
0 ignored issues
show
Bug introduced by
It seems like $cases defined by $node->getResult() on line 28 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...
31 1
        $node->setResult($result);
32 1
    }
33
    
34
    /**
35
     * Choose on of the cases
36
     * 
37
     * @param string $ref    Property name of source
38
     * @param object $cases
39
     * @return mixed
40
     */
41 1
    protected function choose($ref, $cases)
42
    {
43 1
        $test = $this->getByReference($ref, $this->source, $this->target);
44 1
        return isset($cases->$test) ? $cases->$test : null;
45
    }
46
}
47