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 |
||
| 11 | final class ArrayCache extends AbstractCache implements CacheInterface |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Array containing the cached data. |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | private $cache = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Fetches a value from the cache. |
||
| 22 | * |
||
| 23 | * @param string $key The unique key of this item in the cache. |
||
| 24 | * @param mixed $default Default value to return if the key does not exist. |
||
| 25 | * |
||
| 26 | * @return mixed The value of the item from the cache, or $default in case of cache miss. |
||
| 27 | * |
||
| 28 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 29 | */ |
||
| 30 | public function get($key, $default = null) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
||
| 48 | * |
||
| 49 | * @param string $key The key of the item to store. |
||
| 50 | * @param mixed $value The value of the item to store, must be serializable. |
||
| 51 | * @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
||
| 52 | * the driver supports TTL then the library may set a default value |
||
| 53 | * for it or let the driver take care of that. |
||
| 54 | * |
||
| 55 | * @return bool True on success and false on failure. |
||
| 56 | * |
||
| 57 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 58 | */ |
||
| 59 | public function set($key, $value, $ttl = null) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Delete an item from the cache by its unique key. |
||
| 74 | * |
||
| 75 | * @param string $key The unique cache key of the item to delete. |
||
| 76 | * |
||
| 77 | * @return bool True if the item was successfully removed. False if there was an error. |
||
| 78 | * |
||
| 79 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 80 | */ |
||
| 81 | public function delete($key) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Wipes clean the entire cache's keys. |
||
| 90 | * |
||
| 91 | * @return bool True on success and false on failure. |
||
| 92 | */ |
||
| 93 | public function clear() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Obtains multiple cache items by their unique keys. |
||
| 101 | * |
||
| 102 | * @param iterable $keys A list of keys that can obtained in a single operation. |
||
| 103 | * @param mixed $default Default value to return for keys that do not exist. |
||
| 104 | * |
||
| 105 | * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
||
| 106 | * |
||
| 107 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function getMultiple($keys, $default = null) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Persists a set of key => value pairs in the cache, with an optional TTL. |
||
| 121 | * |
||
| 122 | * @param iterable $values A list of key => value pairs for a multiple-set operation. |
||
| 123 | * @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
||
| 124 | * the driver supports TTL then the library may set a default value |
||
| 125 | * for it or let the driver take care of that. |
||
| 126 | * |
||
| 127 | * @return bool True on success and false on failure. |
||
| 128 | * |
||
| 129 | * @throws InvalidArgumentException Thrown if $values is neither an array nor a Traversable, |
||
| 130 | * or if any of the $values are not a legal value. |
||
| 131 | */ |
||
| 132 | public function setMultiple($values, $ttl = null) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Deletes multiple cache items in a single operation. |
||
| 143 | * |
||
| 144 | * @param iterable $keys A list of string-based keys to be deleted. |
||
| 145 | * |
||
| 146 | * @return bool True if the items were successfully removed. False if there was an error. |
||
| 147 | * |
||
| 148 | * @throws InvalidArgumentException Thrown if $keys is neither an array nor a Traversable, |
||
| 149 | * or if any of the $keys are not a legal value. |
||
| 150 | */ |
||
| 151 | public function deleteMultiple($keys) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Determines whether an item is present in the cache. |
||
| 162 | * |
||
| 163 | * NOTE: It is recommended that has() is only to be used for cache warming type purposes |
||
| 164 | * and not to be used within your live applications operations for get/set, as this method |
||
| 165 | * is subject to a race condition where your has() will return true and immediately after, |
||
| 166 | * another script can remove it making the state of your app out of date. |
||
| 167 | * |
||
| 168 | * @param string $key The cache item key. |
||
| 169 | * |
||
| 170 | * @return bool |
||
| 171 | * |
||
| 172 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 173 | */ |
||
| 174 | public function has($key) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Converts the given time to live value to a epoch timestamp. |
||
| 182 | * |
||
| 183 | * @param mixed $key The cache key to validate. |
||
| 184 | * |
||
| 185 | * @return void |
||
| 186 | * |
||
| 187 | * @throws InvalidArgumentException Thrown if the $ttl is not null, an integer or \DateInterval. |
||
| 188 | */ |
||
| 189 | private function getExpires($ttl) |
||
| 205 | } |
||
| 206 |
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.