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) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Delete an ocr job for a given id and userId. |
||
| 146 | * |
||
| 147 | * @param |
||
| 148 | * $jobId |
||
| 149 | * @param string $userId |
||
| 150 | * @return OcrJob |
||
| 151 | */ |
||
| 152 | public function deleteJob($jobId, $userId) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Gets all job objects for a specific user. |
||
| 174 | * |
||
| 175 | * @param string $userId |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function getAllJobsForUser($userId) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * TODO: Verify the security token and so on. |
||
| 199 | * The function the worker will call in order to set the jobs status. |
||
| 200 | * The worker should call it automatically after each processing step. |
||
| 201 | * |
||
| 202 | * @param integer $jobId |
||
| 203 | * @param boolean $failed |
||
| 204 | * @param string $errorMessage |
||
| 205 | */ |
||
| 206 | public function jobFinished($jobId, $failed, $errorMessage) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Finishes all Processed files by copying them to the right path and deleteing the temp files. |
||
| 227 | * Returns the number of processed files. |
||
| 228 | * TODO: adjust behaviour for saving of the worker with the right file extension or not |
||
| 229 | * |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | public function handleProcessed() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Handles all failed orders of ocr processing queue and returns the job objects. |
||
| 264 | * |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | public function handleFailed() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Checks if the given languages are supported or not. |
||
| 288 | * TODO: $this->config->getAppValue() |
||
| 289 | * |
||
| 290 | * @param string[] $languages |
||
| 291 | * @return boolean |
||
| 292 | */ |
||
| 293 | private function checkForAcceptedLanguages($languages) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Handle the possible thrown Exceptions from all methods of this class. |
||
| 308 | * |
||
| 309 | * @param Exception $e |
||
| 310 | * @throws Exception |
||
| 311 | * @throws NotFoundException |
||
| 312 | */ |
||
| 313 | View Code Duplication | private function handleException($e) { |
|
| 324 | } |
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.