Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1063,10 +1063,12 @@ private function specifyTypesForCountFuncCall(

$isConstantArray = $type->isConstantArray();
$isList = $type->isList();
$positiveInt = IntegerRangeType::fromInterval(1, null);
if (
!$isNormalCount->yes()
|| (!$isConstantArray->yes() && !$isList->yes())
|| $type->isIterableAtLeastOnce()->no() // array{} cannot be used for further narrowing
|| !$positiveInt->isSuperTypeOf($sizeType)->yes()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would change if we also included zero here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to also work

) {
return null;
}
Expand Down
22 changes: 21 additions & 1 deletion tests/PHPStan/Analyser/nsrt/list-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ protected function testOptionalKeysInUnionListWithIntRange($row, $twoOrThree, $t
if (count($row) >= $maxThree) {
assertType('array{string}|list{0: int, 1?: string|null, 2?: int|null, 3?: float|null}', $row);
} else {
assertType('list{0: int, 1?: string|null, 2?: int|null, 3?: float|null}', $row);
assertType('array{string}|list{0: int, 1?: string|null, 2?: int|null, 3?: float|null}', $row);
}
}

Expand All @@ -386,3 +386,23 @@ protected function testOptionalKeysInUnionArrayWithIntRange($row, $twoOrThree):
}
}
}

class FooBug
{
public int $totalExpectedRows = 0;

/** @var list<\stdClass> */
public array $importedDaySummaryRows = [];

public function sayHello(): void
{
assertType('int', $this->totalExpectedRows);
assertType('list<stdClass>', $this->importedDaySummaryRows);
if ($this->totalExpectedRows !== count($this->importedDaySummaryRows)) {
assertType('int', $this->totalExpectedRows);
assertType('list<stdClass>', $this->importedDaySummaryRows);
}
assertType('int', $this->totalExpectedRows);
assertType('list<stdClass>', $this->importedDaySummaryRows);
}
}
Loading