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 |
||
26 | class SelectQuery extends AbstractQuery |
||
27 | { |
||
28 | use Executable, Fetchable, From, Limit, OrderBy, Where; |
||
29 | |||
30 | /** |
||
31 | * @var SelectStatement |
||
32 | */ |
||
33 | protected $select; |
||
34 | |||
35 | /** |
||
36 | * @var WhereStatement |
||
37 | */ |
||
38 | protected $having; |
||
39 | |||
40 | /** |
||
41 | * @var OrderStatement |
||
42 | */ |
||
43 | protected $groupBy; |
||
44 | |||
45 | /** |
||
46 | * @var UnionStatement |
||
47 | */ |
||
48 | protected $union; |
||
49 | |||
50 | public function __construct() |
||
61 | |||
62 | /** |
||
63 | * Sets the fields to be selected for the query. |
||
64 | * |
||
65 | * @param array|string $fields fields |
||
66 | * |
||
67 | * @return self |
||
68 | */ |
||
69 | public function select($fields) |
||
75 | |||
76 | /** |
||
77 | * Sets the selected fields to an aggregate function. |
||
78 | * |
||
79 | * @param $function |
||
80 | * @param string $field |
||
81 | * |
||
82 | * @return self |
||
83 | */ |
||
84 | function aggregate($function, $field = '*') |
||
90 | |||
91 | /** |
||
92 | * Sets a COUNT() query. |
||
93 | * |
||
94 | * @param $field |
||
95 | * |
||
96 | * @return self |
||
97 | */ |
||
98 | function count($field = '*') |
||
102 | |||
103 | /** |
||
104 | * Sets a SUM() query. |
||
105 | * |
||
106 | * @param $field |
||
107 | * |
||
108 | * @return self |
||
109 | */ |
||
110 | function sum($field) |
||
114 | |||
115 | /** |
||
116 | * Sets an AVG() query. |
||
117 | * |
||
118 | * @param $field |
||
119 | * |
||
120 | * @return self |
||
121 | */ |
||
122 | function average($field) |
||
126 | |||
127 | /** |
||
128 | * Sets a MIN() query. |
||
129 | * |
||
130 | * @param $field |
||
131 | * |
||
132 | * @return self |
||
133 | */ |
||
134 | function min($field) |
||
138 | |||
139 | /** |
||
140 | * Sets a MAX() query. |
||
141 | * |
||
142 | * @param $field |
||
143 | * |
||
144 | * @return self |
||
145 | */ |
||
146 | function max($field) |
||
150 | |||
151 | /** |
||
152 | * Adds a join to the query. |
||
153 | * |
||
154 | * @param string $table table name |
||
155 | * @param string $on ON condition |
||
156 | * @param string $using USING columns |
||
157 | * @param string $type optional join type if not JOIN |
||
158 | * |
||
159 | * @return self |
||
160 | */ |
||
161 | public function join($table, $on = null, $using = null, $type = 'JOIN') |
||
167 | |||
168 | /** |
||
169 | * Sets the group by fields for the query. |
||
170 | * |
||
171 | * @param string|array $fields |
||
172 | * @param string $direction |
||
173 | * |
||
174 | * @return self |
||
175 | */ |
||
176 | public function groupBy($fields, $direction = false) |
||
182 | |||
183 | /** |
||
184 | * Sets the having conditions for the query. |
||
185 | * |
||
186 | * @param array|string $field |
||
187 | * @param string|bool $condition condition value (optional) |
||
188 | * @param string $operator operator (optional) |
||
189 | * |
||
190 | * @return self |
||
191 | */ |
||
192 | View Code Duplication | public function having($field, $condition = false, $operator = '=') |
|
202 | |||
203 | /** |
||
204 | * Unions another select query with this query. |
||
205 | * |
||
206 | * @param SelectQuery $query |
||
207 | * @param string $type optional union type |
||
208 | * |
||
209 | * @return self |
||
210 | */ |
||
211 | public function union(SelectQuery $query, $type = '') |
||
217 | |||
218 | /** |
||
219 | * Gets the select statement for the query. |
||
220 | * |
||
221 | * @return SelectStatement |
||
222 | */ |
||
223 | public function getSelect() |
||
227 | |||
228 | /** |
||
229 | * Gets the group by statement for the query. |
||
230 | * |
||
231 | * @return GroupByStatement |
||
232 | */ |
||
233 | public function getGroupBy() |
||
237 | |||
238 | /** |
||
239 | * Gets the having statement for the query. |
||
240 | * |
||
241 | * @return WhereStatement |
||
242 | */ |
||
243 | public function getHaving() |
||
247 | |||
248 | /** |
||
249 | * Gets the union statement for the query. |
||
250 | * |
||
251 | * @return UnionStatement |
||
252 | */ |
||
253 | public function getUnion() |
||
257 | |||
258 | /** |
||
259 | * Generates the raw SQL string for the query. |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | public function build() |
||
292 | |||
293 | public function __clone() |
||
304 | } |
||
305 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.