Completed
Push — master ( a098b4...5a38a9 )
by Renato
05:12
created

FilterWhere::filter()   C

Complexity

Conditions 12
Paths 16

Size

Total Lines 64
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 36
nc 16
nop 3
dl 0
loc 64
ccs 38
cts 38
cp 1
crap 12
rs 6.0561
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
namespace NwLaravel\Repositories\Criterias\Filters;
4
5
use InvalidArgumentException;
6
7
class FilterWhere implements FilterInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $table;
13
14
    /**
15
     * @var array
16
     */
17
    protected $columns;
18
19
    /**
20
     * @var array
21
     */
22
    protected $dates;
23
24
    /**
25
     * @var string
26
     */
27
    protected $operator;
28
29
    /**
30
     * Construct
31
     *
32
     * @param string $table
33
     * @param array  $columns
34
     * @param array  $dates
35
     * @param string $operator
36
     */
37 24
    public function __construct($table, $columns, $dates, $operator = '=')
38
    {
39 24
        $this->table = $table;
40 24
        $this->columns = $columns;
41 24
        $this->dates = $dates;
42 24
        $this->operator = $operator;
43 24
    }
44
45
    /**
46
     * Filter
47
     *
48
     * @param Query\Builder $query
49
     * @param int|string    $key
50
     * @param mixed         $value
51
     *
52
     * @return boolean
53
     */
54 23
    public function filter($query, $key, $value)
55
    {
56 23
        $operator = $this->operator;
57
58 23
        $validOperator = function ($operator) {
59 23
            $operators = ['=', '<', '>', '<=', '>=', '<>', '!='];
60 23
            return in_array($operator, $operators);
61 23
        };
62
63 23
        if (! $validOperator($operator)) {
64 2
            throw new InvalidArgumentException("Illegal operator and value combination.");
65
        }
66
67
        // Raw Expression with Bidding
68 21
        if (strpos($key, '?') !== false) {
69 2
            $query = $query->whereRaw($key, (array) $value);
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70 2
            return true;
71
        }
72
73 20
        $table = $this->table;
74 20
        $column = $key;
75 20
        if (preg_match('/^([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9_\-]+)/', $key, $matches)) {
76 3
            $table = $matches[1];
77 3
            $column = $matches[2];
78 3
        }
79
80
        // Montagem Tabela com Coluns
81 20
        $key = $table.'.'.$column;
82
83
        // Attributes Valids
84 20
        if (in_array($column, $this->columns)) {
85 12
            if (is_null($value)) {
86 3
                if ($operator == '!=' || $operator == '<>') {
87 2
                    $query = $query->whereNotNull($key);
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88 2
                } else {
89 2
                    $query = $query->whereNull($key);
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
                }
91 3
                return true;
92
            }
93
94 10
            if (in_array($column, $this->dates)) {
95 5
                $filterDate = new FilterDate($operator);
96 5
                return $filterDate->filter($query, $key, $value);
97
            }
98
99
            // Using Where In With An Array
100 6
            if (is_array($value)) {
101 3
                if ($operator == '!=' || $operator == '<>') {
102 2
                    $query = $query->whereNotIn($key, $value);
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
103 2
                } else {
104 2
                    $query = $query->whereIn($key, $value);
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
105
                }
106 3
                return true;
107
            }
108
109
            // Busca Direta
110 4
            $query = $query->where($key, $operator, $value);
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111
112 4
            return true;
113
        }
114
115 9
        $filterBetween = new FilterBetween($this->table, $this->columns, $this->dates);
116 9
        return $filterBetween->filter($query, $key, $value);
117
    }
118
}
119