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:
Complex classes like CartRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CartRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class CartRepository extends ChannelAwareEntityRepository |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $excludedSteps = [ |
||
22 | 'converted_to_opportunity', |
||
23 | 'abandoned' |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $excludedStatuses = [ |
||
30 | CartStatus::STATUS_PURCHASED, |
||
31 | CartStatus::STATUS_EXPIRED |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * @param \DateTime $dateFrom |
||
36 | * @param \DateTime $dateTo |
||
37 | * @param Workflow $workflow |
||
38 | * @param AclHelper $aclHelper |
||
39 | * |
||
40 | * @return array |
||
41 | */ |
||
42 | public function getFunnelChartData( |
||
88 | |||
89 | /** |
||
90 | * @param array $steps |
||
91 | * @param AclHelper $aclHelper |
||
92 | * @param \DateTime $dateFrom |
||
93 | * @param \DateTime $dateTo |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | protected function getStepData( |
||
146 | |||
147 | /** |
||
148 | * Update statuses for carts to 'expired' |
||
149 | * |
||
150 | * @param array $ids |
||
151 | */ |
||
152 | public function markExpired(array $ids) |
||
163 | |||
164 | /** |
||
165 | * Returns iterator for fetching IDs pairs by channel and given status |
||
166 | * Each item in iteration will be array with following data: |
||
167 | * [ |
||
168 | * 'id' => ENTITY_ID, |
||
169 | * 'originId' => ENTITY_ORIGIN_ID |
||
170 | * ] |
||
171 | * |
||
172 | * @param Channel $channel |
||
173 | * @param string $status |
||
174 | * |
||
175 | * @return \Doctrine\ORM\QueryBuilder |
||
176 | */ |
||
177 | public function getCartsByChannelIdsIterator(Channel $channel, $status = 'open') |
||
189 | |||
190 | /** |
||
191 | * @param \DateTime $start |
||
192 | * @param \DateTime $end |
||
193 | * @param AclHelper $aclHelper |
||
194 | * |
||
195 | * @return int |
||
196 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
197 | */ |
||
198 | View Code Duplication | public function getAbandonedRevenueByPeriod(\DateTime $start, \DateTime $end, AclHelper $aclHelper) |
|
207 | |||
208 | /** |
||
209 | * @param \DateTime $start |
||
210 | * @param \DateTime $end |
||
211 | * @param AclHelper $aclHelper |
||
212 | * |
||
213 | * @return int |
||
214 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
215 | */ |
||
216 | View Code Duplication | public function getAbandonedCountByPeriod(\DateTime $start, \DateTime $end, AclHelper $aclHelper) |
|
225 | |||
226 | /** |
||
227 | * @param \DateTime $start |
||
228 | * @param \DateTime $end |
||
229 | * @param AclHelper $aclHelper |
||
230 | * |
||
231 | * @return float|null |
||
232 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
233 | */ |
||
234 | public function getAbandonRateByPeriod(\DateTime $start, \DateTime $end, AclHelper $aclHelper) |
||
258 | |||
259 | /** |
||
260 | * @param \DateTime $start |
||
261 | * @param \DateTime $end |
||
262 | * |
||
263 | * @return QueryBuilder |
||
264 | */ |
||
265 | protected function getAbandonedQB(\DateTime $start = null, \DateTime $end = null) |
||
293 | |||
294 | /** |
||
295 | * @param AclHelper $aclHelper |
||
296 | * @param \DateTime $from |
||
297 | * @param \DateTime $to |
||
298 | * |
||
299 | * @return int |
||
300 | */ |
||
301 | View Code Duplication | public function getCustomersCountWhatMakeCarts(AclHelper $aclHelper, \DateTime $from = null, \DateTime $to = null) |
|
325 | |||
326 | /** |
||
327 | * @param string $alias |
||
328 | * |
||
329 | * @return QueryBuilder |
||
330 | */ |
||
331 | View Code Duplication | public function getCustomersCountWhatMakeCartsQB($alias) |
|
345 | |||
346 | /** |
||
347 | * @param string $alias |
||
348 | * @param array $steps |
||
349 | * @param array $excludedStatuses |
||
350 | * |
||
351 | * @return QueryBuilder |
||
352 | */ |
||
353 | public function getStepDataQB($alias, array $steps, array $excludedStatuses = []) |
||
370 | |||
371 | /** |
||
372 | * @return QueryBuilder |
||
373 | */ |
||
374 | public function getAbandonedRevenueQB() |
||
382 | |||
383 | /** |
||
384 | * @return QueryBuilder |
||
385 | */ |
||
386 | public function getAbandonedCountQB() |
||
394 | |||
395 | /** |
||
396 | * @return QueryBuilder |
||
397 | */ |
||
398 | public function getGrandTotalSumQB() |
||
409 | } |
||
410 |
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.