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