Skip to content

Commit 7621e11

Browse files
VincentLangletondrejmirtes
authored andcommitted
Fix array_sum return type
1 parent af20741 commit 7621e11

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/Type/Php/ArraySumFunctionDynamicReturnTypeExtension.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,21 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3535
return new ConstantIntegerType(0);
3636
}
3737

38+
$intUnionFloat = new UnionType([new IntegerType(), new FloatType()]);
39+
3840
if ($arrayType->isIterableAtLeastOnce()->yes()) {
39-
return TypeCombinator::intersect($itemType, new UnionType([new IntegerType(), new FloatType()]));
41+
if ($intUnionFloat->isSuperTypeOf($itemType)->yes()) {
42+
return $itemType;
43+
}
44+
45+
return $intUnionFloat;
46+
}
47+
48+
if ($intUnionFloat->isSuperTypeOf($itemType)->yes()) {
49+
return TypeCombinator::union(new ConstantIntegerType(0), $itemType);
4050
}
4151

42-
return TypeCombinator::union(
43-
new ConstantIntegerType(0),
44-
TypeCombinator::intersect($itemType, new UnionType([new IntegerType(), new FloatType()]))
45-
);
52+
return TypeCombinator::union(new ConstantIntegerType(0), $intUnionFloat);
4653
}
4754

4855
}

tests/PHPStan/Analyser/data/array-sum.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,12 @@ function foo4($list)
3939
$sum = array_sum($list);
4040
assertType('float|int', $sum);
4141
}
42+
43+
/**
44+
* @param string[] $list
45+
*/
46+
function foo5($list)
47+
{
48+
$sum = array_sum($list);
49+
assertType('float|int', $sum);
50+
}

0 commit comments

Comments
 (0)