Conditions | 15 |
Paths | 27 |
Total Lines | 74 |
Code Lines | 44 |
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 |
||
62 | protected function _addWhere($val1, $val2 = null, $operator = null, $bind = true, $join = 'AND'){ |
||
63 | $operator = strtoupper(trim($operator)); |
||
64 | $join = strtoupper(trim($join)); |
||
65 | |||
66 | $operators = [ |
||
67 | '=', '>=', '>', '<=', '<', '<>', '!=', |
||
68 | '|', '&', '<<', '>>', '+', '-', '*', '/', '%', '^', '<=>', '~', '!', 'DIV', 'MOD', |
||
69 | 'IS', 'IS NOT', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'REGEXP', 'NOT REGEXP', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' |
||
70 | ]; |
||
71 | |||
72 | if(in_array($operator, $operators, true)){ |
||
73 | $where = [$this->quote($val1)]; |
||
74 | $values = []; |
||
75 | |||
76 | if(in_array($operator, ['IN', 'NOT IN'], true)){ |
||
77 | |||
78 | if(is_array($val2)){ |
||
79 | if($bind){ |
||
80 | $where[] = 'IN('.implode(',', array_fill(0, count($val2), '?')).')'; |
||
81 | $values = array_merge($values, $val2); |
||
82 | } |
||
83 | else{ |
||
84 | $where[] = 'IN('.implode(',', $val2).')'; // @todo: quote |
||
85 | } |
||
86 | } |
||
87 | else if($val2 instanceof StatementInterface){ |
||
88 | $where[] = 'IN('.$val2->sql().')'; |
||
89 | $values = array_merge($values, $val2->bindValues()); |
||
90 | } |
||
91 | |||
92 | } |
||
93 | else if(in_array($operator, ['BETWEEN', 'NOT BETWEEN'], true)){ |
||
|
|||
94 | |||
95 | } |
||
96 | else{ |
||
97 | $where[] = $operator; |
||
98 | |||
99 | if(is_null($val2)){ |
||
100 | $where[] = 'NULL'; |
||
101 | } |
||
102 | else if(is_bool($val2)){ |
||
103 | $where[] = $val2 ? 'TRUE' : 'FALSE'; |
||
104 | } |
||
105 | else if(in_array(strtolower($val2), ['null', 'false', 'true', 'unknown'])){ |
||
106 | $where[] = strtoupper($val2); |
||
107 | } |
||
108 | else if($val2 instanceof StatementInterface){ |
||
109 | $where[] = '('.$val2->sql().')'; |
||
110 | $values = array_merge($values, $val2->bindValues()); |
||
111 | } |
||
112 | else{ |
||
113 | if($bind){ |
||
114 | $where[] = '?'; |
||
115 | $values[] = $val2; |
||
116 | } |
||
117 | else{ |
||
118 | if(!empty($val2)){ |
||
119 | $where[] = $this->quote($val2); |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
124 | } |
||
125 | |||
126 | $this->bindValues = array_merge($this->bindValues, $values); |
||
127 | $this->where[] = [ |
||
128 | 'join' => in_array($join, ['AND', 'OR', 'XOR']) ? $join : 'AND', |
||
129 | 'stmt' => implode(' ', $where), |
||
130 | ]; |
||
131 | |||
132 | } |
||
133 | |||
134 | return $this; |
||
135 | } |
||
136 | |||
169 |
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.