Conditions | 29 |
Paths | 32 |
Total Lines | 98 |
Code Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
33 | public function process(IAdapter $adapter, $lookup, $column, $value) |
||
34 | { |
||
35 | switch (strtolower($lookup)) { |
||
36 | case 'exact': |
||
37 | if ($value instanceof \DateTime) { |
||
38 | $value = $adapter->getDateTime($value); |
||
|
|||
39 | } |
||
40 | |||
41 | if ($value instanceof Expression) { |
||
42 | $sqlValue = $value->toSQL(); |
||
43 | } |
||
44 | else if ($value instanceof QueryBuilder) { |
||
45 | $sqlValue = '(' . $value->toSQL() . ')'; |
||
46 | } |
||
47 | else if (strpos($value, 'SELECT') !== false) { |
||
48 | $sqlValue = '(' . $value . ')'; |
||
49 | } |
||
50 | else { |
||
51 | $sqlValue = $adapter->quoteValue($value); |
||
52 | } |
||
53 | return $adapter->quoteColumn($column) . '=' . $sqlValue; |
||
54 | |||
55 | case 'gte': |
||
56 | if ($value instanceof \DateTime) { |
||
57 | $value = $adapter->getDateTime($value); |
||
58 | } |
||
59 | return $adapter->quoteColumn($column) . '>=' . $adapter->quoteValue($value); |
||
60 | |||
61 | case 'gt': |
||
62 | if ($value instanceof \DateTime) { |
||
63 | $value = $adapter->getDateTime($value); |
||
64 | } |
||
65 | return $adapter->quoteColumn($column) . '>' . $adapter->quoteValue($value); |
||
66 | |||
67 | case 'lte': |
||
68 | if ($value instanceof \DateTime) { |
||
69 | $value = $adapter->getDateTime($value); |
||
70 | } |
||
71 | return $adapter->quoteColumn($column) . '<=' . $adapter->quoteValue($value); |
||
72 | |||
73 | case 'lt': |
||
74 | if ($value instanceof \DateTime) { |
||
75 | $value = $adapter->getDateTime($value); |
||
76 | } |
||
77 | return $adapter->quoteColumn($column) . '<' . $adapter->quoteValue($value); |
||
78 | |||
79 | case 'range': |
||
80 | list($min, $max) = $value; |
||
81 | return $adapter->quoteColumn($column) . ' BETWEEN ' . $adapter->quoteValue($min) . ' AND ' . $adapter->quoteValue($max); |
||
82 | |||
83 | case 'isnt': |
||
84 | if (in_array($adapter->getSqlType($value), ['TRUE', 'FALSE', 'NULL'])) { |
||
85 | return $adapter->quoteColumn($column) . ' IS NOT ' . $adapter->getSqlType($value); |
||
86 | } |
||
87 | |||
88 | return $adapter->quoteColumn($column) . '!=' . $adapter->quoteValue($value); |
||
89 | |||
90 | case 'isnull': |
||
91 | return $adapter->quoteColumn($column) . ' ' . ((bool)$value ? 'IS NULL' : 'IS NOT NULL'); |
||
92 | |||
93 | case 'contains': |
||
94 | return $adapter->quoteColumn($column) . ' LIKE ' . $adapter->quoteValue('%' . $value . '%'); |
||
95 | |||
96 | case 'icontains': |
||
97 | return 'LOWER(' . $adapter->quoteColumn($column) . ') LIKE ' . $adapter->quoteValue('%' . mb_strtolower($value, 'UTF-8') . '%'); |
||
98 | |||
99 | case 'startswith': |
||
100 | return $adapter->quoteColumn($column) . ' LIKE ' . $adapter->quoteValue($value . '%'); |
||
101 | |||
102 | case 'istartswith': |
||
103 | return 'LOWER(' . $adapter->quoteColumn($column) . ') LIKE ' . $adapter->quoteValue(mb_strtolower($value, 'UTF-8') . '%'); |
||
104 | |||
105 | case 'endswith': |
||
106 | return $adapter->quoteColumn($column) . ' LIKE ' . $adapter->quoteValue('%' . $value); |
||
107 | |||
108 | case 'iendswith': |
||
109 | return 'LOWER(' . $adapter->quoteColumn($column) . ') LIKE ' . $adapter->quoteValue('%' . mb_strtolower($value, 'UTF-8')); |
||
110 | |||
111 | case 'in': |
||
112 | if (is_array($value)) { |
||
113 | $quotedValues = array_map(function($item) use ($adapter) { |
||
114 | return $adapter->quoteValue($item); |
||
115 | }, $value); |
||
116 | $sqlValue = implode(', ', $quotedValues); |
||
117 | } |
||
118 | else if ($value instanceof QueryBuilder) { |
||
119 | $sqlValue = $value->toSQL(); |
||
120 | } |
||
121 | else { |
||
122 | $sqlValue = $adapter->quoteSql($value); |
||
123 | } |
||
124 | return $adapter->quoteColumn($column) . ' IN (' . $sqlValue . ')'; |
||
125 | |||
126 | case 'raw': |
||
127 | return $adapter->quoteColumn($column) . ' ' . $adapter->quoteSql($value); |
||
128 | |||
129 | default: |
||
130 | return null; |
||
131 | } |
||
133 | } |