Skip to content

Commit b293f1f

Browse files
committed
Simplify and generalise safe-guards
1 parent 29d5146 commit b293f1f

File tree

2 files changed

+4
-17
lines changed

2 files changed

+4
-17
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,10 +1087,7 @@ private function specifyTypesForCountFuncCall(
10871087
if (
10881088
$sizeType instanceof ConstantIntegerType
10891089
&& $sizeType->getValue() < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT
1090-
&& (
1091-
$isList->yes()
1092-
|| $isConstantArray->yes() && $arrayType->getKeyType()->isSuperTypeOf(IntegerRangeType::fromInterval(0, $sizeType->getValue() - 1))->yes()
1093-
)
1090+
&& $arrayType->getKeyType()->isSuperTypeOf(IntegerRangeType::fromInterval(0, $sizeType->getValue() - 1))->yes()
10941091
) {
10951092
// turn optional offsets non-optional
10961093
$valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty();
@@ -1107,28 +1104,18 @@ private function specifyTypesForCountFuncCall(
11071104
&& $sizeType->getMin() !== null
11081105
&& $sizeType->getMin() <= ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT
11091106
&& ($sizeType->getMax() === null || $sizeType->getMax() - $sizeType->getMin() <= ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT)
1110-
&& (
1111-
$isList->yes()
1112-
|| $isConstantArray->yes() && $arrayType->getKeyType()->isSuperTypeOf(IntegerRangeType::fromInterval(0, $sizeType->getMin() - 1))->yes()
1113-
)
1107+
&& $arrayType->getKeyType()->isSuperTypeOf(IntegerRangeType::fromInterval(0, $sizeType->getMin() - 1))->yes()
1108+
&& ($sizeType->getMax() === null || $arrayType->getKeyType()->isSuperTypeOf(IntegerRangeType::fromInterval(0, $sizeType->getMax() - 1))->yes())
11141109
) {
11151110
// turn optional offsets non-optional
11161111
$valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty();
11171112
for ($i = 0; $i < $sizeType->getMin(); $i++) {
11181113
$offsetType = new ConstantIntegerType($i);
1119-
$hasOffset = $arrayType->hasOffsetValueType($offsetType);
1120-
if ($hasOffset->no()) {
1121-
break;
1122-
}
11231114
$valueTypesBuilder->setOffsetValueType($offsetType, $arrayType->getOffsetValueType($offsetType));
11241115
}
11251116
if ($sizeType->getMax() !== null) {
11261117
for ($i = $sizeType->getMin(); $i < $sizeType->getMax(); $i++) {
11271118
$offsetType = new ConstantIntegerType($i);
1128-
$hasOffset = $arrayType->hasOffsetValueType($offsetType);
1129-
if ($hasOffset->no()) {
1130-
break;
1131-
}
11321119
$valueTypesBuilder->setOffsetValueType($offsetType, $arrayType->getOffsetValueType($offsetType), true);
11331120
}
11341121
} elseif ($arrayType->isConstantArray()->yes()) {

tests/PHPStan/Analyser/nsrt/list-count.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ protected function testOptionalKeysInUnionListWithIntRange($row, $listRow, $twoO
376376
}
377377

378378
if (count($row) >= $threeOrMoreInRangeLimit) {
379-
assertType('array{0: int, 1: string|null, 2: int|null, 3?: float|null}', $row);
379+
assertType('list{0: int, 1?: string|null, 2?: int|null, 3?: float|null}', $row);
380380
} else {
381381
assertType('array{string}|list{0: int, 1?: string|null, 2?: int|null, 3?: float|null}', $row);
382382
}

0 commit comments

Comments
 (0)