Skip to content

Commit 36d722a

Browse files
committed
breaking: migrate to php 80, add test on php 8.1
1 parent 584d1f8 commit 36d722a

File tree

8 files changed

+107
-130
lines changed

8 files changed

+107
-130
lines changed

.github/workflows/php.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
strategy:
1111
fail-fast: true
1212
matrix:
13-
php: [8.0] #
13+
php: [8.0, 8.1] #
1414
# os: [ubuntu-latest, macOS-latest] # windows-latest,
1515

1616
steps:

src/Arr/ArrayHelper.php

+39-40
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ArrayHelper
6161
*
6262
* @return bool
6363
*/
64-
public static function accessible($value): bool
64+
public static function accessible(mixed $value): bool
6565
{
6666
return is_array($value) || $value instanceof ArrayAccess;
6767
}
@@ -86,7 +86,7 @@ public static function isAssoc(array $array): bool
8686
*
8787
* @return Traversable
8888
*/
89-
public static function toIterator($array): Traversable
89+
public static function toIterator(mixed $array): Traversable
9090
{
9191
if (!$array instanceof Traversable) {
9292
$array = new ArrayObject((array)$array);
@@ -98,12 +98,12 @@ public static function toIterator($array): Traversable
9898
/**
9999
* array data to object
100100
*
101-
* @param array|Traversable $array
101+
* @param Traversable|array $array
102102
* @param string $class
103103
*
104104
* @return mixed
105105
*/
106-
public static function toObject($array, string $class = stdClass::class)
106+
public static function toObject(Traversable|array $array, string $class = stdClass::class): mixed
107107
{
108108
$object = new $class;
109109

@@ -127,7 +127,7 @@ public static function toObject($array, string $class = stdClass::class)
127127
*
128128
* @return array|string
129129
*/
130-
public static function valueTrim(array $data)
130+
public static function valueTrim(array $data): array|string
131131
{
132132
if (is_scalar($data)) {
133133
return trim($data);
@@ -148,7 +148,7 @@ public static function valueTrim(array $data)
148148
*
149149
* @return bool
150150
*/
151-
public static function keyExists($key, array $arr): bool
151+
public static function keyExists(int|string $key, array $arr): bool
152152
{
153153
return array_key_exists(strtolower($key), array_change_key_case($arr));
154154
}
@@ -176,12 +176,12 @@ public static function valueToUpper(array $arr): array
176176
/**
177177
* 将数组中的值全部转为大写或小写
178178
*
179-
* @param array|iterable $arr
179+
* @param Traversable|array $arr
180180
* @param bool $toUpper
181181
*
182182
* @return array
183183
*/
184-
public static function changeValueCase($arr, bool $toUpper = true): array
184+
public static function changeValueCase(Traversable|array $arr, bool $toUpper = true): array
185185
{
186186
$function = $toUpper ? 'strtoupper' : 'strtolower';
187187
$newArr = []; //格式化后的数组
@@ -202,13 +202,13 @@ public static function changeValueCase($arr, bool $toUpper = true): array
202202
* ******* 检查 一个或多个值是否全部存在数组中 *******
203203
* 有一个不存在即返回 false
204204
*
205-
* @param string|array $check
205+
* @param array|string $check
206206
* @param array $sampleArr 只能检查一维数组
207207
* 注: 不分类型, 区分大小写 2 == '2' ‘a' != 'A'
208208
*
209209
* @return bool
210210
*/
211-
public static function valueExistsAll($check, array $sampleArr): bool
211+
public static function valueExistsAll(array|string $check, array $sampleArr): bool
212212
{
213213
// 以逗号分隔的会被拆开,组成数组
214214
if (is_string($check)) {
@@ -223,12 +223,12 @@ public static function valueExistsAll($check, array $sampleArr): bool
223223
* ******* 检查 一个或多个值是否存在数组中 *******
224224
* 有一个存在就返回 true 都不存在 return false
225225
*
226-
* @param string|array $check
226+
* @param array|string $check
227227
* @param array $sampleArr 只能检查一维数组
228228
*
229229
* @return bool
230230
*/
231-
public static function valueExistsOne($check, array $sampleArr): bool
231+
public static function valueExistsOne(array|string $check, array $sampleArr): bool
232232
{
233233
// 以逗号分隔的会被拆开,组成数组
234234
if (is_string($check)) {
@@ -243,13 +243,13 @@ public static function valueExistsOne($check, array $sampleArr): bool
243243
* ******* 不区分大小写,检查 一个或多个值是否 全存在数组中 *******
244244
* 有一个不存在即返回 false
245245
*
246-
* @param string|array $need
247-
* @param array|iterable $arr 只能检查一维数组
246+
* @param array|string $need
247+
* @param Traversable|array $arr 只能检查一维数组
248248
* @param bool $type 是否同时验证类型
249249
*
250250
* @return bool | string 不存在的会返回 检查到的 字段,判断时 请使用 ArrHelper::existsAll($need,$arr)===true 来验证是否全存在
251251
*/
252-
public static function existsAll($need, $arr, bool $type = false)
252+
public static function existsAll(array|string $need, Traversable|array $arr, bool $type = false): bool|string
253253
{
254254
if (is_array($need)) {
255255
foreach ((array)$need as $v) {
@@ -274,13 +274,13 @@ public static function existsAll($need, $arr, bool $type = false)
274274
* ******* 不区分大小写,检查 一个或多个值是否存在数组中 *******
275275
* 有一个存在就返回 true 都不存在 return false
276276
*
277-
* @param string|array $need
278-
* @param array|iterable $arr 只能检查一维数组
277+
* @param array|string $need
278+
* @param Traversable|array $arr 只能检查一维数组
279279
* @param bool $type 是否同时验证类型
280280
*
281281
* @return bool
282282
*/
283-
public static function existsOne($need, $arr, bool $type = false): bool
283+
public static function existsOne(array|string $need, Traversable|array $arr, bool $type = false): bool
284284
{
285285
if (is_array($need)) {
286286
foreach ((array)$need as $v) {
@@ -456,7 +456,7 @@ public static function dot(array $array, string $prepend = ''): array
456456
*
457457
* @return array
458458
*/
459-
public static function except(array $array, $keys): array
459+
public static function except(array $array, array|string $keys): array
460460
{
461461
static::forget($array, $keys);
462462

@@ -467,11 +467,11 @@ public static function except(array $array, $keys): array
467467
* Determine if the given key exists in the provided array.
468468
*
469469
* @param ArrayAccess|array $array
470-
* @param string|int $key
470+
* @param int|string $key
471471
*
472472
* @return bool
473473
*/
474-
public static function exists(array $array, $key): bool
474+
public static function exists(array $array, int|string $key): bool
475475
{
476476
if ($array instanceof ArrayAccess) {
477477
return $array->offsetExists($key);
@@ -515,7 +515,7 @@ public static function flatten(array $array, int $depth = INF): array
515515
*
516516
* @return void
517517
*/
518-
public static function forget(array &$array, $keys): void
518+
public static function forget(array &$array, array|string $keys): void
519519
{
520520
$original = &$array;
521521
$keys = (array)$keys;
@@ -554,12 +554,12 @@ public static function forget(array &$array, $keys): void
554554
/**
555555
* Check if an item or items exist in an array using "dot" notation.
556556
*
557-
* @param ArrayAccess|array $array
558-
* @param string|array $keys
557+
* @param Traversable|array $array
558+
* @param array|string $keys
559559
*
560560
* @return bool
561561
*/
562-
public static function has($array, $keys): bool
562+
public static function has(Traversable|array $array, array|string $keys): bool
563563
{
564564
if (null === $keys) {
565565
return false;
@@ -577,7 +577,6 @@ public static function has($array, $keys): bool
577577

578578
foreach ($keys as $key) {
579579
$subKeyArray = $array;
580-
581580
if (static::exists($array, $key)) {
582581
continue;
583582
}
@@ -599,11 +598,11 @@ public static function has($array, $keys): bool
599598
*
600599
* @param array $array
601600
* @param mixed $value
602-
* @param mixed $key
601+
* @param mixed|null $key
603602
*
604603
* @return array
605604
*/
606-
public static function prepend(array $array, $value, $key = null): array
605+
public static function prepend(array $array, mixed $value, mixed $key = null): array
607606
{
608607
if (null === $key) {
609608
array_unshift($array, $value);
@@ -617,13 +616,13 @@ public static function prepend(array $array, $value, $key = null): array
617616
/**
618617
* remove the $key of the $arr, and return value.
619618
*
620-
* @param string|int $key
619+
* @param int|string $key
621620
* @param array $arr
622-
* @param mixed $default
621+
* @param mixed|null $default
623622
*
624623
* @return mixed
625624
*/
626-
public static function remove(array &$arr, $key, $default = null)
625+
public static function remove(array &$arr, int|string $key, mixed $default = null): mixed
627626
{
628627
if (isset($arr[$key])) {
629628
$value = $arr[$key];
@@ -638,13 +637,13 @@ public static function remove(array &$arr, $key, $default = null)
638637
/**
639638
* Get a value from the array, and remove it.
640639
*
641-
* @param array|ArrayAccess $array
642-
* @param string|int $key
643-
* @param mixed $default
640+
* @param ArrayAccess|array $array
641+
* @param int|string $key
642+
* @param mixed|null $default
644643
*
645644
* @return mixed
646645
*/
647-
public static function pull(&$array, $key, $default = null)
646+
public static function pull(ArrayAccess|array $array, int|string $key, mixed $default = null): mixed
648647
{
649648
$value = static::get($array, $key, $default);
650649

@@ -661,7 +660,7 @@ public static function pull(&$array, $key, $default = null)
661660
*
662661
* @return array
663662
*/
664-
public static function only(array $array, $keys): array
663+
public static function only(array $array, array|string $keys): array
665664
{
666665
return array_intersect_key($array, array_flip((array)$keys));
667666
}
@@ -700,7 +699,7 @@ public static function where(array $array, callable $callback): array
700699
*
701700
* @return array
702701
*/
703-
public static function wrap($value): array
702+
public static function wrap(mixed $value): array
704703
{
705704
return !is_array($value) ? (array)$value : $value;
706705
}
@@ -712,7 +711,7 @@ public static function wrap($value): array
712711
/**
713712
* array 递归 转换成 字符串
714713
*
715-
* @param array $array
714+
* @param array $array
716715
* @param int $length
717716
* @param int $cycles 至多循环六次 $num >= 6
718717
* @param bool $showKey
@@ -723,7 +722,7 @@ public static function wrap($value): array
723722
* @return string
724723
*/
725724
public static function toString(
726-
$array,
725+
array $array,
727726
int $length = 800,
728727
int $cycles = 6,
729728
bool $showKey = true,
@@ -741,7 +740,7 @@ public static function toString(
741740
foreach ($array as $key => $value) {
742741
$num++;
743742

744-
if ($num >= $cycles || strlen($string) > (int)$length) {
743+
if ($num >= $cycles || strlen($string) > $length) {
745744
$string .= '... ...';
746745
break;
747746
}

src/Arr/Traits/ArrayValueGetSetTrait.php

+16-21
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ trait ArrayValueGetSetTrait
3838
*
3939
* @return array
4040
*/
41-
public static function add(array $array, $key, $value): array
41+
public static function add(array $array, string $key, mixed $value): array
4242
{
4343
if (static::has($array, $key)) {
4444
static::set($array, $key, $value);
@@ -51,21 +51,17 @@ public static function add(array $array, $key, $value): array
5151
* Get an item from an array using "dot" notation.
5252
*
5353
* @param ArrayAccess|array $array
54-
* @param string $key
55-
* @param mixed $default
54+
* @param string $key
55+
* @param mixed|null $default
5656
*
5757
* @return mixed
5858
*/
59-
public static function get($array, $key, $default = null)
59+
public static function get(ArrayAccess|array $array, string $key, mixed $default = null): mixed
6060
{
6161
if (!static::accessible($array)) {
6262
return Php::value($default);
6363
}
6464

65-
if (null === $key) {
66-
return $array;
67-
}
68-
6965
if (static::exists($array, $key)) {
7066
return $array[$key];
7167
}
@@ -91,16 +87,15 @@ public static function get($array, $key, $default = null)
9187
*
9288
* @return array
9389
*/
94-
public static function set(array &$array, $key, $value): array
90+
public static function set(array &$array, string $key, mixed $value): array
9591
{
96-
if (null === $key) {
97-
return ($array = $value);
98-
}
92+
// if (null === $key) {
93+
// return ($array = $value);
94+
// }
9995

10096
$keys = explode('.', $key);
101-
10297
while (count($keys) > 1) {
103-
$key = array_shift($keys);
98+
$key = (string)array_shift($keys);
10499
// If the key doesn't exist at this depth, we will just create an empty array
105100
// to hold the next value, allowing us to create the arrays to hold final
106101
// values at the correct depth. Then we'll keep digging into the array.
@@ -168,14 +163,14 @@ public static function gets(array &$data, array $needKeys = [], bool $unsetKey =
168163
* Get data from array or object by path.
169164
* Example: `DataCollector::getByPath($array, 'foo.bar.yoo')` equals to $array['foo']['bar']['yoo'].
170165
*
171-
* @param array|Traversable $data An array or object to get value.
166+
* @param Traversable|array $data An array or object to get value.
172167
* @param string $path The key path.
173-
* @param mixed $default
168+
* @param mixed|null $default
174169
* @param string $separator Separator of paths.
175170
*
176171
* @return mixed Found value, null if not exists.
177172
*/
178-
public static function getByPath($data, string $path, $default = null, string $separator = '.')
173+
public static function getByPath(Traversable|array $data, string $path, mixed $default = null, string $separator = '.'): mixed
179174
{
180175
if (isset($data[$path])) {
181176
return $data[$path];
@@ -206,11 +201,11 @@ public static function getByPath($data, string $path, $default = null, string $s
206201
*
207202
* @param array $data
208203
* @param array $nodes
209-
* @param mixed $default
204+
* @param mixed|null $default
210205
*
211206
* @return mixed
212207
*/
213-
public static function getValueByNodes(array $data, array $nodes, $default = null)
208+
public static function getValueByNodes(array $data, array $nodes, mixed $default = null): mixed
214209
{
215210
$temp = $data;
216211

@@ -229,12 +224,12 @@ public static function getValueByNodes(array $data, array $nodes, $default = nul
229224
/**
230225
* setByPath
231226
*
232-
* @param array|ArrayAccess &$data
227+
* @param ArrayAccess|array &$data
233228
* @param string $path
234229
* @param mixed $value
235230
* @param string $separator
236231
*/
237-
public static function setByPath(&$data, string $path, $value, string $separator = '.'): void
232+
public static function setByPath(ArrayAccess|array &$data, string $path, mixed $value, string $separator = '.'): void
238233
{
239234
if (!str_contains($path, $separator)) {
240235
$data[$path] = $value;

0 commit comments

Comments
 (0)