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 |
||
9 | final class MemoryCacheItemPool implements CacheItemPoolInterface |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * |
||
14 | * @var array |
||
15 | */ |
||
16 | private $data = []; |
||
17 | |||
18 | private $deferred = []; |
||
19 | |||
20 | /** |
||
21 | * Returns a Cache Item representing the specified key. |
||
22 | * |
||
23 | * This method must always return a CacheItemInterface object, even in case of |
||
24 | * a cache miss. It MUST NOT return null. |
||
25 | * |
||
26 | * @param string $key |
||
27 | * The key for which to return the corresponding Cache Item. |
||
28 | * |
||
29 | * @throws InvalidArgumentException If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||
30 | * MUST be thrown. |
||
31 | * |
||
32 | * @return CacheItemInterface |
||
33 | */ |
||
34 | public function getItem($key) |
||
42 | |||
43 | /** |
||
44 | * Returns a traversable set of cache items. |
||
45 | * |
||
46 | * @param array $keys |
||
47 | * An indexed array of keys of items to retrieve. |
||
48 | * |
||
49 | * @throws InvalidArgumentException If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||
50 | * MUST be thrown. |
||
51 | * |
||
52 | * @return array|\Traversable A traversable collection of Cache Items keyed by the cache keys of |
||
53 | * each item. A Cache item will be returned for each key, even if that |
||
54 | * key is not found. However, if no keys are specified then an empty |
||
55 | * traversable MUST be returned instead. |
||
56 | */ |
||
57 | View Code Duplication | public function getItems(array $keys = []) |
|
67 | |||
68 | /** |
||
69 | * Confirms if the cache contains specified cache item. |
||
70 | * |
||
71 | * Note: This method MAY avoid retrieving the cached value for performance reasons. |
||
72 | * This could result in a race condition with CacheItemInterface::get(). To avoid |
||
73 | * such situation use CacheItemInterface::isHit() instead. |
||
74 | * |
||
75 | * @param string $key |
||
76 | * The key for which to check existence. |
||
77 | * |
||
78 | * @throws InvalidArgumentException If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||
79 | * MUST be thrown. |
||
80 | * |
||
81 | * @return bool True if item exists in the cache, false otherwise. |
||
82 | */ |
||
83 | public function hasItem($key) |
||
97 | |||
98 | /** |
||
99 | * Deletes all items in the pool. |
||
100 | * |
||
101 | * @return bool True if the pool was successfully cleared. False if there was an error. |
||
102 | */ |
||
103 | public function clear() |
||
109 | |||
110 | /** |
||
111 | * Removes the item from the pool. |
||
112 | * |
||
113 | * @param string $key |
||
114 | * The key for which to delete |
||
115 | * |
||
116 | * @throws InvalidArgumentException If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||
117 | * MUST be thrown. |
||
118 | * |
||
119 | * @return bool True if the item was successfully removed. False if there was an error. |
||
120 | */ |
||
121 | public function deleteItem($key) |
||
127 | |||
128 | /** |
||
129 | * Removes multiple items from the pool. |
||
130 | * |
||
131 | * @param array $keys |
||
132 | * An array of keys that should be removed from the pool. |
||
133 | * |
||
134 | * @throws InvalidArgumentException If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||
135 | * MUST be thrown. |
||
136 | * |
||
137 | * @return bool True if the items were successfully removed. False if there was an error. |
||
138 | */ |
||
139 | public function deleteItems(array $keys) |
||
147 | |||
148 | /** |
||
149 | * Persists a cache item immediately. |
||
150 | * |
||
151 | * @param CacheItemInterface $item |
||
152 | * The cache item to save. |
||
153 | * |
||
154 | * @return bool True if the item was successfully persisted. False if there was an error. |
||
155 | */ |
||
156 | public function save(CacheItemInterface $item) |
||
164 | |||
165 | /** |
||
166 | * Sets a cache item to be persisted later. |
||
167 | * |
||
168 | * @param CacheItemInterface $item |
||
169 | * The cache item to save. |
||
170 | * |
||
171 | * @return bool False if the item could not be queued or if a commit was attempted and failed. True otherwise. |
||
172 | */ |
||
173 | public function saveDeferred(CacheItemInterface $item) |
||
179 | |||
180 | /** |
||
181 | * Persists any deferred cache items. |
||
182 | * |
||
183 | * @return bool True if all not-yet-saved items were successfully saved or there were none. False otherwise. |
||
184 | */ |
||
185 | public function commit() |
||
196 | } |
||
197 |
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.