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 |
||
20 | trait ExtendedCacheItemPoolTrait |
||
21 | { |
||
22 | use StandardPsr6StructureTrait; |
||
23 | |||
24 | /** |
||
25 | * Deletes all items in the pool. |
||
26 | * @deprecated Use clear() instead |
||
27 | * Will be removed in 5.1 |
||
28 | * |
||
29 | * @return bool |
||
30 | * True if the pool was successfully cleared. False if there was an error. |
||
31 | */ |
||
32 | public function clean() |
||
37 | |||
38 | /** |
||
39 | * @param array $keys |
||
40 | * An indexed array of keys of items to retrieve. |
||
41 | * @param int $option json_encode() options |
||
42 | * @param int $depth json_encode() depth |
||
43 | * @return string |
||
44 | * @throws \InvalidArgumentException |
||
45 | */ |
||
46 | public function getItemsAsJsonString(array $keys = [], $option = 0, $depth = 512) |
||
53 | |||
54 | /** |
||
55 | * @param string $tagName |
||
56 | * @return \phpFastCache\Cache\ExtendedCacheItemInterface[] |
||
57 | * @throws InvalidArgumentException |
||
58 | */ |
||
59 | public function getItemsByTag($tagName) |
||
74 | |||
75 | /** |
||
76 | * @param array $tagNames |
||
77 | * @return \phpFastCache\Cache\ExtendedCacheItemInterface[] |
||
78 | * @throws InvalidArgumentException |
||
79 | */ |
||
80 | public function getItemsByTags(array $tagNames) |
||
89 | |||
90 | /** |
||
91 | * Returns A json string that represents an array of items by tags-based. |
||
92 | * |
||
93 | * @param array $tagNames |
||
94 | * An indexed array of keys of items to retrieve. |
||
95 | * @param int $option json_encode() options |
||
96 | * @param int $depth json_encode() depth |
||
97 | * |
||
98 | * @throws InvalidArgumentException |
||
99 | * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||
100 | * MUST be thrown. |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | public function getItemsByTagsAsJsonString(array $tagNames, $option = 0, $depth = 512) |
||
112 | |||
113 | /** |
||
114 | * @param string $tagName |
||
115 | * @return bool|null |
||
116 | * @throws InvalidArgumentException |
||
117 | */ |
||
118 | public function deleteItemsByTag($tagName) |
||
134 | |||
135 | /** |
||
136 | * @param array $tagNames |
||
137 | * @return bool|null |
||
138 | * @throws InvalidArgumentException |
||
139 | */ |
||
140 | View Code Duplication | public function deleteItemsByTags(array $tagNames) |
|
152 | |||
153 | /** |
||
154 | * @inheritdoc |
||
155 | */ |
||
156 | View Code Duplication | public function incrementItemsByTag($tagName, $step = 1) |
|
169 | |||
170 | /** |
||
171 | * @inheritdoc |
||
172 | */ |
||
173 | View Code Duplication | public function incrementItemsByTags(array $tagNames, $step = 1) |
|
185 | |||
186 | /** |
||
187 | * @inheritdoc |
||
188 | */ |
||
189 | View Code Duplication | public function decrementItemsByTag($tagName, $step = 1) |
|
202 | |||
203 | /** |
||
204 | * @inheritdoc |
||
205 | */ |
||
206 | View Code Duplication | public function decrementItemsByTags(array $tagNames, $step = 1) |
|
218 | |||
219 | /** |
||
220 | * @inheritdoc |
||
221 | */ |
||
222 | View Code Duplication | public function appendItemsByTag($tagName, $data) |
|
235 | |||
236 | /** |
||
237 | * @inheritdoc |
||
238 | */ |
||
239 | View Code Duplication | public function appendItemsByTags(array $tagNames, $data) |
|
251 | |||
252 | /** |
||
253 | * @inheritdoc |
||
254 | */ |
||
255 | View Code Duplication | public function prependItemsByTag($tagName, $data) |
|
268 | |||
269 | /** |
||
270 | * @inheritdoc |
||
271 | */ |
||
272 | View Code Duplication | public function prependItemsByTags(array $tagNames, $data) |
|
284 | } |
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.