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 namespace Comodojo\Cache\Drivers; |
||
22 | View Code Duplication | class Apc extends AbstractDriver { |
|
|
|||
23 | |||
24 | const DRIVER_NAME = "apc"; |
||
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | 47 | public function __construct(array $configuration = []) { |
|
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | 47 | public function test() { |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 35 | public function get($key, $namespace) { |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 27 | public function set($key, $namespace, $value, $ttl = null) { |
|
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | 3 | public function delete($key, $namespace) { |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 11 | public function clear($namespace = null) { |
|
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 2 | public function getMultiple(array $keys, $namespace) { |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 1 | public function setMultiple(array $key_values, $namespace, $ttl = null) { |
|
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | 3 | public function deleteMultiple(array $keys, $namespace) { |
|
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | 5 | public function has($key, $namespace) { |
|
206 | |||
207 | /** |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | 2 | public function stats() { |
|
215 | |||
216 | /** |
||
217 | * Set namespace key |
||
218 | * |
||
219 | * @return mixed |
||
220 | */ |
||
221 | 5 | private function setNamespaceKey($namespace) { |
|
228 | |||
229 | /** |
||
230 | * Get namespace key |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | 37 | private function getNamespaceKey($namespace) { |
|
239 | |||
240 | } |
||
241 |
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.