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 |
||
27 | class SelectQuery extends AbstractQuery |
||
28 | { |
||
29 | use Executable; |
||
30 | use Fetchable; |
||
31 | use From; |
||
32 | use Limit; |
||
33 | use OrderBy; |
||
34 | use Where; |
||
35 | |||
36 | /** |
||
37 | * @var SelectStatement |
||
38 | */ |
||
39 | protected $select; |
||
40 | |||
41 | /** |
||
42 | * @var WhereStatement |
||
43 | */ |
||
44 | protected $having; |
||
45 | |||
46 | /** |
||
47 | * @var OrderStatement |
||
48 | */ |
||
49 | protected $groupBy; |
||
50 | |||
51 | /** |
||
52 | * @var UnionStatement |
||
53 | */ |
||
54 | protected $union; |
||
55 | |||
56 | public function __construct() |
||
67 | |||
68 | /** |
||
69 | * Sets the fields to be selected for the query. |
||
70 | * |
||
71 | * @param array|string $fields fields |
||
72 | * |
||
73 | * @return self |
||
74 | */ |
||
75 | public function select($fields) |
||
81 | |||
82 | /** |
||
83 | * Sets the selected fields to an aggregate function. |
||
84 | * |
||
85 | * @param $function |
||
86 | * @param string $field |
||
87 | * |
||
88 | * @return self |
||
89 | */ |
||
90 | public function aggregate($function, $field = '*') |
||
96 | |||
97 | /** |
||
98 | * Sets a COUNT() query. |
||
99 | * |
||
100 | * @param $field |
||
101 | * |
||
102 | * @return self |
||
103 | */ |
||
104 | public function count($field = '*') |
||
108 | |||
109 | /** |
||
110 | * Sets a SUM() query. |
||
111 | * |
||
112 | * @param $field |
||
113 | * |
||
114 | * @return self |
||
115 | */ |
||
116 | public function sum($field) |
||
120 | |||
121 | /** |
||
122 | * Sets an AVG() query. |
||
123 | * |
||
124 | * @param $field |
||
125 | * |
||
126 | * @return self |
||
127 | */ |
||
128 | public function average($field) |
||
132 | |||
133 | /** |
||
134 | * Sets a MIN() query. |
||
135 | * |
||
136 | * @param $field |
||
137 | * |
||
138 | * @return self |
||
139 | */ |
||
140 | public function min($field) |
||
144 | |||
145 | /** |
||
146 | * Sets a MAX() query. |
||
147 | * |
||
148 | * @param $field |
||
149 | * |
||
150 | * @return self |
||
151 | */ |
||
152 | public function max($field) |
||
156 | |||
157 | /** |
||
158 | * Adds a join to the query. |
||
159 | * |
||
160 | * @param string $table table name |
||
161 | * @param string $on ON condition |
||
162 | * @param string $using USING columns |
||
163 | * @param string $type optional join type if not JOIN |
||
164 | * |
||
165 | * @return self |
||
166 | */ |
||
167 | public function join($table, $on = null, $using = null, $type = 'JOIN') |
||
173 | |||
174 | /** |
||
175 | * Sets the group by fields for the query. |
||
176 | * |
||
177 | * @param string|array $fields |
||
178 | * @param string $direction |
||
179 | * |
||
180 | * @return self |
||
181 | */ |
||
182 | public function groupBy($fields, $direction = false) |
||
188 | |||
189 | /** |
||
190 | * Sets the having conditions for the query. |
||
191 | * |
||
192 | * @param array|string $field |
||
193 | * @param string|bool $condition condition value (optional) |
||
194 | * @param string $operator operator (optional) |
||
195 | * |
||
196 | * @return self |
||
197 | */ |
||
198 | View Code Duplication | public function having($field, $condition = false, $operator = '=') |
|
208 | |||
209 | /** |
||
210 | * Unions another select query with this query. |
||
211 | * |
||
212 | * @param SelectQuery $query |
||
213 | * @param string $type optional union type |
||
214 | * |
||
215 | * @return self |
||
216 | */ |
||
217 | public function union(self $query, $type = '') |
||
223 | |||
224 | /** |
||
225 | * Gets the select statement for the query. |
||
226 | * |
||
227 | * @return SelectStatement |
||
228 | */ |
||
229 | public function getSelect() |
||
233 | |||
234 | /** |
||
235 | * Gets the group by statement for the query. |
||
236 | * |
||
237 | * @return OrderStatement |
||
238 | */ |
||
239 | public function getGroupBy() |
||
243 | |||
244 | /** |
||
245 | * Gets the having statement for the query. |
||
246 | * |
||
247 | * @return WhereStatement |
||
248 | */ |
||
249 | public function getHaving() |
||
253 | |||
254 | /** |
||
255 | * Gets the union statement for the query. |
||
256 | * |
||
257 | * @return UnionStatement |
||
258 | */ |
||
259 | public function getUnion() |
||
263 | |||
264 | /** |
||
265 | * Generates the raw SQL string for the query. |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | public function build() |
||
299 | |||
300 | public function __clone() |
||
311 | } |
||
312 |