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) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Delete an ocr job for a given id and userId. |
||
| 160 | * |
||
| 161 | * @param |
||
| 162 | * $jobId |
||
| 163 | * @param string $userId |
||
| 164 | * @return OcrJob |
||
| 165 | */ |
||
| 166 | public function deleteJob($jobId, $userId) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Gets all job objects for a specific user. |
||
| 188 | * TODO: use the mapping of the zettel |
||
| 189 | * |
||
| 190 | * @param string $userId |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function getAllJobsForUser($userId) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * TODO: finish the job |
||
| 214 | * The function the worker will call in order to set the jobs status. |
||
| 215 | * The worker should call it automatically after each processing step. |
||
| 216 | * |
||
| 217 | * @param integer $jobId |
||
| 218 | * @param boolean $failed |
||
| 219 | * @param string $errorMessage |
||
| 220 | */ |
||
| 221 | public function jobFinished($jobId, $failed, $errorMessage) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Finishes all Processed files by copying them to the right path and deleteing the temp files. |
||
| 242 | * Returns the number of processed files. |
||
| 243 | * TODO: adjust behaviour for saving of the worker with the right file extension or not |
||
| 244 | * |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | public function handleProcessed() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Handles all failed orders of ocr processing queue and returns the job objects. |
||
| 274 | * |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | public function handleFailed() { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Gives a temp file name back depending on the type of the OCR. |
||
| 298 | * Later in the worker this file is used as an output. |
||
| 299 | * |
||
| 300 | * @param integer $type |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | private function getTempFile($type) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Checks if the given languages are supported or not. |
||
| 313 | * |
||
| 314 | * @param string[] $languages |
||
| 315 | * @return boolean |
||
| 316 | */ |
||
| 317 | private function checkForAcceptedLanguages($languages) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Checks if the process should be initiated without any language specified. |
||
| 328 | * |
||
| 329 | * @param string[] $languages |
||
| 330 | * @return boolean |
||
| 331 | */ |
||
| 332 | private function noLanguage($languages) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Handle the possible thrown Exceptions from all methods of this class. |
||
| 342 | * |
||
| 343 | * @param Exception $e |
||
| 344 | * @throws Exception |
||
| 345 | * @throws NotFoundException |
||
| 346 | */ |
||
| 347 | View Code Duplication | private function handleException($e) { |
|
| 358 | } |
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.