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 string $orderBy |
||
51 | * @param string $direction |
||
52 | * |
||
53 | * @return QueryBuilder |
||
54 | * |
||
55 | */ |
||
56 | public function getGroupedOpportunitiesByStatusQB( |
||
90 | |||
91 | /** |
||
92 | * @param AclHelper $aclHelper |
||
93 | * @param $dateStart |
||
94 | * @param $dateEnd |
||
95 | * @param array $states |
||
96 | * @param int[] $owners |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | protected function getOpportunitiesDataByStatus( |
||
142 | |||
143 | /** |
||
144 | * @param array $ownerIds |
||
145 | * @param \DateTime $date |
||
146 | * @param AclHelper $aclHelper |
||
147 | * |
||
148 | * @param string|null $start |
||
149 | * @param string|null $end |
||
150 | * |
||
151 | * @return mixed |
||
152 | */ |
||
153 | public function getForecastOfOpportunitiesData($ownerIds, $date, AclHelper $aclHelper, $start = null, $end = null) |
||
161 | |||
162 | /** |
||
163 | * @param array $ownerIds |
||
164 | * @param AclHelper $aclHelper |
||
165 | * @param string|null $start |
||
166 | * @param string|null $end |
||
167 | * |
||
168 | * @return mixed |
||
169 | */ |
||
170 | protected function getForecastOfOpportunitiesCurrentData( |
||
213 | |||
214 | /** |
||
215 | * @param array $ownerIds |
||
216 | * @param \DateTime $date |
||
217 | * @param AclHelper $aclHelper |
||
218 | * |
||
219 | * @return mixed |
||
220 | */ |
||
221 | protected function getForecastOfOpportunitiesOldData($ownerIds, $date, AclHelper $aclHelper) |
||
263 | |||
264 | /** |
||
265 | * @param mixed $opportunityHistory |
||
266 | * @param string $field |
||
267 | * |
||
268 | * @return mixed |
||
269 | */ |
||
270 | protected function getHistoryOldValue($opportunityHistory, $field) |
||
283 | |||
284 | /** |
||
285 | * @param array $opportunityHistory |
||
286 | * @param Opportunity $opportunity |
||
287 | * |
||
288 | * @return bool |
||
289 | */ |
||
290 | protected function isStatusOk($opportunityHistory, $opportunity) |
||
300 | |||
301 | /** |
||
302 | * @param array $ownerIds |
||
303 | * @param array $opportunityHistory |
||
304 | * @param Opportunity $opportunity |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | protected function isOwnerOk($ownerIds, $opportunityHistory, $opportunity) |
||
319 | |||
320 | /** |
||
321 | * @param array $result |
||
322 | * @param array $opportunityHistory |
||
323 | * @param Opportunity $opportunity |
||
324 | * @param mixed $probability |
||
325 | * |
||
326 | * @return array |
||
327 | */ |
||
328 | protected function calculateOpportunityOldValue($result, $opportunityHistory, $opportunity, $probability) |
||
339 | |||
340 | /** |
||
341 | * @param AclHelper $aclHelper |
||
342 | * @param \DateTime $start |
||
343 | * @param \DateTime $end |
||
344 | * @param int[] $owners |
||
345 | * |
||
346 | * @return int |
||
347 | */ |
||
348 | View Code Duplication | public function getOpportunitiesCount( |
|
358 | |||
359 | /** |
||
360 | * @param AclHelper $aclHelper |
||
361 | * @param \DateTime $start |
||
362 | * @param \DateTime $end |
||
363 | * @param int[] $owners |
||
364 | * |
||
365 | * @return int |
||
366 | */ |
||
367 | View Code Duplication | public function getNewOpportunitiesCount( |
|
377 | |||
378 | /** |
||
379 | * @param \DateTime $start |
||
380 | * @param \DateTime $end |
||
381 | * @param int[] $owners |
||
382 | * |
||
383 | * @return QueryBuilder |
||
384 | */ |
||
385 | View Code Duplication | public function createOpportunitiesCountQb(\DateTime $start = null, \DateTime $end = null, $owners = []) |
|
406 | |||
407 | /** |
||
408 | * @param AclHelper $aclHelper |
||
409 | * @param \DateTime $start |
||
410 | * @param \DateTime $end |
||
411 | * @param int[] $owners |
||
412 | * |
||
413 | * @return double |
||
414 | */ |
||
415 | public function getTotalServicePipelineAmount( |
||
447 | |||
448 | /** |
||
449 | * @param AclHelper $aclHelper |
||
450 | * @param \DateTime $start |
||
451 | * @param \DateTime $end |
||
452 | * |
||
453 | * @return double |
||
454 | */ |
||
455 | public function getTotalServicePipelineAmountInProgress( |
||
481 | |||
482 | /** |
||
483 | * @param AclHelper $aclHelper |
||
484 | * @param \DateTime $start |
||
485 | * @param \DateTime $end |
||
486 | * |
||
487 | * @return double |
||
488 | */ |
||
489 | View Code Duplication | public function getWeightedPipelineAmount(AclHelper $aclHelper, \DateTime $start = null, \DateTime $end = null) |
|
507 | |||
508 | /** |
||
509 | * @param AclHelper $aclHelper |
||
510 | * @param \DateTime $start |
||
511 | * @param \DateTime $end |
||
512 | * @param int[] $owners |
||
513 | * |
||
514 | * @return double |
||
515 | */ |
||
516 | View Code Duplication | public function getOpenWeightedPipelineAmount( |
|
539 | |||
540 | /** |
||
541 | * @param AclHelper $aclHelper |
||
542 | * @param \DateTime $start |
||
543 | * @param \DateTime $end |
||
544 | * @param int[] $owners |
||
545 | * |
||
546 | * @return double |
||
547 | */ |
||
548 | public function getNewOpportunitiesAmount( |
||
566 | |||
567 | /** |
||
568 | * @param AclHelper $aclHelper |
||
569 | * @param \DateTime $start |
||
570 | * @param \DateTime $end |
||
571 | * @param int[] $owners |
||
572 | * |
||
573 | * @return int |
||
574 | */ |
||
575 | View Code Duplication | public function getWonOpportunitiesToDateCount( |
|
594 | |||
595 | /** |
||
596 | * @param AclHelper $aclHelper |
||
597 | * @param \DateTime $start |
||
598 | * @param \DateTime $end |
||
599 | * @param int[] $owners |
||
600 | * |
||
601 | * @return double |
||
602 | */ |
||
603 | View Code Duplication | public function getWonOpportunitiesToDateAmount( |
|
622 | |||
623 | /** |
||
624 | * @param QueryBuilder $qb |
||
625 | * @param \DateTime|null $start |
||
626 | * @param \DateTime|null $end |
||
627 | */ |
||
628 | View Code Duplication | protected function setCreationPeriod(QueryBuilder $qb, \DateTime $start = null, \DateTime $end = null) |
|
635 | |||
636 | /** |
||
637 | * @param QueryBuilder $qb |
||
638 | * @param \DateTime|null $start |
||
639 | * @param \DateTime|null $end |
||
640 | */ |
||
641 | View Code Duplication | protected function setClosedPeriod(QueryBuilder $qb, \DateTime $start = null, \DateTime $end = null) |
|
648 | |||
649 | /** |
||
650 | * Returns count of opportunities grouped by lead source |
||
651 | * |
||
652 | * @param AclHelper $aclHelper |
||
653 | * @param DateFilterProcessor $dateFilterProcessor |
||
654 | * @param array $dateRange |
||
655 | * @param array $owners |
||
656 | * |
||
657 | * @return array [value, source] |
||
658 | */ |
||
659 | View Code Duplication | public function getOpportunitiesCountGroupByLeadSource( |
|
670 | |||
671 | /** |
||
672 | * Returns budget amount of opportunities grouped by lead source |
||
673 | * |
||
674 | * @param AclHelper $aclHelper |
||
675 | * @param DateFilterProcessor $dateFilterProcessor |
||
676 | * @param array $dateRange |
||
677 | * @param array $owners |
||
678 | * |
||
679 | * @return array [value, source] |
||
680 | */ |
||
681 | View Code Duplication | public function getOpportunitiesAmountGroupByLeadSource( |
|
692 | |||
693 | /** |
||
694 | * Returns opportunities QB grouped by lead source filtered by $dateRange and $owners |
||
695 | * |
||
696 | * @param DateFilterProcessor $dateFilterProcessor |
||
697 | * @param array $dateRange |
||
698 | * @param array $owners |
||
699 | * |
||
700 | * @return QueryBuilder |
||
701 | */ |
||
702 | protected function getOpportunitiesGroupByLeadSourceQueryBuilder( |
||
721 | |||
722 | /** |
||
723 | * @param string $alias |
||
724 | * @param array $excludedStatuses |
||
725 | * |
||
726 | * @return QueryBuilder |
||
727 | */ |
||
728 | public function getForecastQB($alias = 'o', array $excludedStatuses = ['lost', 'won']) |
||
743 | } |
||
744 |
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.