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 | 5 | public function __construct( |
|
55 | JobRepositoryInterface $oJobRepositoryLocal, |
||
56 | JobRepositoryInterface $oJobRepositoryChronos, |
||
57 | DiffCompareInterface $oDiffCompare, |
||
58 | DatePeriodFactoryInterface $oDatePeriodFactory, |
||
59 | LoggerInterface $oLogger |
||
60 | ) |
||
61 | { |
||
62 | 5 | $this->oJobRepositoryLocal = $oJobRepositoryLocal; |
|
63 | 5 | $this->oJobRepositoryChronos = $oJobRepositoryChronos; |
|
64 | 5 | $this->oDiffCompare = $oDiffCompare; |
|
65 | 5 | $this->oDatePeriodFactory = $oDatePeriodFactory; |
|
66 | 5 | $this->oLogger = $oLogger; |
|
67 | 5 | } |
|
68 | |||
69 | /** |
||
70 | * @return string[] |
||
|
|||
71 | */ |
||
72 | public function getLocalMissingJobs() |
||
73 | { |
||
74 | return $this->getMissingJobsInCollectionA( |
||
75 | $this->oJobRepositoryLocal->getJobs(), |
||
76 | $this->oJobRepositoryChronos->getJobs() |
||
77 | ); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return string[] |
||
82 | */ |
||
83 | public function getChronosMissingJobs() |
||
84 | { |
||
85 | return $this->getMissingJobsInCollectionA( |
||
86 | $this->oJobRepositoryChronos->getJobs(), |
||
87 | $this->oJobRepositoryLocal->getJobs() |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return string[] |
||
93 | */ |
||
94 | 3 | public function getLocalJobUpdates() |
|
95 | { |
||
96 | 3 | $_aJobsLocal = $this->oJobRepositoryLocal->getJobs(); |
|
97 | 3 | $_aLocalJobUpdates = []; |
|
98 | |||
99 | /** @var JobEntity $_oJobEntity */ |
||
100 | 3 | foreach ($_aJobsLocal as $_oJobEntityLocal) |
|
101 | { |
||
102 | 3 | $_oJobEntityChronos = $this->oJobRepositoryChronos->getJob($_oJobEntityLocal->name); |
|
103 | |||
104 | // if job already exist in chronos (not new or deleted in chronos) |
||
105 | 3 | if (!empty($_oJobEntityChronos->name)) |
|
106 | 3 | { |
|
107 | 3 | $_aNonidenticalProperties = $this->compareJobEntities($_oJobEntityLocal, $_oJobEntityChronos); |
|
108 | 3 | if (!empty($_aNonidenticalProperties)) |
|
109 | 3 | { |
|
110 | 3 | $_aLocalJobUpdates[] = $_oJobEntityLocal->name; |
|
111 | 3 | } |
|
112 | 3 | } |
|
113 | 3 | } |
|
114 | |||
115 | 3 | return $_aLocalJobUpdates; |
|
116 | } |
||
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 | 3 | private function compareJobEntities(JobEntity $oJobEntityA, JobEntity $oJobEntityB) |
|
164 | { |
||
165 | 3 | $_aNonidenticalProperties = []; |
|
166 | |||
167 | 3 | $_aDiff = array_merge( |
|
168 | 3 | array_diff_assoc( |
|
169 | 3 | $oJobEntityA->getSimpleArrayCopy(), |
|
170 | 3 | $oJobEntityB->getSimpleArrayCopy() |
|
171 | 3 | ), |
|
172 | 3 | array_diff_assoc( |
|
173 | 3 | $oJobEntityB->getSimpleArrayCopy(), |
|
174 | 3 | $oJobEntityA->getSimpleArrayCopy() |
|
175 | 3 | ) |
|
176 | 3 | ); |
|
177 | |||
178 | 3 | if (count($_aDiff) > 0) |
|
179 | 3 | { |
|
180 | 3 | $_aDiffKeys = array_keys($_aDiff); |
|
181 | 3 | foreach ($_aDiffKeys as $_sDiffKey) |
|
182 | { |
||
183 | 3 | if (!$this->isJobEntityValueIdentical($_sDiffKey, $oJobEntityA, $oJobEntityB)) |
|
184 | 3 | { |
|
185 | 3 | $_aNonidenticalProperties[] = $_sDiffKey; |
|
186 | 3 | } |
|
187 | 3 | } |
|
188 | 3 | } |
|
189 | |||
190 | 3 | return $_aNonidenticalProperties; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param string $sProperty |
||
195 | * @param JobEntity $oJobEntityA |
||
196 | * @param JobEntity $oJobEntityB |
||
197 | * @return bool |
||
198 | */ |
||
199 | 3 | private function isJobEntityValueIdentical($sProperty, JobEntity $oJobEntityA, JobEntity $oJobEntityB) |
|
200 | { |
||
201 | 3 | $mValueA = $oJobEntityA->{$sProperty}; |
|
202 | 3 | $mValueB = $oJobEntityB->{$sProperty}; |
|
203 | |||
204 | switch ($sProperty) |
||
205 | { |
||
206 | 3 | case 'schedule': |
|
207 | 1 | return $this->isSchedulePropertyIdentical($oJobEntityA, $oJobEntityB); |
|
208 | |||
209 | 3 | case 'scheduleTimeZone': |
|
210 | 1 | return $this->isScheduleTimeZonePropertyIdentical($oJobEntityA, $oJobEntityB); |
|
211 | |||
212 | 2 | 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 | 2 | case 'successCount': |
|
221 | 2 | case 'lastSuccess': |
|
222 | 2 | case 'errorCount': |
|
223 | 2 | case 'errorsSinceLastSuccess': |
|
224 | 2 | case 'lastError': |
|
225 | return true; |
||
226 | |||
227 | 2 | default: |
|
228 | 2 | return ($mValueA == $mValueB); |
|
229 | 2 | } |
|
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) |
|
324 | |||
325 | /** |
||
326 | * @param string $sIso8601String |
||
327 | * @param string $sTimeZone |
||
328 | * @return \DateTime |
||
329 | */ |
||
330 | 1 | private function createDateTimeObj($sIso8601String, $sTimeZone = '') |
|
346 | |||
347 | /** |
||
348 | * @param JobCollection $oJobCollectionA |
||
349 | * @param JobCollection $oJobCollectionB |
||
350 | * @return string[] |
||
351 | */ |
||
352 | private function getMissingJobsInCollectionA(JobCollection $oJobCollectionA, JobCollection $oJobCollectionB) |
||
359 | } |
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.