From 911404a0ee93a49aafafdfdfb13355d600fc5443 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 27 Feb 2026 16:04:28 +0000 Subject: [PATCH] Fix redundant type cast dropping type narrowing in comparisons - Unwrap redundant cast expressions in Smaller/SmallerOrEqual type specifying so that narrowing propagates to the inner variable instead of the cast expr - When a cast produces the same type as its inner expression (e.g. (int) on an int variable), the narrowing is now applied to the original variable - New regression test in tests/PHPStan/Analyser/nsrt/bug-7858.php Closes https://github.com/phpstan/phpstan/issues/7858 --- src/Analyser/TypeSpecifier.php | 33 ++++++++++++++++++------ tests/PHPStan/Analyser/nsrt/bug-7858.php | 21 +++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 tests/PHPStan/Analyser/nsrt/bug-7858.php diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index 0949e9eaf9..945fee0067 100644 --- a/src/Analyser/TypeSpecifier.php +++ b/src/Analyser/TypeSpecifier.php @@ -428,21 +428,38 @@ public function specifyTypesInCondition( } } + $leftExpr = $expr->left; + if ($leftExpr instanceof Expr\Cast) { + $castedType = $scope->getType($leftExpr); + $innerType = $scope->getType($leftExpr->expr); + if ($castedType->equals($innerType)) { + $leftExpr = $leftExpr->expr; + } + } + $rightExpr = $expr->right; + if ($rightExpr instanceof Expr\Cast) { + $castedType = $scope->getType($rightExpr); + $innerType = $scope->getType($rightExpr->expr); + if ($castedType->equals($innerType)) { + $rightExpr = $rightExpr->expr; + } + } + if ($context->true()) { - if (!$expr->left instanceof Node\Scalar) { + if (!$leftExpr instanceof Node\Scalar) { $result = $result->unionWith( $this->create( - $expr->left, + $leftExpr, $orEqual ? $rightType->getSmallerOrEqualType($this->phpVersion) : $rightType->getSmallerType($this->phpVersion), TypeSpecifierContext::createTruthy(), $scope, )->setRootExpr($expr), ); } - if (!$expr->right instanceof Node\Scalar) { + if (!$rightExpr instanceof Node\Scalar) { $result = $result->unionWith( $this->create( - $expr->right, + $rightExpr, $orEqual ? $leftType->getGreaterOrEqualType($this->phpVersion) : $leftType->getGreaterType($this->phpVersion), TypeSpecifierContext::createTruthy(), $scope, @@ -450,20 +467,20 @@ public function specifyTypesInCondition( ); } } elseif ($context->false()) { - if (!$expr->left instanceof Node\Scalar) { + if (!$leftExpr instanceof Node\Scalar) { $result = $result->unionWith( $this->create( - $expr->left, + $leftExpr, $orEqual ? $rightType->getGreaterType($this->phpVersion) : $rightType->getGreaterOrEqualType($this->phpVersion), TypeSpecifierContext::createTruthy(), $scope, )->setRootExpr($expr), ); } - if (!$expr->right instanceof Node\Scalar) { + if (!$rightExpr instanceof Node\Scalar) { $result = $result->unionWith( $this->create( - $expr->right, + $rightExpr, $orEqual ? $leftType->getSmallerType($this->phpVersion) : $leftType->getSmallerOrEqualType($this->phpVersion), TypeSpecifierContext::createTruthy(), $scope, diff --git a/tests/PHPStan/Analyser/nsrt/bug-7858.php b/tests/PHPStan/Analyser/nsrt/bug-7858.php new file mode 100644 index 0000000000..4c067fc1c6 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-7858.php @@ -0,0 +1,21 @@ +', $year); +} + +function bar(int $year): void +{ + if (!ctype_digit($year) || $year < 2022) { + throw new \RuntimeException(); + } + assertType('int<2022, max>', $year); +}