20
20
use function array_map ;
21
21
use function count ;
22
22
use function explode ;
23
- use function gettype ;
24
23
use function is_array ;
25
24
use function is_numeric ;
26
- use function is_object ;
27
25
use function iter \chunk ;
28
26
use function iter \mapWithKeys ;
29
27
use function iter \values ;
30
28
use function Safe \stream_get_contents ;
31
29
use function sprintf ;
30
+ use function str_contains ;
32
31
use function str_replace ;
33
32
use const PHP_EOL ;
34
33
35
34
final class ChunkedItemsIterator
36
35
{
37
- /**
38
- * @var list<string>|Iterator<string>
39
- */
40
- private iterable $ items ;
41
-
42
36
/**
43
37
* @var Iterator<list<string>>
44
38
*/
45
- private Iterator $ itemsChunks ;
39
+ private readonly Iterator $ itemsChunks ;
46
40
47
41
/**
48
42
* @var 0|positive-int|null
49
43
*/
50
- private ?int $ numberOfItems ;
44
+ private readonly ?int $ numberOfItems ;
51
45
52
46
/**
53
47
* @internal Use the static factory methods instead.
54
48
*
55
49
* @param list<string>|Iterator<string> $items
56
50
* @param positive-int $batchSize
57
51
*/
58
- public function __construct (iterable $ items , int $ batchSize )
59
- {
60
- $ this ->items = $ items ;
52
+ public function __construct (
53
+ private readonly iterable $ items ,
54
+ int $ batchSize ,
55
+ ) {
61
56
$ this ->itemsChunks = chunk ($ items , $ batchSize );
62
57
$ this ->numberOfItems = is_array ($ items ) ? count ($ items ) : null ;
63
58
}
@@ -138,18 +133,16 @@ private static function normalizeItems(array $items): array
138
133
}
139
134
140
135
/**
141
- * @param mixed $items
142
- *
143
136
* @return Iterator<string>
144
137
*/
145
- private static function normalizeItemStream ($ items ): Iterator
138
+ private static function normalizeItemStream (mixed $ items ): Iterator
146
139
{
147
140
Assert::isIterable (
148
141
$ items ,
149
142
sprintf (
150
143
'Expected the fetched items to be a list or an iterable of strings. Got "%s". ' ,
151
144
// TODO: use get_debug_type when dropping PHP 7.4 support
152
- is_object ( $ items ) ? $ items ::class : gettype ($ items ),
145
+ get_debug_type ($ items ),
153
146
),
154
147
);
155
148
@@ -161,11 +154,7 @@ private static function normalizeItemStream($items): Iterator
161
154
);
162
155
}
163
156
164
- /**
165
- * @param mixed $item
166
- * @param array-key $index
167
- */
168
- private static function normalizeItem ($ item , $ index ): string
157
+ private static function normalizeItem (mixed $ item , int |string $ index ): string
169
158
{
170
159
if (is_numeric ($ item )) {
171
160
return (string ) $ item ;
@@ -176,12 +165,12 @@ private static function normalizeItem($item, $index): string
176
165
sprintf (
177
166
'The items are potentially passed to the child processes via the STDIN. For this reason they are expected to be string values. Got "%s" for the item "%s". ' ,
178
167
// TODO: use get_debug_type when dropping PHP 7.4 support
179
- is_object ( $ item ) ? $ item ::class : gettype ($ item ),
168
+ get_debug_type ($ item ),
180
169
$ index ,
181
170
),
182
171
);
183
172
Assert::false (
184
- '' !== PHP_EOL && false !== mb_strpos ($ item , PHP_EOL ),
173
+ '' !== PHP_EOL && str_contains ($ item , PHP_EOL ),
185
174
sprintf (
186
175
'An item cannot contain a line return. Got one for "%s" for the item "%s". ' ,
187
176
str_replace (PHP_EOL , '<lineReturn> ' , $ item ),
0 commit comments