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; |
||
24 | class Memcached extends AbstractDriver { |
||
25 | |||
26 | use InstanceTrait; |
||
27 | |||
28 | const DRIVER_NAME = "memcached"; |
||
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | 45 | public function __construct(array $configuration = []) { |
|
34 | |||
35 | 45 | if ( class_exists('Memcached') === false ) throw new Exception("ext-memcached not available"); |
|
36 | |||
37 | 45 | $instance = new MemcachedInstance($configuration['persistent_id']); |
|
38 | |||
39 | 45 | $instance->addServer( |
|
40 | 45 | $configuration['server'], |
|
41 | 45 | $configuration['port'], |
|
42 | 45 | $configuration['weight'] |
|
43 | 45 | ); |
|
44 | |||
45 | 45 | if ( !empty($configuration['username']) && !empty($configuration['password']) ) { |
|
46 | $instance->setOption(MemcachedInstance::OPT_BINARY_PROTOCOL, true); |
||
47 | $instance->setSaslAuthData($configuration['username'], $configuration['password']); |
||
48 | } |
||
49 | |||
50 | 45 | $this->setInstance($instance); |
|
51 | |||
52 | 45 | } |
|
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | 45 | public function test() { |
|
62 | |||
63 | 45 | public function ping() { |
|
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 34 | public function get($key, $namespace) { |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 25 | public function set($key, $namespace, $value, $ttl = null) { |
|
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | 3 | View Code Duplication | public function delete($key, $namespace) { |
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | 11 | public function clear($namespace = null) { |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 2 | public function getMultiple(array $keys, $namespace) { |
|
154 | |||
155 | 2 | if ( empty($keys) ) return []; |
|
156 | |||
157 | 2 | $keypad = array_combine($keys, array_fill(0, count($keys), null)); |
|
158 | |||
159 | 2 | $scope = $this->getNamespaceKey($namespace); |
|
160 | |||
161 | 2 | if ( $scope === false ) return $keypad; |
|
162 | |||
163 | $keyscope = array_map(function($key) use($scope) { |
||
164 | 2 | return "$scope-$key"; |
|
165 | 2 | }, $keys); |
|
166 | |||
167 | 2 | if ( version_compare(phpversion(), '7.0.0', '<') ) { |
|
168 | 2 | $data = $this->getInstance()->getMulti($keyscope, $null = null, MemcachedInstance::GET_PRESERVE_ORDER); |
|
169 | 2 | } else { |
|
170 | $data = $this->getInstance()->getMulti($keyscope, MemcachedInstance::GET_PRESERVE_ORDER); |
||
171 | } |
||
172 | |||
173 | 2 | $return = []; |
|
174 | |||
175 | 2 | foreach ( $data as $scoped_key => $value ) { |
|
176 | 2 | $key = substr($scoped_key, strlen("$scope-")); |
|
177 | 2 | $return[$key] = $value; |
|
178 | 2 | } |
|
179 | |||
180 | 2 | return array_replace($keypad, $return); |
|
181 | |||
182 | } |
||
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | 1 | public function setMultiple(array $key_values, $namespace, $ttl = null) { |
|
188 | |||
189 | 1 | if ( $ttl == null ) $ttl = 0; |
|
190 | |||
191 | 1 | $scope = $this->getNamespaceKey($namespace); |
|
192 | |||
193 | 1 | if ( $scope === false ) $scope = $this->setNamespaceKey($namespace); |
|
194 | |||
195 | 1 | if ( $scope === false ) return false; |
|
196 | |||
197 | 1 | $shadowNames = []; |
|
198 | |||
199 | 1 | foreach ( $key_values as $key => $value ) { |
|
200 | 1 | $shadowNames["$scope-$key"] = $value; |
|
201 | 1 | } |
|
202 | |||
203 | 1 | return $this->getInstance()->setMulti($shadowNames, $ttl); |
|
204 | |||
205 | } |
||
206 | |||
207 | /** |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | 3 | public function deleteMultiple(array $keys, $namespace) { |
|
225 | |||
226 | /** |
||
227 | * {@inheritdoc} |
||
228 | */ |
||
229 | 5 | public function has($key, $namespace) { |
|
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | 2 | public function stats() { |
|
243 | |||
244 | /** |
||
245 | * Set namespace key |
||
246 | * |
||
247 | * @return mixed |
||
248 | */ |
||
249 | 5 | View Code Duplication | private function setNamespaceKey($namespace) { |
256 | |||
257 | /** |
||
258 | * Get namespace key |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | 35 | private function getNamespaceKey($namespace) { |
|
267 | |||
268 | } |
||
269 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.