Skip to content

Commit 940faf7

Browse files
phpstan-botclaude
andcommitted
Simplify cast unwrapping in comparisons to always unwrap
The previous approach only unwrapped cast expressions when the cast type equaled the inner type (redundant cast). This failed for cases where the variable had a union type with non-integer parts (e.g. int|numeric-string after ctype_digit), because the cast was not redundant and the narrowing was never applied. The narrowing type (e.g. mixed~(0.0|int<min, 2021>|false|null)) already correctly preserves non-integer parts of the type through intersection, so unconditional unwrapping produces correct results for all cases. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent aba682d commit 940faf7

1 file changed

Lines changed: 2 additions & 10 deletions

File tree

src/Analyser/TypeSpecifier.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -476,19 +476,11 @@ public function specifyTypesInCondition(
476476

477477
$leftExpr = $expr->left;
478478
if ($leftExpr instanceof Expr\Cast) {
479-
$castedType = $scope->getType($leftExpr);
480-
$innerType = $scope->getType($leftExpr->expr);
481-
if ($castedType->equals($innerType)) {
482-
$leftExpr = $leftExpr->expr;
483-
}
479+
$leftExpr = $leftExpr->expr;
484480
}
485481
$rightExpr = $expr->right;
486482
if ($rightExpr instanceof Expr\Cast) {
487-
$castedType = $scope->getType($rightExpr);
488-
$innerType = $scope->getType($rightExpr->expr);
489-
if ($castedType->equals($innerType)) {
490-
$rightExpr = $rightExpr->expr;
491-
}
483+
$rightExpr = $rightExpr->expr;
492484
}
493485

494486
if ($context->true()) {

0 commit comments

Comments
 (0)