1 | <?php |
||||||
2 | |||||||
3 | declare(strict_types=1); |
||||||
4 | |||||||
5 | namespace Soupmix\Cache; |
||||||
6 | |||||||
7 | use DateInterval; |
||||||
8 | use DateTime; |
||||||
9 | use Soupmix\Cache\Exception\InvalidArgumentException; |
||||||
10 | |||||||
11 | use function is_string; |
||||||
12 | use function sprintf; |
||||||
13 | use function str_contains; |
||||||
14 | 20 | use function time; |
|||||
15 | |||||||
16 | 20 | abstract class Common |
|||||
17 | 4 | { |
|||||
18 | 4 | private const PSR16_RESERVED_CHARACTERS = ['{', '}', '(', ')', '/', '@', ':']; |
|||||
19 | |||||||
20 | 16 | protected function checkReservedCharacters($key): void |
|||||
21 | 16 | { |
|||||
22 | 4 | if (! is_string($key)) { |
|||||
23 | 4 | $message = sprintf('key %s is not a string.', $key); |
|||||
24 | |||||||
25 | throw new InvalidArgumentException($message); |
||||||
26 | 12 | } |
|||||
27 | |||||||
28 | foreach (self::PSR16_RESERVED_CHARACTERS as $needle) { |
||||||
29 | 2 | if (str_contains($key, $needle)) { |
|||||
30 | $message = sprintf('%s string is not a legal value.', $key); |
||||||
31 | 2 | ||||||
32 | 2 | throw new InvalidArgumentException($message); |
|||||
33 | 2 | } |
|||||
34 | 2 | } |
|||||
35 | } |
||||||
36 | 2 | ||||||
37 | public function getMultiple($keys, $default = null): array |
||||||
38 | { |
||||||
39 | 2 | $values = []; |
|||||
40 | foreach ($keys as $key) { |
||||||
41 | 2 | $this->checkReservedCharacters($key); |
|||||
42 | 2 | $values[$key] = $this->get($key, $default); |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
43 | 2 | } |
|||||
44 | |||||||
45 | 2 | return $values; |
|||||
46 | 2 | } |
|||||
47 | |||||||
48 | 2 | public function setMultiple($values, $ttl = null): bool |
|||||
49 | 2 | { |
|||||
50 | 2 | $isEverythingOK = true; |
|||||
51 | 2 | if ($ttl instanceof DateInterval) { |
|||||
52 | $ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
||||||
53 | } |
||||||
54 | |||||||
55 | 2 | if ($ttl !== null) { |
|||||
56 | $ttl = ((int) $ttl) + time(); |
||||||
57 | } |
||||||
58 | 3 | ||||||
59 | foreach ($values as $key => $value) { |
||||||
60 | 3 | $this->checkReservedCharacters($key); |
|||||
61 | 3 | $result = $this->set($key, $value, $ttl); |
|||||
0 ignored issues
–
show
The method
set() does not exist on Soupmix\Cache\Common . Since it exists in all sub-types, consider adding an abstract or default implementation to Soupmix\Cache\Common .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
62 | 3 | if ($result) { |
|||||
63 | 3 | continue; |
|||||
64 | 3 | } |
|||||
65 | |||||||
66 | $isEverythingOK = false; |
||||||
67 | } |
||||||
68 | 3 | ||||||
69 | return $isEverythingOK; |
||||||
70 | 1 | } |
|||||
71 | |||||||
72 | public function deleteMultiple($keys): bool |
||||||
73 | { |
||||||
74 | $isEverythingOK = true; |
||||||
75 | foreach ($keys as $key) { |
||||||
76 | $this->checkReservedCharacters($key); |
||||||
77 | $result = $this->delete($key); |
||||||
0 ignored issues
–
show
The method
delete() does not exist on Soupmix\Cache\Common . Did you maybe mean deleteMultiple() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
78 | if ($result) { |
||||||
79 | continue; |
||||||
80 | } |
||||||
81 | |||||||
82 | $isEverythingOK = false; |
||||||
83 | } |
||||||
84 | |||||||
85 | return $isEverythingOK; |
||||||
86 | } |
||||||
87 | } |
||||||
88 |