1515use function array_keys ;
1616use function array_map ;
1717use function count ;
18- use function gettype ;
19- use function is_iterable ;
20- use function is_string ;
2118use function iterator_to_array ;
2219use function serialize ;
2320use function strpbrk ;
@@ -43,14 +40,14 @@ public function __construct(ClientInterface $client)
4340 $ this ->client = $ client ;
4441 }
4542
46- public function get ($ key , $ default = null )
43+ public function get (string $ key , mixed $ default = null ): mixed
4744 {
4845 $ this ->validateKey ($ key );
4946 $ value = $ this ->client ->get ($ key );
5047 return $ value === null ? $ default : unserialize ($ value );
5148 }
5249
53- public function set ($ key , $ value , $ ttl = null ): bool
50+ public function set (string $ key , mixed $ value , null | int | DateInterval $ ttl = null ): bool
5451 {
5552 $ ttl = $ this ->normalizeTtl ($ ttl );
5653
@@ -69,7 +66,7 @@ public function set($key, $value, $ttl = null): bool
6966 return $ result !== null ;
7067 }
7168
72- public function delete ($ key ): bool
69+ public function delete (string $ key ): bool
7370 {
7471 return !$ this ->has ($ key ) || $ this ->client ->del ($ key ) === 1 ;
7572 }
@@ -79,27 +76,27 @@ public function clear(): bool
7976 return $ this ->client ->flushdb () !== null ;
8077 }
8178
82- public function getMultiple ($ keys , $ default = null ): iterable
79+ public function getMultiple (iterable $ keys , mixed $ default = null ): iterable
8380 {
81+ /** @var string[] $keys */
8482 $ keys = $ this ->iterableToArray ($ keys );
8583 $ this ->validateKeys ($ keys );
86- /** @var string[] $keys */
8784 $ values = array_fill_keys ($ keys , $ default );
8885 /** @var null[]|string[] $valuesFromCache */
8986 $ valuesFromCache = $ this ->client ->mget ($ keys );
9087
9188 $ i = 0 ;
92- /** @var mixed $default */
93- foreach ( $ values as $ key => $ default ) {
94- /** @psalm-suppress MixedAssignment */
95- $ values [$ key ] = isset ($ valuesFromCache [$ i ]) ? unserialize ($ valuesFromCache [$ i ]) : $ default ;
89+
90+ /** @psalm-suppress MixedAssignment */
91+ foreach ( $ values as $ key => $ value ) {
92+ $ values [$ key ] = isset ($ valuesFromCache [$ i ]) ? unserialize ($ valuesFromCache [$ i ]) : $ value ;
9693 $ i ++;
9794 }
9895
9996 return $ values ;
10097 }
10198
102- public function setMultiple ($ values , $ ttl = null ): bool
99+ public function setMultiple (iterable $ values , null | int | DateInterval $ ttl = null ): bool
103100 {
104101 $ values = $ this ->iterableToArray ($ values );
105102 $ keys = array_map ('\strval ' , array_keys ($ values ));
@@ -140,7 +137,7 @@ public function setMultiple($values, $ttl = null): bool
140137 return true ;
141138 }
142139
143- public function deleteMultiple ($ keys ): bool
140+ public function deleteMultiple (iterable $ keys ): bool
144141 {
145142 $ keys = $ this ->iterableToArray ($ keys );
146143
@@ -154,7 +151,7 @@ public function deleteMultiple($keys): bool
154151 return empty ($ keys ) || $ this ->client ->del ($ keys ) === count ($ keys );
155152 }
156153
157- public function has ($ key ): bool
154+ public function has (string $ key ): bool
158155 {
159156 $ this ->validateKey ($ key );
160157 $ ttl = $ this ->client ->ttl ($ key );
@@ -167,9 +164,9 @@ public function has($key): bool
167164 *
168165 * @param DateInterval|int|string|null $ttl The raw TTL.
169166 *
170- * @return int TTL value as UNIX timestamp.
167+ * @return int|null TTL value as UNIX timestamp.
171168 */
172- private function normalizeTtl ($ ttl ): ?int
169+ private function normalizeTtl (null | int | string | DateInterval $ ttl ): ?int
173170 {
174171 if ($ ttl === null ) {
175172 return null ;
@@ -185,42 +182,34 @@ private function normalizeTtl($ttl): ?int
185182 }
186183
187184 /**
188- * Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException.
185+ * Converts iterable to array.
189186 *
190- * @param mixed $iterable
187+ * @param iterable $iterable
191188 *
192189 * @return array
193190 */
194- private function iterableToArray ($ iterable ): array
191+ private function iterableToArray (iterable $ iterable ): array
195192 {
196- if (!is_iterable ($ iterable )) {
197- throw new InvalidArgumentException ('Iterable is expected, got ' . gettype ($ iterable ));
198- }
199-
200193 /** @psalm-suppress RedundantCast */
201194 return $ iterable instanceof Traversable ? iterator_to_array ($ iterable ) : (array ) $ iterable ;
202195 }
203196
204- /**
205- * @param mixed $key
206- */
207- private function validateKey ($ key ): void
197+ private function validateKey (string $ key ): void
208198 {
209- if (! is_string ( $ key ) || $ key === '' || strpbrk ($ key , '{}()/\@: ' )) {
199+ if ($ key === '' || strpbrk ($ key , '{}()/\@: ' )) {
210200 throw new InvalidArgumentException ('Invalid key value. ' );
211201 }
212202 }
213203
214204 /**
215- * @param array $keys
205+ * @param string[] $keys
216206 */
217207 private function validateKeys (array $ keys ): void
218208 {
219- if (empty ( $ keys) ) {
209+ if ([] === $ keys ) {
220210 throw new InvalidArgumentException ('Invalid key values. ' );
221211 }
222212
223- /** @var mixed $key */
224213 foreach ($ keys as $ key ) {
225214 $ this ->validateKey ($ key );
226215 }
0 commit comments