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 True if the pool was successfully cleared. False if there was an error. |
||
27 | * |
||
28 | * @since __DEPLOY_VERSION__ |
||
29 | */ |
||
30 | 18 | public function clear() |
|
34 | |||
35 | /** |
||
36 | * Returns a Cache Item representing the specified key. |
||
37 | * |
||
38 | * @param string $key The key for which to return the corresponding Cache Item. |
||
39 | * |
||
40 | * @return CacheItemInterface The corresponding Cache Item. |
||
41 | * |
||
42 | * @since __DEPLOY_VERSION__ |
||
43 | */ |
||
44 | 9 | public function getItem($key) |
|
45 | { |
||
46 | 9 | $success = false; |
|
47 | 9 | $value = apcu_fetch($key, $success); |
|
48 | 9 | $item = new Item($key); |
|
49 | |||
50 | if ($success) |
||
51 | 9 | { |
|
52 | 7 | $item->set($value); |
|
53 | 7 | } |
|
54 | |||
55 | 9 | return $item; |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * Returns a traversable set of cache items. |
||
60 | * |
||
61 | * @param string[] $keys An indexed array of keys of items to retrieve. |
||
62 | * |
||
63 | * @return array A traversable collection of Cache Items keyed by the cache keys of each item. |
||
64 | * A Cache item will be returned for each key, even if that key is not found. |
||
65 | * |
||
66 | * @since __DEPLOY_VERSION__ |
||
67 | */ |
||
68 | 4 | public function getItems(array $keys = []) |
|
89 | |||
90 | /** |
||
91 | * Removes the item from the pool. |
||
92 | * |
||
93 | * @param string $key The key to delete. |
||
94 | * |
||
95 | * @return boolean True if the item was successfully removed. False if there was an error. |
||
96 | * |
||
97 | * @since __DEPLOY_VERSION__ |
||
98 | */ |
||
99 | 4 | 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 boolean True if the item was successfully persisted. False if there was an error. |
||
116 | * |
||
117 | * @since __DEPLOY_VERSION__ |
||
118 | */ |
||
119 | 15 | public function save(CacheItemInterface $item) |
|
120 | { |
||
121 | // If we are able to find out when the item expires - find out. Else bail. |
||
122 | 15 | if ($item instanceof HasExpirationDateInterface) |
|
123 | 15 | { |
|
124 | 7 | $ttl = $this->convertItemExpiryToSeconds($item); |
|
125 | 7 | } |
|
126 | else |
||
127 | { |
||
128 | 8 | $ttl = 0; |
|
129 | } |
||
130 | |||
131 | 15 | return apcu_store($item->getKey(), $item->get(), $ttl); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Confirms if the cache contains specified cache item. |
||
136 | * |
||
137 | * @param string $key The key for which to check existence. |
||
138 | * |
||
139 | * @return boolean True if item exists in the cache, false otherwise. |
||
140 | * |
||
141 | * @since __DEPLOY_VERSION__ |
||
142 | */ |
||
143 | 8 | public function hasItem($key) |
|
147 | |||
148 | /** |
||
149 | * Test to see if the CacheItemPoolInterface is available |
||
150 | * |
||
151 | * @return boolean True on success, false otherwise |
||
152 | * |
||
153 | * @since __DEPLOY_VERSION__ |
||
154 | */ |
||
155 | 18 | public static function isSupported() |
|
167 | } |
||
168 |
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.