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 |
||
20 | class QueueService { |
||
21 | |||
22 | private $mapper; |
||
23 | |||
24 | private $logger; |
||
25 | |||
26 | private $l10n; |
||
27 | |||
28 | 4 | public function __construct(OcrStatusMapper $mapper, IL10N $l10n, ILogger $logger) { |
|
33 | |||
34 | /** |
||
35 | * Inits the client and sends the task to the background worker (async) |
||
36 | * |
||
37 | * @param OcrStatus $status |
||
38 | * @param string $datadirectory |
||
39 | * @param string $path |
||
40 | * @param string $language |
||
41 | * @param string $occDir |
||
42 | */ |
||
43 | 1 | public function clientSend($status, $datadirectory, $path, $language, $occDir) { |
|
44 | try { |
||
45 | 1 | $this->mapper->insert($status); |
|
46 | 1 | $queue = msg_get_queue(21671); |
|
47 | 1 | $msg = json_encode(array( |
|
48 | 1 | 'type' => $status->getType(), |
|
49 | 1 | 'datadirectory' => $datadirectory, |
|
50 | 1 | 'path' => $path, |
|
51 | 1 | 'tempfile' => $status->getTempFile(), |
|
52 | 1 | 'language' => $language, |
|
53 | 1 | 'statusid' => $status->getId(), |
|
54 | 'occdir' => $occDir |
||
55 | 1 | )); |
|
56 | 1 | if (msg_send($queue, 1, $msg)) { |
|
57 | 1 | $this->logger->debug('Client message: ' . json_encode($msg), ['app' => 'ocr']); |
|
58 | 1 | } else { |
|
59 | $this->mapper->delete($status); |
||
60 | throw new NotFoundException($this->l10n->t('Could not add files to the ocr processing queue.')); |
||
61 | } |
||
62 | 1 | } catch (Exception $e) { |
|
63 | exec('rm ' . $status->getTempFile()); |
||
64 | $this->handleException($e); |
||
65 | } |
||
66 | 1 | } |
|
67 | |||
68 | /** |
||
69 | * Handle the possible thrown Exceptions from all methods of this class. |
||
70 | * |
||
71 | * @param Exception $e |
||
72 | * @throws Exception |
||
73 | * @throws NotFoundException |
||
74 | */ |
||
75 | View Code Duplication | private function handleException($e) { |
|
83 | |||
84 | } |
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.