Completed
Push — master ( 610136...82575c )
by De Cramer
06:15 queued 03:00
created

Condition::apply()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oliverde8\Component\RuleEngine\Rules;
4
5
use Oliverde8\Component\RuleEngine\Exceptions\Condition\UnSupportedOperationException;
6
use Oliverde8\Component\RuleEngine\Exceptions\RuleException;
7
8
/**
9
 * Class Condition
10
 *
11
 * @author    de Cramer Oliver<[email protected]>
12
 * @copyright 2018 Oliverde8
13
 * @package Oliverde8\Component\RuleEngine\Rules
14
 */
15
class Condition extends AbstractRule
16
{
17
    /**
18
     * @inheritdoc
19
     */
20 6
    public function apply($rowData, &$transformedData, $options = [])
21
    {
22 6
        $valueToCmp = $this->applyRules($rowData, $transformedData, $options['if'], $options);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $valueToCmp is correct as $this->applyRules($rowDa...ptions['if'], $options) targeting Oliverde8\Component\Rule...Condition::applyRules() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
23 6
        $value = $this->applyRules($rowData, $transformedData, $options['value'], $options);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as $this->applyRules($rowDa...ons['value'], $options) targeting Oliverde8\Component\Rule...Condition::applyRules() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
24
25 6
        $result = $this->compare($valueToCmp, $value, $options['operation'])? $options['then'] : $options['else'];
26
27 5
        return $this->applyRules($rowData, $transformedData, $result, $options);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->applyRules($rowDa...ata, $result, $options) targeting Oliverde8\Component\Rule...Condition::applyRules() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
28
    }
29
30
    /**
31
     * Compare a value.
32
     *
33
     * @param mixed $value      The value to compare.
34
     * @param mixed $with       The value to compare the value with.
35
     * @param string $operation The operation to use for the comparison.
36
     *
37
     * @throws UnSupportedOperationException
38
     *
39
     * @return bool
40
     */
41 6
    protected function compare($value, $with, $operation)
42
    {
43
        switch ($operation) {
44 6
            case 'eq' :
45 2
                return $value == $with;
46 4
            case 'neq' :
47 1
                return $value != $with;
48 3
            case 'in' :
49 2
                return in_array($value, $with);
50
            default:
51 1
                throw new UnSupportedOperationException("Operation '$operation' is not supported!");
52
        }
53
    }
54
55
    /**
56
     * Apply sub rules.
57
     *
58
     * @param array $rowData         Row to apply the transformation on
59
     * @param array $transformedData Data already transformed.
60
     * @param array $rules           Rule definition to apply.
61
     * @param array $options         Options for the rules.
62
     *
63
     * @return null
64
     * @throws RuleException
65
     */
66 6
    protected function applyRules($rowData, $transformedData, $rules, $options)
67
    {
68
        // Remove fields used by the current rule and not needed by childs.
69 6
        unset($options['if']);
70 6
        unset($options['value']);
71 6
        unset($options['operation']);
72 6
        unset($options['then']);
73 6
        unset($options['else']);
74
75 6
        return $this->ruleApplier->apply($rowData, $transformedData, $rules, $options);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->ruleApplie...Data, $rules, $options) also could return the type string which is incompatible with the documented return type null.
Loading history...
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 6
    public function validate($options)
82
    {
83 6
        $this->requireOption('if', $options);
84 5
        $this->requireOption('value', $options);
85 5
        $this->requireOption('operation', $options);
86 5
        $this->requireOption('then', $options);
87 5
        $this->requireOption('else', $options);
88 5
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93 2
    public function getRuleCode()
94
    {
95 2
        return 'condition';
96
    }
97
}