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 |
||
25 | class Builder |
||
26 | { |
||
27 | /** |
||
28 | * @var QueryBuilder |
||
29 | */ |
||
30 | protected $select; |
||
31 | |||
32 | /** |
||
33 | * @var QueryBuilder |
||
34 | */ |
||
35 | protected $total; |
||
36 | |||
37 | /** |
||
38 | * @param Registry $doctrine |
||
39 | */ |
||
40 | 34 | public function __construct(Registry $doctrine) |
|
49 | |||
50 | /** |
||
51 | * @return QueryBuilder |
||
52 | */ |
||
53 | 1 | public function getQuerySelect() |
|
57 | |||
58 | /** |
||
59 | * @return QueryBuilder |
||
60 | */ |
||
61 | 1 | public function getQueryTotal() |
|
65 | |||
66 | /** |
||
67 | * @param Search $entity |
||
68 | * |
||
69 | * @return Builder |
||
70 | */ |
||
71 | 6 | public function addName(Search $entity) |
|
85 | |||
86 | /** |
||
87 | * @param Search $entity |
||
88 | * |
||
89 | * @return Builder |
||
90 | */ |
||
91 | 2 | View Code Duplication | public function addDateAdd(Search $entity) |
103 | |||
104 | /** |
||
105 | * @param Search $entity |
||
106 | * |
||
107 | * @return Builder |
||
108 | */ |
||
109 | 2 | View Code Duplication | public function addDatePremiere(Search $entity) |
121 | |||
122 | /** |
||
123 | * @param Search $entity |
||
124 | * |
||
125 | * @return Builder |
||
126 | */ |
||
127 | 2 | View Code Duplication | public function addDateEnd(Search $entity) |
139 | |||
140 | /** |
||
141 | * @param Search $entity |
||
142 | * |
||
143 | * @return Builder |
||
144 | */ |
||
145 | 2 | public function addCountry(Search $entity) |
|
157 | |||
158 | /** |
||
159 | * @param Search $entity |
||
160 | * |
||
161 | * @return Builder |
||
162 | */ |
||
163 | 2 | public function addStorage(Search $entity) |
|
175 | |||
176 | /** |
||
177 | * @param Search $entity |
||
178 | * |
||
179 | * @return Builder |
||
180 | */ |
||
181 | 2 | public function addType(Search $entity) |
|
193 | |||
194 | /** |
||
195 | * @param Search $entity |
||
196 | * |
||
197 | * @return Builder |
||
198 | */ |
||
199 | 2 | View Code Duplication | public function addGenres(Search $entity) |
216 | |||
217 | /** |
||
218 | * Add labels. |
||
219 | * |
||
220 | * @param Search $entity |
||
221 | * |
||
222 | * @return Builder |
||
223 | */ |
||
224 | 2 | View Code Duplication | public function addLabels(Search $entity) |
241 | |||
242 | /** |
||
243 | * @param Search $entity |
||
244 | * |
||
245 | * @return Builder |
||
246 | */ |
||
247 | 2 | public function addStudio(Search $entity) |
|
259 | |||
260 | /** |
||
261 | * Do add data to queries. |
||
262 | * |
||
263 | * @param \Closure $adder |
||
264 | */ |
||
265 | 14 | protected function add(\Closure $adder) |
|
270 | |||
271 | /** |
||
272 | * @param int $limit |
||
273 | * |
||
274 | * @return Builder |
||
275 | */ |
||
276 | 3 | public function limit($limit) |
|
284 | |||
285 | /** |
||
286 | * @param int $offset |
||
287 | * |
||
288 | * @return Builder |
||
289 | */ |
||
290 | 3 | public function offset($offset) |
|
298 | |||
299 | /** |
||
300 | * @param string $column |
||
301 | * @param string $direction |
||
302 | * |
||
303 | * @return Builder |
||
304 | */ |
||
305 | 1 | public function sort($column, $direction) |
|
311 | } |
||
312 |
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.