Skip to content

Commit 2140696

Browse files
committedMar 10, 2025
Avoid specifying incorrect falsey context
1 parent 1a20efd commit 2140696

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed
 

Diff for: ‎src/Analyser/TypeSpecifier.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1071,10 +1071,10 @@ private function specifyTypesForCountFuncCall(
10711071
}
10721072

10731073
$isSizeSuperTypeOfArraySize = $sizeType->isSuperTypeOf($type->getArraySize());
1074-
if ($context->truthy() && $isSizeSuperTypeOfArraySize->no()) {
1074+
if ($isSizeSuperTypeOfArraySize->no()) {
10751075
return new NeverType();
10761076
}
1077-
if ($context->falsey() && !$isSizeSuperTypeOfArraySize->yes()) {
1077+
if ($context->falsey() && $isSizeSuperTypeOfArraySize->maybe()) {
10781078
return new NeverType();
10791079
}
10801080

@@ -1123,10 +1123,10 @@ private function specifyTypesForCountFuncCall(
11231123
return $valueTypesBuilder->getArray();
11241124
}
11251125

1126-
return $type;
1126+
return $context->truthy() ? $type : new NeverType();
11271127
}
11281128

1129-
return TypeCombinator::intersect($type, new NonEmptyArrayType());
1129+
return $context->truthy() ? TypeCombinator::intersect($type, new NonEmptyArrayType()) : new NeverType();
11301130
});
11311131

11321132
return $this->create($countFuncCall->getArgs()[0]->value, $resultType, $context, $scope)->setRootExpr($rootExpr);

Diff for: ‎tests/PHPStan/Analyser/nsrt/bug11480.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function intUnionCount(): void
8484
if (count($x) >= $count) {
8585
assertType("array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
8686
} else {
87-
assertType("array{}", $x);
87+
assertType("array{}|array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
8888
}
8989
assertType("array{}|array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
9090
}

Diff for: ‎tests/PHPStan/Analyser/nsrt/list-count.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ protected function testOptionalKeysInUnionListWithIntRange($row, $twoOrThree, $t
369369
if (count($row) >= $maxThree) {
370370
assertType('array{string}|list{0: int, 1?: string|null, 2?: int|null, 3?: float|null}', $row);
371371
} else {
372-
assertType('list{0: int, 1?: string|null, 2?: int|null, 3?: float|null}', $row);
372+
assertType('array{string}|list{0: int, 1?: string|null, 2?: int|null, 3?: float|null}', $row);
373373
}
374374
}
375375

0 commit comments

Comments
 (0)