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:
Complex classes like ExtendedCacheItemPoolTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExtendedCacheItemPoolTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | trait ExtendedCacheItemPoolTrait |
||
23 | { |
||
24 | use CacheItemPoolTrait; |
||
25 | |||
26 | /** |
||
27 | * Deletes all items in the pool. |
||
28 | * @deprecated Use clear() instead |
||
29 | * Will be removed in 5.1 |
||
30 | * |
||
31 | * @return bool |
||
32 | * True if the pool was successfully cleared. False if there was an error. |
||
33 | */ |
||
34 | public function clean() |
||
39 | |||
40 | /** |
||
41 | * @param array $keys |
||
42 | * An indexed array of keys of items to retrieve. |
||
43 | * @param int $option json_encode() options |
||
44 | * @param int $depth json_encode() depth |
||
45 | * @return string |
||
46 | * @throws \InvalidArgumentException |
||
47 | */ |
||
48 | View Code Duplication | public function getItemsAsJsonString(array $keys = [], $option = 0, $depth = 512) |
|
55 | |||
56 | /** |
||
57 | * @param string $tagName |
||
58 | * @return \phpFastCache\Core\Item\ExtendedCacheItemInterface[] |
||
59 | * @throws InvalidArgumentException |
||
60 | */ |
||
61 | public function getItemsByTag($tagName) |
||
88 | |||
89 | /** |
||
90 | * @param array $tagNames |
||
91 | * @return \phpFastCache\Core\Item\ExtendedCacheItemInterface[] |
||
92 | * @throws InvalidArgumentException |
||
93 | */ |
||
94 | public function getItemsByTags(array $tagNames) |
||
103 | |||
104 | /** |
||
105 | * Returns A json string that represents an array of items by tags-based. |
||
106 | * |
||
107 | * @param array $tagNames |
||
108 | * An indexed array of keys of items to retrieve. |
||
109 | * @param int $option json_encode() options |
||
110 | * @param int $depth json_encode() depth |
||
111 | * |
||
112 | * @throws InvalidArgumentException |
||
113 | * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||
114 | * MUST be thrown. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | View Code Duplication | public function getItemsByTagsAsJsonString(array $tagNames, $option = 0, $depth = 512) |
|
126 | |||
127 | /** |
||
128 | * @param string $tagName |
||
129 | * @return bool|null |
||
130 | * @throws InvalidArgumentException |
||
131 | */ |
||
132 | public function deleteItemsByTag($tagName) |
||
148 | |||
149 | /** |
||
150 | * @param array $tagNames |
||
151 | * @return bool|null |
||
152 | * @throws InvalidArgumentException |
||
153 | */ |
||
154 | View Code Duplication | public function deleteItemsByTags(array $tagNames) |
|
166 | |||
167 | /** |
||
168 | * @inheritdoc |
||
169 | */ |
||
170 | View Code Duplication | public function incrementItemsByTag($tagName, $step = 1) |
|
183 | |||
184 | /** |
||
185 | * @inheritdoc |
||
186 | */ |
||
187 | View Code Duplication | public function incrementItemsByTags(array $tagNames, $step = 1) |
|
199 | |||
200 | /** |
||
201 | * @inheritdoc |
||
202 | */ |
||
203 | View Code Duplication | public function decrementItemsByTag($tagName, $step = 1) |
|
216 | |||
217 | /** |
||
218 | * @inheritdoc |
||
219 | */ |
||
220 | View Code Duplication | public function decrementItemsByTags(array $tagNames, $step = 1) |
|
232 | |||
233 | /** |
||
234 | * @inheritdoc |
||
235 | */ |
||
236 | View Code Duplication | public function appendItemsByTag($tagName, $data) |
|
249 | |||
250 | /** |
||
251 | * @inheritdoc |
||
252 | */ |
||
253 | View Code Duplication | public function appendItemsByTags(array $tagNames, $data) |
|
265 | |||
266 | /** |
||
267 | * @inheritdoc |
||
268 | */ |
||
269 | View Code Duplication | public function prependItemsByTag($tagName, $data) |
|
282 | |||
283 | /** |
||
284 | * @inheritdoc |
||
285 | */ |
||
286 | View Code Duplication | public function prependItemsByTags(array $tagNames, $data) |
|
298 | |||
299 | /** |
||
300 | * @param \Psr\Cache\CacheItemInterface $item |
||
301 | * @return void |
||
302 | */ |
||
303 | public function detachItem(CacheItemInterface $item) |
||
309 | |||
310 | /** |
||
311 | * @return void |
||
312 | */ |
||
313 | public function detachAllItems() |
||
319 | |||
320 | /** |
||
321 | * @param \Psr\Cache\CacheItemInterface $item |
||
322 | * @return void |
||
323 | * @throws \LogicException |
||
324 | */ |
||
325 | public function attachItem(CacheItemInterface $item) |
||
333 | |||
334 | |||
335 | /** |
||
336 | * @internal This method de-register an item from $this->itemInstances |
||
337 | * @param CacheItemInterface|string $item |
||
338 | * @throws \InvalidArgumentException |
||
339 | */ |
||
340 | protected function deregisterItem($item) |
||
354 | |||
355 | /** |
||
356 | * Returns true if the item exists, is attached and the Spl Hash matches |
||
357 | * Returns false if the item exists, is attached and the Spl Hash mismatches |
||
358 | * Returns null if the item does not exists |
||
359 | * |
||
360 | * @param \Psr\Cache\CacheItemInterface $item |
||
361 | * @return bool|null |
||
362 | * @throws \LogicException |
||
363 | */ |
||
364 | public function isAttached(CacheItemInterface $item) |
||
371 | |||
372 | /** |
||
373 | * Driver-related methods |
||
374 | */ |
||
375 | |||
376 | /** |
||
377 | * @param \Psr\Cache\CacheItemInterface $item |
||
378 | * @return array [ |
||
379 | * 'd' => 'THE ITEM DATA' |
||
380 | * 't' => 'THE ITEM DATE EXPIRATION' |
||
381 | * 'g' => 'THE ITEM TAGS' |
||
382 | * ] |
||
383 | * |
||
384 | */ |
||
385 | abstract protected function driverRead(CacheItemInterface $item); |
||
386 | |||
387 | /** |
||
388 | * @param \Psr\Cache\CacheItemInterface $item |
||
389 | * @return mixed |
||
390 | */ |
||
391 | abstract protected function driverWrite(CacheItemInterface $item); |
||
392 | |||
393 | /** |
||
394 | * @return bool |
||
395 | */ |
||
396 | abstract protected function driverClear(); |
||
397 | |||
398 | /** |
||
399 | * @return bool |
||
400 | */ |
||
401 | abstract protected function driverConnect(); |
||
402 | |||
403 | /** |
||
404 | * @param \Psr\Cache\CacheItemInterface $item |
||
405 | * @return bool |
||
406 | */ |
||
407 | abstract protected function driverDelete(CacheItemInterface $item); |
||
408 | } |
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.