Skip to content

Commit 187596f

Browse files
committed
Avoid specifying incorrect falsey context
1 parent 2f65530 commit 187596f

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
@@ -997,10 +997,10 @@ private function specifyTypesForCountFuncCall(
997997
}
998998

999999
$isSizeSuperTypeOfArraySize = $sizeType->isSuperTypeOf($type->getArraySize());
1000-
if ($context->truthy() && $isSizeSuperTypeOfArraySize->no()) {
1000+
if ($isSizeSuperTypeOfArraySize->no()) {
10011001
return new NeverType();
10021002
}
1003-
if ($context->falsey() && !$isSizeSuperTypeOfArraySize->yes()) {
1003+
if ($context->falsey() && $isSizeSuperTypeOfArraySize->maybe()) {
10041004
return new NeverType();
10051005
}
10061006

@@ -1049,10 +1049,10 @@ private function specifyTypesForCountFuncCall(
10491049
return $valueTypesBuilder->getArray();
10501050
}
10511051

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

1055-
return TypeCombinator::intersect($type, new NonEmptyArrayType());
1055+
return $context->truthy() ? TypeCombinator::intersect($type, new NonEmptyArrayType()) : new NeverType();
10561056
});
10571057

10581058
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)