diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index 69b72a1772..9bf24f318d 100644 --- a/src/Analyser/TypeSpecifier.php +++ b/src/Analyser/TypeSpecifier.php @@ -198,6 +198,7 @@ public function specifyTypesInCondition( ); } elseif ( $expr instanceof Expr\Cast\String_ + || $expr instanceof Expr\Cast\Double || $expr instanceof Expr\Cast\Int_ || $expr instanceof Expr\Cast\Bool_ ) { diff --git a/tests/PHPStan/Analyser/nsrt/narrow-cast.php b/tests/PHPStan/Analyser/nsrt/narrow-cast.php index 0d883cd6ba..afd5224ae0 100644 --- a/tests/PHPStan/Analyser/nsrt/narrow-cast.php +++ b/tests/PHPStan/Analyser/nsrt/narrow-cast.php @@ -69,3 +69,24 @@ function castInt($x, string $s, bool $b) { assertType('string', $s); } } + +/** @param int<-5, 5> $x */ +function castFloat($x, string $s, bool $b) { + if ((float) $x) { + assertType('int<-5, -1>|int<1, 5>', $x); + } else { + assertType('0', $x); + } + + if ((float) $b) { + assertType('true', $b); + } else { + assertType('false', $b); + } + + if ((float) $s) { + assertType('non-falsy-string', $s); + } else { + assertType("''|'0'", $s); + } +}