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 |
||
| 19 | trait ExtendedCacheItemPoolTrait |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @param array $keys |
||
| 23 | * An indexed array of keys of items to retrieve. |
||
| 24 | * @param int $option json_encode() options |
||
| 25 | * @param int $depth json_encode() depth |
||
| 26 | * @return string |
||
| 27 | * @throws \InvalidArgumentException |
||
| 28 | */ |
||
| 29 | public function getItemsAsJsonString(array $keys = [], $option = 0, $depth = 512) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param string $tagName |
||
| 36 | * @return \phpFastCache\Cache\ExtendedCacheItemInterface[] |
||
| 37 | * @throws InvalidArgumentException |
||
| 38 | */ |
||
| 39 | public function getItemsByTag($tagName) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param array $tagNames |
||
| 57 | * @return \phpFastCache\Cache\ExtendedCacheItemInterface[] |
||
| 58 | * @throws InvalidArgumentException |
||
| 59 | */ |
||
| 60 | public function getItemsByTags(array $tagNames) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns A json string that represents an array of items by tags-based. |
||
| 72 | * |
||
| 73 | * @param array $tagNames |
||
| 74 | * An indexed array of keys of items to retrieve. |
||
| 75 | * @param int $option json_encode() options |
||
| 76 | * @param int $depth json_encode() depth |
||
| 77 | * |
||
| 78 | * @throws InvalidArgumentException |
||
| 79 | * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||
| 80 | * MUST be thrown. |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | public function getItemsByTagsAsJsonString(array $tagNames, $option = 0, $depth = 512) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $tagName |
||
| 91 | * @return bool|null |
||
| 92 | * @throws InvalidArgumentException |
||
| 93 | */ |
||
| 94 | public function deleteItemsByTag($tagName) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param array $tagNames |
||
| 113 | * @return bool|null |
||
| 114 | * @throws InvalidArgumentException |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function deleteItemsByTags(array $tagNames) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @inheritdoc |
||
| 131 | */ |
||
| 132 | View Code Duplication | public function incrementItemsByTag($tagName, $step = 1) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @inheritdoc |
||
| 148 | */ |
||
| 149 | View Code Duplication | public function incrementItemsByTags(array $tagNames, $step = 1) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @inheritdoc |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function decrementItemsByTag($tagName, $step = 1) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @inheritdoc |
||
| 181 | */ |
||
| 182 | View Code Duplication | public function decrementItemsByTags(array $tagNames, $step = 1) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @inheritdoc |
||
| 197 | */ |
||
| 198 | View Code Duplication | public function appendItemsByTag($tagName, $data) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @inheritdoc |
||
| 214 | */ |
||
| 215 | View Code Duplication | public function appendItemsByTags(array $tagNames, $data) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @inheritdoc |
||
| 230 | */ |
||
| 231 | View Code Duplication | public function prependItemsByTag($tagName, $data) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @inheritdoc |
||
| 247 | */ |
||
| 248 | View Code Duplication | public function prependItemsByTags(array $tagNames, $data) |
|
| 260 | } |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.