Skip to content

Commit 4dfe703

Browse files
committed
Avoid specifying incorrect falsey context
1 parent ad72180 commit 4dfe703

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

src/Analyser/TypeSpecifier.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -994,10 +994,10 @@ private function specifyTypesForCountFuncCall(
994994
}
995995

996996
$isSizeSuperTypeOfArraySize = $sizeType->isSuperTypeOf($type->getArraySize());
997-
if ($context->truthy() && $isSizeSuperTypeOfArraySize->no()) {
997+
if ($isSizeSuperTypeOfArraySize->no()) {
998998
return new NeverType();
999999
}
1000-
if ($context->falsey() && !$isSizeSuperTypeOfArraySize->yes()) {
1000+
if ($context->falsey() && $isSizeSuperTypeOfArraySize->maybe()) {
10011001
return new NeverType();
10021002
}
10031003

@@ -1046,10 +1046,10 @@ private function specifyTypesForCountFuncCall(
10461046
return $valueTypesBuilder->getArray();
10471047
}
10481048

1049-
return $type;
1049+
return $context->truthy() ? $type : new NeverType();
10501050
}
10511051

1052-
return TypeCombinator::intersect($type, new NonEmptyArrayType());
1052+
return $context->truthy() ? TypeCombinator::intersect($type, new NonEmptyArrayType()) : new NeverType();
10531053
});
10541054

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

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
}

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)