Conditions | 30 |
Paths | 1000 |
Total Lines | 154 |
Code Lines | 83 |
Lines | 70 |
Ratio | 45.45 % |
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 |
||
134 | private function getWhereSql($table, $names = null, $values = null, |
||
135 | $pairs = null, $pair_operator = 'AND', $name_prefix = '') { |
||
136 | |||
137 | // @todo short circuit test |
||
138 | |||
139 | $return = array ( |
||
140 | 'joins' => array (), |
||
141 | 'wheres' => array(), |
||
142 | ); |
||
143 | |||
144 | $return['joins'][] = "JOIN {$this->table} ps on |
||
145 | {$table}.guid = ps.entity_guid"; |
||
146 | |||
147 | $wheres = array(); |
||
148 | |||
149 | // get names wheres |
||
150 | $names_where = ''; |
||
151 | View Code Duplication | if ($names !== null) { |
|
152 | if (!is_array($names)) { |
||
153 | $names = array($names); |
||
154 | } |
||
155 | |||
156 | $sanitised_names = array(); |
||
157 | foreach ($names as $name) { |
||
158 | $name = $name_prefix . $name; |
||
159 | $sanitised_names[] = '\'' . $this->db->sanitizeString($name) . '\''; |
||
160 | } |
||
161 | |||
162 | $names_str = implode(',', $sanitised_names); |
||
163 | if ($names_str) { |
||
164 | $names_where = "(ps.name IN ($names_str))"; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | // get values wheres |
||
169 | $values_where = ''; |
||
170 | View Code Duplication | if ($values !== null) { |
|
171 | if (!is_array($values)) { |
||
172 | $values = array($values); |
||
173 | } |
||
174 | |||
175 | $sanitised_values = array(); |
||
176 | foreach ($values as $value) { |
||
177 | // normalize to 0 |
||
178 | if (!$value) { |
||
179 | $value = 0; |
||
180 | } |
||
181 | $sanitised_values[] = '\'' . $this->db->sanitizeString($value) . '\''; |
||
182 | } |
||
183 | |||
184 | $values_str = implode(',', $sanitised_values); |
||
185 | if ($values_str) { |
||
186 | $values_where = "(ps.value IN ($values_str))"; |
||
187 | } |
||
188 | } |
||
189 | |||
190 | View Code Duplication | if ($names_where && $values_where) { |
|
191 | $wheres[] = "($names_where AND $values_where)"; |
||
192 | } elseif ($names_where) { |
||
193 | $wheres[] = "($names_where)"; |
||
194 | } elseif ($values_where) { |
||
195 | $wheres[] = "($values_where)"; |
||
196 | } |
||
197 | |||
198 | // add pairs which must be in arrays. |
||
199 | if (is_array($pairs)) { |
||
200 | // join counter for incremental joins in pairs |
||
201 | $i = 1; |
||
202 | |||
203 | // check if this is an array of pairs or just a single pair. |
||
204 | if (isset($pairs['name']) || isset($pairs['value'])) { |
||
205 | $pairs = array($pairs); |
||
206 | } |
||
207 | |||
208 | $pair_wheres = array(); |
||
209 | |||
210 | foreach ($pairs as $index => $pair) { |
||
211 | // @todo move this elsewhere? |
||
212 | // support shortcut 'n' => 'v' method. |
||
213 | if (!is_array($pair)) { |
||
214 | $pair = array( |
||
215 | 'name' => $index, |
||
216 | 'value' => $pair |
||
217 | ); |
||
218 | } |
||
219 | |||
220 | // must have at least a name and value |
||
221 | if (!isset($pair['name']) || !isset($pair['value'])) { |
||
222 | // @todo should probably return false. |
||
223 | continue; |
||
224 | } |
||
225 | |||
226 | View Code Duplication | if (isset($pair['operand'])) { |
|
227 | $operand = $this->db->sanitizeString($pair['operand']); |
||
228 | } else { |
||
229 | $operand = ' = '; |
||
230 | } |
||
231 | |||
232 | // for comparing |
||
233 | $trimmed_operand = trim(strtolower($operand)); |
||
234 | |||
235 | // if the value is an int, don't quote it because str '15' < str '5' |
||
236 | // if the operand is IN don't quote it because quoting should be done already. |
||
237 | if (is_numeric($pair['value'])) { |
||
238 | $value = $this->db->sanitizeString($pair['value']); |
||
239 | View Code Duplication | } else if (is_array($pair['value'])) { |
|
240 | $values_array = array(); |
||
241 | |||
242 | foreach ($pair['value'] as $pair_value) { |
||
243 | if (is_numeric($pair_value)) { |
||
244 | $values_array[] = $this->db->sanitizeString($pair_value); |
||
245 | } else { |
||
246 | $values_array[] = "'" . $this->db->sanitizeString($pair_value) . "'"; |
||
247 | } |
||
248 | } |
||
249 | |||
250 | if ($values_array) { |
||
251 | $value = '(' . implode(', ', $values_array) . ')'; |
||
252 | } |
||
253 | |||
254 | // @todo allow support for non IN operands with array of values. |
||
255 | // will have to do more silly joins. |
||
256 | $operand = 'IN'; |
||
257 | } else if ($trimmed_operand == 'in') { |
||
258 | $value = "({$pair['value']})"; |
||
259 | } else { |
||
260 | $value = "'" . $this->db->sanitizeString($pair['value']) . "'"; |
||
261 | } |
||
262 | |||
263 | $name = $this->db->sanitizeString($name_prefix . $pair['name']); |
||
264 | |||
265 | // @todo The multiple joins are only needed when the operator is AND |
||
266 | $return['joins'][] = "JOIN {$this->table} ps{$i} |
||
267 | on {$table}.guid = ps{$i}.entity_guid"; |
||
268 | |||
269 | $pair_wheres[] = "(ps{$i}.name = '$name' AND ps{$i}.value |
||
270 | $operand $value)"; |
||
271 | |||
272 | $i++; |
||
273 | } |
||
274 | |||
275 | $where = implode(" $pair_operator ", $pair_wheres); |
||
276 | if ($where) { |
||
277 | $wheres[] = "($where)"; |
||
278 | } |
||
279 | } |
||
280 | |||
281 | $where = implode(' AND ', $wheres); |
||
282 | if ($where) { |
||
283 | $return['wheres'][] = "($where)"; |
||
284 | } |
||
285 | |||
286 | return $return; |
||
287 | } |
||
288 | |||
413 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..