Skip to content

Commit 0492220

Browse files
authored
Cover non-empty-array in array_combine
1 parent 3166ba3 commit 0492220

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/Type/Php/ArrayCombineFunctionReturnTypeExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
use PHPStan\Php\PhpVersion;
99
use PHPStan\Reflection\FunctionReflection;
1010
use PHPStan\Reflection\ParametersAcceptorSelector;
11+
use PHPStan\Type\Accessory\NonEmptyArrayType;
1112
use PHPStan\Type\ArrayType;
1213
use PHPStan\Type\Constant\ConstantArrayType;
1314
use PHPStan\Type\Constant\ConstantBooleanType;
1415
use PHPStan\Type\Constant\ConstantIntegerType;
1516
use PHPStan\Type\Constant\ConstantStringType;
1617
use PHPStan\Type\MixedType;
1718
use PHPStan\Type\Type;
19+
use PHPStan\Type\TypeCombinator;
1820
use PHPStan\Type\UnionType;
1921

2022
class ArrayCombineFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
@@ -69,6 +71,10 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
6971
$valuesParamType instanceof ArrayType ? $valuesParamType->getItemType() : new MixedType()
7072
);
7173

74+
if ($keysParamType->isIterableAtLeastOnce()->yes() && $valuesParamType->isIterableAtLeastOnce()->yes()) {
75+
$arrayType = TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
76+
}
77+
7278
if ($this->phpVersion->throwsTypeErrorForInternalFunctions()) {
7379
return $arrayType;
7480
}

tests/PHPStan/Analyser/data/non-empty-array.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ public function doFoo(
3434
assertType('mixed', $invalidList2);
3535
}
3636

37+
/**
38+
* @param non-empty-array $array
39+
* @param non-empty-list $list
40+
*/
41+
public function arrayFunctions($array, $list): void
42+
{
43+
assertType('array&nonEmpty', array_combine($array, $array));
44+
assertType('array&nonEmpty', array_combine($list, $list));
45+
}
3746
}

0 commit comments

Comments
 (0)