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 | * |
||
| 181 | * @param string $userId |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function getAllJobsForUser($userId) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * TODO: Verify the security token and so on. |
||
| 205 | * The function the worker will call in order to set the jobs status. |
||
| 206 | * The worker should call it automatically after each processing step. |
||
| 207 | * |
||
| 208 | * @param integer $jobId |
||
| 209 | * @param boolean $failed |
||
| 210 | * @param string $errorMessage |
||
| 211 | */ |
||
| 212 | public function jobFinished($jobId, $failed, $errorMessage) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Finishes all Processed files by copying them to the right path and deleteing the temp files. |
||
| 233 | * Returns the number of processed files. |
||
| 234 | * TODO: adjust behaviour for saving of the worker with the right file extension or not |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function handleProcessed() { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Handles all failed orders of ocr processing queue and returns the job objects. |
||
| 270 | * |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | public function handleFailed() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Checks if the given languages are supported or not. |
||
| 294 | * TODO: $this->config->getAppValue() |
||
| 295 | * |
||
| 296 | * @param string[] $languages |
||
| 297 | * @return boolean |
||
| 298 | */ |
||
| 299 | private function checkForAcceptedLanguages($languages) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Checks if the process should be initiated without any language specified. |
||
| 312 | * |
||
| 313 | * @param string[] $languages |
||
| 314 | * @return boolean |
||
| 315 | */ |
||
| 316 | private function noLanguage($languages) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Handle the possible thrown Exceptions from all methods of this class. |
||
| 326 | * |
||
| 327 | * @param Exception $e |
||
| 328 | * @throws Exception |
||
| 329 | * @throws NotFoundException |
||
| 330 | */ |
||
| 331 | View Code Duplication | private function handleException($e) { |
|
| 342 | } |
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.