Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,42 +428,59 @@ 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,
)->setRootExpr($expr),
);
}
} 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,
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-7858.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace Bug7858;

use function PHPStan\Testing\assertType;

function foo(int $year): void
{
if (!ctype_digit($year) || (int) $year < 2022) {
throw new \RuntimeException();
}
assertType('int<2022, max>', $year);
}

function bar(int $year): void
{
if (!ctype_digit($year) || $year < 2022) {
throw new \RuntimeException();
}
assertType('int<2022, max>', $year);
}
Loading