@@ -23,11 +23,9 @@ class Arrays
2323 /**
2424 * Returns item from array. If it does not exist, it throws an exception, unless a default value is set.
2525 * @param string|int|array $key one or more keys
26- * @param mixed $default
27- * @return mixed
2826 * @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
2927 */
30- public static function get (array $ array , $ key , $ default = null )
28+ public static function get (array $ array , string | int | array $ key , mixed $ default = null ): mixed
3129 {
3230 foreach (is_array ($ key ) ? $ key : [$ key ] as $ k ) {
3331 if (is_array ($ array ) && array_key_exists ($ k , $ array )) {
@@ -46,10 +44,9 @@ public static function get(array $array, $key, $default = null)
4644 /**
4745 * Returns reference to array item. If the index does not exist, new one is created with value null.
4846 * @param string|int|array $key one or more keys
49- * @return mixed
5047 * @throws Nette\InvalidArgumentException if traversed item is not an array
5148 */
52- public static function &getRef (array &$ array , $ key )
49+ public static function &getRef (array &$ array , string | int | array $ key ): mixed
5350 {
5451 foreach (is_array ($ key ) ? $ key : [$ key ] as $ k ) {
5552 if (is_array ($ array ) || $ array === null ) {
@@ -81,10 +78,8 @@ public static function mergeTree(array $array1, array $array2): array
8178
8279 /**
8380 * Returns zero-indexed position of given array key. Returns null if key is not found.
84- * @param string|int $key
85- * @return int|null offset if it is found, null otherwise
8681 */
87- public static function getKeyOffset (array $ array , $ key ): ?int
82+ public static function getKeyOffset (array $ array , string | int $ key ): ?int
8883 {
8984 return Helpers::falseToNull (array_search (self ::toKey ($ key ), array_keys ($ array ), true ));
9085 }
@@ -101,29 +96,26 @@ public static function searchKey(array $array, $key): ?int
10196
10297 /**
10398 * Tests an array for the presence of value.
104- * @param mixed $value
10599 */
106- public static function contains (array $ array , $ value ): bool
100+ public static function contains (array $ array , mixed $ value ): bool
107101 {
108102 return in_array ($ value , $ array , true );
109103 }
110104
111105
112106 /**
113107 * Returns the first item from the array or null if array is empty.
114- * @return mixed
115108 */
116- public static function first (array $ array )
109+ public static function first (array $ array ): mixed
117110 {
118111 return count ($ array ) ? reset ($ array ) : null ;
119112 }
120113
121114
122115 /**
123116 * Returns the last item from the array or null if array is empty.
124- * @return mixed
125117 */
126- public static function last (array $ array )
118+ public static function last (array $ array ): mixed
127119 {
128120 return count ($ array ) ? end ($ array ) : null ;
129121 }
@@ -132,9 +124,8 @@ public static function last(array $array)
132124 /**
133125 * Inserts the contents of the $inserted array into the $array immediately after the $key.
134126 * If $key is null (or does not exist), it is inserted at the beginning.
135- * @param string|int|null $key
136127 */
137- public static function insertBefore (array &$ array , $ key , array $ inserted ): void
128+ public static function insertBefore (array &$ array , string | int | null $ key , array $ inserted ): void
138129 {
139130 $ offset = $ key === null ? 0 : (int ) self ::getKeyOffset ($ array , $ key );
140131 $ array = array_slice ($ array , 0 , $ offset , true )
@@ -146,9 +137,8 @@ public static function insertBefore(array &$array, $key, array $inserted): void
146137 /**
147138 * Inserts the contents of the $inserted array into the $array before the $key.
148139 * If $key is null (or does not exist), it is inserted at the end.
149- * @param string|int|null $key
150140 */
151- public static function insertAfter (array &$ array , $ key , array $ inserted ): void
141+ public static function insertAfter (array &$ array , string | int | null $ key , array $ inserted ): void
152142 {
153143 if ($ key === null || ($ offset = self ::getKeyOffset ($ array , $ key )) === null ) {
154144 $ offset = count ($ array ) - 1 ;
@@ -161,10 +151,8 @@ public static function insertAfter(array &$array, $key, array $inserted): void
161151
162152 /**
163153 * Renames key in array.
164- * @param string|int $oldKey
165- * @param string|int $newKey
166154 */
167- public static function renameKey (array &$ array , $ oldKey , $ newKey ): bool
155+ public static function renameKey (array &$ array , string | int $ oldKey , string | int $ newKey ): bool
168156 {
169157 $ offset = self ::getKeyOffset ($ array , $ oldKey );
170158 if ($ offset === null ) {
@@ -205,9 +193,8 @@ public static function flatten(array $array, bool $preserveKeys = false): array
205193
206194 /**
207195 * Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
208- * @param mixed $value
209196 */
210- public static function isList ($ value ): bool
197+ public static function isList (mixed $ value ): bool
211198 {
212199 return is_array ($ value ) && (!$ value || array_keys ($ value ) === range (0 , count ($ value ) - 1 ));
213200 }
@@ -216,9 +203,8 @@ public static function isList($value): bool
216203 /**
217204 * Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
218205 * @param string|string[] $path
219- * @return array|\stdClass
220206 */
221- public static function associate (array $ array , $ path )
207+ public static function associate (array $ array , $ path ): array | \ stdClass
222208 {
223209 $ parts = is_array ($ path )
224210 ? $ path
@@ -271,9 +257,8 @@ public static function associate(array $array, $path)
271257
272258 /**
273259 * Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
274- * @param mixed $filling
275260 */
276- public static function normalize (array $ array , $ filling = null ): array
261+ public static function normalize (array $ array , mixed $ filling = null ): array
277262 {
278263 $ res = [];
279264 foreach ($ array as $ k => $ v ) {
@@ -286,12 +271,9 @@ public static function normalize(array $array, $filling = null): array
286271 /**
287272 * Returns and removes the value of an item from an array. If it does not exist, it throws an exception,
288273 * or returns $default, if provided.
289- * @param string|int $key
290- * @param mixed $default
291- * @return mixed
292274 * @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
293275 */
294- public static function pick (array &$ array , $ key , $ default = null )
276+ public static function pick (array &$ array , string | int $ key , mixed $ default = null ): mixed
295277 {
296278 if (array_key_exists ($ key , $ array )) {
297279 $ value = $ array [$ key ];
@@ -381,10 +363,8 @@ public static function invokeMethod(iterable $objects, string $method, ...$args)
381363
382364 /**
383365 * Copies the elements of the $array array to the $object object and then returns it.
384- * @param object $object
385- * @return object
386366 */
387- public static function toObject (iterable $ array , $ object )
367+ public static function toObject (iterable $ array , object $ object ): object
388368 {
389369 foreach ($ array as $ k => $ v ) {
390370 $ object ->$ k = $ v ;
@@ -395,10 +375,8 @@ public static function toObject(iterable $array, $object)
395375
396376 /**
397377 * Converts value to array key.
398- * @param mixed $value
399- * @return int|string
400378 */
401- public static function toKey ($ value )
379+ public static function toKey (mixed $ value ): int | string
402380 {
403381 return key ([$ value => null ]);
404382 }
0 commit comments