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 |
||
22 | class Memcached extends AbstractCacheItemPool |
||
23 | { |
||
24 | /** |
||
25 | * The Memcached driver |
||
26 | * |
||
27 | * @var \Memcached |
||
28 | * @since 1.0 |
||
29 | */ |
||
30 | private $driver; |
||
31 | |||
32 | /** |
||
33 | * Constructor. |
||
34 | * |
||
35 | * @param \Memcached $memcached The Memcached driver being used for this pool |
||
36 | * @param array|\ArrayAccess $options An options array, or an object that implements \ArrayAccess |
||
37 | * |
||
38 | * @since 1.0 |
||
39 | * @throws \RuntimeException |
||
40 | */ |
||
41 | 12 | public function __construct(\Memcached $memcached, $options = []) |
|
48 | |||
49 | /** |
||
50 | * This will wipe out the entire cache's keys |
||
51 | * |
||
52 | * @return boolean The result of the clear operation. |
||
53 | * |
||
54 | * @since 1.0 |
||
55 | */ |
||
56 | 12 | public function clear() |
|
60 | |||
61 | /** |
||
62 | * Method to get a storage entry value from a key. |
||
63 | * |
||
64 | * @param string $key The storage entry identifier. |
||
65 | * |
||
66 | * @return CacheItemInterface |
||
67 | * |
||
68 | * @since 1.0 |
||
69 | */ |
||
70 | 4 | View Code Duplication | public function getItem($key) |
83 | |||
84 | /** |
||
85 | * Returns a traversable set of cache items. |
||
86 | * |
||
87 | * @param array $keys A list of keys that can obtained in a single operation. |
||
88 | * |
||
89 | * @return CacheItemInterface[] An associative array of CacheItemInterface objects keyed on the cache key. |
||
90 | * |
||
91 | * @since __DEPLOY_VERSION__ |
||
92 | * @throws RuntimeException |
||
93 | */ |
||
94 | 1 | public function getItems(array $keys = []) |
|
119 | |||
120 | /** |
||
121 | * Method to remove a storage entry for a key. |
||
122 | * |
||
123 | * @param string $key The storage entry identifier. |
||
124 | * |
||
125 | * @return boolean |
||
126 | * |
||
127 | * @since 1.0 |
||
128 | */ |
||
129 | 1 | public function deleteItem($key) |
|
146 | |||
147 | /** |
||
148 | * Removes multiple items from the pool. |
||
149 | * |
||
150 | * @param array $keys An array of keys that should be removed from the pool. |
||
151 | * |
||
152 | * @return boolean |
||
153 | * |
||
154 | * @since __DEPLOY_VERSION__ |
||
155 | */ |
||
156 | 1 | public function deleteItems(array $keys) |
|
180 | |||
181 | /** |
||
182 | * Persists a cache item immediately. |
||
183 | * |
||
184 | * @param CacheItemInterface $item The cache item to save. |
||
185 | * |
||
186 | * @return boolean |
||
187 | * |
||
188 | * @since __DEPLOY_VERSION__ |
||
189 | */ |
||
190 | 9 | public function save(CacheItemInterface $item) |
|
205 | |||
206 | /** |
||
207 | * Method to determine whether a storage entry has been set for a key. |
||
208 | * |
||
209 | * @param string $key The storage entry identifier. |
||
210 | * |
||
211 | * @return boolean |
||
212 | * |
||
213 | * @since 1.0 |
||
214 | */ |
||
215 | 4 | public function hasItem($key) |
|
221 | |||
222 | /** |
||
223 | * Test to see if the CacheItemPoolInterface is available |
||
224 | * |
||
225 | * @return boolean True on success, false otherwise |
||
226 | * |
||
227 | * @since __DEPLOY_VERSION__ |
||
228 | */ |
||
229 | 12 | public static function isSupported() |
|
237 | } |
||
238 |
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.