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); +}