Skip to content

Commit f074ee3

Browse files
authored
[TypeDeclaration] Handle crash on combine union types on AddClosureParamTypeForArrayReduceRector (#7636)
1 parent 39027f6 commit f074ee3

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayReduceRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayReduceRector\Source\PaymentPeriod;
6+
7+
final class CombineUnion
8+
{
9+
/**
10+
* @param array<PaymentPeriod> $arr
11+
*/
12+
public function run(array $arr)
13+
{
14+
$sumCents = \array_reduce($arr, function ($carry, PaymentPeriod $paymentPeriod) : float|int {
15+
return $carry + $paymentPeriod->getSum();
16+
}, 0);
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayReduceRector\Fixture;
25+
26+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayReduceRector\Source\PaymentPeriod;
27+
28+
final class CombineUnion
29+
{
30+
/**
31+
* @param array<PaymentPeriod> $arr
32+
*/
33+
public function run(array $arr)
34+
{
35+
$sumCents = \array_reduce($arr, function (int|float $carry, PaymentPeriod $paymentPeriod) : float|int {
36+
return $carry + $paymentPeriod->getSum();
37+
}, 0);
38+
}
39+
}
40+
41+
?>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayReduceRector\Source;
4+
5+
interface PaymentPeriod {
6+
public function getSum() : float|null;
7+
}

rules/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayReduceRector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,16 @@ private function combineTypes(array $types): ?Type
178178
return $types[0];
179179
}
180180

181-
foreach ($types as $type) {
181+
foreach ($types as $key => $type) {
182182
if ($type instanceof UnionType) {
183183
foreach ($type->getTypes() as $unionedType) {
184184
if ($unionedType instanceof IntersectionType) {
185185
return null;
186186
}
187187
}
188+
189+
$types = array_merge($types, $type->getTypes());
190+
unset($types[$key]);
188191
}
189192
}
190193

0 commit comments

Comments
 (0)