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 |
||
| 12 | class InMemoryAdapter extends Adapter implements CacheAdapter |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | private static $registry = []; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var self Reference to singleton instance |
||
| 21 | */ |
||
| 22 | private static $instance; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * is not allowed to call from outside: private! |
||
| 26 | * |
||
| 27 | */ |
||
| 28 | protected function __construct() |
||
| 31 | |||
| 32 | /** |
||
| 33 | * gets the instance via lazy initialization (created on first usage) |
||
| 34 | * |
||
| 35 | * @return self |
||
| 36 | */ |
||
| 37 | public static function getInstance() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get a value identified by $key. |
||
| 47 | * |
||
| 48 | * @param string $key |
||
| 49 | * |
||
| 50 | * @return bool|mixed |
||
| 51 | */ |
||
| 52 | public function get($key) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $key |
||
| 71 | * |
||
| 72 | * @return mixed |
||
| 73 | */ |
||
| 74 | protected function restoreDataStructure($key) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param $key |
||
| 87 | * |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | private function isSerializedArray($key) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Set a value identified by $key and with an optional $ttl. |
||
| 101 | * |
||
| 102 | * @param string $key |
||
| 103 | * @param mixed $value |
||
| 104 | * @param int $ttl |
||
| 105 | * |
||
| 106 | * @return $this |
||
| 107 | */ |
||
| 108 | public function set($key, $value, $ttl = 0) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param $ttl |
||
| 126 | * |
||
| 127 | * @return int |
||
| 128 | */ |
||
| 129 | View Code Duplication | private function getCalculatedTtl($ttl) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @param $value |
||
| 140 | * |
||
| 141 | * @return mixed |
||
| 142 | */ |
||
| 143 | protected function storageDataStructure($value) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Delete a value identified by $key. |
||
| 154 | * |
||
| 155 | * @param string $key |
||
| 156 | * |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function delete($key) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Checks the availability of the cache service. |
||
| 171 | * |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | public function isAvailable() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Clears all expired values from cache. |
||
| 181 | * |
||
| 182 | * @return mixed |
||
| 183 | */ |
||
| 184 | public function clear() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Clear an item if it expired. |
||
| 194 | * |
||
| 195 | * @param $key |
||
| 196 | * @param DateTime $dateTime |
||
| 197 | */ |
||
| 198 | private function clearExpiredKey($key, DateTime $dateTime) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Clears all values from the cache. |
||
| 207 | * |
||
| 208 | * @return mixed |
||
| 209 | */ |
||
| 210 | public function drop() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * prevent the instance from being cloned |
||
| 217 | * |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | protected function __clone() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * prevent from being unserialized |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | protected function __wakeup() |
||
| 232 | } |
||
| 233 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: