1111
1212use function array_keys ;
1313use function array_map ;
14- use function gettype ;
15- use function is_iterable ;
1614use function is_object ;
1715use function is_string ;
1816use function iterator_to_array ;
@@ -32,7 +30,7 @@ final class ArrayCache implements \Psr\SimpleCache\CacheInterface
3230 /** @psalm-var array<string, array{0: mixed, 1: int}> */
3331 private array $ cache = [];
3432
35- public function get ($ key , $ default = null )
33+ public function get (string $ key , mixed $ default = null ): mixed
3634 {
3735 if ($ this ->has ($ key )) {
3836 /** @var mixed */
@@ -48,7 +46,7 @@ public function get($key, $default = null)
4846 return $ default ;
4947 }
5048
51- public function set ($ key , $ value , $ ttl = null ): bool
49+ public function set (string $ key , mixed $ value , null | int | DateInterval $ ttl = null ): bool
5250 {
5351 $ this ->validateKey ($ key );
5452 $ expiration = $ this ->ttlToExpiration ($ ttl );
@@ -65,7 +63,7 @@ public function set($key, $value, $ttl = null): bool
6563 return true ;
6664 }
6765
68- public function delete ($ key ): bool
66+ public function delete (string $ key ): bool
6967 {
7068 $ this ->validateKey ($ key );
7169 unset($ this ->cache [$ key ]);
@@ -78,9 +76,10 @@ public function clear(): bool
7876 return true ;
7977 }
8078
81- public function getMultiple ($ keys , $ default = null ): iterable
79+ public function getMultiple (iterable $ keys , mixed $ default = null ): iterable
8280 {
8381 $ keys = $ this ->iterableToArray ($ keys );
82+ /** @psalm-suppress RedundantCondition */
8483 $ this ->validateKeys ($ keys );
8584 $ results = [];
8685
@@ -94,7 +93,7 @@ public function getMultiple($keys, $default = null): iterable
9493 return $ results ;
9594 }
9695
97- public function setMultiple ($ values , $ ttl = null ): bool
96+ public function setMultiple (iterable $ values , null | int | DateInterval $ ttl = null ): bool
9897 {
9998 $ values = $ this ->iterableToArray ($ values );
10099 $ this ->validateKeysOfValues ($ values );
@@ -107,9 +106,10 @@ public function setMultiple($values, $ttl = null): bool
107106 return true ;
108107 }
109108
110- public function deleteMultiple ($ keys ): bool
109+ public function deleteMultiple (iterable $ keys ): bool
111110 {
112111 $ keys = $ this ->iterableToArray ($ keys );
112+ /** @psalm-suppress RedundantCondition */
113113 $ this ->validateKeys ($ keys );
114114
115115 foreach ($ keys as $ key ) {
@@ -119,7 +119,7 @@ public function deleteMultiple($keys): bool
119119 return true ;
120120 }
121121
122- public function has ($ key ): bool
122+ public function has (string $ key ): bool
123123 {
124124 $ this ->validateKey ($ key );
125125 return !$ this ->isExpired ($ key );
@@ -138,13 +138,9 @@ private function isExpired(string $key): bool
138138 }
139139
140140 /**
141- * Converts TTL to expiration
142- *
143- * @param DateInterval|int|null $ttl
144- *
145- * @return int
141+ * Converts TTL to expiration.
146142 */
147- private function ttlToExpiration ($ ttl ): int
143+ private function ttlToExpiration (DateInterval | int | null $ ttl ): int
148144 {
149145 $ ttl = $ this ->normalizeTtl ($ ttl );
150146
@@ -166,7 +162,7 @@ private function ttlToExpiration($ttl): int
166162 *
167163 * @return int|null TTL value as UNIX timestamp or null meaning infinity
168164 */
169- private function normalizeTtl ($ ttl ): ?int
165+ private function normalizeTtl (DateInterval | int | string | null $ ttl ): ?int
170166 {
171167 if ($ ttl instanceof DateInterval) {
172168 return (new DateTime ('@0 ' ))
@@ -182,34 +178,25 @@ private function normalizeTtl($ttl): ?int
182178 }
183179
184180 /**
185- * Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException
181+ * Converts iterable to array.
186182 *
187- * @param mixed $iterable
188- *
189- * @return array
183+ * @psalm-template T
184+ * @psalm-param iterable<T> $iterable
185+ * @psalm- return array<array-key,T>
190186 */
191- private function iterableToArray ($ iterable ): array
187+ private function iterableToArray (iterable $ iterable ): array
192188 {
193- if (!is_iterable ($ iterable )) {
194- throw new InvalidArgumentException ('Iterable is expected, got ' . gettype ($ iterable ));
195- }
196-
197- /** @psalm-suppress RedundantCast */
198- return $ iterable instanceof Traversable ? iterator_to_array ($ iterable ) : (array ) $ iterable ;
189+ return $ iterable instanceof Traversable ? iterator_to_array ($ iterable ) : $ iterable ;
199190 }
200191
201- /**
202- * @param mixed $key
203- */
204- private function validateKey ($ key ): void
192+ private function validateKey (mixed $ key ): void
205193 {
206194 if (!is_string ($ key ) || $ key === '' || strpbrk ($ key , '{}()/\@: ' )) {
207195 throw new InvalidArgumentException ('Invalid key value. ' );
208196 }
209197 }
210198
211199 /**
212- * @param array $keys
213200 * @psalm-assert string[] $keys
214201 */
215202 private function validateKeys (array $ keys ): void
0 commit comments