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 | 55 | protected function parse_const(&$YYBUFFER) { |
|
| 73 | |||
| 74 | 60 | protected function parse_ident(&$YYBUFFER) { |
|
| 75 | /* parse identifier regs 1,2 and 3 |
||
| 76 | |||
| 77 | reg[1]: tablename |
||
| 78 | reg[2]: property name |
||
| 79 | reg[3]: only used with 'my' properties |
||
| 80 | */ |
||
| 81 | 60 | $reg_id='^[[:space:]]*(([a-z_][a-z0-9_]*)(:[a-z]+)?([.][a-z_][a-z0-9_]*)?([.][a-z_][a-z0-9_]*)?)'; |
|
| 82 | 60 | $reg_id.='[[:space:]]*'; |
|
| 83 | |||
| 84 | 60 | $YYCURSOR = 0; |
|
| 85 | 60 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
| 86 | 2 | $YYCURSOR++; |
|
| 87 | } |
||
| 88 | 60 | $value = ''; |
|
| 89 | 60 | $yych = $YYBUFFER[$YYCURSOR]; |
|
| 90 | |||
| 91 | 60 | if ($this->_SCAN_AZ[$yych]) { |
|
| 92 | 59 | $value .= $yych; |
|
| 93 | 59 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 94 | 59 | while (isset($this->_SCAN_AZ_09[$yych])) { |
|
| 95 | 59 | $value .= $yych; |
|
| 96 | 59 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 97 | 27 | } |
|
| 98 | 59 | $match_1 = $value; $value = ''; |
|
| 99 | 59 | if ($yych === ':') { |
|
| 100 | $yych = $YYBUFFER[++$YYCURSOR]; |
||
| 101 | while (isset($this->_SCAN_AZ[$yych])) { |
||
| 102 | $value .= $yych; |
||
| 103 | $yych = $YYBUFFER[++$YYCURSOR]; |
||
| 104 | } |
||
| 105 | $record_id = $value; $value = ''; |
||
| 106 | } |
||
| 107 | 59 | View Code Duplication | if ($yych === '.') { |
| 108 | 56 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 109 | 56 | if ($this->_SCAN_AZ[$yych]) { |
|
| 110 | 56 | $value .= $yych; |
|
| 111 | 56 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 112 | 56 | while (isset($this->_SCAN_AZ_09[$yych])) { |
|
| 113 | 56 | $value .= $yych; |
|
| 114 | 56 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 115 | 27 | } |
|
| 116 | 27 | } |
|
| 117 | 56 | $match_2 = $value; $value = ''; |
|
| 118 | 27 | } |
|
| 119 | 59 | View Code Duplication | if ($yych === '.') { |
| 120 | $yych = $YYBUFFER[++$YYCURSOR]; |
||
| 121 | if ($this->_SCAN_AZ[$yych]) { |
||
| 122 | $value .= $yych; |
||
| 123 | $yych = $YYBUFFER[++$YYCURSOR]; |
||
| 124 | while (isset($this->_SCAN_AZ_09[$yych])) { |
||
| 125 | $value .= $yych; |
||
| 126 | $yych = $YYBUFFER[++$YYCURSOR]; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | $match_3 = $value; $value = ''; |
||
| 130 | } |
||
| 131 | |||
| 132 | 27 | } |
|
| 133 | |||
| 134 | |||
| 135 | 60 | if($match_1) { |
|
| 136 | 59 | if (!$match_2) { |
|
| 137 | /* default table is 'object' */ |
||
| 138 | 51 | $match_2 = $match_1; |
|
| 139 | 51 | $match_1 = "object"; |
|
| 140 | 24 | } |
|
| 141 | 59 | $node["id"]="ident"; |
|
| 142 | |||
| 143 | 59 | $table=$match_1; |
|
| 144 | 59 | $field=$match_2; |
|
| 145 | 59 | if ($table=="object") { |
|
| 146 | switch ($field) { |
||
| 147 | 54 | case "implements": |
|
| 148 | $node["id"]="implements"; |
||
| 149 | break; |
||
| 150 | 54 | case "path": |
|
| 151 | 54 | case "parent": |
|
| 152 | 54 | case "priority": |
|
| 153 | 1 | $node["table"]="nodes"; |
|
| 154 | 1 | $node["field"]=$field; |
|
| 155 | 1 | break; |
|
| 156 | 25 | default: |
|
| 157 | 54 | $node["table"]="objects"; |
|
| 158 | 54 | $node["field"]=$field; |
|
| 159 | 25 | } |
|
| 160 | 25 | } else |
|
| 161 | 53 | if ($table === "my") { |
|
| 162 | $node["id"] = "custom"; |
||
| 163 | if ($match_3) { |
||
| 164 | $node["nls"] = $field; |
||
| 165 | $field = $match_3; |
||
| 166 | } |
||
| 167 | $node["field"] = $field; |
||
| 168 | $node["record_id"] = $record_id; |
||
| 169 | } else { |
||
| 170 | 53 | $node["id"]="property"; |
|
| 171 | 53 | if ($match_3) { |
|
| 172 | $node["nls"] = $field; |
||
| 173 | $field = $match_3; |
||
| 174 | } |
||
| 175 | 53 | $node["table"]="prop_".$table; |
|
| 176 | 53 | $node["field"]="AR_".$field; |
|
| 177 | 53 | $node["record_id"] = $record_id; |
|
| 178 | } |
||
| 179 | 27 | } |
|
| 180 | 60 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR); |
|
| 181 | 60 | return $node; |
|
| 182 | } |
||
| 183 | |||
| 184 | 58 | protected function parse_cmp_expr(&$YYBUFFER) { |
|
| 217 | |||
| 218 | 58 | protected function parse_group_expr(&$YYBUFFER) { |
|
| 219 | 58 | $YYCURSOR = 0; |
|
| 220 | 58 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
| 221 | 2 | $YYCURSOR++; |
|
| 222 | } |
||
| 223 | 58 | $yych = $YYBUFFER[$YYCURSOR++]; |
|
| 224 | 58 | if ($yych === '(') { |
|
| 225 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR); |
||
| 226 | $result = $this->parse_or_expr($YYBUFFER); |
||
| 227 | $YYCURSOR = 0; |
||
| 228 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
||
| 229 | $YYCURSOR++; |
||
| 230 | } |
||
| 231 | $yych = $YYBUFFER[$YYCURSOR++]; |
||
| 232 | if ($yych === ')') { |
||
| 233 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR); |
||
| 234 | $node["id"]="group"; |
||
| 235 | $node["left"]=$result; |
||
| 236 | $result=$node; |
||
| 237 | } else { |
||
| 238 | unset($result); |
||
| 239 | $this->error = "missing closing group sign near '$YYBUFFER'"; |
||
| 240 | } |
||
| 241 | } else { |
||
| 242 | 58 | $result = $this->parse_cmp_expr($YYBUFFER); |
|
| 243 | } |
||
| 244 | 58 | return $result; |
|
| 245 | } |
||
| 246 | |||
| 247 | 58 | View Code Duplication | protected function parse_and_expr(&$YYBUFFER) { |
| 248 | 58 | $result=$this->parse_group_expr($YYBUFFER); |
|
| 249 | 58 | while (is_array($result)) { |
|
| 250 | 57 | $YYCURSOR = 0; |
|
| 251 | 57 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
| 252 | 49 | $YYCURSOR++; |
|
| 253 | 24 | } |
|
| 254 | 57 | $ident = strtolower(substr($YYBUFFER, $YYCURSOR, 3)); |
|
| 255 | 57 | if ($ident === 'and' && !isset($this->_SCAN_AZ_09[$YYBUFFER[$YYCURSOR + 3]]) ) { |
|
| 256 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR + 3); |
||
| 257 | $right = $this->parse_group_expr($YYBUFFER); |
||
| 258 | if (is_array($right)) { |
||
| 259 | $result = array( |
||
| 260 | 'id' => $ident, |
||
| 261 | 'left' => $result, |
||
| 262 | 'right' => $right |
||
| 263 | ); |
||
| 264 | } else { |
||
| 265 | unset($result); |
||
| 266 | } |
||
| 267 | } else { |
||
| 268 | 57 | break; |
|
| 269 | } |
||
| 270 | } |
||
| 271 | 58 | return $result; |
|
| 272 | } |
||
| 273 | |||
| 274 | 58 | View Code Duplication | protected function parse_or_expr(&$YYBUFFER) { |
| 300 | |||
| 301 | 52 | protected function parse_orderby(&$YYBUFFER) { |
|
| 369 | |||
| 370 | |||
| 371 | protected function parse_join_target_properties(&$query) { |
||
| 386 | |||
| 387 | 60 | protected function parse_query(&$query) { |
|
| 467 | |||
| 468 | // virtual (&private) method. To be implemented in the sql specific compiler |
||
| 469 | protected abstract function priv_sql_compile($node) ; |
||
| 470 | |||
| 471 | 60 | 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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.