Skip to content

Commit 9b98540

Browse files
committed
Make array_count_values return non-empty-array types.
1 parent f247414 commit 9b98540

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/Type/Php/ArrayCountValuesDynamicReturnTypeExtension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\AutowiredService;
88
use PHPStan\Reflection\FunctionReflection;
9+
use PHPStan\Type\Accessory\NonEmptyArrayType;
910
use PHPStan\Type\ArrayType;
1011
use PHPStan\Type\Constant\ConstantArrayType;
1112
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
@@ -56,9 +57,9 @@ public function getTypeFromFunctionCall(
5657
continue;
5758
}
5859

59-
$outputTypes[] = new ArrayType(
60-
$itemType,
61-
IntegerRangeType::fromInterval(1, null),
60+
$outputTypes[] = TypeCombinator::intersect(
61+
new ArrayType($itemType, IntegerRangeType::fromInterval(1, null)),
62+
new NonEmptyArrayType(),
6263
);
6364
}
6465

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
$ints = array_count_values([1, 2, 2, 3]);
88

9-
assertType('array<1|2|3, int<1, max>>', $ints);
9+
assertType('non-empty-array<1|2|3, int<1, max>>', $ints);
1010

1111
$strings = array_count_values(['one', 'two', 'two', 'three']);
1212

13-
assertType('array<\'one\'|\'three\'|\'two\', int<1, max>>', $strings);
13+
assertType('non-empty-array<\'one\'|\'three\'|\'two\', int<1, max>>', $strings);
1414

1515
$objects = array_count_values([new \stdClass()]);
1616

@@ -25,7 +25,7 @@ function returnsStringOrObjectArray(): array
2525
}
2626

2727
// Objects are ignored by array_count_values, with a warning emitted.
28-
assertType('array<string, int<1, max>>', array_count_values(returnsStringOrObjectArray()));
28+
assertType('non-empty-array<string, int<1, max>>', array_count_values(returnsStringOrObjectArray()));
2929

3030
class StringableObject
3131
{
@@ -40,4 +40,4 @@ public function __toString(): string
4040
// Stringable objects are ignored by array_count_values, with a warning emitted.
4141
$stringable = array_count_values([new StringableObject(), 'string', 1]);
4242

43-
assertType('array<1|\'string\', int<1, max>>', $stringable);
43+
assertType('non-empty-array<1|\'string\', int<1, max>>', $stringable);

0 commit comments

Comments
 (0)