2020use function array_map ;
2121use function count ;
2222use function explode ;
23- use function gettype ;
2423use function is_array ;
2524use function is_numeric ;
26- use function is_object ;
2725use function iter \chunk ;
2826use function iter \mapWithKeys ;
2927use function iter \values ;
3028use function Safe \stream_get_contents ;
3129use function sprintf ;
30+ use function str_contains ;
3231use function str_replace ;
3332use const PHP_EOL ;
3433
3534final class ChunkedItemsIterator
3635{
37- /**
38- * @var list<string>|Iterator<string>
39- */
40- private iterable $ items ;
41-
4236 /**
4337 * @var Iterator<list<string>>
4438 */
45- private Iterator $ itemsChunks ;
39+ private readonly Iterator $ itemsChunks ;
4640
4741 /**
4842 * @var 0|positive-int|null
4943 */
50- private ?int $ numberOfItems ;
44+ private readonly ?int $ numberOfItems ;
5145
5246 /**
5347 * @internal Use the static factory methods instead.
5448 *
5549 * @param list<string>|Iterator<string> $items
5650 * @param positive-int $batchSize
5751 */
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+ ) {
6156 $ this ->itemsChunks = chunk ($ items , $ batchSize );
6257 $ this ->numberOfItems = is_array ($ items ) ? count ($ items ) : null ;
6358 }
@@ -138,18 +133,16 @@ private static function normalizeItems(array $items): array
138133 }
139134
140135 /**
141- * @param mixed $items
142- *
143136 * @return Iterator<string>
144137 */
145- private static function normalizeItemStream ($ items ): Iterator
138+ private static function normalizeItemStream (mixed $ items ): Iterator
146139 {
147140 Assert::isIterable (
148141 $ items ,
149142 sprintf (
150143 'Expected the fetched items to be a list or an iterable of strings. Got "%s". ' ,
151144 // TODO: use get_debug_type when dropping PHP 7.4 support
152- is_object ( $ items ) ? $ items ::class : gettype ($ items ),
145+ get_debug_type ($ items ),
153146 ),
154147 );
155148
@@ -161,11 +154,7 @@ private static function normalizeItemStream($items): Iterator
161154 );
162155 }
163156
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
169158 {
170159 if (is_numeric ($ item )) {
171160 return (string ) $ item ;
@@ -176,12 +165,12 @@ private static function normalizeItem($item, $index): string
176165 sprintf (
177166 '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". ' ,
178167 // TODO: use get_debug_type when dropping PHP 7.4 support
179- is_object ( $ item ) ? $ item ::class : gettype ($ item ),
168+ get_debug_type ($ item ),
180169 $ index ,
181170 ),
182171 );
183172 Assert::false (
184- '' !== PHP_EOL && false !== mb_strpos ($ item , PHP_EOL ),
173+ '' !== PHP_EOL && str_contains ($ item , PHP_EOL ),
185174 sprintf (
186175 'An item cannot contain a line return. Got one for "%s" for the item "%s". ' ,
187176 str_replace (PHP_EOL , '<lineReturn> ' , $ item ),
0 commit comments