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:
1 | <?php |
||
22 | class SelectQuery extends AbstractQuery |
||
23 | { |
||
24 | use Executable, Fetchable; |
||
25 | |||
26 | /** |
||
27 | * @var SelectStatement |
||
28 | */ |
||
29 | protected $select; |
||
30 | |||
31 | /** |
||
32 | * @var FromStatement |
||
33 | */ |
||
34 | protected $from; |
||
35 | |||
36 | /** |
||
37 | * @var WhereStatement |
||
38 | */ |
||
39 | protected $where; |
||
40 | |||
41 | /** |
||
42 | * @var WhereStatement |
||
43 | */ |
||
44 | protected $having; |
||
45 | |||
46 | /** |
||
47 | * @var OrderStatement |
||
48 | */ |
||
49 | protected $orderBy; |
||
50 | |||
51 | /** |
||
52 | * @var OrderStatement |
||
53 | */ |
||
54 | protected $groupBy; |
||
55 | |||
56 | /** |
||
57 | * @var LimitStatement |
||
58 | */ |
||
59 | protected $limit; |
||
60 | |||
61 | /** |
||
62 | * @var UnionStatement |
||
63 | */ |
||
64 | protected $union; |
||
65 | |||
66 | public function __construct() |
||
77 | |||
78 | /** |
||
79 | * Sets the fields to be selected for the query. |
||
80 | * |
||
81 | * @param array|string $fields fields |
||
82 | * |
||
83 | * @return self |
||
84 | */ |
||
85 | public function select($fields) |
||
91 | |||
92 | /** |
||
93 | * Sets the table for the query. |
||
94 | * |
||
95 | * @param string $table table name |
||
96 | * |
||
97 | * @return self |
||
98 | */ |
||
99 | public function from($table) |
||
105 | |||
106 | /** |
||
107 | * Adds a join to the query. |
||
108 | * |
||
109 | * @param string $table table name |
||
110 | * @param string $on ON condition |
||
111 | * @param string $using USING columns |
||
112 | * @param string $type optional join type if not JOIN |
||
113 | * |
||
114 | * @return self |
||
115 | */ |
||
116 | public function join($table, $on = null, $using = null, $type = 'JOIN') |
||
122 | |||
123 | /** |
||
124 | * Adds a where condition to the query. |
||
125 | * |
||
126 | * @param array|string $field |
||
127 | * @param string $condition condition value (optional) |
||
128 | * @param string $operator operator (optional) |
||
129 | * |
||
130 | * @return self |
||
131 | */ |
||
132 | View Code Duplication | public function where($field, $condition = false, $operator = '=') |
|
142 | |||
143 | /** |
||
144 | * Adds a where between condition to the query. |
||
145 | * |
||
146 | * @param string $field |
||
147 | * @param mixed $a first between value |
||
148 | * @param mixed $b second between value |
||
149 | * |
||
150 | * @return self |
||
151 | */ |
||
152 | public function between($field, $a, $b) |
||
158 | |||
159 | /** |
||
160 | * Adds a where not between condition to the query. |
||
161 | * |
||
162 | * @param string $field |
||
163 | * @param mixed $a first between value |
||
164 | * @param mixed $b second between value |
||
165 | * |
||
166 | * @return self |
||
167 | */ |
||
168 | public function notBetween($field, $a, $b) |
||
174 | |||
175 | /** |
||
176 | * Sets the limit for the query. |
||
177 | * |
||
178 | * @param int $limit |
||
179 | * @param int $offset |
||
180 | * |
||
181 | * @return self |
||
182 | */ |
||
183 | public function limit($limit, $offset = 0) |
||
189 | |||
190 | /** |
||
191 | * Sets the group by fields for the query. |
||
192 | * |
||
193 | * @param string|array $fields |
||
194 | * @param string $direction |
||
195 | * |
||
196 | * @return self |
||
197 | */ |
||
198 | public function groupBy($fields, $direction = false) |
||
204 | |||
205 | /** |
||
206 | * Sets the having conditions for the query. |
||
207 | * |
||
208 | * @param array|string $field |
||
209 | * @param string|bool $condition condition value (optional) |
||
210 | * @param string $operator operator (optional) |
||
211 | * |
||
212 | * @return self |
||
213 | */ |
||
214 | View Code Duplication | public function having($field, $condition = false, $operator = '=') |
|
224 | |||
225 | /** |
||
226 | * Sets the order for the query. |
||
227 | * |
||
228 | * @param string|array $fields |
||
229 | * @param string $direction |
||
230 | * |
||
231 | * @return self |
||
232 | */ |
||
233 | public function orderBy($fields, $direction = false) |
||
239 | |||
240 | /** |
||
241 | * Unions another select query with this query. |
||
242 | * |
||
243 | * @param SelectQuery $query |
||
244 | * @param string $type optional union type |
||
245 | * |
||
246 | * @return self |
||
247 | */ |
||
248 | public function union(SelectQuery $query, $type = false) |
||
254 | |||
255 | /** |
||
256 | * Gets the select statement for the query. |
||
257 | * |
||
258 | * @return SelectStatement |
||
259 | */ |
||
260 | public function getSelect() |
||
264 | |||
265 | /** |
||
266 | * Gets the from statement for the query. |
||
267 | * |
||
268 | * @return FromStatement |
||
269 | */ |
||
270 | public function getFrom() |
||
274 | |||
275 | /** |
||
276 | * Gets the where statement for the query. |
||
277 | * |
||
278 | * @return WhereStatement |
||
279 | */ |
||
280 | public function getWhere() |
||
284 | |||
285 | /** |
||
286 | * Gets the limit statement for the query. |
||
287 | * |
||
288 | * @return LimitStatement |
||
289 | */ |
||
290 | public function getLimit() |
||
294 | |||
295 | /** |
||
296 | * Gets the group by statement for the query. |
||
297 | * |
||
298 | * @return GroupByStatement |
||
299 | */ |
||
300 | public function getGroupBy() |
||
304 | |||
305 | /** |
||
306 | * Gets the having statement for the query. |
||
307 | * |
||
308 | * @return HavingStatement |
||
309 | */ |
||
310 | public function getHaving() |
||
314 | |||
315 | /** |
||
316 | * Gets the order by statement for the query. |
||
317 | * |
||
318 | * @return OrderByStatement |
||
319 | */ |
||
320 | public function getOrderBy() |
||
324 | |||
325 | /** |
||
326 | * Gets the union statement for the query. |
||
327 | * |
||
328 | * @return UnionStatement |
||
329 | */ |
||
330 | public function getUnion() |
||
334 | |||
335 | /** |
||
336 | * Generates the raw SQL string for the query. |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | public function build() |
||
360 | |||
361 | public function __clone() |
||
372 | } |
||
373 |