@@ -22,12 +22,15 @@ final class Reduce
2222 */
2323 public static function toValue (iterable $ data , callable $ reducer , mixed $ initialValue = null ): mixed
2424 {
25+ /** @var mixed $carry */
2526 $ carry = $ initialValue ;
2627
2728 foreach ($ data as $ datum ) {
29+ /** @var mixed $datum */
2830 $ carry = $ reducer ($ carry , $ datum );
2931 }
3032
33+ /** @var T */
3134 return $ carry ;
3235 }
3336
@@ -49,13 +52,13 @@ public static function toMin(iterable $data, ?callable $compareBy = null): mixed
4952 if ($ compareBy !== null ) {
5053 return static ::toValue (
5154 $ data ,
52- fn ($ carry , $ datum ) => $ compareBy ($ datum ) < $ compareBy ($ carry ?? $ datum )
55+ fn (mixed $ carry , mixed $ datum ): mixed => $ compareBy ($ datum ) < $ compareBy ($ carry ?? $ datum )
5356 ? $ datum
5457 : $ carry ?? $ datum
5558 );
5659 }
5760
58- return static ::toValue ($ data , fn ($ carry , $ datum ) => \min ($ carry ?? $ datum , $ datum ));
61+ return static ::toValue ($ data , fn (mixed $ carry , mixed $ datum ): mixed => \min ($ carry ?? $ datum , $ datum ));
5962 }
6063
6164 /**
@@ -76,13 +79,13 @@ public static function toMax(iterable $data, ?callable $compareBy = null): mixed
7679 if ($ compareBy !== null ) {
7780 return static ::toValue (
7881 $ data ,
79- fn ($ carry , $ datum ) => $ compareBy ($ datum ) > $ compareBy ($ carry ?? $ datum )
82+ fn (mixed $ carry , mixed $ datum ): mixed => $ compareBy ($ datum ) > $ compareBy ($ carry ?? $ datum )
8083 ? $ datum
8184 : $ carry ?? $ datum
8285 );
8386 }
8487
85- return static ::toValue ($ data , fn ($ carry , $ datum ) => \max ($ carry ?? $ datum , $ datum ));
88+ return static ::toValue ($ data , fn (mixed $ carry , mixed $ datum ): mixed => \max ($ carry ?? $ datum , $ datum ));
8689 }
8790
8891 /**
@@ -103,7 +106,7 @@ public static function toMax(iterable $data, ?callable $compareBy = null): mixed
103106 public static function toMinMax (iterable $ numbers , ?callable $ compareBy = null ): array
104107 {
105108 if ($ compareBy !== null ) {
106- return static ::toValue ($ numbers , static function (array $ carry , $ datum ) use ($ compareBy ) {
109+ return static ::toValue ($ numbers , static function (array $ carry , mixed $ datum ) use ($ compareBy ): array {
107110 return [
108111 $ compareBy ($ datum ) <= $ compareBy ($ carry [0 ] ?? $ datum )
109112 ? $ datum
@@ -118,7 +121,7 @@ public static function toMinMax(iterable $numbers, ?callable $compareBy = null):
118121 return static ::toValue (
119122 $ numbers ,
120123 /** @param array{numeric|null, numeric|null} $carry */
121- fn (array $ carry , $ datum ) => [
124+ fn (array $ carry , mixed $ datum ): array => [
122125 \min ($ carry [0 ] ?? $ datum , $ datum ),
123126 \max ($ carry [1 ] ?? $ datum , $ datum )
124127 ],
@@ -139,7 +142,7 @@ public static function toCount(iterable $data): int
139142 return \count ($ data );
140143 }
141144
142- return static ::toValue ($ data , fn (int $ carry ) => $ carry + 1 , 0 );
145+ return static ::toValue ($ data , fn (int $ carry ): int => $ carry + 1 , 0 );
143146 }
144147
145148 /**
@@ -152,8 +155,8 @@ public static function toCount(iterable $data): int
152155 */
153156 public static function toSum (iterable $ data ): int |float
154157 {
155- /** @phpstan-ignore binaryOp.invalid */
156- return static ::toValue ($ data , fn ($ carry , $ datum ) => $ carry + $ datum , 0 );
158+ /** @psalm-suppress MixedOperand */
159+ return static ::toValue ($ data , fn (int | float $ carry , mixed $ datum ): int | float => $ carry + $ datum , 0 ); // @phpstan-ignore binaryOp.invalid
157160 }
158161
159162 /**
@@ -168,8 +171,8 @@ public static function toSum(iterable $data): int|float
168171 */
169172 public static function toProduct (iterable $ data ): int |float |null
170173 {
171- /** @phpstan-ignore binaryOp.invalid */
172- return static ::toValue ($ data , fn ($ carry , $ datum ) => ($ carry ?? 1 ) * $ datum );
174+ /** @psalm-suppress MixedOperand */
175+ return static ::toValue ($ data , fn (int | float | null $ carry , mixed $ datum ): int | float => ($ carry ?? 1 ) * $ datum ); // @phpstan-ignore binaryOp.invalid
173176 }
174177
175178 /**
@@ -188,16 +191,20 @@ public static function toAverage(iterable $data): int|float|null
188191 * @param int|float $datum
189192 * @return array{int, int|float}
190193 */
194+ /** @psalm-suppress MixedOperand */
191195 $ accumulator = static function (array $ carry , mixed $ datum ): array {
196+ /** @var int $count */
197+ /** @var int|float $sum */
192198 [$ count , $ sum ] = $ carry ;
193- /** @phpstan-ignore binaryOp.invalid, binaryOp.invalid */
199+ /** @phpstan-ignore binaryOp.invalid */
194200 return [$ count + 1 , $ sum + $ datum ];
195201 };
196202
197203 /** @var array{int, int|float} $result */
198204 $ result = static ::toValue ($ data , $ accumulator , [0 , 0 ]);
199205 [$ count , $ sum ] = $ result ;
200206
207+ /** @psalm-suppress InvalidOperand */
201208 return $ count ? ($ sum / $ count ) : null ;
202209 }
203210
@@ -215,8 +222,10 @@ public static function toAverage(iterable $data): int|float|null
215222 */
216223 public static function toString (iterable $ data , string $ separator = '' , string $ prefix = '' , string $ suffix = '' ): string
217224 {
225+ /** @var list<string|int|float> $items */
218226 $ items = [];
219227 foreach ($ data as $ datum ) {
228+ /** @var string|int|float $datum */
220229 $ items [] = $ datum ;
221230 }
222231
@@ -237,6 +246,7 @@ public static function toRange(iterable $numbers): int|float
237246 {
238247 [$ min , $ max ] = static ::toMinMax ($ numbers );
239248
249+ /** @psalm-suppress InvalidOperand */
240250 return ($ max ?? 0 ) - ($ min ?? 0 );
241251 }
242252
@@ -251,6 +261,7 @@ public static function toRange(iterable $numbers): int|float
251261 public static function toFirst (iterable $ data ): mixed
252262 {
253263 foreach ($ data as $ datum ) {
264+ /** @var mixed $datum */
254265 return $ datum ;
255266 }
256267
@@ -268,7 +279,7 @@ public static function toFirst(iterable $data): mixed
268279 public static function toLast (iterable $ data ): mixed
269280 {
270281 /** @var mixed|NoValueMonad $result */
271- $ result = static ::toValue ($ data , fn ($ carry , $ datum ) => $ datum , NoValueMonad::getInstance ());
282+ $ result = static ::toValue ($ data , fn (mixed $ carry , mixed $ datum ): mixed => $ datum , NoValueMonad::getInstance ());
272283
273284 if ($ result instanceof NoValueMonad) {
274285 throw new \LengthException ('collection is empty ' );
@@ -310,6 +321,7 @@ public static function toRandomValue(iterable $data): mixed
310321
311322 $ index = 0 ;
312323 foreach ($ data as $ datum ) {
324+ /** @var mixed $datum */
313325 if ($ targetIndex === $ index ) {
314326 return $ datum ;
315327 }
0 commit comments