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

FilterBetween   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 64
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B filter() 0 23 6
1
<?php
2
3
namespace NwLaravel\Repositories\Criterias\Filters;
4
5
class FilterBetween implements FilterInterface
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $table;
11
12
    /**
13
     * @var array
14
     */
15
    protected $columns;
16
17
    /**
18
     * @var array
19
     */
20
    protected $dates;
21
22
    /**
23
     * Construct
24
     *
25
     * @param string $table
26
     * @param array  $columns
27
     * @param array  $dates
28
     */
29 15
    public function __construct($table, $columns, $dates)
30
    {
31 15
        $this->table = $table;
32 15
        $this->columns = $columns;
33 15
        $this->dates = $dates;
34 15
    }
35
36
    /**
37
     * Filter
38
     *
39
     * @param Query\Builder $query
40
     * @param int|string    $key
41
     * @param mixed         $value
42
     *
43
     * @return boolean
44
     */
45 14
    public function filter($query, $key, $value)
46
    {
47 14
        $table = $this->table;
48 14
        $column = $key;
49 14
        if (preg_match('/^([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9_\-]+)/', $key, $matches)) {
50 11
            $table = $matches[1];
51 11
            $column = $matches[2];
52 11
        }
53
54 14
        if (preg_match('/^(.+)(_ini|_fim)$/', $column, $match) && in_array($match[1], $this->columns)) {
55 5
            $field = $match[1];
56 5
            $operator = ($match[2]=='_ini')? '>=' : '<=';
57 5
            if (in_array($field, $this->dates)) {
58 2
                $filterDate = new FilterDate($operator);
59 2
                return $filterDate->filter($query, $table.'.'.$field, $value);
60
            }
61
            
62 4
            $query = $query->where($table.'.'.$field, $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...
63 4
            return true;
64
        }
65
66 10
        return false;
67
    }
68
}
69