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:
Complex classes like OcrService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OcrService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class OcrService { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var ILogger |
||
| 32 | */ |
||
| 33 | private $logger; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var ITempManager |
||
| 37 | */ |
||
| 38 | private $tempM; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var IConfig |
||
| 42 | */ |
||
| 43 | private $config; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var GearmanWorkerService |
||
| 47 | */ |
||
| 48 | private $workerService; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var OcrStatusMapper |
||
| 52 | */ |
||
| 53 | private $statusMapper; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var View |
||
| 57 | */ |
||
| 58 | private $view; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var |
||
| 62 | */ |
||
| 63 | private $userId; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Array of allowed mimetypes for ocr processing |
||
| 67 | */ |
||
| 68 | const ALLOWED_MIMETYPES = ['application/pdf', 'image/png', 'image/jpeg', 'image/tiff']; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * the correct mimetype for a pdf file |
||
| 72 | */ |
||
| 73 | const MIMETYPE_PDF = 'application/pdf'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * the only allowed image mimetypes by tesseract |
||
| 77 | */ |
||
| 78 | const MIMETYPES_IMAGE = ['image/png', 'image/jpeg', 'image/tiff']; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * OcrService constructor. |
||
| 82 | * |
||
| 83 | * @param ILogger $logger |
||
| 84 | */ |
||
| 85 | 2 | public function __construct(ITempManager $tempManager, IConfig $config, GearmanWorkerService $workerService, OcrStatusMapper $mapper, View $view, $userId, ILogger $logger) { |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Gets the list of all available tesseract-ocr languages. |
||
| 97 | * |
||
| 98 | * @return array Languages |
||
| 99 | */ |
||
| 100 | public function listLanguages(){ |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Processes and prepares the files for ocr. |
||
| 130 | * Sends the stuff to the gearman client in order to ocr async. |
||
| 131 | * |
||
| 132 | * @param $language |
||
| 133 | * @param array $files |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function process($language, $files) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * A function which returns the JSONResponse for all required status checks and tasks. |
||
| 177 | * It will check for already processed, pending and failed ocr tasks and return them as needed. |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function status(){ |
||
| 195 | |||
| 196 | /** |
||
| 197 | * The command ocr:complete for occ will call this function in order to set the status. |
||
| 198 | * the gearman worker should call it automatically after each processing step. |
||
| 199 | * |
||
| 200 | * @param $statusId |
||
| 201 | * @param $failed |
||
| 202 | */ |
||
| 203 | public function complete($statusId, $failed){ |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Finishes all Processed files by copying them to the right path and deleteing the temp files. |
||
| 225 | * Returns the number of processed files. |
||
| 226 | * |
||
| 227 | * @return int |
||
| 228 | */ |
||
| 229 | private function handleProcessed(){ |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Handles all failed orders of ocr processing queue and returns the status objects. |
||
| 257 | * |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | private function handleFailed(){ |
||
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns a not existing file name for pdf or image processing |
||
| 279 | * |
||
| 280 | * @param Files\FileInfo $fileInfo |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | private function buildNewName(Files\FileInfo $fileInfo){ |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Returns the fileInfo for each file in files and checks |
||
| 301 | * if it has a allowed mimetype and some other conditions. |
||
| 302 | * |
||
| 303 | * @param array $files |
||
| 304 | * @return array of Files\FileInfo |
||
| 305 | * @throws NotFoundException |
||
| 306 | */ |
||
| 307 | private function buildFileInfo(array $files){ |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Returns the correct path based on delivered file variable |
||
| 337 | * @param $file |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | private function getCorrectPath($file){ |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Inits the Gearman client and sends the task to the background worker (async) |
||
| 351 | * @param $type |
||
| 352 | * @param $datadirectory |
||
| 353 | * @param $path |
||
| 354 | * @param $tempFile |
||
| 355 | * @param $language |
||
| 356 | * @param $statusId |
||
| 357 | */ |
||
| 358 | private function sendGearmanJob($type, $datadirectory, $path, $tempFile, $language, $status, $occDir){ |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Handle the possible thrown Exceptions from all methods of this class. |
||
| 384 | * |
||
| 385 | * @param Exception $e |
||
| 386 | * @throws Exception |
||
| 387 | * @throws NotFoundException |
||
| 388 | */ |
||
| 389 | View Code Duplication | private function handleException($e) { |
|
| 397 | } |
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.