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) |
|
200 | { |
||
201 | 2 | $mValueA = $oJobEntityA->{$sProperty}; |
|
202 | 2 | $mValueB = $oJobEntityB->{$sProperty}; |
|
203 | |||
204 | switch ($sProperty) |
||
205 | { |
||
206 | 2 | case 'schedule': |
|
207 | 1 | return $this->isSchedulePropertyIdentical($oJobEntityA, $oJobEntityB); |
|
208 | |||
209 | 2 | case 'scheduleTimeZone': |
|
210 | 1 | return $this->isScheduleTimeZonePropertyIdentical($oJobEntityA, $oJobEntityB); |
|
211 | |||
212 | 1 | case 'parents': |
|
213 | return ( |
||
214 | is_array($mValueA) |
||
215 | && is_array($mValueB) |
||
216 | && count(array_diff($mValueA, $mValueB)) == 0 |
||
217 | && count(array_diff($mValueB, $mValueA)) == 0 |
||
218 | ); |
||
219 | |||
220 | 1 | case 'successCount': |
|
221 | 1 | case 'lastSuccess': |
|
222 | 1 | case 'errorCount': |
|
223 | 1 | case 'errorsSinceLastSuccess': |
|
224 | 1 | case 'lastError': |
|
225 | return true; |
||
226 | |||
227 | 1 | default: |
|
228 | 1 | return ($mValueA == $mValueB); |
|
229 | 1 | } |
|
230 | } |
||
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
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.