Skip to content

Commit 0e4337d

Browse files
Clean code ArrayHelper::class, StringHelper::class. (#550)
1 parent e66baf6 commit 0e4337d

2 files changed

Lines changed: 75 additions & 42 deletions

File tree

src/Helper/ArrayHelper.php

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,27 @@
55
namespace Yiisoft\Db\Helper;
66

77
use 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
*/
1224
final 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,

src/Helper/StringHelper.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,40 @@
44

55
namespace Yiisoft\Db\Helper;
66

7+
use function addslashes;
8+
use function is_float;
9+
use function mb_strrpos;
10+
use function mb_strtolower;
11+
use function mb_substr;
12+
use function preg_replace;
13+
use function rtrim;
14+
use function str_replace;
15+
use function trim;
16+
717
final class StringHelper
818
{
919
/**
1020
* Returns the trailing name component of a path.
11-
* This method is similar to the php function `basename()` except that it will
12-
* treat both \ and / as directory separators, independent of the operating system.
13-
* This method was mainly created to work on php namespaces. When working with real
14-
* file paths, PHP's `basename()` should work fine for you.
21+
*
22+
* This method is similar to the php function `basename()` except that it will treat both \ and / as directory
23+
* separators, independent of the operating system.
24+
*
25+
* This method was mainly created to work on php namespaces. When working with real file paths, PHP's `basename()`
26+
* should work fine for you.
27+
*
1528
* Note: this method is not aware of the actual filesystem, or path components such as "..".
1629
*
1730
* @param string $path A path string.
1831
*
1932
* @return string The trailing name component of the given path.
2033
*
21-
* @see http://www.php.net/manual/en/function.basename.php
34+
* @link http://www.php.net/manual/en/function.basename.php
2235
*/
2336
public static function baseName(string $path): string
2437
{
2538
$path = rtrim(str_replace('\\', '/', $path), '/\\');
2639
$position = mb_strrpos($path, '/');
40+
2741
if ($position !== false) {
2842
return mb_substr($path, $position + 1);
2943
}
@@ -34,14 +48,12 @@ public static function baseName(string $path): string
3448
/**
3549
* Returns string representation of a number value without thousands separators and with dot as decimal separator.
3650
*
37-
* @param float|string $value
38-
*
39-
* @return string
51+
* @param float|string $value The number value to be normalized.
4052
*/
4153
public static function normalizeFloat(float|string $value): string
4254
{
4355
if (is_float($value)) {
44-
$value = (string)$value;
56+
$value = (string) $value;
4557
}
4658

4759
$value = str_replace([' ', ','], ['', '.'], $value);
@@ -50,8 +62,8 @@ public static function normalizeFloat(float|string $value): string
5062

5163
/**
5264
* Converts a PascalCase name into an ID in lowercase.
53-
* Words in the ID may be concatenated using '_'.
54-
* For example, 'PostTag' will be converted to 'post_tag'.
65+
*
66+
* Words in the ID may be concatenated using '_'. For example, 'PostTag' will be converted to 'post_tag'.
5567
*
5668
* @param string $input The string to be converted.
5769
*
@@ -60,9 +72,7 @@ public static function normalizeFloat(float|string $value): string
6072
public static function pascalCaseToId(string $input): string
6173
{
6274
$separator = '_';
63-
6475
$result = preg_replace('/(?<=\p{L})(?<!\p{Lu})(\p{Lu})/u', addslashes($separator) . '\1', $input);
65-
6676
return mb_strtolower(trim($result, $separator));
6777
}
6878
}

0 commit comments

Comments
 (0)