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 |
||
37 | abstract class AbstractSelect extends AbstractWhere implements |
||
38 | \IteratorAggregate, |
||
39 | PaginatorAwareInterface |
||
40 | { |
||
41 | use JoinsTrait, LimitsTrait, PaginatorTrait; |
||
42 | |||
43 | /** |
||
44 | * Query type. |
||
45 | */ |
||
46 | const QUERY_TYPE = QueryCompiler::SELECT_QUERY; |
||
47 | |||
48 | /** |
||
49 | * Sort directions. |
||
50 | */ |
||
51 | const SORT_ASC = 'ASC'; |
||
52 | const SORT_DESC = 'DESC'; |
||
53 | |||
54 | /** |
||
55 | * Query must return only unique rows. |
||
56 | * |
||
57 | * @var bool|string |
||
58 | */ |
||
59 | protected $distinct = false; |
||
60 | |||
61 | /** |
||
62 | * Columns or expressions to be fetched from database, can include aliases (AS). |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $columns = ['*']; |
||
67 | |||
68 | /** |
||
69 | * Set of generated having tokens, format must be supported by QueryCompilers. |
||
70 | * |
||
71 | * @see AbstractWhere |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $havingTokens = []; |
||
76 | |||
77 | /** |
||
78 | * Parameters collected while generating HAVING tokens, must be in a same order as parameters |
||
79 | * in resulted query. |
||
80 | * |
||
81 | * @see AbstractWhere |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $havingParameters = []; |
||
86 | |||
87 | /** |
||
88 | * Columns/expression associated with their sort direction (ASK|DESC). |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $ordering = []; |
||
93 | |||
94 | /** |
||
95 | * Columns/expressions to group by. |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | protected $grouping = []; |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | View Code Duplication | public function getParameters(QueryCompiler $compiler = null): array |
|
117 | |||
118 | /** |
||
119 | * Mark query to return only distinct results. |
||
120 | * |
||
121 | * @param bool|string $distinct You are only allowed to use string value for Postgres databases. |
||
122 | * |
||
123 | * @return self|$this |
||
124 | */ |
||
125 | public function distinct($distinct = true): AbstractSelect |
||
131 | |||
132 | /** |
||
133 | * Simple HAVING condition with various set of arguments. |
||
134 | * |
||
135 | * @see AbstractWhere |
||
136 | * |
||
137 | * @param string|mixed $identifier Column or expression. |
||
138 | * @param mixed $variousA Operator or value. |
||
139 | * @param mixed $variousB Value, if operator specified. |
||
140 | * @param mixed $variousC Required only in between statements. |
||
141 | * |
||
142 | * @return self|$this |
||
143 | * |
||
144 | * @throws BuilderException |
||
145 | */ |
||
146 | View Code Duplication | public function having( |
|
156 | |||
157 | /** |
||
158 | * Simple AND HAVING condition with various set of arguments. |
||
159 | * |
||
160 | * @see AbstractWhere |
||
161 | * |
||
162 | * @param string|mixed $identifier Column or expression. |
||
163 | * @param mixed $variousA Operator or value. |
||
164 | * @param mixed $variousB Value, if operator specified. |
||
165 | * @param mixed $variousC Required only in between statements. |
||
166 | * |
||
167 | * @return self|$this |
||
168 | * |
||
169 | * @throws BuilderException |
||
170 | */ |
||
171 | View Code Duplication | public function andHaving( |
|
181 | |||
182 | /** |
||
183 | * Simple OR HAVING condition with various set of arguments. |
||
184 | * |
||
185 | * @see AbstractWhere |
||
186 | * |
||
187 | * @param string|mixed $identifier Column or expression. |
||
188 | * @param mixed $variousA Operator or value. |
||
189 | * @param mixed $variousB Value, if operator specified. |
||
190 | * @param mixed $variousC Required only in between statements. |
||
191 | * |
||
192 | * @return self|$this |
||
193 | * |
||
194 | * @throws BuilderException |
||
195 | */ |
||
196 | View Code Duplication | public function orHaving( |
|
206 | |||
207 | /** |
||
208 | * Sort result by column/expression. You can apply multiple sortings to query via calling method |
||
209 | * few times or by specifying values using array of sort parameters:. |
||
210 | * |
||
211 | * $select->orderBy([ |
||
212 | * 'id' => SelectQuery::SORT_DESC, |
||
213 | * 'name' => SelectQuery::SORT_ASC |
||
214 | * ]); |
||
215 | * |
||
216 | * @param string|array $expression |
||
217 | * @param string $direction Sorting direction, ASC|DESC. |
||
218 | * |
||
219 | * @return self|$this |
||
220 | */ |
||
221 | public function orderBy($expression, $direction = self::SORT_ASC): AbstractSelect |
||
235 | |||
236 | /** |
||
237 | * Column or expression to group query by. |
||
238 | * |
||
239 | * @param string $expression |
||
240 | * |
||
241 | * @return self|$this |
||
242 | */ |
||
243 | public function groupBy($expression): AbstractSelect |
||
249 | |||
250 | /** |
||
251 | * Applied to every potential parameter while having tokens generation. |
||
252 | * |
||
253 | * @return \Closure |
||
254 | */ |
||
255 | View Code Duplication | private function havingWrapper() |
|
281 | } |
||
282 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.