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

PhpFilterer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 88
Duplicated Lines 18.18 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 84%

Importance

Changes 0
Metric Value
dl 16
loc 88
ccs 42
cts 50
cp 0.84
rs 10
c 0
b 0
f 0
wmc 16
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
D validateRule() 16 81 16

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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