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 RedisService { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * |
||
| 31 | * @var IConfig |
||
| 32 | */ |
||
| 33 | private $config; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * |
||
| 37 | * @var OcrJobMapper |
||
| 38 | */ |
||
| 39 | private $mapper; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * |
||
| 43 | * @var FileService |
||
| 44 | */ |
||
| 45 | private $fileService; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * |
||
| 49 | * @var ILogger |
||
| 50 | */ |
||
| 51 | private $logger; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * |
||
| 55 | * @var IL10N |
||
| 56 | */ |
||
| 57 | private $l10n; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | private $redisHost; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * |
||
| 67 | * @var integer |
||
| 68 | */ |
||
| 69 | private $redisPort; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * |
||
| 73 | * @var integer |
||
| 74 | */ |
||
| 75 | private $redisDb; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | private $appName = 'ocr'; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * QueueService constructor. |
||
| 85 | * |
||
| 86 | * @param OcrJobMapper $mapper |
||
| 87 | * @param FileService $fileService |
||
| 88 | * @param IConfig $config |
||
| 89 | * @param IL10N $l10n |
||
| 90 | * @param ILogger $logger |
||
| 91 | */ |
||
| 92 | 5 | public function __construct(OcrJobMapper $mapper, FileService $fileService, IConfig $config, IL10N $l10n, |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Inits the client and sends the task to the background worker (async) |
||
| 106 | * |
||
| 107 | * @param OcrJob $job |
||
| 108 | * @param string[] $languages |
||
| 109 | */ |
||
| 110 | public function sendJob($job, $languages) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Retrieves all finished jobs from redis and returns them. |
||
| 137 | * |
||
| 138 | * @return string[] |
||
| 139 | */ |
||
| 140 | public function readingFinishedJobs() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Setup the Redis instance and return to whom ever it needs. |
||
| 159 | * |
||
| 160 | * @throws NotFoundException |
||
| 161 | * @return \Redis |
||
| 162 | */ |
||
| 163 | private function setupRedisInstance() { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Handle the possible thrown Exceptions from all methods of this class. |
||
| 184 | * |
||
| 185 | * @param Exception $e |
||
| 186 | * @throws Exception |
||
| 187 | * @throws NotFoundException |
||
| 188 | */ |
||
| 189 | private function handleException($e) { |
||
| 195 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.