LookupCollection::process()   C
last analyzed

Complexity

Conditions 13
Paths 13

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 29
nc 13
nop 4
dl 0
loc 44
rs 6.6166
c 0
b 0
f 0

How to fix   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 Tsukasa\QueryBuilder\Database\Sqlite;
4
5
use Tsukasa\QueryBuilder\BaseLookupCollection;
6
use Tsukasa\QueryBuilder\Interfaces\IAdapter;
7
8
class LookupCollection extends BaseLookupCollection
9
{
10
    public function has($lookup)
11
    {
12
        $lookups = [
13
            'regex', 'iregex', 'second', 'year', 'minute',
14
            'hour', 'day', 'month', 'week_day'
15
        ];
16
        if (in_array(strtolower($lookup), $lookups, true)) {
17
            return true;
18
        }
19
20
        return parent::has($lookup);
21
    }
22
23
    /**
24
     * @param IAdapter $adapter
25
     * @param $lookup
26
     * @param $column
27
     * @param $value
28
     * @return string
29
     */
30
    public function process(IAdapter $adapter, $lookup, $column, $value)
31
    {
32
        switch (strtolower($lookup)) {
33
            case 'regex':
34
                return $adapter->quoteColumn($column) . ' REGEXP ' . $adapter->quoteValue('/' . $value . '/');
35
36
            case 'iregex':
37
                return $adapter->quoteColumn($column) . ' REGEXP ' . $adapter->quoteValue('/' . $value . '/i');
38
39
            case 'second':
40
                return "strftime('%S', " . $adapter->quoteColumn($column) . ')=' . $adapter->quoteValue((string)$value);
41
42
            case 'year':
43
                return "strftime('%Y', " . $adapter->quoteColumn($column) . ')=' . $adapter->quoteValue((string)$value);
44
45
            case 'minute':
46
                return "strftime('%M', " . $adapter->quoteColumn($column) . ')=' . $adapter->quoteValue((string)$value);
47
48
            case 'hour':
49
                return "strftime('%H', " . $adapter->quoteColumn($column) . ')=' . $adapter->quoteValue((string)$value);
50
51
            case 'day':
52
                return "strftime('%d', " . $adapter->quoteColumn($column) . ')=' . $adapter->quoteValue((string)$value);
53
54
            case 'month':
55
                $value = (int)$value;
56
                if (strlen($value) == 1) {
57
                    $value = "0" . (string)$value;
58
                }
59
                return "strftime('%m', " . $adapter->quoteColumn($column) . ')=' . $adapter->quoteValue((string)$value);
60
61
            case 'week_day':
62
                $value = (int)$value + 1;
63
                if ($value == 7) {
64
                    $value = 1;
65
                }
66
                return "strftime('%w', " . $adapter->quoteColumn($column) . ')=' . $adapter->quoteValue((string)$value);
67
68
            case 'range':
69
                list($min, $max) = $value;
70
                return $adapter->quoteColumn($column) . ' BETWEEN ' . (int)$min . ' AND ' . (int)$max;
71
        }
72
73
        return parent::process($adapter, $lookup, $column, $value);
74
    }
75
}