Total Complexity | 56 |
Total Lines | 429 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ItemExtendedTrait 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 ItemExtendedTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | trait ItemExtendedTrait |
||
37 | { |
||
38 | /******************** |
||
39 | * |
||
40 | * PSR-6 Extended Methods |
||
41 | * |
||
42 | *******************/ |
||
43 | |||
44 | /** |
||
45 | * @var EventManager |
||
46 | */ |
||
47 | protected $eventManager; |
||
48 | |||
49 | |||
50 | /** |
||
51 | * @var ExtendedCacheItemPoolInterface |
||
52 | */ |
||
53 | protected $driver; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $encodedKey; |
||
59 | |||
60 | /** |
||
61 | * Item constructor. |
||
62 | * @param ExtendedCacheItemPoolInterface $driver |
||
63 | * @param $key |
||
64 | * @throws PhpfastcacheInvalidArgumentException |
||
65 | */ |
||
66 | public function __construct(ExtendedCacheItemPoolInterface $driver, $key) |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return string |
||
84 | */ |
||
85 | public function getEncodedKey(): string |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return \DateTimeInterface |
||
102 | */ |
||
103 | public function getExpirationDate(): \DateTimeInterface |
||
104 | { |
||
105 | return $this->expirationDate; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Alias of expireAt() with forced $expiration param |
||
110 | * |
||
111 | * @param \DateTimeInterface $expiration |
||
112 | * The point in time after which the item MUST be considered expired. |
||
113 | * If null is passed explicitly, a default value MAY be used. If none is set, |
||
114 | * the value should be stored permanently or for as long as the |
||
115 | * implementation allows. |
||
116 | * |
||
117 | * @return ExtendedCacheItemInterface |
||
118 | * The called object. |
||
119 | */ |
||
120 | public function setExpirationDate(\DateTimeInterface $expiration): ExtendedCacheItemInterface |
||
121 | { |
||
122 | return $this->expiresAt($expiration); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @return \DateTimeInterface |
||
127 | * @throws PhpfastcacheLogicException |
||
128 | */ |
||
129 | public function getCreationDate(): \DateTimeInterface |
||
130 | { |
||
131 | if ($this->driver->getConfig()->isItemDetailedDate()) { |
||
132 | return $this->creationDate; |
||
133 | } |
||
134 | |||
135 | throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param \DateTimeInterface $date |
||
140 | * @return ExtendedCacheItemInterface |
||
141 | * @throws PhpfastcacheLogicException |
||
142 | */ |
||
143 | public function setCreationDate(\DateTimeInterface $date): ExtendedCacheItemInterface |
||
144 | { |
||
145 | if ($this->driver->getConfig()->isItemDetailedDate()) { |
||
146 | $this->creationDate = $date; |
||
147 | return $this; |
||
148 | } |
||
149 | |||
150 | throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @return \DateTimeInterface |
||
155 | * @throws PhpfastcacheLogicException |
||
156 | */ |
||
157 | public function getModificationDate(): \DateTimeInterface |
||
158 | { |
||
159 | if ($this->driver->getConfig()->isItemDetailedDate()) { |
||
160 | return $this->modificationDate; |
||
161 | } |
||
162 | |||
163 | throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param \DateTimeInterface $date |
||
168 | * @return ExtendedCacheItemInterface |
||
169 | * @throws PhpfastcacheLogicException |
||
170 | */ |
||
171 | public function setModificationDate(\DateTimeInterface $date): ExtendedCacheItemInterface |
||
172 | { |
||
173 | if ($this->driver->getConfig()->isItemDetailedDate()) { |
||
174 | $this->modificationDate = $date; |
||
175 | return $this; |
||
176 | } |
||
177 | |||
178 | throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return int |
||
183 | */ |
||
184 | public function getTtl(): int |
||
185 | { |
||
186 | return max(0, $this->expirationDate->getTimestamp() - \time()); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @return bool |
||
191 | */ |
||
192 | public function isExpired(): bool |
||
193 | { |
||
194 | return $this->expirationDate->getTimestamp() < (new \DateTime())->getTimestamp(); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function isNull(): bool |
||
201 | { |
||
202 | return $this->data === null; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function isEmpty(): bool |
||
209 | { |
||
210 | return empty($this->data); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Return the data length: |
||
215 | * Either the string length if it's a string (binary mode) |
||
216 | * # or the number of element (count) if it's an array |
||
217 | * # or the number returned by count() if it's an object implementing \Countable interface |
||
218 | * # -1 for anything else |
||
219 | * @return int |
||
220 | */ |
||
221 | public function getLength(): int |
||
222 | { |
||
223 | switch (\gettype($this->data)) |
||
224 | { |
||
225 | case 'array': |
||
226 | case 'object': |
||
227 | if(\is_array($this->data) || $this->data instanceof \Countable){ |
||
228 | return \count($this->data); |
||
229 | } |
||
230 | break; |
||
231 | |||
232 | case 'string': |
||
233 | return \strlen($this->data); |
||
234 | break; |
||
235 | } |
||
236 | |||
237 | return -1; |
||
238 | } |
||
239 | |||
240 | |||
241 | /** |
||
242 | * @param int $step |
||
243 | * @return ExtendedCacheItemInterface |
||
244 | * @throws PhpfastcacheInvalidArgumentException |
||
245 | */ |
||
246 | public function increment($step = 1): ExtendedCacheItemInterface |
||
247 | { |
||
248 | if (\is_int($step)) { |
||
249 | $this->fetched = true; |
||
250 | $this->data += $step; |
||
251 | } else { |
||
252 | throw new PhpfastcacheInvalidArgumentException('$step must be numeric.'); |
||
253 | } |
||
254 | |||
255 | return $this; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param int $step |
||
260 | * @return ExtendedCacheItemInterface |
||
261 | * @throws PhpfastcacheInvalidArgumentException |
||
262 | */ |
||
263 | public function decrement($step = 1): ExtendedCacheItemInterface |
||
264 | { |
||
265 | if (\is_int($step)) { |
||
266 | $this->fetched = true; |
||
267 | $this->data -= $step; |
||
268 | } else { |
||
269 | throw new PhpfastcacheInvalidArgumentException('$step must be numeric.'); |
||
270 | } |
||
271 | |||
272 | return $this; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * @param array|string $data |
||
277 | * @return ExtendedCacheItemInterface |
||
278 | * @throws PhpfastcacheInvalidArgumentException |
||
279 | */ |
||
280 | public function append($data): ExtendedCacheItemInterface |
||
281 | { |
||
282 | if (\is_array($this->data)) { |
||
283 | $this->data[] = $data; |
||
284 | } else if (\is_string($data)) { |
||
285 | $this->data .= (string)$data; |
||
286 | } else { |
||
287 | throw new PhpfastcacheInvalidArgumentException('$data must be either array nor string.'); |
||
288 | } |
||
289 | |||
290 | return $this; |
||
291 | } |
||
292 | |||
293 | |||
294 | /** |
||
295 | * @param array|string $data |
||
296 | * @return ExtendedCacheItemInterface |
||
297 | * @throws PhpfastcacheInvalidArgumentException |
||
298 | */ |
||
299 | public function prepend($data): ExtendedCacheItemInterface |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @param $tagName |
||
314 | * @return ExtendedCacheItemInterface |
||
315 | * @throws PhpfastcacheInvalidArgumentException |
||
316 | */ |
||
317 | public function addTag($tagName): ExtendedCacheItemInterface |
||
318 | { |
||
319 | if (\is_string($tagName)) { |
||
320 | $this->tags = \array_unique(\array_merge($this->tags, [$tagName])); |
||
321 | |||
322 | return $this; |
||
323 | } |
||
324 | |||
325 | throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @param array $tagNames |
||
330 | * @return ExtendedCacheItemInterface |
||
331 | */ |
||
332 | public function addTags(array $tagNames): ExtendedCacheItemInterface |
||
333 | { |
||
334 | foreach ($tagNames as $tagName) { |
||
335 | $this->addTag($tagName); |
||
336 | } |
||
337 | |||
338 | return $this; |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @param array $tags |
||
343 | * @return ExtendedCacheItemInterface |
||
344 | * @throws PhpfastcacheInvalidArgumentException |
||
345 | */ |
||
346 | public function setTags(array $tags): ExtendedCacheItemInterface |
||
347 | { |
||
348 | if (\count($tags)) { |
||
349 | if (\array_filter($tags, 'is_string')) { |
||
350 | $this->tags = $tags; |
||
351 | } else { |
||
352 | throw new PhpfastcacheInvalidArgumentException('$tagName must be an array of string'); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | return $this; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * @return array |
||
361 | */ |
||
362 | public function getTags(): array |
||
363 | { |
||
364 | return $this->tags; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * @param string $separator |
||
369 | * @return string |
||
370 | */ |
||
371 | public function getTagsAsString($separator = ', '): string |
||
372 | { |
||
373 | return \implode($separator, $this->tags); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * @param $tagName |
||
378 | * @return ExtendedCacheItemInterface |
||
379 | */ |
||
380 | public function removeTag($tagName): ExtendedCacheItemInterface |
||
381 | { |
||
382 | if (($key = array_search($tagName, $this->tags)) !== false) { |
||
383 | unset($this->tags[ $key ]); |
||
384 | $this->removedTags[] = $tagName; |
||
385 | } |
||
386 | |||
387 | return $this; |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @param array $tagNames |
||
392 | * @return ExtendedCacheItemInterface |
||
393 | */ |
||
394 | public function removeTags(array $tagNames): ExtendedCacheItemInterface |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * @return array |
||
405 | */ |
||
406 | public function getRemovedTags(): array |
||
407 | { |
||
408 | return \array_diff($this->removedTags, $this->tags); |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Return the data as a well-formatted string. |
||
413 | * Any scalar value will be casted to an array |
||
414 | * @param int $option \json_encode() options |
||
415 | * @param int $depth \json_encode() depth |
||
416 | * @return string |
||
417 | */ |
||
418 | public function getDataAsJsonString($option = 0, $depth = 512): string |
||
419 | { |
||
420 | $data = $this->get(); |
||
421 | |||
422 | if (\is_object($data) || \is_array($data)) { |
||
423 | $data = \json_encode($data, $option, $depth); |
||
424 | } else { |
||
425 | $data = \json_encode([$data], $option, $depth); |
||
426 | } |
||
427 | |||
428 | return \json_encode($data, $option, $depth); |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Implements \JsonSerializable interface |
||
433 | * @return mixed |
||
434 | */ |
||
435 | public function jsonSerialize() |
||
438 | } |
||
439 | |||
440 | |||
441 | /** |
||
442 | * Set the EventManager instance |
||
443 | * |
||
444 | * @param EventManager $em |
||
445 | * @return ExtendedCacheItemInterface |
||
446 | */ |
||
447 | public function setEventManager(EventManager $em): ExtendedCacheItemInterface |
||
448 | { |
||
449 | $this->eventManager = $em; |
||
450 | |||
451 | return $this; |
||
452 | } |
||
453 | |||
454 | |||
455 | /** |
||
456 | * Prevent recursions for Debug (php 5.6+) |
||
457 | * @return array |
||
458 | */ |
||
459 | final public function __debugInfo() |
||
465 | } |
||
466 | } |