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) { |
|
| 21 | 192 | $YYCURSOR = 0; |
|
| 22 | 192 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
| 23 | 192 | $YYCURSOR++; |
|
| 24 | 145 | } |
|
| 25 | 192 | $value = ''; |
|
| 26 | 192 | $yych = $YYBUFFER[$YYCURSOR]; |
|
| 27 | switch (true) { |
||
| 28 | 192 | case '"' === $yych: |
|
| 29 | 192 | case "'" === $yych: |
|
| 30 | 188 | $quote = $yych; |
|
| 31 | 188 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 32 | 188 | while ($yych !== "\0" && $yych !== $quote) { |
|
| 33 | 188 | if ($yych === "\\") { |
|
| 34 | 48 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 35 | 2 | if ($yych !== $quote && $yych != "\\") { |
|
| 36 | $value .= "\\"; |
||
| 37 | } |
||
| 38 | 48 | } |
|
| 39 | 188 | $value .= $yych; |
|
| 40 | 188 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 41 | 142 | } |
|
| 42 | 188 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR + 1); |
|
| 43 | 188 | $node["id"] = "string"; |
|
|
|
|||
| 44 | 188 | $node["type"] = ($quote === '"') ? "double" : "single"; |
|
| 45 | 188 | $node["value"] = stripslashes($value); |
|
| 46 | 188 | return $node; |
|
| 47 | break; |
||
| 48 | 4 | case $this->_SCAN_NUM_START[$yych]: |
|
| 49 | 4 | $value = $yych; |
|
| 50 | 4 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 51 | 4 | while (isset($this->_SCAN_NUM[$yych])) { |
|
| 52 | $value .= $yych; |
||
| 53 | 1 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 54 | } |
||
| 55 | 4 | if ($yych === '.') { |
|
| 56 | $value .= $yych; |
||
| 57 | $yych = $YYBUFFER[++$YYCURSOR]; |
||
| 58 | while (isset($this->_SCAN_NUM[$yych])) { |
||
| 59 | $value .= $yych; |
||
| 60 | $yych = $YYBUFFER[++$YYCURSOR]; |
||
| 61 | } |
||
| 62 | $node["id"]="float"; |
||
| 63 | $node["value"]=(float)$value; |
||
| 64 | } else { |
||
| 65 | 4 | $node["id"]="int"; |
|
| 66 | 4 | $node["value"]=(int)$value;; |
|
| 67 | } |
||
| 68 | 4 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR); |
|
| 69 | 4 | return $node; |
|
| 70 | break; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | 202 | 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 | 202 | $reg_id='^[[:space:]]*(([a-z_][a-z0-9_]*)(:[a-z]+)?([.][a-z_][a-z0-9_]*)?([.][a-z_][a-z0-9_]*)?)'; |
|
| 82 | 202 | $reg_id.='[[:space:]]*'; |
|
| 83 | |||
| 84 | 202 | $YYCURSOR = 0; |
|
| 85 | 202 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
| 86 | 188 | $YYCURSOR++; |
|
| 87 | 142 | } |
|
| 88 | 202 | $value = ''; |
|
| 89 | 202 | $yych = $YYBUFFER[$YYCURSOR]; |
|
| 90 | |||
| 91 | 202 | if ($this->_SCAN_AZ[$yych]) { |
|
| 92 | 200 | $value .= $yych; |
|
| 93 | 200 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 94 | 200 | while (isset($this->_SCAN_AZ_09[$yych])) { |
|
| 95 | 200 | $value .= $yych; |
|
| 96 | 200 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 97 | 153 | } |
|
| 98 | 200 | $match_1 = $value; $value = ''; |
|
| 99 | 200 | 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 | 200 | View Code Duplication | if ($yych === '.') { |
| 108 | 194 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 109 | 194 | if ($this->_SCAN_AZ[$yych]) { |
|
| 110 | 194 | $value .= $yych; |
|
| 111 | 194 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 112 | 194 | while (isset($this->_SCAN_AZ_09[$yych])) { |
|
| 113 | 194 | $value .= $yych; |
|
| 114 | 194 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 115 | 147 | } |
|
| 116 | 147 | } |
|
| 117 | 194 | $match_2 = $value; $value = ''; |
|
| 118 | 147 | } |
|
| 119 | 200 | 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 | 153 | } |
|
| 133 | |||
| 134 | |||
| 135 | 202 | if($match_1) { |
|
| 136 | 200 | if (!$match_2) { |
|
| 137 | /* default table is 'object' */ |
||
| 138 | 54 | $match_2 = $match_1; |
|
| 139 | 54 | $match_1 = "object"; |
|
| 140 | 42 | } |
|
| 141 | 200 | $node["id"]="ident"; |
|
| 142 | |||
| 143 | 200 | $table=$match_1; |
|
| 144 | 200 | $field=$match_2; |
|
| 145 | 200 | if ($table=="object") { |
|
| 146 | switch ($field) { |
||
| 147 | 194 | case "implements": |
|
| 148 | 184 | $node["id"]="implements"; |
|
| 149 | 184 | break; |
|
| 150 | 58 | case "path": |
|
| 151 | 58 | case "parent": |
|
| 152 | 58 | case "priority": |
|
| 153 | 2 | $node["table"]="nodes"; |
|
| 154 | 2 | $node["field"]=$field; |
|
| 155 | 2 | break; |
|
| 156 | 45 | default: |
|
| 157 | 58 | $node["table"]="objects"; |
|
| 158 | 92 | $node["field"]=$field; |
|
| 159 | 45 | } |
|
| 160 | 147 | } else |
|
| 161 | 190 | 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 | 190 | $node["id"]="property"; |
|
| 171 | 190 | if ($match_3) { |
|
| 172 | $node["nls"] = $field; |
||
| 173 | $field = $match_3; |
||
| 174 | } |
||
| 175 | 190 | $node["table"]="prop_".$table; |
|
| 176 | 190 | $node["field"]="AR_".$field; |
|
| 177 | 190 | $node["record_id"] = $record_id; |
|
| 178 | } |
||
| 179 | 153 | } |
|
| 180 | 202 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR); |
|
| 181 | 202 | return $node; |
|
| 182 | } |
||
| 183 | |||
| 184 | 198 | protected function parse_cmp_expr(&$YYBUFFER) { |
|
| 185 | 198 | $result=$this->parse_ident($YYBUFFER); |
|
| 186 | 198 | if ($result) { |
|
| 187 | 196 | $YYCURSOR = 0; |
|
| 188 | 196 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
| 189 | 194 | $YYCURSOR++; |
|
| 190 | 147 | } |
|
| 191 | 196 | $yych = $YYBUFFER[$YYCURSOR]; |
|
| 192 | 196 | $YYCURSOR_START = $YYCURSOR; |
|
| 193 | 196 | $RULES = &$this->_SCAN_CMP; |
|
| 194 | 196 | while (isset($RULES[$yych])) { |
|
| 195 | 192 | $RULES = &$RULES[$yych]; |
|
| 196 | 192 | if (isset($RULES['FIN'])) { |
|
| 197 | 192 | $YYMATCH = $YYCURSOR; |
|
| 198 | 145 | } |
|
| 199 | 192 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 200 | 145 | } |
|
| 201 | 196 | if (isset($YYMATCH)) { |
|
| 202 | 192 | $node["id"]="cmp"; |
|
| 203 | 192 | $node["operator"]=substr($YYBUFFER, $YYCURSOR_START, ($YYMATCH + 1) - $YYCURSOR_START); |
|
| 204 | 192 | $node["left"]=$result; |
|
| 205 | 192 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR); |
|
| 206 | 192 | $result=$this->parse_const($YYBUFFER); |
|
| 207 | 192 | if ($result) { |
|
| 208 | 192 | $node["right"]=$result; |
|
| 209 | 145 | } |
|
| 210 | 192 | $result=$node; |
|
| 211 | 145 | } else { |
|
| 212 | 4 | $this->error="unknow compare-operator near '$YYBUFFER'"; |
|
| 213 | } |
||
| 214 | 149 | } |
|
| 215 | 198 | return $result; |
|
| 216 | } |
||
| 217 | |||
| 218 | 198 | protected function parse_group_expr(&$YYBUFFER) { |
|
| 219 | 198 | $YYCURSOR = 0; |
|
| 220 | 198 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
| 221 | 188 | $YYCURSOR++; |
|
| 222 | 142 | } |
|
| 223 | 198 | $yych = $YYBUFFER[$YYCURSOR++]; |
|
| 224 | 198 | 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 | 198 | $result = $this->parse_cmp_expr($YYBUFFER); |
|
| 243 | } |
||
| 244 | 198 | return $result; |
|
| 245 | } |
||
| 246 | |||
| 247 | 198 | View Code Duplication | protected function parse_and_expr(&$YYBUFFER) { |
| 248 | 198 | $result=$this->parse_group_expr($YYBUFFER); |
|
| 249 | 198 | while (is_array($result)) { |
|
| 250 | 196 | $YYCURSOR = 0; |
|
| 251 | 196 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
| 252 | 186 | $YYCURSOR++; |
|
| 253 | 140 | } |
|
| 254 | 196 | $ident = strtolower(substr($YYBUFFER, $YYCURSOR, 3)); |
|
| 255 | 196 | if ($ident === 'and' && !isset($this->_SCAN_AZ_09[$YYBUFFER[$YYCURSOR + 3]]) ) { |
|
| 256 | 184 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR + 3); |
|
| 257 | 184 | $right = $this->parse_group_expr($YYBUFFER); |
|
| 258 | 184 | if (is_array($right)) { |
|
| 259 | $result = array( |
||
| 260 | 184 | 'id' => $ident, |
|
| 261 | 184 | 'left' => $result, |
|
| 262 | 46 | 'right' => $right |
|
| 263 | 138 | ); |
|
| 264 | 138 | } else { |
|
| 265 | 46 | unset($result); |
|
| 266 | } |
||
| 267 | 138 | } else { |
|
| 268 | 196 | break; |
|
| 269 | } |
||
| 270 | 138 | } |
|
| 271 | 198 | return $result; |
|
| 272 | } |
||
| 273 | |||
| 274 | 198 | View Code Duplication | protected function parse_or_expr(&$YYBUFFER) { |
| 275 | 198 | $result=$this->parse_and_expr($YYBUFFER); |
|
| 276 | 198 | while (is_array($result)) { |
|
| 277 | 196 | $YYCURSOR = 0; |
|
| 278 | 196 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
| 279 | 50 | $YYCURSOR++; |
|
| 280 | 38 | } |
|
| 281 | 196 | $ident = strtolower(substr($YYBUFFER, $YYCURSOR, 2)); |
|
| 282 | 196 | if ($ident === 'or' && !isset($this->_SCAN_AZ_09[$YYBUFFER[$YYCURSOR + 2]]) ) { |
|
| 283 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR + 2); |
||
| 284 | $right = $this->parse_and_expr($YYBUFFER); |
||
| 285 | if (is_array($right)) { |
||
| 286 | $result = array( |
||
| 287 | 'id' => $ident, |
||
| 288 | 'left' => $result, |
||
| 289 | 'right' => $right |
||
| 290 | ); |
||
| 291 | } else { |
||
| 292 | unset($result); |
||
| 293 | } |
||
| 294 | } else { |
||
| 295 | 196 | break; |
|
| 296 | } |
||
| 297 | } |
||
| 298 | 198 | return $result; |
|
| 299 | } |
||
| 300 | |||
| 301 | 124 | protected function parse_orderby(&$YYBUFFER) { |
|
| 302 | 52 | $field = $this->parse_ident($YYBUFFER); |
|
| 303 | |||
| 304 | 52 | $YYCURSOR = 0; |
|
| 305 | 52 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
| 306 | 2 | $YYCURSOR++; |
|
| 307 | 2 | } |
|
| 308 | 52 | $value = ''; |
|
| 309 | 52 | $yych = $YYBUFFER[$YYCURSOR]; |
|
| 310 | 52 | View Code Duplication | if ($this->_SCAN_AZ[$yych]) { |
| 311 | 2 | $value .= $yych; |
|
| 312 | 2 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 313 | 2 | while (isset($this->_SCAN_AZ[$yych])) { |
|
| 314 | 2 | $value .= $yych; |
|
| 315 | 2 | $yych = $YYBUFFER[++$YYCURSOR]; |
|
| 316 | 2 | } |
|
| 317 | 2 | $sort_type = strtoupper($value); |
|
| 318 | 2 | 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 | 2 | $sort_type = 'ASC'; |
|
| 320 | 2 | $YYCURSOR = $YYCURSOR - strlen($value); |
|
| 321 | 2 | $value = ''; |
|
| 322 | 2 | } |
|
| 323 | 2 | } else { |
|
| 324 | 50 | $sort_type = 'ASC'; |
|
| 325 | } |
||
| 326 | 52 | while (is_array($field)) { |
|
| 327 | $result = array( |
||
| 328 | 124 | 'id' => 'orderbyfield', |
|
| 329 | 52 | 'type' => $sort_type, |
|
| 330 | 52 | 'right' => $field, |
|
| 331 | 12 | 'left' => $result |
|
| 332 | 40 | ); |
|
| 333 | 52 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
| 334 | $YYCURSOR++; |
||
| 335 | } |
||
| 336 | 52 | $yych = $YYBUFFER[$YYCURSOR]; |
|
| 337 | 52 | if ($yych !== ',') { |
|
| 338 | 52 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR); |
|
| 339 | 52 | unset($field); |
|
| 340 | 40 | } else { |
|
| 341 | 2 | $YYBUFFER = substr($YYBUFFER, $YYCURSOR + 1); |
|
| 342 | 2 | $field = $this->parse_ident($YYBUFFER); |
|
| 343 | 2 | $YYCURSOR = 0; |
|
| 344 | 2 | while (isset($this->_SCAN_WS[$YYBUFFER[$YYCURSOR]])) { |
|
| 345 | $YYCURSOR++; |
||
| 346 | } |
||
| 347 | 2 | $value = ''; |
|
| 348 | 2 | $yych = $YYBUFFER[$YYCURSOR]; |
|
| 349 | 2 | 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 | 2 | $sort_type = 'ASC'; |
|
| 364 | } |
||
| 365 | } |
||
| 366 | 40 | } |
|
| 367 | 52 | return $result; |
|
| 368 | } |
||
| 369 | |||
| 370 | |||
| 371 | protected function parse_join_target_properties(&$query) { |
||
| 386 | |||
| 387 | 202 | protected function parse_query(&$query) { |
|
| 388 | |||
| 389 | 202 | if (!preg_match('|^[[:space:]]*order[[:space:]]*by[[:space:]]+|i', $query, $regs)) { |
|
| 390 | 198 | $result=$this->parse_or_expr($query); |
|
| 391 | 151 | } else { |
|
| 392 | 4 | $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 | 202 | 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 | 202 | $matching = preg_match('|^[[:space:]]*order[[:space:]]*by[[:space:]]+|i', $query, $regs); |
|
| 445 | 202 | if ( $matching || $no_selection ) { |
|
| 446 | 52 | $query=substr($query, strlen($regs[0])); |
|
| 447 | 52 | $node["id"]="orderby"; |
|
| 448 | 52 | $node["right"]=$this->parse_orderby($query); |
|
| 449 | 52 | $node["left"]=$result; |
|
| 450 | 52 | $result=$node; |
|
| 451 | 40 | } |
|
| 452 | 202 | if (preg_match('|^[[:space:]]*limit[[:space:]]+([0-9]+)[[:space:]]*([,][[:space:]]*([0-9]+))?|i', $query, $regs)) { |
|
| 453 | 2 | $query=substr($query, strlen($regs[0])); |
|
| 454 | 2 | $limit_s["id"]="limit"; |
|
| 455 | 2 | $limit_s["offset"]=$regs[1]; |
|
| 456 | 2 | $limit_s["limit"]=$regs[3]; |
|
| 457 | 2 | } else { |
|
| 458 | 200 | $limit_s["id"]="limit"; |
|
| 459 | 200 | $limit_s["offset"]=($this->offset) ? $this->offset : 0; |
|
| 460 | 200 | $limit_s["limit"]=($this->limit) ? $this->limit : 0; |
|
| 461 | } |
||
| 462 | 202 | $limit_s["left"]=$result; |
|
| 463 | 202 | $result=$limit_s; |
|
| 464 | |||
| 465 | 202 | 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 | 202 | public function compile($path, $query, $limit=100, $offset=0, $layers = array()) { |
|
| 472 | 202 | debug("sql_compiler::compile ($path, $query, $limit, $offset)", "store"); |
|
| 473 | 202 | $this->error=""; |
|
| 474 | 202 | $this->path = $path; |
|
| 475 | |||
| 476 | 202 | $this->limit=$limit; |
|
| 477 | 202 | $this->offset=$offset; |
|
| 478 | 202 | $this->layers=$layers; |
|
| 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.