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
26 changes: 25 additions & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,23 @@ private function specifyTypesForCountFuncCall(
return null;
}

if ($context->falsey() && $isConstantArray->yes()) {
$remainingSize = TypeCombinator::remove($type->getArraySize(), $sizeType);
if (!$remainingSize instanceof NeverType) {
$result = $this->specifyTypesForCountFuncCall(
$countFuncCall,
$type,
$remainingSize,
$context->negate(),
$scope,
$rootExpr,
);
if ($result !== null) {
return $result;
}
}
}

$resultTypes = [];
foreach ($type->getArrays() as $arrayType) {
$isSizeSuperTypeOfArraySize = $sizeType->isSuperTypeOf($arrayType->getArraySize());
Expand Down Expand Up @@ -1372,7 +1389,14 @@ private function specifyTypesForCountFuncCall(
$builder->setOffsetValueType($offsetType, $valueType, $optional);
}

$resultTypes[] = $builder->getArray();
$builtArray = $builder->getArray();
if ($isList->yes() && !$builder->isList()) {
$constantArrays = $builtArray->getConstantArrays();
if (count($constantArrays) === 1) {
$builtArray = $constantArrays[0]->makeList();
}
}
$resultTypes[] = $builtArray;
continue;
}

Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14297.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace Bug14297;

use function PHPStan\Testing\assertType;

function (): void {
$a = [rand(0, 1) ? 'a' : null];
if (rand(0, 1)) {
$a[] = rand(0, 1) ? 'b' : null;
}

$a = array_values(array_filter($a));
if (count($a) === 0) {
return;
}

assertType("non-empty-list{0?: 'a'|'b', 1?: 'b'}", $a);
assertType("int<1, 2>", count($a));

if (count($a) === 2) {
assertType("array{'a'|'b', 'b'}", $a);
} else {
assertType("array{'a'|'b'}", $a);
}
};
10 changes: 5 additions & 5 deletions tests/PHPStan/Analyser/nsrt/list-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ protected function testOptionalKeys($row): void
if (count($row) === 1) {
assertType('array{mixed}', $row);
} else {
assertType('array{0: mixed, 1?: string|null}', $row);
assertType('array{mixed, string|null}', $row);
}

if (count($row) === 2) {
assertType('array{mixed, string|null}', $row);
} else {
assertType('array{0: mixed, 1?: string|null}', $row);
assertType('array{mixed}', $row);
}

if (count($row) === 3) {
Expand All @@ -263,13 +263,13 @@ protected function testOptionalKeysInUnion($row): void
if (count($row) === 1) {
assertType('array{mixed}', $row);
} else {
assertType('array{0: mixed, 1?: string|null}', $row);
assertType('array{mixed, string|null}', $row);
}

if (count($row) === 2) {
assertType('array{mixed, string|null}', $row);
} else {
assertType('array{0: mixed, 1?: string|null}', $row);
assertType('array{mixed}', $row);
}

if (count($row) === 3) {
Expand All @@ -293,7 +293,7 @@ protected function testOptionalKeysInListsOfTaggedUnion($row): void
if (count($row) === 1) {
assertType('array{0: int, 1?: string|null}|array{string}', $row);
} else {
assertType('array{0: int, 1?: string|null}', $row);
assertType('array{int, string|null}', $row);
}

if (count($row) === 2) {
Expand Down
Loading