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 |
||
| 14 | final class MongoCache extends AbstractCache implements CacheInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * MongoDB collection containing the cached responses. |
||
| 18 | * |
||
| 19 | * @var Collection |
||
| 20 | */ |
||
| 21 | private $collection; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Construct a new instance of MongoCache. |
||
| 25 | * |
||
| 26 | * @param Collection $collection The collection containing the cached data. |
||
| 27 | * @param integer $defaultTimeToLive The default time to live in seconds. |
||
|
|
|||
| 28 | */ |
||
| 29 | public function __construct(Collection $collection) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Fetches a value from the cache. |
||
| 37 | * |
||
| 38 | * @param string $key The unique key of this item in the cache. |
||
| 39 | * @param mixed $default Default value to return if the key does not exist. |
||
| 40 | * |
||
| 41 | * @return mixed The value of the item from the cache, or $default in case of cache miss. |
||
| 42 | * |
||
| 43 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 44 | */ |
||
| 45 | public function get($key, $default = null) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
||
| 68 | * |
||
| 69 | * @param string $key The key of the item to store. |
||
| 70 | * @param mixed $value The value of the item to store, must be serializable. |
||
| 71 | * @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
||
| 72 | * the driver supports TTL then the library may set a default value |
||
| 73 | * for it or let the driver take care of that. |
||
| 74 | * |
||
| 75 | * @return bool True on success and false on failure. |
||
| 76 | * |
||
| 77 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 78 | */ |
||
| 79 | public function set($key, $value, $ttl = null) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Delete an item from the cache by its unique key. |
||
| 88 | * |
||
| 89 | * @param string $key The unique cache key of the item to delete. |
||
| 90 | * |
||
| 91 | * @return bool True if the item was successfully removed. False if there was an error. |
||
| 92 | * |
||
| 93 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 94 | */ |
||
| 95 | public function delete($key) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Wipes clean the entire cache's keys. |
||
| 108 | * |
||
| 109 | * @return bool True on success and false on failure. |
||
| 110 | */ |
||
| 111 | public function clear() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Obtains multiple cache items by their unique keys. |
||
| 123 | * |
||
| 124 | * @param iterable $keys A list of keys that can obtained in a single operation. |
||
| 125 | * @param mixed $default Default value to return for keys that do not exist. |
||
| 126 | * |
||
| 127 | * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
||
| 128 | * |
||
| 129 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function getMultiple($keys, $default = null) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Persists a set of key => value pairs in the cache, with an optional TTL. |
||
| 143 | * |
||
| 144 | * @param iterable $values A list of key => value pairs for a multiple-set operation. |
||
| 145 | * @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
||
| 146 | * the driver supports TTL then the library may set a default value |
||
| 147 | * for it or let the driver take care of that. |
||
| 148 | * |
||
| 149 | * @return bool True on success and false on failure. |
||
| 150 | * |
||
| 151 | * @throws InvalidArgumentException Thrown if $values is neither an array nor a Traversable, |
||
| 152 | * or if any of the $values are not a legal value. |
||
| 153 | */ |
||
| 154 | public function setMultiple($values, $ttl = null) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Deletes multiple cache items in a single operation. |
||
| 172 | * |
||
| 173 | * @param iterable $keys A list of string-based keys to be deleted. |
||
| 174 | * |
||
| 175 | * @return bool True if the items were successfully removed. False if there was an error. |
||
| 176 | * |
||
| 177 | * @throws InvalidArgumentException Thrown if $keys is neither an array nor a Traversable, |
||
| 178 | * or if any of the $keys are not a legal value. |
||
| 179 | */ |
||
| 180 | public function deleteMultiple($keys) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Determines whether an item is present in the cache. |
||
| 194 | * |
||
| 195 | * NOTE: It is recommended that has() is only to be used for cache warming type purposes |
||
| 196 | * and not to be used within your live applications operations for get/set, as this method |
||
| 197 | * is subject to a race condition where your has() will return true and immediately after, |
||
| 198 | * another script can remove it making the state of your app out of date. |
||
| 199 | * |
||
| 200 | * @param string $key The cache item key. |
||
| 201 | * |
||
| 202 | * @return bool |
||
| 203 | * |
||
| 204 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 205 | */ |
||
| 206 | public function has($key) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Upserts a PSR-7 response in the cache. |
||
| 214 | * |
||
| 215 | * @param string $key The key of the response to store. |
||
| 216 | * @param ResponseInterface $response The response to store. |
||
| 217 | * @param UTCDateTime $expires The expire date of the cache item. |
||
| 218 | * |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | private function updateCache(string $key, ResponseInterface $response, UTCDateTime $expires) : bool |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Converts the given time to live value to a UTCDateTime instance; |
||
| 242 | * |
||
| 243 | * @param mixed $key The cache key to validate. |
||
| 244 | * |
||
| 245 | * @return UTCDateTime |
||
| 246 | * |
||
| 247 | * @throws InvalidArgumentException Thrown if the $ttl is not null, an integer or \DateInterval. |
||
| 248 | */ |
||
| 249 | private function getExpires($ttl) |
||
| 265 | } |
||
| 266 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.