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 |
||
| 21 | class OpportunityRepository extends EntityRepository |
||
| 22 | { |
||
| 23 | const OPPORTUNITY_STATE_IN_PROGRESS = 'In Progress'; |
||
| 24 | const OPPORTUNITY_STATE_IN_PROGRESS_CODE = 'in_progress'; |
||
| 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( |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param array $ownerIds |
||
| 222 | * @param \DateTime $date |
||
| 223 | * @param AclHelper $aclHelper |
||
| 224 | * |
||
| 225 | * @return mixed |
||
| 226 | */ |
||
| 227 | protected function getForecastOfOpportunitiesOldData($ownerIds, $date, AclHelper $aclHelper) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param mixed $opportunityHistory |
||
| 272 | * @param string $field |
||
| 273 | * |
||
| 274 | * @return mixed |
||
| 275 | */ |
||
| 276 | protected function getHistoryOldValue($opportunityHistory, $field) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param array $opportunityHistory |
||
| 292 | * @param Opportunity $opportunity |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | protected function isStatusOk($opportunityHistory, $opportunity) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param array $ownerIds |
||
| 309 | * @param array $opportunityHistory |
||
| 310 | * @param Opportunity $opportunity |
||
| 311 | * |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | protected function isOwnerOk($ownerIds, $opportunityHistory, $opportunity) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param array $result |
||
| 328 | * @param array $opportunityHistory |
||
| 329 | * @param Opportunity $opportunity |
||
| 330 | * @param mixed $probability |
||
| 331 | * |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | protected function calculateOpportunityOldValue($result, $opportunityHistory, $opportunity, $probability) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param AclHelper $aclHelper |
||
| 348 | * @param DateTime $start |
||
| 349 | * @param DateTime $end |
||
| 350 | * @param int[] $owners |
||
| 351 | * |
||
| 352 | * @return int |
||
| 353 | */ |
||
| 354 | View Code Duplication | public function getOpportunitiesCount( |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @param AclHelper $aclHelper |
||
| 367 | * @param DateTime $start |
||
| 368 | * @param DateTime $end |
||
| 369 | * @param int[] $owners |
||
| 370 | * |
||
| 371 | * @return int |
||
| 372 | */ |
||
| 373 | View Code Duplication | public function getNewOpportunitiesCount( |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @param DateTime $start |
||
| 387 | * @param DateTime $end |
||
| 388 | * @param int[] $owners |
||
| 389 | * |
||
| 390 | * @return QueryBuilder |
||
| 391 | */ |
||
| 392 | View Code Duplication | public function createOpportunitiesCountQb(DateTime $start = null, DateTime $end = null, $owners = []) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @param AclHelper $aclHelper |
||
| 416 | * @param DateTime $start |
||
| 417 | * @param DateTime $end |
||
| 418 | * @param int[] $owners |
||
| 419 | * |
||
| 420 | * @return double |
||
| 421 | */ |
||
| 422 | View Code Duplication | public function getTotalServicePipelineAmount( |
|
| 454 | |||
| 455 | /** |
||
| 456 | * @param AclHelper $aclHelper |
||
| 457 | * @param DateTime $start |
||
| 458 | * @param DateTime $end |
||
| 459 | * |
||
| 460 | * @return double |
||
| 461 | */ |
||
| 462 | public function getTotalServicePipelineAmountInProgress( |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param AclHelper $aclHelper |
||
| 491 | * @param DateTime $start |
||
| 492 | * @param DateTime $end |
||
| 493 | * |
||
| 494 | * @return double |
||
| 495 | */ |
||
| 496 | View Code Duplication | public function getWeightedPipelineAmount(AclHelper $aclHelper, DateTime $start = null, DateTime $end = null) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * @param AclHelper $aclHelper |
||
| 517 | * @param DateTime $start |
||
| 518 | * @param DateTime $end |
||
| 519 | * @param int[] $owners |
||
| 520 | * |
||
| 521 | * @return double |
||
| 522 | */ |
||
| 523 | View Code Duplication | public function getOpenWeightedPipelineAmount( |
|
| 554 | } |
||
| 555 |
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.