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 |
||
21 | abstract class AbstractCacheItemPool implements CacheItemPoolInterface, CacheInterface |
||
22 | { |
||
23 | /** |
||
24 | * The options for the cache object. |
||
25 | * |
||
26 | * @var array|\ArrayAccess |
||
27 | * @since 1.0 |
||
28 | */ |
||
29 | protected $options; |
||
30 | |||
31 | /** |
||
32 | * The deferred items to store |
||
33 | * |
||
34 | * @var Item\Item[] |
||
35 | * @since 1.0 |
||
36 | */ |
||
37 | private $deferred = []; |
||
38 | |||
39 | /** |
||
40 | * Constructor. |
||
41 | * |
||
42 | * @param array|\ArrayAccess $options An options array, or an object that implements \ArrayAccess |
||
43 | * |
||
44 | * @since 1.0 |
||
45 | * @throws InvalidArgumentException |
||
46 | */ |
||
47 | 130 | public function __construct($options = []) |
|
56 | |||
57 | /** |
||
58 | * Returns a traversable set of cache items. |
||
59 | * |
||
60 | * @param string[] $keys An indexed array of keys of items to retrieve. |
||
61 | * |
||
62 | * @return array A traversable collection of Cache Items keyed by the cache keys of each item. |
||
63 | * A Cache item will be returned for each key, even if that key is not found. |
||
64 | * |
||
65 | * @since __DEPLOY_VERSION__ |
||
66 | */ |
||
67 | 16 | public function getItems(array $keys = []) |
|
78 | |||
79 | /** |
||
80 | * Get an option from the Cache instance. |
||
81 | * |
||
82 | * @param string $key The name of the option to get. |
||
83 | * |
||
84 | * @return mixed The option value. |
||
85 | * |
||
86 | * @since 1.0 |
||
87 | */ |
||
88 | 14 | public function getOption($key) |
|
92 | |||
93 | /** |
||
94 | * Removes multiple items from the pool. |
||
95 | * |
||
96 | * @param array $keys An array of keys that should be removed from the pool. |
||
97 | * |
||
98 | * @return boolean True if the items were successfully removed. False if there was an error. |
||
99 | * |
||
100 | * @since __DEPLOY_VERSION__ |
||
101 | */ |
||
102 | 12 | public function deleteItems(array $keys) |
|
114 | |||
115 | /** |
||
116 | * Set an option for the Cache instance. |
||
117 | * |
||
118 | * @param string $key The name of the option to set. |
||
119 | * @param mixed $value The option value to set. |
||
120 | * |
||
121 | * @return $this |
||
122 | * |
||
123 | * @since 1.0 |
||
124 | */ |
||
125 | 7 | public function setOption($key, $value) |
|
131 | |||
132 | /** |
||
133 | * Sets a cache item to be persisted later. |
||
134 | * |
||
135 | * @param CacheItemInterface $item The cache item to save. |
||
136 | * |
||
137 | * @return boolean False if the item could not be queued or if a commit was attempted and failed. True otherwise. |
||
138 | * |
||
139 | * @since __DEPLOY_VERSION__ |
||
140 | */ |
||
141 | 21 | public function saveDeferred(CacheItemInterface $item) |
|
147 | |||
148 | /** |
||
149 | * Persists any deferred cache items. |
||
150 | * |
||
151 | * @return boolean True if all not-yet-saved items were successfully saved or there were none. False otherwise. |
||
152 | * |
||
153 | * @since __DEPLOY_VERSION__ |
||
154 | */ |
||
155 | 21 | public function commit() |
|
173 | |||
174 | /** |
||
175 | * Fetches a value from the cache. |
||
176 | * |
||
177 | * @param string $key The unique key of this item in the cache. |
||
178 | * @param mixed $default Default value to return if the key does not exist. |
||
179 | * |
||
180 | * @return mixed The value of the item from the cache, or $default in case of cache miss. |
||
181 | * |
||
182 | * @since __DEPLOY_VERSION__ |
||
183 | */ |
||
184 | 24 | public function get($key, $default = null) |
|
195 | |||
196 | /** |
||
197 | * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
||
198 | * |
||
199 | * @param string $key The key of the item to store. |
||
200 | * @param mixed $value The value of the item to store, must be serializable. |
||
201 | * @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
||
202 | * the driver supports TTL then the library may set a default value |
||
203 | * for it or let the driver take care of that. |
||
204 | * |
||
205 | * @return boolean True on success and false on failure. |
||
206 | * |
||
207 | * @since __DEPLOY_VERSION__ |
||
208 | */ |
||
209 | 31 | public function set($key, $value, $ttl = null) |
|
217 | |||
218 | /** |
||
219 | * Delete an item from the cache by its unique key. |
||
220 | * |
||
221 | * @param string $key The unique cache key of the item to delete. |
||
222 | * |
||
223 | * @return boolean True if the item was successfully removed. False if there was an error. |
||
224 | * |
||
225 | * @since __DEPLOY_VERSION__ |
||
226 | */ |
||
227 | 7 | public function delete($key) |
|
231 | |||
232 | /** |
||
233 | * Obtains multiple cache items by their unique keys. |
||
234 | * |
||
235 | * @param iterable $keys A list of keys that can obtained in a single operation. |
||
236 | * @param mixed $default Default value to return for keys that do not exist. |
||
237 | * |
||
238 | * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
||
239 | * |
||
240 | * @since __DEPLOY_VERSION__ |
||
241 | * @throws InvalidArgumentException |
||
242 | */ |
||
243 | 7 | public function getMultiple($keys, $default = null) |
|
259 | |||
260 | /** |
||
261 | * Persists a set of key => value pairs in the cache, with an optional TTL. |
||
262 | * |
||
263 | * @param iterable $values A list of key => value pairs for a multiple-set operation. |
||
264 | * @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
||
265 | * the driver supports TTL then the library may set a default value |
||
266 | * for it or let the driver take care of that. |
||
267 | * |
||
268 | * @return boolean True on success and false on failure. |
||
269 | * |
||
270 | * @since __DEPLOY_VERSION__ |
||
271 | * @throws InvalidArgumentException |
||
272 | */ |
||
273 | 14 | public function setMultiple($values, $ttl = null) |
|
311 | |||
312 | /** |
||
313 | * Deletes multiple cache items in a single operation. |
||
314 | * |
||
315 | * @param iterable $keys A list of string-based keys to be deleted. |
||
316 | * |
||
317 | * @return boolean True if the items were successfully removed. False if there was an error. |
||
318 | * |
||
319 | * @since __DEPLOY_VERSION__ |
||
320 | * @throws InvalidArgumentException |
||
321 | */ |
||
322 | public function deleteMultiple($keys) |
||
336 | |||
337 | /** |
||
338 | * Determines whether an item is present in the cache. |
||
339 | * |
||
340 | * NOTE: It is recommended that has() is only to be used for cache warming type purposes |
||
341 | * and not to be used within your live applications operations for get/set, as this method |
||
342 | * is subject to a race condition where your has() will return true and immediately after, |
||
343 | * another script can remove it making the state of your app out of date. |
||
344 | * |
||
345 | * @param string $key The cache item key. |
||
346 | * |
||
347 | * @return bool |
||
348 | * |
||
349 | * @since __DEPLOY_VERSION__ |
||
350 | * @throws InvalidArgumentException |
||
351 | */ |
||
352 | 7 | public function has($key) |
|
356 | |||
357 | /** |
||
358 | * Converts a DateTime object from the cache item to the expiry time in seconds from the present |
||
359 | * |
||
360 | * @param HasExpirationDateInterface $item The cache item |
||
361 | * |
||
362 | * @return integer The time in seconds until expiry |
||
363 | * |
||
364 | * @since __DEPLOY_VERSION__ |
||
365 | */ |
||
366 | 38 | protected function convertItemExpiryToSeconds(HasExpirationDateInterface $item) |
|
375 | |||
376 | /** |
||
377 | * Generate the values for the PSR-16 getMultiple method |
||
378 | * |
||
379 | * @param mixed $default Default value to return for keys that do not exist. |
||
380 | * @param array $items The items to process |
||
381 | * |
||
382 | * @return \Generator |
||
383 | * |
||
384 | * @since __DEPLOY_VERSION__ |
||
385 | */ |
||
386 | 7 | private function generateValues($default, $items) |
|
401 | } |
||
402 |
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.