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 |
||
27 | class JobService { |
||
28 | |||
29 | /** |
||
30 | * |
||
31 | * @var ILogger |
||
32 | */ |
||
33 | private $logger; |
||
34 | |||
35 | /** |
||
36 | * |
||
37 | * @var QueueService |
||
38 | */ |
||
39 | private $queueService; |
||
40 | |||
41 | /** |
||
42 | * |
||
43 | * @var OcrJobMapper |
||
44 | */ |
||
45 | private $jobMapper; |
||
46 | |||
47 | /** |
||
48 | * |
||
49 | * @var View |
||
50 | */ |
||
51 | private $view; |
||
52 | |||
53 | /** |
||
54 | * |
||
55 | * @var String |
||
56 | */ |
||
57 | private $userId; |
||
58 | |||
59 | /** |
||
60 | * |
||
61 | * @var IL10N |
||
62 | */ |
||
63 | private $l10n; |
||
64 | |||
65 | /** |
||
66 | * |
||
67 | * @var FileService |
||
68 | */ |
||
69 | private $fileService; |
||
70 | |||
71 | /** |
||
72 | * |
||
73 | * @var ITempManager |
||
74 | */ |
||
75 | private $tempM; |
||
76 | |||
77 | /** |
||
78 | * JobService constructor. |
||
79 | * |
||
80 | * @param IL10N $l10n |
||
81 | * @param ILogger $logger |
||
82 | * @param string $userId |
||
83 | * @param View $view |
||
84 | * @param QueueService $queueService |
||
85 | * @param OcrJobMapper $mapper |
||
86 | * @param FileService $fileService |
||
87 | */ |
||
88 | public function __construct(IL10N $l10n, ILogger $logger, $userId, View $view, ITempManager $tempManager, QueueService $queueService, OcrJobMapper $mapper, FileService $fileService) { |
||
98 | |||
99 | /** |
||
100 | * Processes and prepares the files for OCR. |
||
101 | * Sends the stuff to the client in order to OCR async. |
||
102 | * |
||
103 | * @param string[] $languages |
||
104 | * @param array $files |
||
105 | * @return string |
||
106 | */ |
||
107 | public function process($languages, $files) { |
||
149 | |||
150 | /** |
||
151 | * Delete an ocr job for a given id and userId. |
||
152 | * |
||
153 | * @param |
||
154 | * $jobId |
||
155 | * @param string $userId |
||
156 | * @return OcrJob |
||
157 | */ |
||
158 | public function deleteJob($jobId, $userId) { |
||
177 | |||
178 | /** |
||
179 | * Gets all job objects for a specific user. |
||
180 | * TODO: use the mapping of the zettel |
||
181 | * |
||
182 | * @param string $userId |
||
183 | * @return array |
||
184 | */ |
||
185 | public function getAllJobsForUser($userId) { |
||
203 | |||
204 | /** |
||
205 | * TODO: Verify the security token and so on. |
||
206 | * The function the worker will call in order to set the jobs status. |
||
207 | * The worker should call it automatically after each processing step. |
||
208 | * |
||
209 | * @param integer $jobId |
||
210 | * @param boolean $failed |
||
211 | * @param string $errorMessage |
||
212 | */ |
||
213 | public function jobFinished($jobId, $failed, $errorMessage) { |
||
231 | |||
232 | /** |
||
233 | * Finishes all Processed files by copying them to the right path and deleteing the temp files. |
||
234 | * Returns the number of processed files. |
||
235 | * TODO: adjust behaviour for saving of the worker with the right file extension or not |
||
236 | * |
||
237 | * @return array |
||
238 | */ |
||
239 | public function handleProcessed() { |
||
268 | |||
269 | /** |
||
270 | * Handles all failed orders of ocr processing queue and returns the job objects. |
||
271 | * |
||
272 | * @return array |
||
273 | */ |
||
274 | public function handleFailed() { |
||
292 | |||
293 | /** |
||
294 | * Checks if the given languages are supported or not. |
||
295 | * TODO: $this->config->getAppValue() |
||
296 | * |
||
297 | * @param string[] $languages |
||
298 | * @return boolean |
||
299 | */ |
||
300 | private function checkForAcceptedLanguages($languages) { |
||
310 | |||
311 | /** |
||
312 | * Checks if the process should be initiated without any language specified. |
||
313 | * |
||
314 | * @param string[] $languages |
||
315 | * @return boolean |
||
316 | */ |
||
317 | private function noLanguage($languages) { |
||
324 | |||
325 | /** |
||
326 | * Handle the possible thrown Exceptions from all methods of this class. |
||
327 | * |
||
328 | * @param Exception $e |
||
329 | * @throws Exception |
||
330 | * @throws NotFoundException |
||
331 | */ |
||
332 | View Code Duplication | private function handleException($e) { |
|
343 | } |
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.