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 JobComparisonBusinessCase 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 JobComparisonBusinessCase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class JobComparisonBusinessCase implements JobComparisonInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var JobRepositoryInterface |
||
| 23 | */ |
||
| 24 | private $oJobRepositoryLocal; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var JobRepositoryInterface |
||
| 28 | */ |
||
| 29 | private $oJobRepositoryChronos; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var DiffCompareInterface |
||
| 33 | */ |
||
| 34 | private $oDiffCompare; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var DatePeriodFactoryInterface |
||
| 38 | */ |
||
| 39 | private $oDatePeriodFactory; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var LoggerInterface |
||
| 43 | */ |
||
| 44 | private $oLogger; |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * @param JobRepositoryInterface $oJobRepositoryLocal |
||
| 49 | * @param JobRepositoryInterface $oJobRepositoryChronos |
||
| 50 | * @param DiffCompareInterface $oDiffCompare |
||
| 51 | * @param DatePeriodFactoryInterface $oDatePeriodFactory |
||
| 52 | * @param LoggerInterface $oLogger |
||
| 53 | */ |
||
| 54 | 4 | public function __construct( |
|
| 55 | JobRepositoryInterface $oJobRepositoryLocal, |
||
| 56 | JobRepositoryInterface $oJobRepositoryChronos, |
||
| 57 | DiffCompareInterface $oDiffCompare, |
||
| 58 | DatePeriodFactoryInterface $oDatePeriodFactory, |
||
| 59 | LoggerInterface $oLogger |
||
| 60 | ) |
||
| 61 | { |
||
| 62 | 4 | $this->oJobRepositoryLocal = $oJobRepositoryLocal; |
|
| 63 | 4 | $this->oJobRepositoryChronos = $oJobRepositoryChronos; |
|
| 64 | 4 | $this->oDiffCompare = $oDiffCompare; |
|
| 65 | 4 | $this->oDatePeriodFactory = $oDatePeriodFactory; |
|
| 66 | 4 | $this->oLogger = $oLogger; |
|
| 67 | 4 | } |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @return string[] |
||
|
|
|||
| 71 | */ |
||
| 72 | public function getLocalMissingJobs() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return string[] |
||
| 82 | */ |
||
| 83 | public function getChronosMissingJobs() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return string[] |
||
| 93 | */ |
||
| 94 | 2 | public function getLocalJobUpdates() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $sJobName |
||
| 120 | * @return string[] |
||
| 121 | */ |
||
| 122 | public function getJobDiff($sJobName) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param JobEntity $oJobEntityA |
||
| 147 | * @param JobEntity $oJobEntityB |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | 2 | public function hasSameJobType(JobEntity $oJobEntityA, JobEntity $oJobEntityB) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param JobEntity $oJobEntityA |
||
| 160 | * @param JobEntity $oJobEntityB |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | 2 | private function compareJobEntities(JobEntity $oJobEntityA, JobEntity $oJobEntityB) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $sProperty |
||
| 195 | * @param JobEntity $oJobEntityA |
||
| 196 | * @param JobEntity $oJobEntityB |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | 2 | private function isJobEntityValueIdentical($sProperty, JobEntity $oJobEntityA, JobEntity $oJobEntityB) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param JobEntity $oJobEntityA |
||
| 234 | * @param JobEntity $oJobEntityB |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | 1 | private function isScheduleTimeZonePropertyIdentical(JobEntity $oJobEntityA, JobEntity $oJobEntityB) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @param JobEntity $oJobEntityA |
||
| 257 | * @param JobEntity $oJobEntityB |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | 1 | private function isSchedulePropertyIdentical(JobEntity $oJobEntityA, JobEntity $oJobEntityB) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $sIso8601String |
||
| 322 | * @param string $sTimeZone |
||
| 323 | * @return \DateTime |
||
| 324 | */ |
||
| 325 | 1 | private function createDateTimeObj($sIso8601String, $sTimeZone = '') |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @param string $sIso8601StringA |
||
| 344 | * @param string $sIso8601StringB |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | 1 | private function isEqualInterval($sIso8601StringA, $sIso8601StringB) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @param JobCollection $oJobCollectionA |
||
| 357 | * @param JobCollection $oJobCollectionB |
||
| 358 | * @return string[] |
||
| 359 | */ |
||
| 360 | private function getMissingJobsInCollectionA(JobCollection $oJobCollectionA, JobCollection $oJobCollectionB) |
||
| 367 | } |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.