Total Complexity | 41 |
Total Lines | 277 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ExtendedCacheItemTrait 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.
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 ExtendedCacheItemTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | trait ExtendedCacheItemTrait |
||
26 | { |
||
27 | use CacheItemTrait; |
||
28 | |||
29 | protected ExtendedCacheItemPoolInterface $driver; |
||
30 | |||
31 | protected string $encodedKey; |
||
32 | |||
33 | /** |
||
34 | * Item constructor. |
||
35 | * @param ExtendedCacheItemPoolInterface $driver |
||
36 | * @param string $key |
||
37 | * @param EventManagerInterface $em |
||
38 | * @throws PhpfastcacheInvalidArgumentException |
||
39 | */ |
||
40 | public function __construct(ExtendedCacheItemPoolInterface $driver, string $key, EventManagerInterface $em) |
||
41 | { |
||
42 | $this->data = null; |
||
43 | $this->key = $key; |
||
44 | $this->setEventManager($em); |
||
45 | $this->setDriver($driver); |
||
46 | if ($driver->getConfig()->isUseStaticItemCaching()) { |
||
47 | $this->driver->setItem($this); |
||
1 ignored issue
–
show
|
|||
48 | } |
||
49 | $this->expirationDate = new DateTime(); |
||
50 | if ($this->driver->getConfig()->isItemDetailedDate()) { |
||
51 | $this->creationDate = new DateTime(); |
||
52 | $this->modificationDate = new DateTime(); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param ExtendedCacheItemPoolInterface $driver |
||
58 | * @return ExtendedCacheItemInterface |
||
59 | * @throws PhpfastcacheInvalidArgumentException |
||
60 | */ |
||
61 | public function setDriver(ExtendedCacheItemPoolInterface $driver): ExtendedCacheItemInterface |
||
62 | { |
||
63 | $driverClass = $this->getDriverClass(); |
||
64 | if ($driver instanceof $driverClass) { |
||
65 | $this->driver = $driver; |
||
66 | |||
67 | return $this; |
||
1 ignored issue
–
show
|
|||
68 | } |
||
69 | |||
70 | throw new PhpfastcacheInvalidArgumentException(\sprintf('Invalid driver instance "%s" for cache item "%s"', $driver::class, static::class)); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @inheritDoc |
||
75 | */ |
||
76 | public function getEncodedKey(): string |
||
77 | { |
||
78 | // Only calculate the encoded key on demand to save resources |
||
79 | if (!isset($this->encodedKey)) { |
||
80 | $keyHashFunction = $this->driver->getConfig()->getDefaultKeyHashFunction(); |
||
81 | |||
82 | if ($keyHashFunction) { |
||
83 | $this->encodedKey = $keyHashFunction($this->getKey()); |
||
84 | } else { |
||
85 | $this->encodedKey = $this->getKey(); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | return $this->encodedKey; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @inheritDoc |
||
94 | */ |
||
95 | public function getRawValue(): mixed |
||
96 | { |
||
97 | return $this->data; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @inheritDoc |
||
102 | */ |
||
103 | public function getExpirationDate(): DateTimeInterface |
||
104 | { |
||
105 | return $this->expirationDate; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @inheritDoc |
||
110 | * @throws PhpfastcacheInvalidArgumentException |
||
111 | */ |
||
112 | public function setExpirationDate(DateTimeInterface $expiration): ExtendedCacheItemInterface |
||
113 | { |
||
114 | return $this->expiresAt($expiration); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @inheritDoc |
||
119 | * @throws PhpfastcacheLogicException |
||
120 | */ |
||
121 | public function getCreationDate(): DateTimeInterface |
||
122 | { |
||
123 | if ($this->driver->getConfig()->isItemDetailedDate()) { |
||
124 | return $this->creationDate; |
||
125 | } |
||
126 | |||
127 | throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @inheritDoc |
||
132 | * @throws PhpfastcacheLogicException |
||
133 | */ |
||
134 | public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @inheritDoc |
||
146 | * @throws PhpfastcacheLogicException |
||
147 | */ |
||
148 | public function getModificationDate(): DateTimeInterface |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @inheritDoc |
||
159 | * @throws PhpfastcacheLogicException |
||
160 | */ |
||
161 | public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
||
162 | { |
||
163 | if ($this->driver->getConfig()->isItemDetailedDate()) { |
||
164 | $this->modificationDate = $date; |
||
165 | return $this; |
||
1 ignored issue
–
show
|
|||
166 | } |
||
167 | |||
168 | throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
||
169 | } |
||
170 | |||
171 | public function getTtl(): int |
||
174 | } |
||
175 | |||
176 | public function isExpired(): bool |
||
177 | { |
||
178 | return $this->expirationDate->getTimestamp() < (new DateTime())->getTimestamp(); |
||
179 | } |
||
180 | |||
181 | public function isNull(): bool |
||
182 | { |
||
183 | return $this->data === null; |
||
184 | } |
||
185 | |||
186 | public function isEmpty(): bool |
||
187 | { |
||
188 | return empty($this->data); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Return the data length: |
||
193 | * Either the string length if it's a string (binary mode) |
||
194 | * # or the number of element (count) if it's an array |
||
195 | * # or the number returned by count() if it's an object implementing \Countable interface |
||
196 | * # -1 for anything else |
||
197 | * @return int |
||
198 | */ |
||
199 | public function getLength(): int |
||
200 | { |
||
201 | switch (\gettype($this->data)) { |
||
202 | case 'array': |
||
203 | case 'object': |
||
204 | if (\is_countable($this->data)) { |
||
205 | return \count($this->data); |
||
206 | } |
||
207 | break; |
||
208 | |||
209 | case 'string': |
||
210 | return \strlen($this->data); |
||
211 | } |
||
212 | |||
213 | return -1; |
||
214 | } |
||
215 | |||
216 | public function increment(int $step = 1): ExtendedCacheItemInterface |
||
217 | { |
||
218 | $this->data += $step; |
||
219 | |||
220 | return $this; |
||
1 ignored issue
–
show
|
|||
221 | } |
||
222 | |||
223 | public function decrement(int $step = 1): ExtendedCacheItemInterface |
||
224 | { |
||
225 | $this->data -= $step; |
||
226 | |||
227 | return $this; |
||
1 ignored issue
–
show
|
|||
228 | } |
||
229 | |||
230 | public function append(array|string $data): ExtendedCacheItemInterface |
||
231 | { |
||
232 | if (\is_array($this->data)) { |
||
233 | $this->data[] = $data; |
||
234 | } else { |
||
235 | $this->data .= $data; |
||
236 | } |
||
237 | |||
238 | return $this; |
||
1 ignored issue
–
show
|
|||
239 | } |
||
240 | |||
241 | public function prepend(array|string $data): ExtendedCacheItemInterface |
||
242 | { |
||
243 | if (\is_array($this->data)) { |
||
244 | \array_unshift($this->data, $data); |
||
245 | } else { |
||
246 | $this->data = $data . $this->data; |
||
1 ignored issue
–
show
|
|||
247 | } |
||
248 | |||
249 | return $this; |
||
1 ignored issue
–
show
|
|||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Return the data as a well-formatted string. |
||
254 | * Any scalar value will be casted to an array |
||
255 | * @param int $options \json_encode() options |
||
256 | * @param int $depth \json_encode() depth |
||
257 | * @return string |
||
258 | */ |
||
259 | public function getDataAsJsonString(int $options = JSON_THROW_ON_ERROR, int $depth = 512): string |
||
260 | { |
||
261 | $data = $this->get(); |
||
262 | |||
263 | if (\is_object($data) || \is_array($data)) { |
||
264 | $data = \json_encode($data, $options, $depth); |
||
265 | } else { |
||
266 | $data = \json_encode([$data], $options, $depth); |
||
267 | } |
||
268 | |||
269 | return \json_encode($data, $options, $depth); |
||
270 | } |
||
271 | |||
272 | public function jsonSerialize(): mixed |
||
273 | { |
||
274 | return $this->get(); |
||
275 | } |
||
276 | |||
277 | public function doesItemBelongToThatDriverBackend(ExtendedCacheItemPoolInterface $driverPool): bool |
||
278 | { |
||
279 | return $driverPool::getClassNamespace() === self::getClassNamespace(); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @throws PhpfastcacheLogicException |
||
284 | * @throws PhpfastcacheInvalidArgumentException |
||
285 | */ |
||
286 | public function cloneInto(ExtendedCacheItemInterface $itemTarget, ?ExtendedCacheItemPoolInterface $itemPoolTarget = null): void |
||
298 | } |
||
299 | } |
||
300 | |||
301 | abstract protected function getDriverClass(): string; |
||
302 | } |
||
303 |