55namespace Yiisoft \Db \Helper ;
66
77use Closure ;
8+ use Exception ;
9+
10+ use function array_key_exists ;
11+ use function array_map ;
12+ use function array_multisort ;
13+ use function count ;
14+ use function is_array ;
15+ use function is_object ;
16+ use function property_exists ;
17+ use function range ;
18+ use function strrpos ;
19+ use function substr ;
820
921/**
10- * Short implementation of ArrayHelper from Yii2
22+ * Short implementation of ArrayHelper from Yii2.
1123 */
1224final class ArrayHelper
1325{
1426 /**
1527 * Returns the values of a specified column in an array.
28+ *
1629 * The input array should be multidimensional or an array of objects.
1730 *
1831 * For example,
@@ -31,10 +44,10 @@ final class ArrayHelper
3144 * });
3245 * ```
3346 *
34- * @param array $array
35- * @param string $name
47+ * @param array $array Array to extract values from.
48+ * @param string $name The column name.
3649 *
37- * @return array the list of column values
50+ * @return array The list of column values.
3851 */
3952 public static function getColumn (array $ array , string $ name ): array
4053 {
@@ -48,7 +61,9 @@ static function (array|object $element) use ($name): mixed {
4861
4962 /**
5063 * Retrieves the value of an array element or object property with the given key or property name.
64+ *
5165 * If the key does not exist in the array, the default value will be returned instead.
66+ *
5267 * Not used when getting value from an object.
5368 *
5469 * The key may be specified in a dot format to retrieve the value of a sub-array or the property
@@ -76,15 +91,15 @@ static function (array|object $element) use ($name): mixed {
7691 * $value = \yii\helpers\ArrayHelper::getValue($versions, ['1.0', 'date']);
7792 * ```
7893 *
79- * @param array|object $array array or object to extract value from
80- * @param Closure|string $key key name of the array element, an array of keys or property name of the object,
81- * or an anonymous function returning the value. The anonymous function signature should be:
94+ * @param array|object $array Array or object to extract value from.
95+ * @param Closure|string $key Key name of the array element, an array of keys or property name of the object, or an
96+ * anonymous function returning the value. The anonymous function signature should be:
97+ *
8298 * `function($array, $defaultValue)`.
83- * The possibility to pass an array of keys is available since version 2.0.4.
84- * @param mixed|null $default the default value to be returned if the specified array key does not exist. Not used when
85- * getting value from an object.
99+ * @param mixed|null $default The default value to be returned if the specified array key does not exist. Not used
100+ * when getting value from an object.
86101 *
87- * @return mixed the value of the element if found, default value otherwise
102+ * @return mixed The value of the element if found, default value otherwise
88103 */
89104 public static function getValueByPath (object |array $ array , Closure |string $ key , mixed $ default = null ): mixed
90105 {
@@ -121,13 +136,14 @@ public static function getValueByPath(object|array $array, Closure|string $key,
121136
122137 /**
123138 * Indexes and/or groups the array according to a specified key.
139+ *
124140 * The input should be either multidimensional array or an array of objects.
125141 *
126- * The $key can be either a key name of the sub-array, a property name of object, or an anonymous
127- * function that must return the value that will be used as a key.
142+ * The $key can be either a key name of the sub-array, a property name of object, or an anonymous function that must
143+ * return the value that will be used as a key.
128144 *
129- * $groups is an array of keys, that will be used to group the input array into one or more sub-arrays based
130- * on keys specified.
145+ * $groups is an array of keys, that will be used to group the input array into one or more sub-arrays based on keys
146+ * specified.
131147 *
132148 * If the `$key` is specified as `null` or a value of an element corresponding to the key is `null` in addition
133149 * to `$groups` not specified then the element is discarded.
@@ -195,16 +211,19 @@ public static function getValueByPath(object|array $array, Closure|string $key,
195211 * ]
196212 * ```
197213 *
198- * @param array[] $array the array that needs to be indexed or grouped
199- * @param string|null $key the column name or anonymous function which result will be used to index the array
200- * @param string[] $groups the array of keys, that will be used to group the input array
201- * by one or more keys. If the $key attribute or its value for the particular element is null and $groups is not
202- * defined, the array element will be discarded. Otherwise, if $groups is specified, array element will be added
203- * to the result array without any key. This parameter is available since version 2.0.8.
214+ * @param array $array The array that needs to be indexed or grouped.
215+ * @param string|null $key The column name or anonymous function which result will be used to index the array.
216+ * @param array $groups The array of keys, that will be used to group the input array by one or more keys. If the
217+ * $key attribute or its value for the particular element is null and $groups is not defined, the array element will
218+ * be discarded. Otherwise, if $groups is specified, array element will be added to the result array without any
219+ * key.
220+ *
221+ * @throws Exception
204222 *
205- * @throws \Exception
223+ * @return array The indexed and/or grouped array.
206224 *
207- * @return array the indexed and/or grouped array
225+ * @psalm-param array[] $array The array that needs to be indexed or grouped.
226+ * @psalm-param string[] $groups The array of keys, that will be used to group the input array by one or more keys.
208227 *
209228 * @psalm-suppress MixedArrayAssignment
210229 */
@@ -231,10 +250,12 @@ public static function index(array $array, string|null $key = null, array $group
231250 } else {
232251 /** @psalm-var mixed $value */
233252 $ value = self ::getValueByPath ($ element , $ key );
253+
234254 if ($ value !== null ) {
235255 $ lastArray [(string ) $ value ] = $ element ;
236256 }
237257 }
258+
238259 unset($ lastArray );
239260 }
240261
@@ -249,9 +270,9 @@ public static function index(array $array, string|null $key = null, array $group
249270 *
250271 * Note that an empty array will NOT be considered associative.
251272 *
252- * @param array $array the array being checked
273+ * @param array $array The array being checked
253274 *
254- * @return bool whether the array is associative
275+ * @return bool Whether the array is associative
255276 */
256277 public static function isAssociative (array $ array ): bool
257278 {
@@ -271,8 +292,8 @@ public static function isAssociative(array $array): bool
271292 /**
272293 * Sorts an array of objects or arrays (with the same structure) by one or several keys.
273294 *
274- * @param array $array the array to be sorted. The array will be modified after calling this method.
275- * @param string $key the key(s) to be sorted by.
295+ * @param array $array The array to be sorted. The array will be modified after calling this method.
296+ * @param string $key The key(s) to be sorted by.
276297 */
277298 public static function multisort (
278299 array &$ array ,
@@ -289,8 +310,10 @@ public static function multisort(
289310 SORT_ASC ,
290311 SORT_NUMERIC ,
291312
292- // This fix is used for cases when main sorting specified by columns has equal values
293- // Without it will lead to Fatal Error: Nesting level too deep - recursive dependency?
313+ /**
314+ * This fix is used for cases when main sorting specified by columns has equal values without it will lead
315+ * to Fatal Error: Nesting level too deep - recursive dependency?
316+ */
294317 range (1 , count ($ array )),
295318 SORT_ASC ,
296319 SORT_NUMERIC ,
0 commit comments