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 |
||
8 | class ElggMemcache extends \ElggSharedMemoryCache { |
||
9 | /** |
||
10 | * Global Elgg configuration |
||
11 | * |
||
12 | * @var \stdClass |
||
13 | */ |
||
14 | private $CONFIG; |
||
15 | |||
16 | /** |
||
17 | * Minimum version of memcached needed to run |
||
18 | * |
||
19 | */ |
||
20 | private static $MINSERVERVERSION = '1.1.12'; |
||
21 | |||
22 | /** |
||
23 | * Memcache object |
||
24 | */ |
||
25 | private $memcache; |
||
26 | |||
27 | /** |
||
28 | * Expiry of saved items (default timeout after a day to prevent anything getting too stale) |
||
29 | */ |
||
30 | private $expires = 86400; |
||
31 | |||
32 | /** |
||
33 | * The version of memcache running |
||
34 | */ |
||
35 | private $version = 0; |
||
36 | |||
37 | /** |
||
38 | * Connect to memcache. |
||
39 | * |
||
40 | * @param string $namespace The namespace for this cache to write to - |
||
41 | * note, namespaces of the same name are shared! |
||
42 | * |
||
43 | * @throws ConfigurationException |
||
44 | */ |
||
45 | public function __construct($namespace = 'default') { |
||
114 | |||
115 | /** |
||
116 | * Set the default expiry. |
||
117 | * |
||
118 | * @param int $expires The lifetime as a unix timestamp or time from now. Defaults forever. |
||
119 | * |
||
120 | * @return void |
||
121 | */ |
||
122 | public function setDefaultExpiry($expires = 0) { |
||
125 | |||
126 | /** |
||
127 | * Combine a key with the namespace. |
||
128 | * Memcache can only accept <250 char key. If the given key is too long it is shortened. |
||
129 | * |
||
130 | * @param string $key The key |
||
131 | * |
||
132 | * @return string The new key. |
||
133 | */ |
||
134 | private function makeMemcacheKey($key) { |
||
143 | |||
144 | /** |
||
145 | * Saves a name and value to the cache |
||
146 | * |
||
147 | * @param string $key Name |
||
148 | * @param string $data Value |
||
149 | * @param integer $expires Expires (in seconds) |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function save($key, $data, $expires = null) { |
||
169 | |||
170 | /** |
||
171 | * Retrieves data. |
||
172 | * |
||
173 | * @param string $key Name of data to retrieve |
||
174 | * @param int $offset Offset |
||
175 | * @param int $limit Limit |
||
176 | * |
||
177 | * @return mixed |
||
178 | */ |
||
179 | public function load($key, $offset = 0, $limit = null) { |
||
191 | |||
192 | /** |
||
193 | * Delete data |
||
194 | * |
||
195 | * @param string $key Name of data |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function delete($key) { |
||
204 | |||
205 | /** |
||
206 | * Clears the entire cache |
||
207 | * |
||
208 | * @return true |
||
209 | */ |
||
210 | public function clear() { |
||
224 | } |
||
225 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.