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 |
||
| 20 | class OpportunityRepository extends EntityRepository |
||
| 21 | { |
||
| 22 | const OPPORTUNITY_STATE_IN_PROGRESS = 'In Progress'; |
||
| 23 | const OPPORTUNITY_STATE_IN_PROGRESS_CODE = 'in_progress'; |
||
| 24 | const OPPORTUNITY_STATUS_CLOSED_WON_CODE = 'won'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var WorkflowStep[] |
||
| 28 | */ |
||
| 29 | protected $workflowStepsByName; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Get opportunities by state by current quarter |
||
| 33 | * |
||
| 34 | * @param $aclHelper AclHelper |
||
| 35 | * @param array $dateRange |
||
| 36 | * @param array $states |
||
| 37 | * @param int[] $owners |
||
| 38 | * |
||
| 39 | * @param AclHelper $aclHelper |
||
| 40 | * @param array $dateRange |
||
| 41 | * @param array $states |
||
| 42 | * @param int[] $owners |
||
| 43 | * |
||
| 44 | * @return array |
||
| 45 | */ |
||
| 46 | public function getOpportunitiesByStatus(AclHelper $aclHelper, $dateRange, $states, $owners = []) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $alias |
||
| 56 | * @param string $orderBy |
||
| 57 | * @param string $direction |
||
| 58 | * |
||
| 59 | * @return QueryBuilder |
||
| 60 | * |
||
| 61 | */ |
||
| 62 | public function getGroupedOpportunitiesByStatusQB( |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param AclHelper $aclHelper |
||
| 99 | * @param $dateStart |
||
| 100 | * @param $dateEnd |
||
| 101 | * @param array $states |
||
| 102 | * @param int[] $owners |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | protected function getOpportunitiesDataByStatus( |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param array $ownerIds |
||
| 151 | * @param \DateTime $date |
||
| 152 | * @param AclHelper $aclHelper |
||
| 153 | * |
||
| 154 | * @param string|null $start |
||
| 155 | * @param string|null $end |
||
| 156 | * |
||
| 157 | * @return mixed |
||
| 158 | */ |
||
| 159 | public function getForecastOfOpportunitiesData($ownerIds, $date, AclHelper $aclHelper, $start = null, $end = null) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param array $ownerIds |
||
| 170 | * @param AclHelper $aclHelper |
||
| 171 | * @param string|null $start |
||
| 172 | * @param string|null $end |
||
| 173 | * |
||
| 174 | * @return mixed |
||
| 175 | */ |
||
| 176 | protected function getForecastOfOpportunitiesCurrentData( |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param array $ownerIds |
||
| 213 | * @param \DateTime $date |
||
| 214 | * @param AclHelper $aclHelper |
||
| 215 | * |
||
| 216 | * @return mixed |
||
| 217 | */ |
||
| 218 | protected function getForecastOfOpportunitiesOldData($ownerIds, $date, AclHelper $aclHelper) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param mixed $opportunityHistory |
||
| 260 | * @param string $field |
||
| 261 | * |
||
| 262 | * @return mixed |
||
| 263 | */ |
||
| 264 | protected function getHistoryOldValue($opportunityHistory, $field) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param array $opportunityHistory |
||
| 280 | * @param Opportunity $opportunity |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | protected function isStatusOk($opportunityHistory, $opportunity) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param array $ownerIds |
||
| 297 | * @param array $opportunityHistory |
||
| 298 | * @param Opportunity $opportunity |
||
| 299 | * |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | protected function isOwnerOk($ownerIds, $opportunityHistory, $opportunity) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param array $result |
||
| 316 | * @param array $opportunityHistory |
||
| 317 | * @param Opportunity $opportunity |
||
| 318 | * @param mixed $probability |
||
| 319 | * |
||
| 320 | * @return array |
||
| 321 | */ |
||
| 322 | protected function calculateOpportunityOldValue($result, $opportunityHistory, $opportunity, $probability) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param AclHelper $aclHelper |
||
| 336 | * @param \DateTime $start |
||
| 337 | * @param \DateTime $end |
||
| 338 | * @param int[] $owners |
||
| 339 | * |
||
| 340 | * @return int |
||
| 341 | */ |
||
| 342 | View Code Duplication | public function getOpportunitiesCount( |
|
| 352 | |||
| 353 | /** |
||
| 354 | * @param AclHelper $aclHelper |
||
| 355 | * @param \DateTime $start |
||
| 356 | * @param \DateTime $end |
||
| 357 | * @param int[] $owners |
||
| 358 | * |
||
| 359 | * @return int |
||
| 360 | */ |
||
| 361 | View Code Duplication | public function getNewOpportunitiesCount( |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @param \DateTime $start |
||
| 374 | * @param \DateTime $end |
||
| 375 | * @param int[] $owners |
||
| 376 | * |
||
| 377 | * @return QueryBuilder |
||
| 378 | */ |
||
| 379 | View Code Duplication | public function createOpportunitiesCountQb(\DateTime $start = null, \DateTime $end = null, $owners = []) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @param AclHelper $aclHelper |
||
| 403 | * @param \DateTime $start |
||
| 404 | * @param \DateTime $end |
||
| 405 | * @param int[] $owners |
||
| 406 | * |
||
| 407 | * @return double |
||
| 408 | */ |
||
| 409 | public function getTotalServicePipelineAmount( |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @param AclHelper $aclHelper |
||
| 442 | * @param \DateTime $start |
||
| 443 | * @param \DateTime $end |
||
| 444 | * |
||
| 445 | * @return double |
||
| 446 | */ |
||
| 447 | View Code Duplication | public function getTotalServicePipelineAmountInProgress( |
|
| 471 | |||
| 472 | /** |
||
| 473 | * @param AclHelper $aclHelper |
||
| 474 | * @param \DateTime $start |
||
| 475 | * @param \DateTime $end |
||
| 476 | * |
||
| 477 | * @return double |
||
| 478 | */ |
||
| 479 | View Code Duplication | public function getWeightedPipelineAmount(AclHelper $aclHelper, \DateTime $start = null, \DateTime $end = null) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * @param AclHelper $aclHelper |
||
| 500 | * @param \DateTime $start |
||
| 501 | * @param \DateTime $end |
||
| 502 | * @param int[] $owners |
||
| 503 | * |
||
| 504 | * @return double |
||
| 505 | */ |
||
| 506 | View Code Duplication | public function getOpenWeightedPipelineAmount( |
|
| 527 | |||
| 528 | /** |
||
| 529 | * @param AclHelper $aclHelper |
||
| 530 | * @param \DateTime $start |
||
| 531 | * @param \DateTime $end |
||
| 532 | * @param int[] $owners |
||
| 533 | * |
||
| 534 | * @return double |
||
| 535 | */ |
||
| 536 | public function getNewOpportunitiesAmount( |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @param AclHelper $aclHelper |
||
| 557 | * @param \DateTime $start |
||
| 558 | * @param \DateTime $end |
||
| 559 | * @param int[] $owners |
||
| 560 | * |
||
| 561 | * @return int |
||
| 562 | */ |
||
| 563 | View Code Duplication | public function getWonOpportunitiesToDateCount( |
|
| 582 | |||
| 583 | /** |
||
| 584 | * @param AclHelper $aclHelper |
||
| 585 | * @param \DateTime $start |
||
| 586 | * @param \DateTime $end |
||
| 587 | * @param int[] $owners |
||
| 588 | * |
||
| 589 | * @return double |
||
| 590 | */ |
||
| 591 | View Code Duplication | public function getWonOpportunitiesToDateAmount( |
|
| 610 | |||
| 611 | /** |
||
| 612 | * @param QueryBuilder $qb |
||
| 613 | * @param \DateTime|null $start |
||
| 614 | * @param \DateTime|null $end |
||
| 615 | */ |
||
| 616 | View Code Duplication | protected function setCreationPeriod(QueryBuilder $qb, \DateTime $start = null, \DateTime $end = null) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * @param QueryBuilder $qb |
||
| 629 | * @param \DateTime|null $start |
||
| 630 | * @param \DateTime|null $end |
||
| 631 | */ |
||
| 632 | View Code Duplication | protected function setClosedPeriod(QueryBuilder $qb, \DateTime $start = null, \DateTime $end = null) |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Returns count of opportunities grouped by lead source |
||
| 645 | * |
||
| 646 | * @param AclHelper $aclHelper |
||
| 647 | * @param DateFilterProcessor $dateFilterProcessor |
||
| 648 | * @param array $dateRange |
||
| 649 | * @param array $owners |
||
| 650 | * |
||
| 651 | * @return array [value, source] |
||
| 652 | */ |
||
| 653 | View Code Duplication | public function getOpportunitiesCountGroupByLeadSource( |
|
| 664 | |||
| 665 | /** |
||
| 666 | * Returns budget amount of opportunities grouped by lead source |
||
| 667 | * |
||
| 668 | * @param AclHelper $aclHelper |
||
| 669 | * @param DateFilterProcessor $dateFilterProcessor |
||
| 670 | * @param array $dateRange |
||
| 671 | * @param array $owners |
||
| 672 | * |
||
| 673 | * @return array [value, source] |
||
| 674 | */ |
||
| 675 | View Code Duplication | public function getOpportunitiesAmountGroupByLeadSource( |
|
| 686 | |||
| 687 | /** |
||
| 688 | * Returns opportunities QB grouped by lead source filtered by $dateRange and $owners |
||
| 689 | * |
||
| 690 | * @param DateFilterProcessor $dateFilterProcessor |
||
| 691 | * @param array $dateRange |
||
| 692 | * @param array $owners |
||
| 693 | * |
||
| 694 | * @return QueryBuilder |
||
| 695 | */ |
||
| 696 | protected function getOpportunitiesGroupByLeadSourceQueryBuilder( |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @param string $alias |
||
| 718 | * @param array $excludedStatuses |
||
| 719 | * |
||
| 720 | * @return QueryBuilder |
||
| 721 | */ |
||
| 722 | public function getForecastQB($alias = 'o', array $excludedStatuses = ['lost', 'won']) |
||
| 737 | } |
||
| 738 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.