Skip to content

Commit

Permalink
Consider int range offset in truthy context
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm committed Dec 24, 2024
1 parent 638c2ed commit f340663
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
21 changes: 6 additions & 15 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public function specifyTypesInCondition(
$sizeType = IntegerRangeType::createAllGreaterThan($leftType->getValue());
}
} elseif ($leftType instanceof IntegerRangeType) {
$sizeType = $leftType;
$sizeType = $leftType->shift($offset);
}

$specifiedTypes = $this->specifyTypesForCountFuncCall($expr->right, $argType, $sizeType, $context, $scope, $expr);
Expand Down Expand Up @@ -984,23 +984,14 @@ private function specifyTypesForCountFuncCall(FuncCall $countFuncCall, Type $typ

$arraySize = $type->getArraySize();
$isSize = $sizeType->isSuperTypeOf($arraySize);
if ($context->truthy()) {
if ($isSize->no()) {
return new NeverType();
}

$constArray = $this->turnListIntoConstantArray($type, $sizeType);
if ($constArray !== null) {
$type = $constArray;
}
if ($context->truthy() && $isSize->no()) {
return new NeverType();
}
if ($context->falsey()) {
if (!$isSize->yes()) {
return new NeverType();
}
if ($context->falsey() && !$isSize->yes()) {
return new NeverType();
}

return $type;
return $this->turnListIntoConstantArray($type, $sizeType) ?? $type;
});

return $this->create($countFuncCall->getArgs()[0]->value, $resultType, $context, $scope)->setRootExpr($rootExpr);
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/nsrt/bug-4700.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ function(array $array, int $count): void {
if (isset($array['e'])) $a[] = $array['e'];
if (count($a) > $count) {
assertType('int<2, 5>', count($a));
assertType('array{0: mixed~null, 1?: mixed~null, 2?: mixed~null, 3?: mixed~null, 4?: mixed~null}', $a);
assertType('list{0: mixed~null, 1: mixed~null, 2?: mixed~null, 3?: mixed~null, 4?: mixed~null}', $a);
} else {
assertType('0', count($a));
assertType('array{}', $a);
assertType('int<0, 5>', count($a)); // Could be int<0, 1>
assertType('array{}|array{0: mixed~null, 1?: mixed~null, 2?: mixed~null, 3?: mixed~null, 4?: mixed~null}', $a); // Could be array{}|array{0: mixed~null}
}
};

0 comments on commit f340663

Please sign in to comment.