Skip to content

Commit 7841d8f

Browse files
committed
feat: modernize code for php8+
1 parent 35850f3 commit 7841d8f

9 files changed

+132
-328
lines changed

src/Arrayizes.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait Arrayizes
2121
*
2222
* @return array
2323
*/
24-
public function asArray($data, $cast = true)
24+
public function asArray(mixed $data, bool $cast = true): array
2525
{
2626
if (\is_array($data)) {
2727
return $data;
@@ -41,7 +41,7 @@ public function asArray($data, $cast = true)
4141
return $data->jsonSerialize();
4242
}
4343

44-
if (\method_exists($data, 'toArray')) {
44+
if (\is_object($data) && \method_exists($data, 'toArray')) {
4545
return $data->toArray();
4646
}
4747

@@ -50,17 +50,11 @@ public function asArray($data, $cast = true)
5050

5151
/**
5252
* Convert the data items to array.
53-
*
54-
* @return array
5553
*/
56-
public function toArray()
54+
public function toArray(): array
5755
{
58-
return \array_map(function ($value) {
59-
if (\is_scalar($value)) {
60-
return $value;
61-
}
62-
63-
return $this->asArray($value, false);
64-
}, $this->getData());
56+
return \array_map(
57+
fn ($value) => \is_scalar($value) ? $value : $this->asArray($value, false), $this->getData()
58+
);
6559
}
6660
}

src/HigherOrderMessage.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,17 @@
1818
*/
1919
class HigherOrderMessage
2020
{
21-
protected $underscore;
22-
protected $method;
23-
24-
public function __construct(UnderscoreBase $underscore, $method)
21+
public function __construct(protected UnderscoreBase $underscore, protected string $method)
2522
{
26-
$this->underscore = $underscore;
27-
$this->method = $method;
2823
}
2924

30-
public function __call($method, $args)
25+
public function __call(string $method, array $args): mixed
3126
{
32-
return $this->underscore->{$this->method}(function ($item) use ($method, $args) {
33-
return \call_user_func_array([$item, $method], $args);
34-
});
27+
return $this->underscore->{$this->method}(static fn ($item) => \call_user_func_array([$item, $method], $args));
3528
}
3629

37-
public function __get($prop)
30+
public function __get($prop): mixed
3831
{
39-
return $this->underscore->{$this->method}(function ($item) use ($prop) {
40-
$props = \array_column([$item], $prop);
41-
42-
return empty($props) ? null : $props[0];
43-
});
32+
return $this->underscore->{$this->method}(static fn ($item) => \array_column([$item], $prop)[0] ?? null);
4433
}
4534
}

src/Underscore.php

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class Underscore extends UnderscoreFunction
1818
*
1919
* @param array|mixed $data
2020
*/
21-
public function __construct($data = [])
21+
public function __construct(mixed $data = [])
2222
{
2323
parent::__construct($data);
2424
}
@@ -30,44 +30,31 @@ public function __construct($data = [])
3030
*
3131
* @return self
3232
*/
33-
public static function _($data = null)
33+
public static function _($data = null): self
3434
{
3535
return new static($data);
3636
}
3737

3838
/**
3939
* Generates a function that always returns a constant value.
40-
*
41-
* @param mixed $value
42-
*
43-
* @return callable
4440
*/
45-
public function constant($value)
41+
public function constant(mixed $value): callable
4642
{
47-
return function () use ($value) {
48-
return $value;
49-
};
43+
return fn () => $value;
5044
}
5145

5246
/**
5347
* No operation!
54-
*
55-
* @return void
5648
*/
57-
public function noop()
49+
public function noop(): void
5850
{
5951
// ;)
6052
}
6153

6254
/**
6355
* Run callable n times and create new collection.
64-
*
65-
* @param int $n
66-
* @param callable $fn
67-
*
68-
* @return self
6956
*/
70-
public function times($n, callable $fn)
57+
public function times(int $n, callable $fn): self
7158
{
7259
$data = [];
7360

@@ -80,25 +67,16 @@ public function times($n, callable $fn)
8067

8168
/**
8269
* Return a random integer between min and max (inclusive).
83-
*
84-
* @param int $min
85-
* @param int $max
86-
*
87-
* @return int
8870
*/
89-
public function random($min, $max)
71+
public function random(int $min, int $max): int
9072
{
9173
return \mt_rand($min, $max);
9274
}
9375

9476
/**
9577
* Generate unique ID (unique for current go/session).
96-
*
97-
* @param string $prefix
98-
*
99-
* @return string
10078
*/
101-
public function uniqueId($prefix = '')
79+
public function uniqueId(string $prefix = ''): string
10280
{
10381
static $id = 0;
10482

src/UnderscoreAliases.php

Lines changed: 16 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,190 +15,128 @@ trait UnderscoreAliases
1515
{
1616
/**
1717
* Alias of first().
18-
*
19-
* @param int $n
20-
*
21-
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
2218
*/
23-
public function head($n = 1)
19+
public function head(int $n = 1)
2420
{
2521
return $this->first($n);
2622
}
2723

2824
/**
2925
* Alias of first().
30-
*
31-
* @param int $n
32-
*
33-
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
3426
*/
35-
public function take($n = 1)
27+
public function take(int $n = 1)
3628
{
3729
return $this->first($n);
3830
}
3931

4032
/**
4133
* Alias of last().
42-
*
43-
* @param int $n
44-
*
45-
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
4634
*/
47-
public function tail($n = 1)
35+
public function tail(int $n = 1)
4836
{
4937
return $this->last($n);
5038
}
5139

5240
/**
5341
* Alias of last().
54-
*
55-
* @param int $n
56-
*
57-
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
5842
*/
59-
public function drop($n = 1)
43+
public function drop(int $n = 1)
6044
{
6145
return $this->last($n);
6246
}
6347

6448
/**
6549
* Alias of unique().
66-
*
67-
* @param callable|string $fn The callback. String is resolved to value of that index.
68-
*
69-
* @return self
7050
*/
71-
public function uniq($fn = null)
51+
public function uniq($fn = null): self
7252
{
7353
return $this->unique($fn);
7454
}
7555

7656
/**
7757
* Alias of difference().
78-
*
79-
* @param array|mixed $data Array or array like or array convertible.
80-
*
81-
* @return self
8258
*/
83-
public function without($data)
59+
public function without(mixed $data): self
8460
{
8561
return $this->difference($data);
8662
}
8763

8864
/**
8965
* Alias of map().
90-
*
91-
* @param callable $fn The callback.
92-
*
93-
* @return self
9466
*/
95-
public function collect(callable $fn)
67+
public function collect(callable $fn): self
9668
{
9769
return $this->map($fn);
9870
}
9971

10072
/**
10173
* Alias of reduce().
102-
*
103-
* @param callable $fn The callback.
104-
* @param mixed $memo The initial value carried over to each iteration and returned finally.
105-
*
106-
* @return mixed
10774
*/
108-
public function foldl(callable $fn, $memo)
75+
public function foldl(callable $fn, mixed $memo): mixed
10976
{
11077
return $this->reduce($fn, $memo);
11178
}
11279

11380
/**
11481
* Alias of reduce().
115-
*
116-
* @param callable $fn The callback.
117-
* @param mixed $memo The initial value carried over to each iteration and returned finally.
118-
*
119-
* @return mixed
12082
*/
121-
public function inject(callable $fn, $memo)
83+
public function inject(callable $fn, mixed $memo): mixed
12284
{
12385
return $this->reduce($fn, $memo);
12486
}
12587

12688
/**
12789
* Alias of reduceRight().
128-
*
129-
* @param callable $fn The callback.
130-
* @param mixed $memo The initial value carried over to each iteration and returned finally.
131-
*
132-
* @return mixed
13390
*/
134-
public function foldr(callable $fn, $memo)
91+
public function foldr(callable $fn, mixed $memo): mixed
13592
{
13693
return $this->reduceRight($fn, $memo);
13794
}
13895

13996
/**
14097
* Alias of find().
141-
*
142-
* @param callable $fn The truth test callback.
143-
* @param bool $useValue Whether to return value or the index on match.
144-
*
145-
* @return mixed|null
14698
*/
147-
public function detect(callable $fn)
99+
public function detect(callable $fn): mixed
148100
{
149101
return $this->find($fn);
150102
}
151103

152104
/**
153105
* Alias of filter().
154-
*
155-
* @param callable|string|null $fn The truth test callback.
156-
*
157-
* @return self
158106
*/
159-
public function select(callable $fn = null)
107+
public function select(callable $fn = null): self
160108
{
161109
return $this->filter($fn);
162110
}
163111

164112
/**
165113
* Alias of every().
166-
*
167-
* @param callable $fn The truth test callback.
168-
*
169-
* @return bool
170114
*/
171-
public function all(callable $fn)
115+
public function all(callable $fn): bool
172116
{
173117
return $this->every($fn);
174118
}
175119

176120
/**
177121
* Alias of some().
178-
*
179-
* @param callable $fn The truth test callback.
180-
*
181-
* @return bool
182122
*/
183-
public function any(callable $fn)
123+
public function any(callable $fn): bool
184124
{
185125
return $this->some($fn);
186126
}
187127

188128
/**
189129
* Alias of contains().
190130
*/
191-
public function includes($item)
131+
public function includes(mixed $item): bool
192132
{
193133
return $this->contains($item);
194134
}
195135

196136
/**
197137
* Alias of count().
198-
*
199-
* @return int
200138
*/
201-
public function size()
139+
public function size(): int
202140
{
203141
return $this->count();
204142
}

0 commit comments

Comments
 (0)