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 |
||
24 | trait ExtendedCacheItemPoolTrait |
||
25 | { |
||
26 | use CacheItemPoolTrait; |
||
27 | |||
28 | /** |
||
29 | * @inheritdoc |
||
30 | */ |
||
31 | public function clean() |
||
36 | |||
37 | /** |
||
38 | * @inheritdoc |
||
39 | */ |
||
40 | View Code Duplication | public function getItemsAsJsonString(array $keys = [], $option = 0, $depth = 512) |
|
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | public function getItemsByTag($tagName) |
||
78 | |||
79 | /** |
||
80 | * @inheritdoc |
||
81 | */ |
||
82 | public function getItemsByTags(array $tagNames) |
||
95 | |||
96 | |||
97 | /** |
||
98 | * @inheritdoc |
||
99 | */ |
||
100 | public function getItemsByTagsAll(array $tagNames) |
||
112 | |||
113 | |||
114 | /** |
||
115 | * @inheritdoc |
||
116 | */ |
||
117 | View Code Duplication | public function getItemsByTagsAsJsonString(array $tagNames, $option = 0, $depth = 512) |
|
125 | |||
126 | /** |
||
127 | * @inheritdoc |
||
128 | */ |
||
129 | public function deleteItemsByTag($tagName) |
||
145 | |||
146 | /** |
||
147 | * @inheritdoc |
||
148 | */ |
||
149 | public function deleteItemsByTags(array $tagNames) |
||
161 | |||
162 | /** |
||
163 | * @inheritdoc |
||
164 | */ |
||
165 | public function deleteItemsByTagsAll(array $tagNames) |
||
179 | |||
180 | /** |
||
181 | * @inheritdoc |
||
182 | */ |
||
183 | View Code Duplication | public function incrementItemsByTag($tagName, $step = 1) |
|
196 | |||
197 | /** |
||
198 | * @inheritdoc |
||
199 | */ |
||
200 | public function incrementItemsByTags(array $tagNames, $step = 1) |
||
212 | |||
213 | /** |
||
214 | * @inheritdoc |
||
215 | */ |
||
216 | View Code Duplication | public function incrementItemsByTagsAll(array $tagNames, $step = 1) |
|
230 | |||
231 | /** |
||
232 | * @inheritdoc |
||
233 | */ |
||
234 | View Code Duplication | public function decrementItemsByTag($tagName, $step = 1) |
|
247 | |||
248 | /** |
||
249 | * @inheritdoc |
||
250 | */ |
||
251 | public function decrementItemsByTags(array $tagNames, $step = 1) |
||
263 | |||
264 | /** |
||
265 | * @inheritdoc |
||
266 | */ |
||
267 | View Code Duplication | public function decrementItemsByTagsAll(array $tagNames, $step = 1) |
|
281 | |||
282 | /** |
||
283 | * @inheritdoc |
||
284 | */ |
||
285 | View Code Duplication | public function appendItemsByTag($tagName, $data) |
|
298 | |||
299 | /** |
||
300 | * @inheritdoc |
||
301 | */ |
||
302 | public function appendItemsByTags(array $tagNames, $data) |
||
314 | |||
315 | /** |
||
316 | * @inheritdoc |
||
317 | */ |
||
318 | public function appendItemsByTagsAll(array $tagNames, $data) |
||
332 | |||
333 | /** |
||
334 | * @inheritdoc |
||
335 | */ |
||
336 | View Code Duplication | public function prependItemsByTag($tagName, $data) |
|
349 | |||
350 | /** |
||
351 | * @inheritdoc |
||
352 | */ |
||
353 | public function prependItemsByTags(array $tagNames, $data) |
||
365 | |||
366 | /** |
||
367 | * @inheritdoc |
||
368 | */ |
||
369 | public function prependItemsByTagsAll(array $tagNames, $data) |
||
383 | |||
384 | /** |
||
385 | * @param \Psr\Cache\CacheItemInterface $item |
||
386 | * @return void |
||
387 | */ |
||
388 | public function detachItem(CacheItemInterface $item) |
||
394 | |||
395 | /** |
||
396 | * @inheritdoc |
||
397 | */ |
||
398 | public function detachAllItems() |
||
404 | |||
405 | /** |
||
406 | * @inheritdoc |
||
407 | */ |
||
408 | public function attachItem(CacheItemInterface $item) |
||
416 | |||
417 | |||
418 | /** |
||
419 | * @internal This method de-register an item from $this->itemInstances |
||
420 | * @param CacheItemInterface|string $item |
||
421 | * @throws phpFastCacheInvalidArgumentException |
||
422 | */ |
||
423 | protected function deregisterItem($item) |
||
437 | |||
438 | /** |
||
439 | * Returns true if the item exists, is attached and the Spl Hash matches |
||
440 | * Returns false if the item exists, is attached and the Spl Hash mismatches |
||
441 | * Returns null if the item does not exists |
||
442 | * |
||
443 | * @param \Psr\Cache\CacheItemInterface $item |
||
444 | * @return bool|null |
||
445 | * @throws \LogicException |
||
446 | */ |
||
447 | View Code Duplication | public function isAttached(CacheItemInterface $item) |
|
454 | |||
455 | /** |
||
456 | * Set the EventManager instance |
||
457 | * |
||
458 | * @param EventManager $em |
||
459 | */ |
||
460 | public function setEventManager(EventManager $em) |
||
464 | |||
465 | /** |
||
466 | * @inheritdoc |
||
467 | */ |
||
468 | public function saveMultiple(...$items) |
||
483 | |||
484 | /** |
||
485 | * @return string |
||
486 | */ |
||
487 | public static function getHelp() |
||
491 | |||
492 | /** |
||
493 | * Driver-related methods |
||
494 | */ |
||
495 | |||
496 | /** |
||
497 | * @param \Psr\Cache\CacheItemInterface $item |
||
498 | * @return array [ |
||
499 | * 'd' => 'THE ITEM DATA' |
||
500 | * 't' => 'THE ITEM DATE EXPIRATION' |
||
501 | * 'g' => 'THE ITEM TAGS' |
||
502 | * ] |
||
503 | * |
||
504 | */ |
||
505 | abstract protected function driverRead(CacheItemInterface $item); |
||
506 | |||
507 | /** |
||
508 | * @param \Psr\Cache\CacheItemInterface $item |
||
509 | * @return mixed |
||
510 | */ |
||
511 | abstract protected function driverWrite(CacheItemInterface $item); |
||
512 | |||
513 | /** |
||
514 | * @return bool |
||
515 | */ |
||
516 | abstract protected function driverClear(); |
||
517 | |||
518 | /** |
||
519 | * @return bool |
||
520 | */ |
||
521 | abstract protected function driverConnect(); |
||
522 | |||
523 | /** |
||
524 | * @param \Psr\Cache\CacheItemInterface $item |
||
525 | * @return bool |
||
526 | */ |
||
527 | abstract protected function driverDelete(CacheItemInterface $item); |
||
528 | } |