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; |
||
26 | class Memcached extends AbstractDriver { |
||
27 | |||
28 | use InstanceTrait; |
||
29 | |||
30 | const DRIVER_NAME = "memcached"; |
||
31 | |||
32 | 86 | public function __construct(array $configuration = []) { |
|
41 | |||
42 | 86 | public function test() { |
|
47 | |||
48 | 35 | public function get($key, $namespace) { |
|
63 | |||
64 | 52 | public function set($key, $namespace, $value, $ttl = null) { |
|
79 | |||
80 | 8 | View Code Duplication | public function delete($key, $namespace) { |
91 | |||
92 | 22 | public function clear($namespace = null) { |
|
109 | |||
110 | 2 | public function getMultiple(array $keys, $namespace) { |
|
111 | |||
112 | 2 | if ( empty($keys) ) return []; |
|
113 | |||
114 | 2 | $keypad = array_combine($keys, array_fill(0, count($keys), null)); |
|
115 | |||
116 | 2 | $scope = $this->getNamespaceKey($namespace); |
|
117 | |||
118 | 2 | if ( $scope === false ) return $keypad; |
|
119 | |||
120 | $keyscope = array_map(function($key) use($scope) { |
||
121 | 2 | return "$scope-$key"; |
|
122 | 2 | }, $keys); |
|
123 | |||
124 | 2 | if ( version_compare(phpversion(), '7.0.0', '<') ) { |
|
125 | $data = $this->getInstance()->getMulti($keyscope, $null = null, MemcachedInstance::GET_PRESERVE_ORDER); |
||
126 | } else { |
||
127 | 2 | $data = $this->getInstance()->getMulti($keyscope, MemcachedInstance::GET_PRESERVE_ORDER); |
|
128 | } |
||
129 | |||
130 | 2 | $return = []; |
|
131 | |||
132 | 2 | foreach ( $data as $scoped_key => $value ) { |
|
133 | 2 | $key = substr($scoped_key, strlen("$scope-")); |
|
134 | 2 | $return[$key] = $value; |
|
135 | } |
||
136 | |||
137 | 2 | return array_replace($keypad, $return); |
|
138 | |||
139 | } |
||
140 | |||
141 | 2 | public function setMultiple(array $key_values, $namespace, $ttl = null) { |
|
142 | |||
143 | 2 | if ( $ttl == null ) $ttl = 0; |
|
144 | |||
145 | 2 | $scope = $this->getNamespaceKey($namespace); |
|
146 | |||
147 | 2 | if ( $scope === false ) $scope = $this->setNamespaceKey($namespace); |
|
148 | |||
149 | 2 | if ( $scope === false ) return false; |
|
150 | |||
151 | 2 | $shadowNames = []; |
|
152 | |||
153 | 2 | foreach ( $key_values as $key => $value ) { |
|
154 | 2 | $shadowNames["$scope-$key"] = $value; |
|
155 | } |
||
156 | |||
157 | 2 | return $this->getInstance()->setMulti($shadowNames, $ttl); |
|
158 | |||
159 | } |
||
160 | |||
161 | 4 | public function deleteMultiple(array $keys, $namespace) { |
|
176 | |||
177 | 5 | public function has($key, $namespace) { |
|
182 | |||
183 | 4 | public function stats() { |
|
188 | |||
189 | /** |
||
190 | * Set namespace key |
||
191 | * |
||
192 | * @return mixed |
||
193 | */ |
||
194 | 9 | View Code Duplication | private function setNamespaceKey($namespace) { |
201 | |||
202 | /** |
||
203 | * Get namespace key |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | 63 | private function getNamespaceKey($namespace) { |
|
212 | |||
213 | } |
||
214 |
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.