Skip to content

Fix count inferences #4149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ private function specifyTypesForCountFuncCall(
if (
$sizeType instanceof ConstantIntegerType
&& $sizeType->getValue() < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT
&& $isList->yes()
&& $arrayType->getKeyType()->isSuperTypeOf(IntegerRangeType::fromInterval(0, $sizeType->getValue() - 1))->yes()
) {
// turn optional offsets non-optional
Expand All @@ -1129,6 +1130,7 @@ private function specifyTypesForCountFuncCall(
$sizeType instanceof IntegerRangeType
&& $sizeType->getMin() !== null
&& $sizeType->getMin() < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT
&& $isList->yes()
&& $arrayType->getKeyType()->isSuperTypeOf(IntegerRangeType::fromInterval(0, ($sizeType->getMax() ?? $sizeType->getMin()) - 1))->yes()
) {
$builderData = [];
Expand Down Expand Up @@ -1174,7 +1176,7 @@ private function specifyTypesForCountFuncCall(
continue;
}

$resultTypes[] = $arrayType;
$resultTypes[] = TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
}

return $this->create($countFuncCall->getArgs()[0]->value, TypeCombinator::union(...$resultTypes), $context, $scope)->setRootExpr($rootExpr);
Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13111.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace Bug13111;

use function PHPStan\Testing\assertType;

/** @return array{0?: string, 1?: int} */
function foo() { return [1 => 8]; }

$b = foo();
if (count($b) === 1) {
assertType('non-empty-array{0?: string, 1?: int}', $b);
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/list-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ protected function testOptionalKeysInUnionListWithIntRange($row, $listRow, $twoO
protected function testOptionalKeysInUnionArrayWithIntRange($row, $twoOrThree): void
{
if (count($row) >= $twoOrThree) {
assertType('array{0: int, 1: string|null, 2?: int|null}', $row);
assertType('array{0: int, 1?: string|null, 2?: int|null, 3?: float|null}', $row);
} else {
assertType('array{0: int, 1?: string|null, 2?: int|null, 3?: float|null}|array{string}', $row);
}
Expand Down
Loading