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 OpportunityRepository 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 OpportunityRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class OpportunityRepository extends EntityRepository |
||
20 | { |
||
21 | const OPPORTUNITY_STATE_IN_PROGRESS = 'In Progress'; |
||
22 | const OPPORTUNITY_STATE_IN_PROGRESS_CODE = 'in_progress'; |
||
23 | const OPPORTUNITY_STATUS_CLOSED_WON_CODE = 'won'; |
||
24 | |||
25 | /** |
||
26 | * Get opportunities by state by current quarter |
||
27 | * |
||
28 | * @param $aclHelper AclHelper |
||
29 | * @param array $dateRange |
||
30 | * @param array $states |
||
31 | * @param int[] $owners |
||
32 | * |
||
33 | * @param AclHelper $aclHelper |
||
34 | * @param array $dateRange |
||
35 | * @param array $states |
||
36 | * @param int[] $owners |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | public function getOpportunitiesByStatus(AclHelper $aclHelper, $dateRange, $states, $owners = []) |
||
47 | |||
48 | /** |
||
49 | * @param string $alias |
||
50 | * @param CurrencyQueryBuilderTransformerInterface $qbTransformer |
||
51 | * @param string $orderBy |
||
52 | * @param string $direction |
||
53 | * |
||
54 | * @return QueryBuilder |
||
55 | */ |
||
56 | public function getGroupedOpportunitiesByStatusQB( |
||
92 | |||
93 | /** |
||
94 | * @param AclHelper $aclHelper |
||
95 | * @param $dateStart |
||
96 | * @param $dateEnd |
||
97 | * @param array $states |
||
98 | * @param int[] $owners |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | protected function getOpportunitiesDataByStatus( |
||
144 | |||
145 | /** |
||
146 | * @param array $ownerIds |
||
147 | * @param \DateTime $date |
||
148 | * @param AclHelper $aclHelper |
||
149 | * |
||
150 | * @param string|null $start |
||
151 | * @param string|null $end |
||
152 | * |
||
153 | * @return mixed |
||
154 | */ |
||
155 | public function getForecastOfOpportunitiesData($ownerIds, $date, AclHelper $aclHelper, $start = null, $end = null) |
||
163 | |||
164 | /** |
||
165 | * @param array $ownerIds |
||
166 | * @param AclHelper $aclHelper |
||
167 | * @param string|null $start |
||
168 | * @param string|null $end |
||
169 | * |
||
170 | * @return mixed |
||
171 | */ |
||
172 | protected function getForecastOfOpportunitiesCurrentData( |
||
215 | |||
216 | /** |
||
217 | * @param array $ownerIds |
||
218 | * @param \DateTime $date |
||
219 | * @param AclHelper $aclHelper |
||
220 | * |
||
221 | * @return mixed |
||
222 | */ |
||
223 | protected function getForecastOfOpportunitiesOldData($ownerIds, $date, AclHelper $aclHelper) |
||
265 | |||
266 | /** |
||
267 | * @param mixed $opportunityHistory |
||
268 | * @param string $field |
||
269 | * |
||
270 | * @return mixed |
||
271 | */ |
||
272 | protected function getHistoryOldValue($opportunityHistory, $field) |
||
285 | |||
286 | /** |
||
287 | * @param array $opportunityHistory |
||
288 | * @param Opportunity $opportunity |
||
289 | * |
||
290 | * @return bool |
||
291 | */ |
||
292 | protected function isStatusOk($opportunityHistory, $opportunity) |
||
302 | |||
303 | /** |
||
304 | * @param array $ownerIds |
||
305 | * @param array $opportunityHistory |
||
306 | * @param Opportunity $opportunity |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | protected function isOwnerOk($ownerIds, $opportunityHistory, $opportunity) |
||
321 | |||
322 | /** |
||
323 | * @param array $result |
||
324 | * @param array $opportunityHistory |
||
325 | * @param Opportunity $opportunity |
||
326 | * @param mixed $probability |
||
327 | * |
||
328 | * @return array |
||
329 | */ |
||
330 | protected function calculateOpportunityOldValue($result, $opportunityHistory, $opportunity, $probability) |
||
341 | |||
342 | /** |
||
343 | * @param AclHelper $aclHelper |
||
344 | * @param \DateTime $start |
||
345 | * @param \DateTime $end |
||
346 | * @param int[] $owners |
||
347 | * |
||
348 | * @return int |
||
349 | */ |
||
350 | View Code Duplication | public function getOpportunitiesCount( |
|
360 | |||
361 | /** |
||
362 | * @param AclHelper $aclHelper |
||
363 | * @param \DateTime $start |
||
364 | * @param \DateTime $end |
||
365 | * @param int[] $owners |
||
366 | * |
||
367 | * @return int |
||
368 | */ |
||
369 | View Code Duplication | public function getNewOpportunitiesCount( |
|
379 | |||
380 | /** |
||
381 | * @param \DateTime $start |
||
382 | * @param \DateTime $end |
||
383 | * @param int[] $owners |
||
384 | * |
||
385 | * @return QueryBuilder |
||
386 | */ |
||
387 | View Code Duplication | public function createOpportunitiesCountQb(\DateTime $start = null, \DateTime $end = null, $owners = []) |
|
408 | |||
409 | /** |
||
410 | * @param AclHelper $aclHelper |
||
411 | * @param \DateTime $start |
||
412 | * @param \DateTime $end |
||
413 | * @param int[] $owners |
||
414 | * |
||
415 | * @return double |
||
416 | */ |
||
417 | public function getTotalServicePipelineAmount( |
||
449 | |||
450 | /** |
||
451 | * @param AclHelper $aclHelper |
||
452 | * @param \DateTime $start |
||
453 | * @param \DateTime $end |
||
454 | * |
||
455 | * @return double |
||
456 | */ |
||
457 | public function getTotalServicePipelineAmountInProgress( |
||
483 | |||
484 | /** |
||
485 | * @param AclHelper $aclHelper |
||
486 | * @param \DateTime $start |
||
487 | * @param \DateTime $end |
||
488 | * |
||
489 | * @return double |
||
490 | */ |
||
491 | View Code Duplication | public function getWeightedPipelineAmount(AclHelper $aclHelper, \DateTime $start = null, \DateTime $end = null) |
|
509 | |||
510 | /** |
||
511 | * @param AclHelper $aclHelper |
||
512 | * @param \DateTime $start |
||
513 | * @param \DateTime $end |
||
514 | * @param int[] $owners |
||
515 | * |
||
516 | * @return double |
||
517 | */ |
||
518 | View Code Duplication | public function getOpenWeightedPipelineAmount( |
|
541 | |||
542 | /** |
||
543 | * @param AclHelper $aclHelper |
||
544 | * @param CurrencyQueryBuilderTransformerInterface $qbTransformer |
||
545 | * @param \DateTime $start |
||
546 | * @param \DateTime $end |
||
547 | * @param int[] $owners |
||
548 | * |
||
549 | * @return double |
||
550 | */ |
||
551 | public function getNewOpportunitiesAmount( |
||
570 | |||
571 | /** |
||
572 | * @param AclHelper $aclHelper |
||
573 | * @param \DateTime $start |
||
574 | * @param \DateTime $end |
||
575 | * @param int[] $owners |
||
576 | * |
||
577 | * @return int |
||
578 | */ |
||
579 | View Code Duplication | public function getWonOpportunitiesToDateCount( |
|
598 | |||
599 | /** |
||
600 | * @param AclHelper $aclHelper |
||
601 | * @param CurrencyQueryBuilderTransformerInterface $qbTransformer |
||
602 | * @param \DateTime $start |
||
603 | * @param \DateTime $end |
||
604 | * @param int[] $owners |
||
605 | * |
||
606 | * @return double |
||
607 | */ |
||
608 | public function getWonOpportunitiesToDateAmount( |
||
629 | |||
630 | /** |
||
631 | * @param QueryBuilder $qb |
||
632 | * @param \DateTime|null $start |
||
633 | * @param \DateTime|null $end |
||
634 | */ |
||
635 | View Code Duplication | protected function setCreationPeriod(QueryBuilder $qb, \DateTime $start = null, \DateTime $end = null) |
|
642 | |||
643 | /** |
||
644 | * @param QueryBuilder $qb |
||
645 | * @param \DateTime|null $start |
||
646 | * @param \DateTime|null $end |
||
647 | */ |
||
648 | View Code Duplication | protected function setClosedPeriod(QueryBuilder $qb, \DateTime $start = null, \DateTime $end = null) |
|
655 | |||
656 | /** |
||
657 | * Returns count of opportunities grouped by lead source |
||
658 | * |
||
659 | * @param AclHelper $aclHelper |
||
660 | * @param DateFilterProcessor $dateFilterProcessor |
||
661 | * @param array $dateRange |
||
662 | * @param array $owners |
||
663 | * |
||
664 | * @return array [value, source] |
||
665 | */ |
||
666 | public function getOpportunitiesCountGroupByLeadSource( |
||
677 | |||
678 | /** |
||
679 | * Returns opportunities QB grouped by lead source filtered by $dateRange and $owners |
||
680 | * |
||
681 | * @param DateFilterProcessor $dateFilterProcessor |
||
682 | * @param array $dateRange |
||
683 | * @param array $owners |
||
684 | * |
||
685 | * @return QueryBuilder |
||
686 | */ |
||
687 | public function getOpportunitiesGroupByLeadSourceQueryBuilder( |
||
706 | |||
707 | /** |
||
708 | * @param string $alias |
||
709 | * @param CurrencyQueryBuilderTransformerInterface $qbTransformer |
||
710 | * @param array $excludedStatuses |
||
711 | * |
||
712 | * @return QueryBuilder |
||
713 | */ |
||
714 | public function getForecastQB( |
||
733 | } |
||
734 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.