Skip to content

Commit b769b67

Browse files
phpstan-botclaude
authored 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 d60cc3d commit b769b67

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
@@ -494,19 +494,11 @@ public function specifyTypesInCondition(
494494

495495
$leftExpr = $expr->left;
496496
if ($leftExpr instanceof Expr\Cast) {
497-
$castedType = $scope->getType($leftExpr);
498-
$innerType = $scope->getType($leftExpr->expr);
499-
if ($castedType->equals($innerType)) {
500-
$leftExpr = $leftExpr->expr;
501-
}
497+
$leftExpr = $leftExpr->expr;
502498
}
503499
$rightExpr = $expr->right;
504500
if ($rightExpr instanceof Expr\Cast) {
505-
$castedType = $scope->getType($rightExpr);
506-
$innerType = $scope->getType($rightExpr->expr);
507-
if ($castedType->equals($innerType)) {
508-
$rightExpr = $rightExpr->expr;
509-
}
501+
$rightExpr = $rightExpr->expr;
510502
}
511503

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

0 commit comments

Comments
 (0)