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 |
||
25 | class Pool implements SimpleCacheInterface |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * @var CacheItemPool |
||
30 | */ |
||
31 | protected $cache_item_pool; |
||
32 | |||
33 | /** |
||
34 | * Constructor. |
||
35 | */ |
||
36 | 195 | public function __construct(CacheItemPool $cache_item_pool) |
|
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | 46 | View Code Duplication | public function get($key, $default = null) |
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | 41 | public function getMultiple($keys, $default = null) |
|
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | 23 | public function has($key) |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 1 | public function clear() |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 39 | public function deleteMultiple($keys) |
|
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | 16 | public function delete($key) |
|
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 21 | View Code Duplication | public function set($key, $value, $ttl = null) |
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | 45 | public function setMultiple($values, $ttl = null) |
|
171 | |||
172 | /** |
||
173 | * Sets the properties of an item object. |
||
174 | * |
||
175 | * @param CacheItem $item |
||
176 | * @param mixed $value The item value (unserialized) |
||
177 | * @param integer|\DateInterval|null $ttl |
||
178 | * |
||
179 | * @return static The invoked object. |
||
180 | */ |
||
181 | 13 | protected function setItemProperties( |
|
187 | |||
188 | 80 | private static function _normalizedKeys($keys) |
|
203 | |||
204 | 129 | private static function _rethrow(CacheInvalidArgumentException $e) |
|
210 | |||
211 | /** |
||
212 | * Returns the cache adapter for this pool. |
||
213 | * |
||
214 | * @return CacheAdapter |
||
215 | */ |
||
216 | 7 | public function getCacheAdapter() |
|
220 | } |
||
221 |
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.