@@ -29,7 +29,7 @@ class Arrays
2929 * @return ?T
3030 * @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
3131 */
32- public static function get (array $ array , $ key , $ default = null )
32+ public static function get (array $ array , string | int | array $ key , mixed $ default = null ): mixed
3333 {
3434 foreach (is_array ($ key ) ? $ key : [$ key ] as $ k ) {
3535 if (is_array ($ array ) && array_key_exists ($ k , $ array )) {
@@ -53,7 +53,7 @@ public static function get(array $array, $key, $default = null)
5353 * @return ?T
5454 * @throws Nette\InvalidArgumentException if traversed item is not an array
5555 */
56- public static function &getRef (array &$ array , $ key )
56+ public static function &getRef (array &$ array , string | int | array $ key ): mixed
5757 {
5858 foreach (is_array ($ key ) ? $ key : [$ key ] as $ k ) {
5959 if (is_array ($ array ) || $ array === null ) {
@@ -90,10 +90,8 @@ public static function mergeTree(array $array1, array $array2): array
9090
9191 /**
9292 * Returns zero-indexed position of given array key. Returns null if key is not found.
93- * @param array-key $key
94- * @return int|null offset if it is found, null otherwise
9593 */
96- public static function getKeyOffset (array $ array , $ key ): ?int
94+ public static function getKeyOffset (array $ array , string | int $ key ): ?int
9795 {
9896 return Helpers::falseToNull (array_search (self ::toKey ($ key ), array_keys ($ array ), true ));
9997 }
@@ -110,9 +108,8 @@ public static function searchKey(array $array, $key): ?int
110108
111109 /**
112110 * Tests an array for the presence of value.
113- * @param mixed $value
114111 */
115- public static function contains (array $ array , $ value ): bool
112+ public static function contains (array $ array , mixed $ value ): bool
116113 {
117114 return in_array ($ value , $ array , true );
118115 }
@@ -124,7 +121,7 @@ public static function contains(array $array, $value): bool
124121 * @param array<T> $array
125122 * @return ?T
126123 */
127- public static function first (array $ array )
124+ public static function first (array $ array ): mixed
128125 {
129126 return count ($ array ) ? reset ($ array ) : null ;
130127 }
@@ -136,7 +133,7 @@ public static function first(array $array)
136133 * @param array<T> $array
137134 * @return ?T
138135 */
139- public static function last (array $ array )
136+ public static function last (array $ array ): mixed
140137 {
141138 return count ($ array ) ? end ($ array ) : null ;
142139 }
@@ -145,9 +142,8 @@ public static function last(array $array)
145142 /**
146143 * Inserts the contents of the $inserted array into the $array immediately after the $key.
147144 * If $key is null (or does not exist), it is inserted at the beginning.
148- * @param array-key|null $key
149145 */
150- public static function insertBefore (array &$ array , $ key , array $ inserted ): void
146+ public static function insertBefore (array &$ array , string | int | null $ key , array $ inserted ): void
151147 {
152148 $ offset = $ key === null ? 0 : (int ) self ::getKeyOffset ($ array , $ key );
153149 $ array = array_slice ($ array , 0 , $ offset , true )
@@ -159,9 +155,8 @@ public static function insertBefore(array &$array, $key, array $inserted): void
159155 /**
160156 * Inserts the contents of the $inserted array into the $array before the $key.
161157 * If $key is null (or does not exist), it is inserted at the end.
162- * @param array-key|null $key
163158 */
164- public static function insertAfter (array &$ array , $ key , array $ inserted ): void
159+ public static function insertAfter (array &$ array , string | int | null $ key , array $ inserted ): void
165160 {
166161 if ($ key === null || ($ offset = self ::getKeyOffset ($ array , $ key )) === null ) {
167162 $ offset = count ($ array ) - 1 ;
@@ -174,10 +169,8 @@ public static function insertAfter(array &$array, $key, array $inserted): void
174169
175170 /**
176171 * Renames key in array.
177- * @param array-key $oldKey
178- * @param array-key $newKey
179172 */
180- public static function renameKey (array &$ array , $ oldKey , $ newKey ): bool
173+ public static function renameKey (array &$ array , string | int $ oldKey , string | int $ newKey ): bool
181174 {
182175 $ offset = self ::getKeyOffset ($ array , $ oldKey );
183176 if ($ offset === null ) {
@@ -219,9 +212,8 @@ public static function flatten(array $array, bool $preserveKeys = false): array
219212
220213 /**
221214 * Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
222- * @param mixed $value
223215 */
224- public static function isList ($ value ): bool
216+ public static function isList (mixed $ value ): bool
225217 {
226218 return is_array ($ value ) && (!$ value || array_keys ($ value ) === range (0 , count ($ value ) - 1 ));
227219 }
@@ -230,9 +222,8 @@ public static function isList($value): bool
230222 /**
231223 * Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
232224 * @param string|string[] $path
233- * @return array|\stdClass
234225 */
235- public static function associate (array $ array , $ path )
226+ public static function associate (array $ array , $ path ): array | \ stdClass
236227 {
237228 $ parts = is_array ($ path )
238229 ? $ path
@@ -285,9 +276,8 @@ public static function associate(array $array, $path)
285276
286277 /**
287278 * Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
288- * @param mixed $filling
289279 */
290- public static function normalize (array $ array , $ filling = null ): array
280+ public static function normalize (array $ array , mixed $ filling = null ): array
291281 {
292282 $ res = [];
293283 foreach ($ array as $ k => $ v ) {
@@ -302,12 +292,11 @@ public static function normalize(array $array, $filling = null): array
302292 * or returns $default, if provided.
303293 * @template T
304294 * @param array<T> $array
305- * @param array-key $key
306295 * @param ?T $default
307296 * @return ?T
308297 * @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
309298 */
310- public static function pick (array &$ array , $ key , $ default = null )
299+ public static function pick (array &$ array , string | int $ key , mixed $ default = null ): mixed
311300 {
312301 if (array_key_exists ($ key , $ array )) {
313302 $ value = $ array [$ key ];
@@ -401,7 +390,7 @@ public static function invokeMethod(iterable $objects, string $method, ...$args)
401390 * @param T $object
402391 * @return T
403392 */
404- public static function toObject (iterable $ array , $ object )
393+ public static function toObject (iterable $ array , object $ object ): object
405394 {
406395 foreach ($ array as $ k => $ v ) {
407396 $ object ->$ k = $ v ;
@@ -412,10 +401,8 @@ public static function toObject(iterable $array, $object)
412401
413402 /**
414403 * Converts value to array key.
415- * @param mixed $value
416- * @return array-key
417404 */
418- public static function toKey ($ value )
405+ public static function toKey (mixed $ value ): int | string
419406 {
420407 return key ([$ value => null ]);
421408 }
0 commit comments