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 |
||
15 | class CartRepository extends ChannelAwareEntityRepository |
||
16 | { |
||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $excludedSteps = [ |
||
21 | 'converted_to_opportunity', |
||
22 | 'abandoned' |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $excludedStatuses = [ |
||
29 | CartStatus::STATUS_PURCHASED, |
||
30 | CartStatus::STATUS_EXPIRED |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * @param \DateTime $dateFrom |
||
35 | * @param \DateTime $dateTo |
||
36 | * @param Workflow $workflow |
||
37 | * @param AclHelper $aclHelper |
||
38 | * |
||
39 | * @return array |
||
40 | */ |
||
41 | public function getFunnelChartData( |
||
86 | |||
87 | /** |
||
88 | * @param array $steps |
||
89 | * @param AclHelper $aclHelper |
||
90 | * @param \DateTime $dateFrom |
||
91 | * @param \DateTime $dateTo |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | protected function getStepData( |
||
139 | |||
140 | /** |
||
141 | * Update statuses for carts to 'expired' |
||
142 | * |
||
143 | * @param array $ids |
||
144 | */ |
||
145 | public function markExpired(array $ids) |
||
156 | |||
157 | /** |
||
158 | * Returns iterator for fetching IDs pairs by channel and given status |
||
159 | * Each item in iteration will be array with following data: |
||
160 | * [ |
||
161 | * 'id' => ENTITY_ID, |
||
162 | * 'originId' => ENTITY_ORIGIN_ID |
||
163 | * ] |
||
164 | * |
||
165 | * @param Channel $channel |
||
166 | * @param string $status |
||
167 | * |
||
168 | * @return \Doctrine\ORM\QueryBuilder |
||
169 | */ |
||
170 | public function getCartsByChannelIdsIterator(Channel $channel, $status = 'open') |
||
182 | |||
183 | /** |
||
184 | * @param \DateTime $start |
||
185 | * @param \DateTime $end |
||
186 | * @param AclHelper $aclHelper |
||
187 | * @return int |
||
188 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
189 | */ |
||
190 | View Code Duplication | public function getAbandonedRevenueByPeriod(\DateTime $start, \DateTime $end, AclHelper $aclHelper) |
|
199 | |||
200 | /** |
||
201 | * @param \DateTime $start |
||
202 | * @param \DateTime $end |
||
203 | * @param AclHelper $aclHelper |
||
204 | * @return int |
||
205 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
206 | */ |
||
207 | View Code Duplication | public function getAbandonedCountByPeriod(\DateTime $start, \DateTime $end, AclHelper $aclHelper) |
|
216 | |||
217 | /** |
||
218 | * @param \DateTime $start |
||
219 | * @param \DateTime $end |
||
220 | * @param AclHelper $aclHelper |
||
221 | * @return float|null |
||
222 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
223 | */ |
||
224 | public function getAbandonRateByPeriod(\DateTime $start, \DateTime $end, AclHelper $aclHelper) |
||
225 | { |
||
226 | $result = null; |
||
227 | |||
228 | $qb = $this->createQueryBuilder('cart'); |
||
229 | $qb->join('cart.status', 'cstatus') |
||
230 | ->select('SUM(cart.grandTotal) as val') |
||
231 | ->andWhere('cstatus.name = :statusName') |
||
232 | ->setParameter('statusName', 'open') |
||
233 | ->andWhere($qb->expr()->between('cart.createdAt', ':dateStart', ':dateEnd')) |
||
234 | ->setParameter('dateStart', $start) |
||
235 | ->setParameter('dateEnd', $end); |
||
236 | $this->applyActiveChannelLimitation($qb); |
||
237 | $allCards = $aclHelper->apply($qb)->getOneOrNullResult(); |
||
238 | $allCards = (int)$allCards['val']; |
||
239 | |||
240 | if (0 !== $allCards) { |
||
241 | $abandonedCartsCount = $this->getAbandonedCountByPeriod($start, $end, $aclHelper); |
||
242 | |||
243 | $result = $abandonedCartsCount / $allCards; |
||
244 | } |
||
245 | |||
246 | return $result; |
||
247 | } |
||
248 | |||
249 | protected function getAbandonedQB(\DateTime $start, \DateTime $end) |
||
268 | |||
269 | /** |
||
270 | * @param AclHelper $aclHelper |
||
271 | * @param \DateTime $from |
||
272 | * @param \DateTime $to |
||
273 | * |
||
274 | * @return int |
||
275 | */ |
||
276 | View Code Duplication | public function getCustomersCountWhatMakeCarts(AclHelper $aclHelper, \DateTime $from, \DateTime $to) |
|
295 | } |
||
296 |
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.