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 | View Code Duplication | class Apcu extends AbstractCacheItemPool |
|
|
|||
22 | { |
||
23 | /** |
||
24 | * This will wipe out the entire cache's keys |
||
25 | * |
||
26 | * @return boolean The result of the clear operation. |
||
27 | * |
||
28 | * @since __DEPLOY_VERSION__ |
||
29 | */ |
||
30 | 11 | public function clear() |
|
34 | |||
35 | /** |
||
36 | * Method to get a storage entry value from a key. |
||
37 | * |
||
38 | * @param string $key The storage entry identifier. |
||
39 | * |
||
40 | * @return CacheItemInterface |
||
41 | * |
||
42 | * @since __DEPLOY_VERSION__ |
||
43 | * @throws \RuntimeException |
||
44 | */ |
||
45 | 3 | public function getItem($key) |
|
58 | |||
59 | /** |
||
60 | * Obtain multiple CacheItems by their unique keys. |
||
61 | * |
||
62 | * @param array $keys A list of keys that can obtained in a single operation. |
||
63 | * |
||
64 | * @return array An associative array of CacheItem objects keyed on the cache key. |
||
65 | * |
||
66 | * @since __DEPLOY_VERSION__ |
||
67 | */ |
||
68 | 1 | public function getItems(array $keys = array()) |
|
89 | |||
90 | /** |
||
91 | * Method to remove a storage entry for a key. |
||
92 | * |
||
93 | * @param string $key The storage entry identifier. |
||
94 | * |
||
95 | * @return boolean |
||
96 | * |
||
97 | * @since __DEPLOY_VERSION__ |
||
98 | */ |
||
99 | 2 | public function deleteItem($key) |
|
109 | |||
110 | /** |
||
111 | * Persists a cache item immediately. |
||
112 | * |
||
113 | * @param CacheItemInterface $item The cache item to save. |
||
114 | * |
||
115 | * @return static |
||
116 | * The invoked object. |
||
117 | */ |
||
118 | 8 | public function save(CacheItemInterface $item) |
|
132 | |||
133 | /** |
||
134 | * Method to determine whether a storage entry has been set for a key. |
||
135 | * |
||
136 | * @param string $key The storage entry identifier. |
||
137 | * |
||
138 | * @return boolean |
||
139 | * |
||
140 | * @since __DEPLOY_VERSION__ |
||
141 | */ |
||
142 | 5 | public function hasItem($key) |
|
146 | |||
147 | /** |
||
148 | * Test to see if the CacheItemPoolInterface is available |
||
149 | * |
||
150 | * @return boolean True on success, false otherwise |
||
151 | * |
||
152 | * @since __DEPLOY_VERSION__ |
||
153 | */ |
||
154 | 11 | public static function isSupported() |
|
166 | } |
||
167 |
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.