Conditions | 63 |
Paths | 177 |
Total Lines | 248 |
Code Lines | 192 |
Lines | 148 |
Ratio | 59.68 % |
Tests | 0 |
CRAP Score | 4032 |
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 |
||
39 | function compile_tree(&$node) { |
||
40 | switch ((string)$node["id"]) { |
||
41 | case 'property': |
||
42 | $table=$this->tbl_prefix.$node["table"]; |
||
43 | $field=$node["field"]; |
||
44 | $record_id=$node["record_id"]; |
||
45 | if (!$record_id) { |
||
46 | if (!$this->in_orderby) { |
||
47 | $result=" $table.object = ".$this->tbl_prefix."objects.id and $table.$field "; |
||
48 | $this->used_tables[$table]=$table; |
||
49 | View Code Duplication | } else { |
|
50 | if ($this->in_orderby && $node["nls"]) { |
||
51 | /* |
||
52 | we do a left join so that we will also find non |
||
53 | matching objects |
||
54 | */ |
||
55 | $objects_table = $this->tbl_prefix."objects"; |
||
56 | $this->nls_join[$table] = "left join $table as order_$table on $objects_table.id=order_$table.object and order_$table.AR_nls='".$node["nls"]."' "; |
||
57 | |||
58 | $result = " order_$table.$field "; |
||
59 | $this->select_list["order_".$table.".".$field] = "order_$table.$field"; |
||
60 | |||
61 | } else { |
||
62 | /* |
||
63 | if we are parsing 'orderby' properties we have |
||
64 | to join our tables for the whole query |
||
65 | */ |
||
66 | $this->select_tables[$table]=$table; |
||
67 | $this->used_tables[$table]=$table; |
||
68 | $result=" $table.$field "; |
||
69 | } |
||
70 | } |
||
71 | } else { |
||
72 | $this->used_tables["$table as $table$record_id"] = $table.$record_id; |
||
73 | if (!$this->in_orderby) { |
||
74 | $result=" $table$record_id.object = ".$this->tbl_prefix."objects.id and $table$record_id.$field "; |
||
75 | } else { |
||
76 | $this->select_tables["$table as $table$record_id"] = $table.$record_id; |
||
77 | $result=" $table$record_id.$field "; |
||
78 | } |
||
79 | } |
||
80 | break; |
||
81 | View Code Duplication | case 'ident': |
|
82 | $table=$this->tbl_prefix.$node["table"]; |
||
83 | $field=$node["field"]; |
||
84 | $this->used_tables[$table]=$table; |
||
85 | $result=" $table.$field "; |
||
86 | break; |
||
87 | View Code Duplication | case 'custom': |
|
88 | $table = $this->tbl_prefix."prop_custom"; |
||
89 | $field = $node["field"]; |
||
90 | $nls = $node["nls"]; |
||
91 | $record_id = $node["record_id"]; |
||
92 | /* |
||
93 | when we are compiling orderby properties we always want |
||
94 | to assign it to a new table alias |
||
95 | */ |
||
96 | if ($this->in_orderby) { |
||
97 | $this->custom_id++; |
||
98 | } |
||
99 | $this->custom_ref++; |
||
100 | if (!$record_id) { |
||
101 | $this->used_tables[$table." as $table".$this->custom_id] = $table.$this->custom_id; |
||
102 | $this->select_tables[$table." as $table".$this->custom_id] = 1; |
||
103 | |||
104 | $this->used_custom_fields[$field] = true; |
||
105 | $result = " $table".$this->custom_id.".AR_name = '$field' "; |
||
106 | if ($nls) { |
||
107 | $result = " $result and $table".$this->custom_id.".AR_nls = '$nls' "; |
||
108 | } |
||
109 | |||
110 | if (!$this->in_orderby) { |
||
111 | $result = " $result and $table".$this->custom_id.".AR_value "; |
||
112 | } else { |
||
113 | $this->where_s_ext = $result; |
||
114 | $result = " $table".$this->custom_id.".AR_value "; |
||
115 | } |
||
116 | } else { |
||
117 | $this->used_tables["$table as $table$record_id"] = $table.$record_id; |
||
118 | // $this->select_tables[$table." as $table$record_id"] = 1; |
||
119 | |||
120 | $result = " $table$record_id.AR_name = '$field' "; |
||
121 | if (!$this->in_orderby ) { |
||
122 | if ($this->join_target_properties["prop_my"][":$record_id"]) { |
||
123 | $result=" $result and $table$record_id.object = target.object and $table$record_id.AR_value "; |
||
124 | } else { |
||
125 | $result=" $table$record_id.object = ".$this->tbl_prefix."objects.id and $table$record_id.AR_value "; |
||
126 | } |
||
127 | } else { |
||
128 | if ($this->join_target_properties[$node["table"]]) { |
||
129 | $this->join_target_properties["$table as $table$record_id"] = $table.$record_id; |
||
130 | } |
||
131 | $this->select_tables["$table as $table$record_id"] = $table.$record_id; |
||
132 | $result=" $table$record_id.AR_value "; |
||
133 | } |
||
134 | } |
||
135 | break; |
||
136 | case 'string': |
||
137 | $result=" '".pg_escape_string($node["value"])."' "; |
||
138 | break; |
||
139 | case 'float': |
||
140 | case 'int': |
||
141 | $result=" ".$node["value"]." "; |
||
142 | break; |
||
143 | View Code Duplication | case 'and': |
|
144 | $cr = $this->custom_ref; |
||
145 | $left=$this->compile_tree($node["left"]); |
||
146 | if ($this->custom_ref > $cr) { |
||
147 | $this->custom_id++; |
||
148 | } |
||
149 | |||
150 | $right=$this->compile_tree($node["right"]); |
||
151 | $cr = $this->custom_ref; |
||
152 | if ($this->custom_ref > $cr) { |
||
153 | $this->custom_id++; |
||
154 | } |
||
155 | $result=" $left and $right "; |
||
156 | break; |
||
157 | View Code Duplication | case 'or': |
|
158 | $left=$this->compile_tree($node["left"]); |
||
159 | $right=$this->compile_tree($node["right"]); |
||
160 | $result=" $left or $right "; |
||
161 | break; |
||
162 | case 'cmp': |
||
163 | $not = ''; |
||
164 | switch ($node["operator"]) { |
||
165 | case '=': |
||
166 | case '==': |
||
167 | $operator="="; |
||
168 | break; |
||
169 | case '!=': |
||
170 | case '<=': |
||
171 | case '>=': |
||
172 | case '<': |
||
173 | case '>': |
||
174 | $operator=$node["operator"]; |
||
175 | break; |
||
176 | case '!~': |
||
177 | case '!~~': |
||
178 | $not="NOT "; |
||
179 | case '~=': |
||
180 | case '=~': |
||
181 | View Code Duplication | case '=~~': |
|
182 | if (!strlen($node["operator"])==3) { |
||
183 | $not.="I"; |
||
184 | } |
||
185 | $operator=$not."LIKE"; |
||
186 | break; |
||
187 | case '!/': |
||
188 | case '!//': |
||
189 | $not="!"; |
||
190 | case '=/': |
||
191 | View Code Duplication | case '=//': |
|
192 | $operator=$not."~"; |
||
193 | if (strlen($node["operator"])==3) { |
||
194 | $operator.="*"; |
||
195 | } |
||
196 | break; |
||
197 | } |
||
198 | if ($node["left"]["id"]!=="implements") { |
||
199 | $left=$this->compile_tree($node["left"]); |
||
200 | $right=$this->compile_tree($node["right"]); |
||
201 | if($this->test_for_lowercase($node["left"])){ |
||
202 | // lowercase compile |
||
203 | $result=" lower($left) $operator lower($right) "; |
||
204 | } else { |
||
205 | // normal compile |
||
206 | /* lastchanged == unixtimestamp -> lastchanged == 200201.. */ |
||
207 | if ($node["left"]["field"]=="lastchanged") { |
||
208 | $right = "to_timestamp($right)"; |
||
209 | } |
||
210 | $result=" $left $operator $right "; |
||
211 | } |
||
212 | } else { |
||
213 | $table=$this->tbl_prefix."types"; |
||
214 | $type=$this->compile_tree($node["right"]); |
||
215 | switch ($operator) { |
||
216 | View Code Duplication | case '!=': |
|
217 | $result=" (".$this->tbl_prefix."objects.type not in (select type from ".$this->tbl_prefix."types where implements = $type )) "; |
||
218 | break; |
||
219 | default: |
||
220 | $this->used_tables[$table]=$table; |
||
221 | $result=" (".$this->tbl_prefix."types.implements $operator $type and ".$this->tbl_prefix."objects.vtype = ".$this->tbl_prefix."types.type ) "; |
||
222 | break; |
||
223 | } |
||
224 | } |
||
225 | break; |
||
226 | View Code Duplication | case 'group': |
|
227 | $left=$this->compile_tree($node["left"]); |
||
228 | if ($left) { |
||
229 | $result=" ( $left ) "; |
||
230 | } |
||
231 | break; |
||
232 | |||
233 | View Code Duplication | case 'orderby': |
|
234 | $result=$this->compile_tree($node["left"]); |
||
235 | $this->orderby_s=$this->compile_tree($node["right"]); |
||
236 | break; |
||
237 | |||
238 | case 'orderbyfield': |
||
239 | $this->in_orderby = true; |
||
240 | $left=$this->compile_tree($node["left"]); |
||
241 | if ( $node["right"]["field"] != "none" ) { |
||
242 | $right=$this->compile_tree($node["right"]); |
||
243 | if ($left) { |
||
244 | $result=" $left , $right ".$node["type"]." "; |
||
245 | View Code Duplication | if($node["left"]['id'] == 'property' && !$node["right"]['nls']){ |
|
246 | $lefttablefield = $this->tbl_prefix.$node["left"]['table'].".".$node["left"]['field']; |
||
247 | $this->select_list[$lefttablefield] = $lefttablefield; |
||
248 | } |
||
249 | } else { |
||
250 | $result=" $right ".$node["type"]." "; |
||
251 | } |
||
252 | View Code Duplication | if($node["right"]['id'] == 'property' && !$node["right"]['nls']){ |
|
253 | $righttablefield = $this->tbl_prefix.$node["right"]['table'].".".$node["right"]['field']; |
||
254 | $this->select_list[$righttablefield] = $righttablefield; |
||
255 | } |
||
256 | } else { |
||
257 | $result = ""; |
||
258 | if ($left) { |
||
259 | $result = " $left "; |
||
260 | View Code Duplication | if($node["left"]['id'] == 'property' && !$node["right"]['nls']){ |
|
261 | $lefttablefield = $this->tbl_prefix.$node["left"]['table'].".".$node["left"]['field']; |
||
262 | $this->select_list[$lefttablefield] = $lefttablefield; |
||
263 | } |
||
264 | } |
||
265 | $this->skipDefaultOrderBy = true; |
||
266 | } |
||
267 | break; |
||
268 | |||
269 | View Code Duplication | case 'limit': |
|
270 | $this->where_s=$this->compile_tree($node["left"]); |
||
271 | $this->limit_s=""; |
||
272 | if ($node["limit"]) { |
||
273 | $this->limit_s=" offset ".(int)$node["offset"]." limit ".$node["limit"]." "; |
||
274 | } else |
||
275 | if ($node["offset"]) { |
||
276 | $this->limit_s=" limit ".(int)$node["offset"]." "; |
||
277 | } else { |
||
278 | if ($this->limit) { |
||
279 | $offset = (int)$this->offset; |
||
280 | $this->limit_s=" offset $offset limit ".(int)$this->limit." "; |
||
281 | } |
||
282 | } |
||
283 | break; |
||
284 | } |
||
285 | return $result; |
||
286 | } |
||
287 | |||
356 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.