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 |
||
28 | class JobService { |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | * @var ILogger |
||
33 | */ |
||
34 | private $logger; |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | * @var RedisService |
||
39 | */ |
||
40 | private $redisService; |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | * @var OcrJobMapper |
||
45 | */ |
||
46 | private $jobMapper; |
||
47 | |||
48 | /** |
||
49 | * |
||
50 | * @var View |
||
51 | */ |
||
52 | private $view; |
||
53 | |||
54 | /** |
||
55 | * |
||
56 | * @var String |
||
57 | */ |
||
58 | private $userId; |
||
59 | |||
60 | /** |
||
61 | * |
||
62 | * @var IL10N |
||
63 | */ |
||
64 | private $l10n; |
||
65 | |||
66 | /** |
||
67 | * |
||
68 | * @var FileService |
||
69 | */ |
||
70 | private $fileService; |
||
71 | |||
72 | /** |
||
73 | * |
||
74 | * @var ITempManager |
||
75 | */ |
||
76 | private $tempM; |
||
77 | |||
78 | /** |
||
79 | * |
||
80 | * @var AppConfigService |
||
81 | */ |
||
82 | private $appConfigService; |
||
83 | |||
84 | /** |
||
85 | * JobService constructor. |
||
86 | * |
||
87 | * @param IL10N $l10n |
||
88 | * @param ILogger $logger |
||
89 | * @param string $userId |
||
90 | * @param View $view |
||
91 | * @param RedisService $queueService |
||
92 | * @param OcrJobMapper $mapper |
||
93 | * @param FileService $fileService |
||
94 | * @param AppConfigService $appConfigService |
||
95 | */ |
||
96 | public function __construct(IL10N $l10n, ILogger $logger, $userId, View $view, ITempManager $tempManager, RedisService $queueService, OcrJobMapper $mapper, FileService $fileService, AppConfigService $appConfigService) { |
||
107 | |||
108 | /** |
||
109 | * Processes and prepares the files for OCR. |
||
110 | * Sends the stuff to the client in order to OCR async. |
||
111 | * |
||
112 | * @param string[] $languages |
||
113 | * @param array $files |
||
114 | * @return string |
||
115 | */ |
||
116 | public function process($languages, $files) { |
||
158 | |||
159 | /** |
||
160 | * Delete an ocr job for a given id and userId. |
||
161 | * |
||
162 | * @param |
||
163 | * $jobId |
||
164 | * @param string $userId |
||
165 | * @return OcrJob |
||
166 | */ |
||
167 | public function deleteJob($jobId, $userId) { |
||
192 | |||
193 | /** |
||
194 | * Gets all job objects for a specific user. |
||
195 | * |
||
196 | * @param string $userId |
||
197 | * @return array |
||
198 | */ |
||
199 | public function getAllJobsForUser($userId) { |
||
218 | |||
219 | /** |
||
220 | * |
||
221 | * @throws NotFoundException |
||
222 | */ |
||
223 | public function checkForFinishedJobs() { |
||
235 | |||
236 | /** |
||
237 | * The function the worker will call in order to set the jobs status. |
||
238 | * The worker should call it automatically after each processing step. |
||
239 | * |
||
240 | * @param integer $jobId |
||
241 | * @param boolean $error |
||
242 | * @param string $log |
||
243 | */ |
||
244 | public function jobFinished($jobId, $error, $log) { |
||
260 | |||
261 | /** |
||
262 | * Finishes all Processed files by copying them to the right path and deleteing the temp files. |
||
263 | * Returns the number of processed files. |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | public function handleProcessed() { |
||
289 | |||
290 | /** |
||
291 | * Handles all failed orders of ocr processing queue and returns the job objects. |
||
292 | * |
||
293 | * @return array |
||
294 | */ |
||
295 | public function handleFailed() { |
||
313 | |||
314 | /** |
||
315 | * Gives a temp file name back depending on the type of the OCR. |
||
316 | * Later in the worker this file is used as an output. |
||
317 | * TODO: outsource the php functions to util and handle exceptions accordingly |
||
318 | * |
||
319 | * @param integer $type |
||
320 | * @return string |
||
321 | */ |
||
322 | private function getTempFile($type) { |
||
334 | |||
335 | /** |
||
336 | * Checks if the given languages are supported or not. |
||
337 | * |
||
338 | * @param string[] $languages |
||
339 | * @return boolean |
||
340 | */ |
||
341 | private function checkForAcceptedLanguages($languages) { |
||
349 | |||
350 | /** |
||
351 | * Checks if the process should be initiated without any language specified. |
||
352 | * |
||
353 | * @param string[] $languages |
||
354 | * @return boolean |
||
355 | */ |
||
356 | private function noLanguage($languages) { |
||
363 | |||
364 | /** |
||
365 | * Handle the possible thrown Exceptions from all methods of this class. |
||
366 | * |
||
367 | * @param Exception $e |
||
368 | * @throws Exception |
||
369 | * @throws NotFoundException |
||
370 | */ |
||
371 | View Code Duplication | private function handleException($e) { |
|
382 | } |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.