Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like sql_compiler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use sql_compiler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | abstract class sql_compiler { |
||
4 | protected $skipDefaultOrderBy; |
||
5 | protected $store; |
||
6 | public $error; |
||
7 | protected $join_target_properties; |
||
8 | protected $offset; |
||
9 | protected $limit; |
||
10 | protected $cache; |
||
11 | protected $path; |
||
12 | protected $_SCAN_WS = array(" " => true, "\t" => true, "\n" => true ,"\r" => true); |
||
13 | protected $_SCAN_AZ = array("a" => true, "A" => true, "b" => true, "B" => true, "c" => true, "C" => true, "d" => true, "D" => true, "e" => true, "E" => true, "f" => true, "F" => true, "g" => true, "G" => true, "h" => true, "H" => true, "i" => true, "I" => true, "j" => true, "J" => true, "k" => true, "K" => true, "l" => true, "L" => true, "m" => true, "M" => true, "n" => true, "N" => true, "o" => true, "O" => true, "p" => true, "P" => true, "q" => true, "Q" => true, "r" => true, "R" => true, "s" => true, "S" => true, "t" => true, "T" => true, "u" => true, "U" => true, "v" => true, "V" => true, "w" => true, "W" => true, "x" => true, "X" => true, "y" => true, "Y" => true, "z" => true, "Z" => true); |
||
14 | protected $_SCAN_AZ_09 = array("a" => true, "A" => true, "b" => true, "B" => true, "c" => true, "C" => true, "d" => true, "D" => true, "e" => true, "E" => true, "f" => true, "F" => true, "g" => true, "G" => true, "h" => true, "H" => true, "i" => true, "I" => true, "j" => true, "J" => true, "k" => true, "K" => true, "l" => true, "L" => true, "m" => true, "M" => true, "n" => true, "N" => true, "o" => true, "O" => true, "p" => true, "P" => true, "q" => true, "Q" => true, "r" => true, "R" => true, "s" => true, "S" => true, "t" => true, "T" => true, "u" => true, "U" => true, "v" => true, "V" => true, "w" => true, "W" => true, "x" => true, "X" => true, "y" => true, "Y" => true, "z" => true, "Z" => true, "_" => true, "0" => true, "1" => true, "2" => true, "3" => true, "4" => true, "5" => true, "6" => true, "7" => true, "8" => true, "9" => true); |
||
15 | protected $_SCAN_NUM = array("0" => true, "1" => true, "2" => true, "3" => true, "4" => true, "5" => true, "6" => true, "7" => true, "8" => true, "9" => true); |
||
16 | protected $_SCAN_NUM_START = array("0" => true, "1" => true, "2" => true, "3" => true, "4" => true, "5" => true, "6" => true, "7" => true, "8" => true, "9" => true, "-" => true); |
||
17 | protected $_SCAN_CMP = array("~" => array("=" => array("FIN" => true)), "=" => array("=" => array("FIN" => true), "FIN" => true, "~" => array("FIN" => true, "~" => array("FIN" => true)), "*" => array("FIN" => true, "*" => array("FIN" => true)), "/" => array("FIN" => true)), "!" => array("=" => array("FIN" => true), "~" => array("FIN" => true, "~" => array("FIN" => true)), "*" => array("FIN" => true, "*" => array("FIN" => true)), "/" => array("FIN" => true, "/" => array("FIN" => true))), "<" => array("=" => array("FIN" => true), "FIN" => true), ">" => array("=" => array("FIN" => true), "FIN" => true), "/" => array("=" => array("=" => array("FIN" => true)))); |
||
18 | |||
19 | |||
20 | 192 | protected function parse_const(&$YYBUFFER) { |
|
73 | |||
74 | 197 | protected function parse_ident(&$YYBUFFER) { |
|
183 | |||
184 | 195 | protected function parse_cmp_expr(&$YYBUFFER) { |
|
217 | |||
218 | 195 | protected function parse_group_expr(&$YYBUFFER) { |
|
246 | |||
247 | 195 | View Code Duplication | protected function parse_and_expr(&$YYBUFFER) { |
273 | |||
274 | 195 | View Code Duplication | protected function parse_or_expr(&$YYBUFFER) { |
300 | |||
301 | 122 | protected function parse_orderby(&$YYBUFFER) { |
|
302 | 50 | $field = $this->parse_ident($YYBUFFER); |
|
303 | |||
304 | 50 | $YYCURSOR = 0; |
|
305 | 86 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
306 | 1 | $YYCURSOR++; |
|
307 | 1 | } |
|
308 | 50 | $value = ''; |
|
309 | 50 | $yych = $YYBUFFER[$YYCURSOR]; |
|
310 | 50 | View Code Duplication | if ($this->_SCAN_AZ[$yych]) { |
311 | 1 | $value .= $yych; |
|
312 | 1 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
313 | 1 | while (isset($this->_SCAN_AZ[$yych])) { |
|
314 | 1 | $value .= $yych; |
|
315 | 1 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
316 | 1 | } |
|
317 | 1 | $sort_type = strtoupper($value); |
|
318 | 1 | if (!($sort_type == 'ASC' || $sort_type == 'DESC')) { // If sort type is anything else than ASC or DESC, it is not part of the order by. |
|
319 | 1 | $sort_type = 'ASC'; |
|
320 | 1 | $YYCURSOR = $YYCURSOR - strlen($value); |
|
321 | 49 | $value = ''; |
|
322 | 1 | } |
|
323 | 1 | } else { |
|
324 | 49 | $sort_type = 'ASC'; |
|
325 | } |
||
326 | 50 | while (is_array($field)) { |
|
327 | $result = array( |
||
328 | 86 | 'id' => 'orderbyfield', |
|
329 | 50 | 'type' => $sort_type, |
|
330 | 50 | 'right' => $field, |
|
331 | 12 | 'left' => $result |
|
332 | 38 | ); |
|
333 | 50 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
334 | $YYCURSOR++; |
||
335 | } |
||
336 | 50 | $yych = $YYBUFFER[$YYCURSOR]; |
|
337 | 50 | if ($yych !== ',') { |
|
338 | 50 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR); |
|
339 | 50 | unset($field); |
|
340 | 38 | } else { |
|
341 | 1 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR + 1); |
|
342 | 1 | $field = $this->parse_ident($YYBUFFER); |
|
343 | 1 | $YYCURSOR = 0; |
|
344 | 1 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
345 | 48 | $YYCURSOR++; |
|
346 | } |
||
347 | 1 | $value = ''; |
|
348 | 1 | $yych = $YYBUFFER[$YYCURSOR]; |
|
349 | 49 | View Code Duplication | if ($this->_SCAN_AZ[$yych]) { |
350 | $value .= $yych; |
||
351 | $yych = $YYBUFFER[++$YYCURSOR]; |
||
352 | while (isset($this->_SCAN_AZ[$yych])) { |
||
353 | $value .= $yych; |
||
354 | $yych = $YYBUFFER[++$YYCURSOR]; |
||
355 | } |
||
356 | $sort_type = strtoupper($value); |
||
357 | if (!($sort_type == 'ASC' || $sort_type == 'DESC')) { // If sort type is anything else than ASC or DESC, it is not part of the order by. |
||
358 | $sort_type = 'ASC'; |
||
359 | $YYCURSOR = $YYCURSOR - strlen($value); |
||
360 | $value = ''; |
||
361 | } |
||
362 | } else { |
||
363 | 1 | $sort_type = 'ASC'; |
|
364 | } |
||
365 | } |
||
366 | 38 | } |
|
367 | 50 | return $result; |
|
368 | } |
||
369 | |||
370 | |||
371 | protected function parse_join_target_properties(&$query) { |
||
386 | |||
387 | 197 | protected function parse_query(&$query) { |
|
388 | |||
389 | 197 | if (!preg_match('|^[[:space:]]*order[[:space:]]*by[[:space:]]+|i', $query, $regs)) { |
|
390 | 195 | $result=$this->parse_or_expr($query); |
|
391 | 147 | } else { |
|
392 | 2 | $no_selection = true; |
|
393 | } |
||
394 | |||
395 | /* |
||
396 | $YYCURSOR = 0; |
||
397 | while ($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]]) { |
||
398 | $YYCURSOR++; |
||
399 | } |
||
400 | |||
401 | $yych = $YYBUFFER[$YYCURSOR]; |
||
402 | if ($this->_SCAN_AZ[$yych]) { |
||
403 | $value = $yych; |
||
404 | $yych = $YYBUFFER[++$YYCURSOR]; |
||
405 | while ($this->_SCAN_AZ[$yych]) { |
||
406 | $value .= $yych; |
||
407 | $yych = $YYBUFFER[++$YYCURSOR]; |
||
408 | } |
||
409 | $value = strtolower($value); |
||
410 | if ($value === 'order') { |
||
411 | while ($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]]) { |
||
412 | $YYCURSOR++; |
||
413 | } |
||
414 | $yych = $YYBUFFER[$YYCURSOR]; |
||
415 | if ($this->_SCAN_AZ[$yych]) { |
||
416 | $value = $yych; |
||
417 | $yych = $YYBUFFER[++$YYCURSOR]; |
||
418 | while ($this->_SCAN_AZ[$yych]) { |
||
419 | $value .= $yych; |
||
420 | $yych = $YYBUFFER[++$YYCURSOR]; |
||
421 | } |
||
422 | $value = strtolower($value); |
||
423 | if ($value === 'by') { |
||
424 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR; |
||
425 | $result = $this->parse_or_expr($YYBUFFER); |
||
426 | $YYCURSOR = 0; |
||
427 | $value = ''; |
||
428 | } else { |
||
429 | $this->error = "syntax error near: $YYBUFFER"; |
||
430 | return false; |
||
431 | } |
||
432 | } |
||
433 | } |
||
434 | } |
||
435 | |||
436 | */ |
||
437 | |||
438 | 197 | if (preg_match('|^[[:space:]]*join[[:space:]]*target[[:space:]]*on[[:space:]]*|i', $query, $regs)) { |
|
439 | $this->join_target_properties = array(); |
||
440 | $query = substr($query, strlen($regs[0])); |
||
441 | $this->parse_join_target_properties($query); |
||
442 | } |
||
443 | |||
444 | 197 | $matching = preg_match('|^[[:space:]]*order[[:space:]]*by[[:space:]]+|i', $query, $regs); |
|
445 | 197 | if ( $matching || $no_selection ) { |
|
446 | 50 | $query=substr($query, strlen($regs[0])); |
|
447 | 50 | $node["id"]="orderby"; |
|
448 | 50 | $node["right"]=$this->parse_orderby($query); |
|
449 | 50 | $node["left"]=$result; |
|
450 | 50 | $result=$node; |
|
451 | 38 | } |
|
452 | 197 | if (preg_match('|^[[:space:]]*limit[[:space:]]+([0-9]+)[[:space:]]*([,][[:space:]]*([0-9]+))?|i', $query, $regs)) { |
|
453 | 1 | $query=substr($query, strlen($regs[0])); |
|
454 | 1 | $limit_s["id"]="limit"; |
|
455 | 1 | $limit_s["offset"]=$regs[1]; |
|
456 | 1 | $limit_s["limit"]=$regs[3]; |
|
457 | 1 | } else { |
|
458 | 196 | $limit_s["id"]="limit"; |
|
459 | 196 | $limit_s["offset"]=($this->offset) ? $this->offset : 0; |
|
460 | 196 | $limit_s["limit"]=($this->limit) ? $this->limit : 0; |
|
461 | } |
||
462 | 197 | $limit_s["left"]=$result; |
|
463 | 197 | $result=$limit_s; |
|
464 | |||
465 | 197 | return $result; |
|
466 | } |
||
467 | |||
468 | // virtual (&private) method. To be implemented in the sql specific compiler |
||
469 | protected abstract function priv_sql_compile($node) ; |
||
470 | |||
471 | 197 | public function compile($path, $query, $limit=100, $offset=0, $layers = array()) { |
|
496 | |||
497 | |||
498 | } |
||
499 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.