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
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();
$zeroOrMore = IntegerRangeType::fromInterval(0, null);
if (
!$isNormalCount->yes()
|| (!$isConstantArray->yes() && !$isList->yes())
|| $type->isIterableAtLeastOnce()->no() // array{} cannot be used for further narrowing
|| !$zeroOrMore->isSuperTypeOf($sizeType)->yes()
) {
return null;
}
Expand Down
45 changes: 44 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,46 @@ 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);
}
}

class FooBugPositiveInt
{
/**
* @var positive-int
*/
public int $totalExpectedRows = 1;

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

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