Skip to content

Commit 8aa9b78

Browse files
Fix ArrayCountValues
1 parent 6284186 commit 8aa9b78

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/Type/Php/ArrayCountValuesDynamicReturnTypeExtension.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use PHPStan\Type\ArrayType;
1111
use PHPStan\Type\Constant\ConstantArrayType;
1212
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
13-
use PHPStan\Type\ErrorType;
1413
use PHPStan\Type\IntegerRangeType;
14+
use PHPStan\Type\IntegerType;
15+
use PHPStan\Type\NeverType;
16+
use PHPStan\Type\StringType;
1517
use PHPStan\Type\Type;
1618
use PHPStan\Type\TypeCombinator;
1719
use PHPStan\Type\UnionType;
@@ -43,17 +45,11 @@ public function getTypeFromFunctionCall(
4345
$arrayTypes = $inputType->getArrays();
4446

4547
$outputTypes = [];
48+
$allowedValues = new UnionType([new IntegerType(), new StringType()]);
4649

4750
foreach ($arrayTypes as $arrayType) {
48-
$itemType = $arrayType->getItemType();
49-
50-
if ($itemType instanceof UnionType) {
51-
$itemType = $itemType->filterTypes(
52-
static fn ($type) => !$type->toArrayKey() instanceof ErrorType,
53-
);
54-
}
55-
56-
if ($itemType->toArrayKey() instanceof ErrorType) {
51+
$itemType = TypeCombinator::intersect($arrayType->getItemType(), $allowedValues);
52+
if ($itemType instanceof NeverType) {
5753
continue;
5854
}
5955

tests/PHPStan/Analyser/nsrt/array-count-values.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@ public function __toString(): string
4141
$stringable = array_count_values([new StringableObject(), 'string', 1]);
4242

4343
assertType('non-empty-array<1|\'string\', int<1, max>>', $stringable);
44+
45+
// Booleans, floats and null are ignored by array_count_values even if they can be cast to array key.
46+
$scalar = array_count_values([true, 1.0, false, 0.0, null]);
47+
48+
assertType('array{}', $scalar);

0 commit comments

Comments
 (0)