Passed
Branch master (2a417c)
by Jean
05:00
created

PhpFilterer::validateRule()   D

Complexity

Conditions 16
Paths 52

Size

Total Lines 81

Duplication

Lines 16
Ratio 19.75 %

Code Coverage

Tests 42
CRAP Score 17.0485

Importance

Changes 0
Metric Value
cc 16
nc 52
nop 7
dl 16
loc 81
ccs 42
cts 50
cp 0.84
crap 17.0485
rs 4.8678
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * PhpFilterer
4
 *
5
 * @package php-logical-filter
6
 * @author  Jean Claveau
7
 */
8
namespace JClaveau\LogicalFilter\Filterer;
9
use       JClaveau\LogicalFilter\LogicalFilter;
10
use       JClaveau\LogicalFilter\Rule\EqualRule;
11
use       JClaveau\LogicalFilter\Rule\BelowRule;
12
use       JClaveau\LogicalFilter\Rule\AboveRule;
13
use       JClaveau\LogicalFilter\Rule\NotEqualRule;
14
use       JClaveau\LogicalFilter\Rule\InRule;
15
use       JClaveau\LogicalFilter\Rule\NotInRule;
16
use       JClaveau\LogicalFilter\FilteredKey;
17
use       JClaveau\LogicalFilter\FilteredValue;
18
19
/**
20
 */
21
class PhpFilterer extends Filterer
22
{
23
    /**
24
     */
25 8
    public function validateRule ($field, $operator, $value, $row, array $path, $all_operands, $options)
26
    {
27 8
        if ($field instanceof FilteredValue) {
28 2
            $value_to_validate = $field( $row );
29 2
        }
30 8
        elseif ($field instanceof FilteredKey) {
31 3
            $value_to_validate = $field( array_pop($path) );
32 3
        }
33 6
        elseif ( ! isset($row[(string) $field])) {
34 1
            $value_to_validate = null;
35 1
        }
36
        else {
37 6
            $value_to_validate = $row[ $field ];
38
        }
39
40 8
        if (EqualRule::operator === $operator) {
41 3
            if ( ! isset($value_to_validate)) {
42
                // ['field', '=', null] <=> isset($row['field'])
43
                // [row, '=', null] <=> $row !== null
44 1
                $result = null === $value;
45 1
            }
46
            else {
47
                // TODO support strict comparisons
48 3
                $result = $value_to_validate == $value;
49
            }
50 3
        }
51 6 View Code Duplication
        elseif (InRule::operator === $operator) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
52 1
            if ( ! isset($value_to_validate)) {
53
                $result = false;
54
            }
55
            else {
56 1
                $result = in_array($value_to_validate, $value);
57
            }
58 1
        }
59 5
        elseif (BelowRule::operator === $operator) {
60 2
            if ( ! isset($value_to_validate)) {
61
                $result = false;
62
            }
63
            else {
64 2
                $result = $value_to_validate < $value;
65
            }
66 2
        }
67 5
        elseif (AboveRule::operator === $operator) {
68 2
            if ( ! isset($value_to_validate)) {
69
                $result = false;
70
            }
71
            else {
72 2
                $result = $value_to_validate > $value;
73
            }
74 2
        }
75 5
        elseif (NotEqualRule::operator === $operator) {
76 4
            if (null === $value) {
77 1
                $result = isset($value_to_validate);
78 1
            }
79
            else {
80 3
                $result = $value_to_validate != $value;
81
            }
82 4
        }
83 2 View Code Duplication
        elseif (NotInRule::operator === $operator) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
84 2
            if ( ! isset($value_to_validate)) {
85 1
                $result = true;
86 1
            }
87
            else {
88 1
                $result = ! in_array($value_to_validate, $value);
89
            }
90 2
        }
91
        else {
92
            throw new \InvalidArgumentException(
93
                "Unhandled operator: " . $operator
94
            );
95
        }
96
97
        // var_dump(
98
        // "$field, $operator, " . var_export($value, true)
99
        // . ' vs ' . var_export($value_to_validate, true) . ' => ' . var_export($result, true)
100
        // . "\n\n"
101
        // . var_export($row, true)
102
        // );
103
        // exit;
104 8
        return $result;
105
    }
106
107
    /**/
108
}
109