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 MemcachedCache implements CacheInterface |
||
12 | { |
||
13 | |||
14 | const PSR16_RESERVED_CHARACTERS = ['{','}','(',')','/','@',':']; |
||
15 | |||
16 | public $handler; |
||
17 | |||
18 | /** |
||
19 | * Connect to Memcached service |
||
20 | * |
||
21 | * @param Memcached $handler Memcached handler object |
||
22 | * |
||
23 | */ |
||
24 | 7 | public function __construct(Memcached $handler) |
|
25 | { |
||
26 | 7 | $this->handler = $handler; |
|
27 | 7 | if (defined('Memcached::HAVE_IGBINARY') && extension_loaded('igbinary')) { |
|
28 | 7 | ini_set('memcached.serializer', 'igbinary'); |
|
29 | } |
||
30 | 7 | } |
|
31 | |||
32 | /** |
||
33 | * {@inheritDoc} |
||
34 | */ |
||
35 | 1 | public function get($key, $default = null) |
|
42 | |||
43 | /** |
||
44 | * {@inheritDoc} |
||
45 | */ |
||
46 | 5 | View Code Duplication | public function set($key, $value, $ttl = null) |
55 | |||
56 | /** |
||
57 | * {@inheritDoc} |
||
58 | */ |
||
59 | 1 | public function delete($key) |
|
65 | |||
66 | /** |
||
67 | * {@inheritDoc} |
||
68 | */ |
||
69 | 7 | public function clear() |
|
73 | |||
74 | /** |
||
75 | * {@inheritDoc} |
||
76 | */ |
||
77 | 1 | public function getMultiple($keys, $default = null) |
|
85 | |||
86 | /** |
||
87 | * {@inheritDoc} |
||
88 | */ |
||
89 | 1 | View Code Duplication | public function setMultiple($values, $ttl = null) |
99 | |||
100 | /** |
||
101 | * {@inheritDoc} |
||
102 | */ |
||
103 | 1 | public function deleteMultiple($keys) |
|
110 | |||
111 | /** |
||
112 | * {@inheritDoc} |
||
113 | */ |
||
114 | 1 | public function increment($key, $step = 1) |
|
118 | |||
119 | /** |
||
120 | * {@inheritDoc} |
||
121 | */ |
||
122 | 1 | public function decrement($key, $step = 1) |
|
126 | |||
127 | /** |
||
128 | * {@inheritDoc} |
||
129 | */ |
||
130 | 1 | public function has($key) |
|
136 | |||
137 | 6 | private function checkReservedCharacters($key) |
|
150 | } |
||
151 |
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.