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 |
||
20 | class Memcache extends AbstractCache |
||
21 | { |
||
22 | /** |
||
23 | * Holds an injected adapter. |
||
24 | * @var \Memcache |
||
25 | */ |
||
26 | protected $adapter = null; |
||
27 | |||
28 | /** |
||
29 | * Holds the array of TTLs. |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $ttls = array(); |
||
33 | |||
34 | /** |
||
35 | * Constructor. |
||
36 | * |
||
37 | * @param \Memcache $memcache A Memcache instance. |
||
38 | * @param array $options Array of options. |
||
39 | */ |
||
40 | public function __construct(\Memcache $memcache, array $options = null) |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function loadKey($key) |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function loadTag($tag) |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | View Code Duplication | public function save($data, $key, array $tags = null, $ttl = null) |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | public function clean(array $tags) |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function delete($key) |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function flush($all = false) |
||
171 | |||
172 | /** |
||
173 | * Retrieves the cache item for the given id. |
||
174 | * |
||
175 | * @param string $id The cache id to retrieve. |
||
176 | * @return mixed|null Returns the cached data or null. |
||
177 | */ |
||
178 | public function get($id) |
||
190 | |||
191 | /** |
||
192 | * Returns the ttl sanitased for this cache adapter. |
||
193 | * |
||
194 | * The number of seconds may not exceed 60*60*24*30 = 2,592,000 (30 days). |
||
195 | * @see http://php.net/manual/en/Memcache.expiration.php |
||
196 | * |
||
197 | * @param integer|null $ttl The time-to-live in seconds. |
||
198 | * @return int |
||
199 | */ |
||
200 | public function sanitiseTtl($ttl) |
||
204 | |||
205 | /** |
||
206 | * Returns the named indexer. |
||
207 | * |
||
208 | * @param string $name The name of the index. |
||
209 | * @return Indexer\Adapter |
||
210 | */ |
||
211 | public function getIndex($name) |
||
215 | |||
216 | /** |
||
217 | * Returns a prefixed and sanitased cache id. |
||
218 | * |
||
219 | * @param string $key The base key to prefix. |
||
220 | * @return string |
||
221 | */ |
||
222 | public function mapIdx($key) |
||
226 | |||
227 | /** |
||
228 | * Increments the value of the given key. |
||
229 | * |
||
230 | * @param string $key The key to increment. |
||
231 | * |
||
232 | * @return int|bool Returns the new item's value on success or FALSE on failure. |
||
233 | */ |
||
234 | View Code Duplication | public function increment($key) |
|
247 | |||
248 | /** |
||
249 | * {@inheritdoc} |
||
250 | * |
||
251 | * The number of seconds may not exceed 60*60*24*30 = 2,592,000 (30 days). |
||
252 | */ |
||
253 | public function getTtl($key) |
||
265 | |||
266 | /** |
||
267 | * Gets the injected adapter. |
||
268 | * |
||
269 | * @return \Memcache |
||
270 | */ |
||
271 | public function getAdapter() |
||
275 | } |
||
276 |
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.