@@ -23,11 +23,9 @@ class Arrays
23
23
/**
24
24
* Returns item from array. If it does not exist, it throws an exception, unless a default value is set.
25
25
* @param string|int|array $key one or more keys
26
- * @param mixed $default
27
- * @return mixed
28
26
* @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
29
27
*/
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
31
29
{
32
30
foreach (is_array ($ key ) ? $ key : [$ key ] as $ k ) {
33
31
if (is_array ($ array ) && array_key_exists ($ k , $ array )) {
@@ -46,10 +44,9 @@ public static function get(array $array, $key, $default = null)
46
44
/**
47
45
* Returns reference to array item. If the index does not exist, new one is created with value null.
48
46
* @param string|int|array $key one or more keys
49
- * @return mixed
50
47
* @throws Nette\InvalidArgumentException if traversed item is not an array
51
48
*/
52
- public static function &getRef (array &$ array , $ key )
49
+ public static function &getRef (array &$ array , string | int | array $ key ): mixed
53
50
{
54
51
foreach (is_array ($ key ) ? $ key : [$ key ] as $ k ) {
55
52
if (is_array ($ array ) || $ array === null ) {
@@ -81,10 +78,8 @@ public static function mergeTree(array $array1, array $array2): array
81
78
82
79
/**
83
80
* 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
86
81
*/
87
- public static function getKeyOffset (array $ array , $ key ): ?int
82
+ public static function getKeyOffset (array $ array , string | int $ key ): ?int
88
83
{
89
84
return Helpers::falseToNull (array_search (self ::toKey ($ key ), array_keys ($ array ), true ));
90
85
}
@@ -101,29 +96,26 @@ public static function searchKey(array $array, $key): ?int
101
96
102
97
/**
103
98
* Tests an array for the presence of value.
104
- * @param mixed $value
105
99
*/
106
- public static function contains (array $ array , $ value ): bool
100
+ public static function contains (array $ array , mixed $ value ): bool
107
101
{
108
102
return in_array ($ value , $ array , true );
109
103
}
110
104
111
105
112
106
/**
113
107
* Returns the first item from the array or null if array is empty.
114
- * @return mixed
115
108
*/
116
- public static function first (array $ array )
109
+ public static function first (array $ array ): mixed
117
110
{
118
111
return count ($ array ) ? reset ($ array ) : null ;
119
112
}
120
113
121
114
122
115
/**
123
116
* Returns the last item from the array or null if array is empty.
124
- * @return mixed
125
117
*/
126
- public static function last (array $ array )
118
+ public static function last (array $ array ): mixed
127
119
{
128
120
return count ($ array ) ? end ($ array ) : null ;
129
121
}
@@ -132,9 +124,8 @@ public static function last(array $array)
132
124
/**
133
125
* Inserts the contents of the $inserted array into the $array immediately after the $key.
134
126
* If $key is null (or does not exist), it is inserted at the beginning.
135
- * @param string|int|null $key
136
127
*/
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
138
129
{
139
130
$ offset = $ key === null ? 0 : (int ) self ::getKeyOffset ($ array , $ key );
140
131
$ array = array_slice ($ array , 0 , $ offset , true )
@@ -146,9 +137,8 @@ public static function insertBefore(array &$array, $key, array $inserted): void
146
137
/**
147
138
* Inserts the contents of the $inserted array into the $array before the $key.
148
139
* If $key is null (or does not exist), it is inserted at the end.
149
- * @param string|int|null $key
150
140
*/
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
152
142
{
153
143
if ($ key === null || ($ offset = self ::getKeyOffset ($ array , $ key )) === null ) {
154
144
$ offset = count ($ array ) - 1 ;
@@ -161,10 +151,8 @@ public static function insertAfter(array &$array, $key, array $inserted): void
161
151
162
152
/**
163
153
* Renames key in array.
164
- * @param string|int $oldKey
165
- * @param string|int $newKey
166
154
*/
167
- public static function renameKey (array &$ array , $ oldKey , $ newKey ): bool
155
+ public static function renameKey (array &$ array , string | int $ oldKey , string | int $ newKey ): bool
168
156
{
169
157
$ offset = self ::getKeyOffset ($ array , $ oldKey );
170
158
if ($ offset === null ) {
@@ -205,9 +193,8 @@ public static function flatten(array $array, bool $preserveKeys = false): array
205
193
206
194
/**
207
195
* Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
208
- * @param mixed $value
209
196
*/
210
- public static function isList ($ value ): bool
197
+ public static function isList (mixed $ value ): bool
211
198
{
212
199
return is_array ($ value ) && (!$ value || array_keys ($ value ) === range (0 , count ($ value ) - 1 ));
213
200
}
@@ -216,9 +203,8 @@ public static function isList($value): bool
216
203
/**
217
204
* Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
218
205
* @param string|string[] $path
219
- * @return array|\stdClass
220
206
*/
221
- public static function associate (array $ array , $ path )
207
+ public static function associate (array $ array , $ path ): array | \ stdClass
222
208
{
223
209
$ parts = is_array ($ path )
224
210
? $ path
@@ -271,9 +257,8 @@ public static function associate(array $array, $path)
271
257
272
258
/**
273
259
* Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
274
- * @param mixed $filling
275
260
*/
276
- public static function normalize (array $ array , $ filling = null ): array
261
+ public static function normalize (array $ array , mixed $ filling = null ): array
277
262
{
278
263
$ res = [];
279
264
foreach ($ array as $ k => $ v ) {
@@ -286,12 +271,9 @@ public static function normalize(array $array, $filling = null): array
286
271
/**
287
272
* Returns and removes the value of an item from an array. If it does not exist, it throws an exception,
288
273
* or returns $default, if provided.
289
- * @param string|int $key
290
- * @param mixed $default
291
- * @return mixed
292
274
* @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
293
275
*/
294
- public static function pick (array &$ array , $ key , $ default = null )
276
+ public static function pick (array &$ array , string | int $ key , mixed $ default = null ): mixed
295
277
{
296
278
if (array_key_exists ($ key , $ array )) {
297
279
$ value = $ array [$ key ];
@@ -381,10 +363,8 @@ public static function invokeMethod(iterable $objects, string $method, ...$args)
381
363
382
364
/**
383
365
* Copies the elements of the $array array to the $object object and then returns it.
384
- * @param object $object
385
- * @return object
386
366
*/
387
- public static function toObject (iterable $ array , $ object )
367
+ public static function toObject (iterable $ array , object $ object ): object
388
368
{
389
369
foreach ($ array as $ k => $ v ) {
390
370
$ object ->$ k = $ v ;
@@ -395,10 +375,8 @@ public static function toObject(iterable $array, $object)
395
375
396
376
/**
397
377
* Converts value to array key.
398
- * @param mixed $value
399
- * @return int|string
400
378
*/
401
- public static function toKey ($ value )
379
+ public static function toKey (mixed $ value ): int | string
402
380
{
403
381
return key ([$ value => null ]);
404
382
}
0 commit comments