Skip to content

Commit 74f3f58

Browse files
staabmondrejmirtes
authored andcommitted
TypeSpecifier: Narrow (float) $expr like $expr != false
1 parent 0c9487d commit 74f3f58

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Diff for: src/Analyser/TypeSpecifier.php

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public function specifyTypesInCondition(
198198
);
199199
} elseif (
200200
$expr instanceof Expr\Cast\String_
201+
|| $expr instanceof Expr\Cast\Double
201202
|| $expr instanceof Expr\Cast\Int_
202203
|| $expr instanceof Expr\Cast\Bool_
203204
) {

Diff for: tests/PHPStan/Analyser/nsrt/narrow-cast.php

+21
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,24 @@ function castInt($x, string $s, bool $b) {
6969
assertType('string', $s);
7070
}
7171
}
72+
73+
/** @param int<-5, 5> $x */
74+
function castFloat($x, string $s, bool $b) {
75+
if ((float) $x) {
76+
assertType('int<-5, -1>|int<1, 5>', $x);
77+
} else {
78+
assertType('0', $x);
79+
}
80+
81+
if ((float) $b) {
82+
assertType('true', $b);
83+
} else {
84+
assertType('false', $b);
85+
}
86+
87+
if ((float) $s) {
88+
assertType('non-falsy-string', $s);
89+
} else {
90+
assertType("''|'0'", $s);
91+
}
92+
}

0 commit comments

Comments
 (0)