PhpFilterer::validateRule()   F
last analyzed

Complexity

Conditions 23
Paths 72

Size

Total Lines 116

Duplication

Lines 79
Ratio 68.1 %

Code Coverage

Tests 71
CRAP Score 23.0108

Importance

Changes 0
Metric Value
cc 23
nc 72
nop 7
dl 79
loc 116
ccs 71
cts 73
cp 0.9726
crap 23.0108
rs 3.3333
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
10
use JClaveau\LogicalFilter\LogicalFilter;
11
use JClaveau\LogicalFilter\Rule\EqualRule;
12
use JClaveau\LogicalFilter\Rule\BelowRule;
13
use JClaveau\LogicalFilter\Rule\AboveRule;
14
use JClaveau\LogicalFilter\Rule\BelowOrEqualRule;
15
use JClaveau\LogicalFilter\Rule\AboveOrEqualRule;
16
use JClaveau\LogicalFilter\Rule\NotEqualRule;
17
use JClaveau\LogicalFilter\Rule\InRule;
18
use JClaveau\LogicalFilter\Rule\NotInRule;
19
use JClaveau\LogicalFilter\Rule\RegexpRule;
20
use JClaveau\LogicalFilter\FilteredKey;
21
use JClaveau\LogicalFilter\FilteredValue;
22
23
/**
24
 */
25
class PhpFilterer extends Filterer
26
{
27
    /**
28
     */
29 28
    public function validateRule ($field, $operator, $value, $row, array $path, $all_operands, $options)
30
    {
31 28
        if ($field instanceof FilteredValue) {
32 3
            $value_to_validate = $field( $row );
33 3
        }
34 27
        elseif ($field instanceof FilteredKey) {
35 4
            $value_to_validate = $field( array_pop($path) );
36 4
        }
37 24
        elseif (! isset($row[(string) $field])) {
38 18
            $value_to_validate = null;
39 18
        }
40
        else {
41 24
            $value_to_validate = $row[ $field ];
42
        }
43
44 28
        if (EqualRule::operator === $operator) {
45 7 View Code Duplication
            if (! isset($value_to_validate)) {
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...
46
                // ['field', '=', null] <=> isset($row['field'])
47
                // [row, '=', null] <=> $row !== null
48 5
                $result = null === $value;
49 5
            }
50
            else {
51
                // TODO support strict comparisons
52 7
                $result = $value_to_validate == $value;
53
            }
54 7
        }
55 23 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...
56 2
            $result = in_array(
57 2
                isset($value_to_validate) ? $value_to_validate : null,
58
                $value
59 2
            );
60 2
        }
61 21 View Code Duplication
        elseif (BelowRule::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...
62 4
            if (! isset($value_to_validate)) {
63 1
                $result = false;
64 1
            }
65
            else {
66 4
                $result = $value_to_validate < $value;
67
            }
68 4
        }
69 20 View Code Duplication
        elseif (AboveRule::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...
70 7
            if (! isset($value_to_validate)) {
71 4
                $result = false;
72 4
            }
73
            else {
74 7
                $result = $value_to_validate > $value;
75
            }
76 7
        }
77 15 View Code Duplication
        elseif (NotEqualRule::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...
78 5
            if (null === $value) {
79 1
                $result = isset($value_to_validate);
80 1
            }
81
            else {
82 4
                $result = $value_to_validate != $value;
83
            }
84 5
        }
85 10 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...
86 2
            $result = ! in_array(
87 2
                isset($value_to_validate) ? $value_to_validate : null,
88
                $value
89 2
            );
90 2
        }
91 8 View Code Duplication
        elseif (AboveOrEqualRule::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...
92 3
            if (! isset($value_to_validate)) {
93 3
                $result = false;
94 3
            }
95
            else {
96 3
                $result = $value_to_validate >= $value;
97
            }
98 3
        }
99 6 View Code Duplication
        elseif (BelowOrEqualRule::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...
100 4
            if (! isset($value_to_validate)) {
101 2
                $result = false;
102 2
            }
103
            else {
104 4
                $result = $value_to_validate <= $value;
105
            }
106 4
        }
107 2
        elseif (RegexpRule::operator === $operator) {
108 2
            if (! isset($value_to_validate)) {
109 1
                $result = false;
110 1
            }
111 View Code Duplication
            else {
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...
112
                try {
113
                    // TODO support optionnal parameters (offest mainly) ?
114 2
                    $result = preg_match($value, $value_to_validate);
115
                }
116 2
                catch (\Exception $e) {
117
                    // The documentation of preg_match() is wrong and preg_last_error()
118
                    // is useless as preg_match returns 0 instead of false
119
                    // and then throws an exception with PHP 5.6
120 1
                    throw new \InvalidArgumentException(
121 1
                        "PCRE error ".var_export($e->getMessage(), true).
122 1
                        " while applying the regexp ".var_export($value, true)." to "
123 1
                        .var_export($value_to_validate, true)
124 1
                    );
125
                }
126
127 1
                $result = (bool) $result;
128
            }
129 1
        }
130
        else {
131
            throw new \InvalidArgumentException(
132
                "Unhandled operator: " . $operator
133
            );
134
        }
135
136
        // var_dump(
137
        // "$field, $operator, " . var_export($value, true)
138
        // . ' vs ' . var_export($value_to_validate, true) . ' => ' . var_export($result, true)
139
        // . "\n\n"
140
        // . var_export($row, true)
141
        // );
142
        // exit;
143 27
        return $result;
144
    }
145
146
    /**/
147
}
148