1919use function fileowner ;
2020use function fopen ;
2121use function function_exists ;
22- use function gettype ;
2322use function is_dir ;
2423use function is_file ;
25- use function is_iterable ;
26- use function is_string ;
2724use function iterator_to_array ;
2825use function opendir ;
2926use function posix_geteuid ;
@@ -111,7 +108,7 @@ public function __construct(string $cachePath)
111108 $ this ->cachePath = $ cachePath ;
112109 }
113110
114- public function get ($ key , $ default = null )
111+ public function get (string $ key , mixed $ default = null ): mixed
115112 {
116113 $ this ->validateKey ($ key );
117114 $ file = $ this ->getCacheFile ($ key );
@@ -128,7 +125,7 @@ public function get($key, $default = null)
128125 return unserialize ($ value );
129126 }
130127
131- public function set ($ key , $ value , $ ttl = null ): bool
128+ public function set (string $ key , mixed $ value , null | int | DateInterval $ ttl = null ): bool
132129 {
133130 $ this ->validateKey ($ key );
134131 $ this ->gc ();
@@ -163,7 +160,7 @@ public function set($key, $value, $ttl = null): bool
163160 return touch ($ file , $ expiration );
164161 }
165162
166- public function delete ($ key ): bool
163+ public function delete (string $ key ): bool
167164 {
168165 $ this ->validateKey ($ key );
169166 $ file = $ this ->getCacheFile ($ key );
@@ -181,7 +178,7 @@ public function clear(): bool
181178 return true ;
182179 }
183180
184- public function getMultiple ($ keys , $ default = null ): iterable
181+ public function getMultiple (iterable $ keys , mixed $ default = null ): iterable
185182 {
186183 $ keys = $ this ->iterableToArray ($ keys );
187184 $ this ->validateKeys ($ keys );
@@ -194,7 +191,7 @@ public function getMultiple($keys, $default = null): iterable
194191 return $ results ;
195192 }
196193
197- public function setMultiple ($ values , $ ttl = null ): bool
194+ public function setMultiple (iterable $ values , null | int | DateInterval $ ttl = null ): bool
198195 {
199196 $ values = $ this ->iterableToArray ($ values );
200197 $ this ->validateKeys (array_map ('\strval ' , array_keys ($ values )));
@@ -206,7 +203,7 @@ public function setMultiple($values, $ttl = null): bool
206203 return true ;
207204 }
208205
209- public function deleteMultiple ($ keys ): bool
206+ public function deleteMultiple (iterable $ keys ): bool
210207 {
211208 $ keys = $ this ->iterableToArray ($ keys );
212209 $ this ->validateKeys ($ keys );
@@ -218,7 +215,7 @@ public function deleteMultiple($keys): bool
218215 return true ;
219216 }
220217
221- public function has ($ key ): bool
218+ public function has (string $ key ): bool
222219 {
223220 $ this ->validateKey ($ key );
224221 return $ this ->existsAndNotExpired ($ this ->getCacheFile ($ key ));
@@ -296,11 +293,11 @@ public function withGcProbability(int $gcProbability): self
296293 /**
297294 * Converts TTL to expiration.
298295 *
299- * @param DateInterval|int|null $ttl
296+ * @param DateInterval|int|string| null $ttl
300297 *
301298 * @return int
302299 */
303- private function ttlToExpiration ($ ttl ): int
300+ private function ttlToExpiration (null | int | string | DateInterval $ ttl = null ): int
304301 {
305302 $ ttl = $ this ->normalizeTtl ($ ttl );
306303
@@ -322,7 +319,7 @@ private function ttlToExpiration($ttl): int
322319 *
323320 * @return int|null TTL value as UNIX timestamp or null meaning infinity
324321 */
325- private function normalizeTtl ($ ttl ): ?int
322+ private function normalizeTtl (null | int | string | DateInterval $ ttl = null ): ?int
326323 {
327324 if ($ ttl === null ) {
328325 return null ;
@@ -365,7 +362,7 @@ private function getCacheFile(string $key): string
365362 $ base = $ this ->cachePath ;
366363
367364 for ($ i = 0 ; $ i < $ this ->directoryLevel ; ++$ i ) {
368- if (($ prefix = substr ($ key , $ i + $ i , 2 )) !== false ) {
365+ if (($ prefix = substr ($ key , $ i + $ i , 2 )) !== '' ) {
369366 $ base .= DIRECTORY_SEPARATOR . $ prefix ;
370367 }
371368 }
@@ -421,31 +418,20 @@ private function gc(): void
421418 }
422419 }
423420
424- /**
425- * @param mixed $key
426- */
427- private function validateKey ($ key ): void
421+ private function validateKey (string $ key ): void
428422 {
429- if (! is_string ( $ key ) || $ key === '' || strpbrk ($ key , '{}()/\@: ' )) {
423+ if ($ key === '' || strpbrk ($ key , '{}()/\@: ' )) {
430424 throw new InvalidArgumentException ('Invalid key value. ' );
431425 }
432426 }
433427
434- /**
435- * @param array $keys
436- */
437428 private function validateKeys (array $ keys ): void
438429 {
439430 foreach ($ keys as $ key ) {
440431 $ this ->validateKey ($ key );
441432 }
442433 }
443434
444- /**
445- * @param string $file
446- *
447- * @return bool
448- */
449435 private function existsAndNotExpired (string $ file ): bool
450436 {
451437 return is_file ($ file ) && @filemtime ($ file ) > time ();
@@ -454,16 +440,12 @@ private function existsAndNotExpired(string $file): bool
454440 /**
455441 * Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException.
456442 *
457- * @param mixed $iterable
443+ * @param iterable $iterable
458444 *
459445 * @return array
460446 */
461- private function iterableToArray ($ iterable ): array
447+ private function iterableToArray (iterable $ iterable ): array
462448 {
463- if (!is_iterable ($ iterable )) {
464- throw new InvalidArgumentException ('Iterable is expected, got ' . gettype ($ iterable ));
465- }
466-
467449 /** @psalm-suppress RedundantCast */
468450 return $ iterable instanceof Traversable ? iterator_to_array ($ iterable ) : (array ) $ iterable ;
469451 }
0 commit comments