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:
1 | <?php |
||
19 | class StoreJobBusinessCase implements StoreJobBusinessCaseInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var JobIndexServiceInterface |
||
23 | */ |
||
24 | private $oJobIndexService; |
||
25 | |||
26 | /** |
||
27 | * @var JobRepositoryInterface |
||
28 | */ |
||
29 | private $oJobRepositoryChronos; |
||
30 | |||
31 | /** |
||
32 | * @var JobRepositoryInterface |
||
33 | */ |
||
34 | private $oJobRepositoryLocal; |
||
35 | |||
36 | /** |
||
37 | * @var JobComparisonInterface |
||
38 | */ |
||
39 | private $oJobComparisonBusinessCase; |
||
40 | |||
41 | /** |
||
42 | * @var JobDependencyServiceInterface |
||
43 | */ |
||
44 | private $oJobDependencyService; |
||
45 | |||
46 | /** |
||
47 | * @var LoggerInterface |
||
48 | */ |
||
49 | private $oLogger; |
||
50 | |||
51 | |||
52 | 13 | public function __construct( |
|
53 | JobIndexServiceInterface $oJobIndexService, |
||
54 | JobRepositoryInterface $oJobRepositoryChronos, |
||
55 | JobRepositoryInterface $oJobRepositoryLocal, |
||
56 | JobComparisonInterface $oJobComparisonBusinessCase, |
||
57 | JobDependencyServiceInterface $oJobDependencyService, |
||
58 | LoggerInterface $oLogger |
||
59 | ) |
||
60 | { |
||
61 | 13 | $this->oJobIndexService = $oJobIndexService; |
|
62 | 13 | $this->oJobRepositoryChronos = $oJobRepositoryChronos; |
|
63 | 13 | $this->oJobRepositoryLocal = $oJobRepositoryLocal; |
|
64 | 13 | $this->oJobComparisonBusinessCase = $oJobComparisonBusinessCase; |
|
65 | 13 | $this->oJobDependencyService = $oJobDependencyService; |
|
66 | 13 | $this->oLogger = $oLogger; |
|
67 | 13 | } |
|
68 | |||
69 | /** |
||
70 | * @inheritdoc |
||
71 | */ |
||
72 | 11 | public function storeIndexedJobs() |
|
73 | { |
||
74 | // add new jobs to chronos |
||
75 | 11 | $_aNewJobs = $this->oJobComparisonBusinessCase->getChronosMissingJobs(); |
|
76 | 11 | foreach ($_aNewJobs as $_sJobName) |
|
77 | { |
||
78 | 6 | $this->hasAddedJob($_sJobName); |
|
79 | 11 | } |
|
80 | |||
81 | // delete missing jobs from chronos |
||
82 | 11 | $_aMissingJobs = $this->oJobComparisonBusinessCase->getLocalMissingJobs(); |
|
83 | 11 | foreach ($_aMissingJobs as $_sJobName) |
|
84 | { |
||
85 | 5 | $this->hasRemovedJob($_sJobName); |
|
86 | 11 | } |
|
87 | |||
88 | // update jobs on chronos |
||
89 | 11 | $_aLocalJobUpdates = $this->oJobComparisonBusinessCase->getLocalJobUpdates(); |
|
90 | 11 | foreach ($_aLocalJobUpdates as $_sJobName) |
|
91 | { |
||
92 | 4 | $this->hasUpdatedJob($_sJobName); |
|
93 | 11 | } |
|
94 | 11 | } |
|
95 | |||
96 | /** |
||
97 | * @inheritdoc |
||
98 | */ |
||
99 | 2 | public function storeJobsToLocalRepository(array $aJobNames = [], $bForceOverwrite = false) |
|
173 | |||
174 | /** |
||
175 | * @param string $sJobName |
||
176 | * @return bool |
||
177 | */ |
||
178 | 6 | private function hasAddedJob($sJobName) |
|
179 | { |
||
180 | 6 | $_oJobEntityLocal = $this->oJobRepositoryLocal->getJob($sJobName); |
|
181 | |||
182 | 6 | View Code Duplication | if ($this->isAbleToStoreEntity($_oJobEntityLocal)) |
183 | 6 | { |
|
184 | 4 | if ($this->oJobRepositoryChronos->addJob($_oJobEntityLocal)) |
|
185 | 4 | { |
|
186 | 3 | $this->oJobIndexService->removeJob($_oJobEntityLocal->name); |
|
187 | 3 | $this->oLogger->notice(sprintf( |
|
188 | 3 | 'Job "%s" successfully added to chronos', |
|
189 | 3 | $_oJobEntityLocal->name |
|
190 | 3 | )); |
|
191 | |||
192 | 3 | return true; |
|
193 | } |
||
194 | |||
195 | 1 | $this->oLogger->error(sprintf( |
|
196 | 1 | 'Failed to add job "%s" to chronos', |
|
197 | 1 | $_oJobEntityLocal->name |
|
198 | 1 | )); |
|
199 | 1 | } |
|
200 | |||
201 | 3 | return false; |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param $sJobName |
||
206 | * @return bool |
||
207 | */ |
||
208 | 5 | private function hasRemovedJob($sJobName) |
|
209 | { |
||
210 | 5 | View Code Duplication | if ($this->isAbleToDeleteJob($sJobName)) |
211 | 5 | { |
|
212 | 3 | if ($this->oJobRepositoryChronos->removeJob($sJobName)) |
|
213 | 3 | { |
|
214 | 3 | $this->oJobIndexService->removeJob($sJobName); |
|
215 | 3 | $this->oLogger->notice(sprintf( |
|
216 | 3 | 'Job "%s" successfully removed from chronos', |
|
217 | $sJobName |
||
218 | 3 | )); |
|
219 | |||
220 | 3 | return true; |
|
221 | } |
||
222 | |||
223 | $this->oLogger->error(sprintf( |
||
224 | 'Failed to remove job "%s" from chronos', |
||
225 | $sJobName |
||
226 | )); |
||
227 | } |
||
228 | |||
229 | 2 | return false; |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param string $sJobName |
||
234 | * @return bool |
||
235 | */ |
||
236 | 4 | private function hasUpdatedJob($sJobName) |
|
237 | { |
||
238 | 4 | $_oJobEntityLocal = $this->oJobRepositoryLocal->getJob($sJobName); |
|
239 | |||
240 | 4 | if ($this->isAbleToStoreEntity($_oJobEntityLocal)) |
|
241 | 4 | { |
|
242 | 3 | $_oJobEntityChronos = $this->oJobRepositoryChronos->getJob($sJobName); |
|
243 | |||
244 | // handle job update |
||
245 | 3 | if ($this->oJobComparisonBusinessCase->hasSameJobType($_oJobEntityLocal, $_oJobEntityChronos)) |
|
246 | 3 | { |
|
247 | 2 | $_bHasUpdatedJob = $this->oJobRepositoryChronos->updateJob($_oJobEntityLocal); |
|
248 | 2 | } |
|
249 | else |
||
250 | { |
||
251 | $_bHasUpdatedJob = ( |
||
252 | 1 | $this->oJobRepositoryChronos->removeJob($_oJobEntityChronos->name) |
|
253 | 1 | && $this->oJobRepositoryChronos->addJob($_oJobEntityLocal) |
|
254 | 1 | ); |
|
255 | } |
||
256 | |||
257 | // handle update result |
||
258 | if ($_bHasUpdatedJob) |
||
259 | 3 | { |
|
260 | 3 | $this->oJobIndexService->removeJob($_oJobEntityLocal->name); |
|
261 | 3 | $this->oLogger->notice(sprintf( |
|
262 | 3 | 'Job "%s" successfully updated in chronos', |
|
263 | 3 | $_oJobEntityLocal->name |
|
264 | 3 | )); |
|
265 | |||
266 | 3 | return true; |
|
267 | } |
||
268 | |||
269 | // in case of an error |
||
270 | $this->oLogger->error(sprintf( |
||
271 | 'Failed to update job "%s" in chronos', |
||
272 | $_oJobEntityLocal->name |
||
273 | )); |
||
274 | } |
||
275 | |||
276 | 1 | return false; |
|
277 | } |
||
278 | |||
279 | /** |
||
280 | * @param JobEntity $oEntity |
||
281 | * @return bool |
||
282 | */ |
||
283 | 8 | private function isAbleToStoreEntity(JobEntity $oEntity) |
|
312 | |||
313 | /** |
||
314 | * @param string $sJobName |
||
315 | * @return bool |
||
316 | */ |
||
317 | 5 | private function isAbleToDeleteJob($sJobName) |
|
348 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.