Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1061,10 +1061,11 @@ private function specifyTypesForCountFuncCall(
$isNormalCount = (new ConstantIntegerType(COUNT_NORMAL))->isSuperTypeOf($mode)->result->or($type->getIterableValueType()->isArray()->negate());
}

$isConstantArray = $type->isConstantArray();
$isList = $type->isList();
if (
!$isNormalCount->yes()
|| (!$type->isConstantArray()->yes() && !$isList->yes())
|| (!$isConstantArray->yes() && !$isList->yes())
|| $type->isIterableAtLeastOnce()->no() // array{} cannot be used for further narrowing
) {
return null;
Expand All @@ -1082,9 +1083,12 @@ private function specifyTypesForCountFuncCall(
}

if (
$isList->yes()
&& $sizeType instanceof ConstantIntegerType
$sizeType instanceof ConstantIntegerType
&& $sizeType->getValue() < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT
&& (
$isList->yes()
|| $isConstantArray->yes() && $arrayType->getKeyType()->isSuperTypeOf(IntegerRangeType::fromInterval(0, $sizeType->getValue() - 1))->yes()
)
) {
// turn optional offsets non-optional
$valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty();
Expand All @@ -1097,9 +1101,12 @@ private function specifyTypesForCountFuncCall(
}

if (
$isList->yes()
&& $sizeType instanceof IntegerRangeType
$sizeType instanceof IntegerRangeType
&& $sizeType->getMin() !== null
&& (
$isList->yes()
|| $isConstantArray->yes() && $arrayType->getKeyType()->isSuperTypeOf(IntegerRangeType::fromInterval(0, $sizeType->getMin() - 1))->yes()
)
) {
// turn optional offsets non-optional
$valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty();
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/nsrt/count-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ public function doBaz(array $arr): void
assertType('1|2', count($arr));
}

public function constantArrayWhichCanBecomeList(string $h): void
{
preg_match('#^([a-z0-9-]+)\..+$#', $h, $matches);
if (count($matches) !== 2) {
return;
}

assertType('array{string, non-empty-string}', $matches);
}

}

/**
Expand Down
3 changes: 1 addition & 2 deletions tests/PHPStan/Analyser/nsrt/list-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,8 @@ protected function testOptionalKeysInUnionListWithIntRange($row, $twoOrThree, $t
*/
protected function testOptionalKeysInUnionArrayWithIntRange($row, $twoOrThree): void
{
// doesn't narrow because no list
if (count($row) >= $twoOrThree) {
assertType('array{0: int, 1?: string|null, 2?: int|null, 3?: float|null}', $row);
assertType('array{0: int, 1: string|null, 2?: int|null}', $row);
} else {
assertType('array{0: int, 1?: string|null, 2?: int|null, 3?: float|null}|array{string}', $row);
}
Expand Down
Loading