Skip to content

Commit ca2ffe9

Browse files
committed
test limits
1 parent ad5734a commit ca2ffe9

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@
9393
final class TypeSpecifier
9494
{
9595

96+
private const MAX_ACCESSORIES_LIMIT = 8;
97+
9698
/** @var MethodTypeSpecifyingExtension[][]|null */
9799
private ?array $methodTypeSpecifyingExtensionsByClass = null;
98100

@@ -1203,12 +1205,19 @@ private function specifyTypesForCountFuncCall(
12031205
$intersection[] = new NonEmptyArrayType();
12041206

12051207
$zero = new ConstantIntegerType(0);
1208+
$i = 0;
12061209
foreach ($builderData as [$offsetType, $valueType]) {
12071210
// non-empty-list already implies the offset 0
12081211
if ($zero->isSuperTypeOf($offsetType)->yes()) {
12091212
continue;
12101213
}
1214+
1215+
if ($i > self::MAX_ACCESSORIES_LIMIT) {
1216+
break;
1217+
}
1218+
12111219
$intersection[] = new HasOffsetValueType($offsetType, $valueType);
1220+
$i++;
12121221
}
12131222

12141223
$resultTypes[] = TypeCombinator::intersect(...$intersection);

tests/PHPStan/Analyser/nsrt/bug-13747.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,27 @@ public function doBar($list): void
5151
assertType('1', count($list));
5252
}
5353
}
54+
55+
/** @param list<int> $list */
56+
public function checkLimit($list): void
57+
{
58+
if (count($list) === 0) {
59+
return;
60+
}
61+
62+
if (count($list) > 9) {
63+
assertType('non-empty-list<int>&hasOffsetValue(1, int)&hasOffsetValue(2, int)&hasOffsetValue(3, int)&hasOffsetValue(4, int)&hasOffsetValue(5, int)&hasOffsetValue(6, int)&hasOffsetValue(7, int)&hasOffsetValue(8, int)&hasOffsetValue(9, int)', $list);
64+
assertType('int<10, max>', count($list));
65+
} else {
66+
assertType('non-empty-list<int>', $list);
67+
}
68+
69+
if (count($list) > 10) {
70+
assertType('non-empty-list<int>&hasOffsetValue(1, int)&hasOffsetValue(2, int)&hasOffsetValue(3, int)&hasOffsetValue(4, int)&hasOffsetValue(5, int)&hasOffsetValue(6, int)&hasOffsetValue(7, int)&hasOffsetValue(8, int)&hasOffsetValue(9, int)', $list);
71+
assertType('int<11, max>', count($list));
72+
} else {
73+
assertType('non-empty-list<int>', $list);
74+
}
75+
76+
}
5477
}

0 commit comments

Comments
 (0)