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 |
||
11 | class Driver implements \Bernard\Driver |
||
12 | { |
||
13 | private $baseDirectory; |
||
14 | |||
15 | private $permissions; |
||
16 | |||
17 | /** |
||
18 | * @param string $baseDirectory The base directory |
||
19 | * @param int $permissions permissions to create the file with |
||
20 | */ |
||
21 | 9 | public function __construct($baseDirectory, $permissions = 0740) |
|
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | 1 | public function listQueues() |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 9 | public function createQueue($queueName) |
|
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function countMessages($queueName) |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 8 | public function pushMessage($queueName, $message) |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 3 | public function popMessage($queueName, $duration = 5) |
|
93 | { |
||
94 | 3 | $startAt = microtime(true); |
|
95 | 3 | $queueDir = $this->getQueueDirectory($queueName); |
|
96 | |||
97 | 3 | $it = new \GlobIterator($queueDir.DIRECTORY_SEPARATOR.'*.job', \FilesystemIterator::KEY_AS_FILENAME); |
|
98 | 3 | $files = array_keys(iterator_to_array($it)); |
|
99 | |||
100 | 3 | natsort($files); |
|
101 | |||
102 | 3 | while (true) { |
|
103 | 3 | if ($files) { |
|
104 | 3 | $id = array_pop($files); |
|
105 | 3 | if (@rename($queueDir.DIRECTORY_SEPARATOR.$id, $queueDir.DIRECTORY_SEPARATOR.$id.'.proceed')) { |
|
106 | 3 | return array(file_get_contents($queueDir.DIRECTORY_SEPARATOR.$id.'.proceed'), $id); |
|
107 | } |
||
108 | } |
||
109 | |||
110 | usleep(10000); |
||
111 | |||
112 | View Code Duplication | if ((microtime(true) - $startAt) >= $duration) { |
|
|
|||
113 | return array(null, null); |
||
114 | } |
||
115 | } |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param string $queueDir |
||
120 | * @param string $id |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | private function processFileOrFail($queueDir, $id) { |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | 1 | public function acknowledgeMessage($queueName, $receipt) |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 1 | public function peekQueue($queueName, $index = 0, $limit = 20) |
|
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | 2 | public function removeQueue($queueName) |
|
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | public function info() |
||
201 | |||
202 | /** |
||
203 | * @param string $queueName |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | 9 | private function getQueueDirectory($queueName) |
|
211 | |||
212 | /** |
||
213 | * Generates a uuid. |
||
214 | * |
||
215 | * @param string $queueName |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | 8 | private function getJobFilename($queueName) |
|
247 | } |
||
248 |
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.