Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 23 additions & 7 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,31 @@ public function specifyTypesInCondition(
$context,
$rootExpr,
);
} elseif (
$expr instanceof Expr\Cast\String_
|| $expr instanceof Expr\Cast\Double
|| $expr instanceof Expr\Cast\Int_
|| $expr instanceof Expr\Cast\Bool_
) {
} elseif ($expr instanceof Expr\Cast\Bool_) {
return $this->specifyTypesInCondition(
$scope,
new Node\Expr\BinaryOp\Equal($expr->expr, new ConstFetch(new Name\FullyQualified('true'))),
$context,
$rootExpr,
);
} elseif ($expr instanceof Expr\Cast\String_) {
return $this->specifyTypesInCondition(
$scope,
new Node\Expr\BinaryOp\NotEqual($expr->expr, new Node\Scalar\String_('')),
$context,
$rootExpr,
);
} elseif ($expr instanceof Expr\Cast\Int_) {
return $this->specifyTypesInCondition(
$scope,
new Node\Expr\BinaryOp\NotEqual($expr->expr, new Node\Scalar\LNumber(0)),
$context,
$rootExpr,
);
} elseif ($expr instanceof Expr\Cast\Double) {
return $this->specifyTypesInCondition(
$scope,
new Node\Expr\BinaryOp\NotEqual($expr->expr, new ConstFetch(new Name\FullyQualified('false'))),
new Node\Expr\BinaryOp\NotEqual($expr->expr, new Node\Scalar\DNumber(0.0)),
$context,
$rootExpr,
);
Expand Down
10 changes: 5 additions & 5 deletions tests/PHPStan/Analyser/nsrt/narrow-cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ function doFoo(string $x, array $arr): void {
/** @param int<-5, 5> $x */
function castString($x, string $s, bool $b) {
if ((string) $x) {
assertType('int<-5, -1>|int<1, 5>', $x);
assertType('int<-5, 5>', $x);
} else {
assertType('0', $x);
assertType('int<-5, 5>', $x);
}

if ((string) $b) {
Expand Down Expand Up @@ -70,7 +70,7 @@ function castInt($x, string $s, bool $b) {
}

if ((int) strpos($s, 'xy')) {
assertType('non-falsy-string', $s);
assertType('string', $s);
} else {
assertType('string', $s);
}
Expand All @@ -79,9 +79,9 @@ function castInt($x, string $s, bool $b) {
/** @param int<-5, 5> $x */
function castFloat($x, string $s, bool $b) {
if ((float) $x) {
assertType('int<-5, -1>|int<1, 5>', $x);
assertType('int<-5, 5>', $x);
} else {
assertType('0', $x);
assertType('int<-5, 5>', $x);
}

if ((float) $b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ public function testBug11674(): void
'Elseif condition is always false.',
28,
],
[
'Elseif condition is always false.',
36,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me a while but I found the bad boy for which the elseif can be false: https://3v4l.org/p1sON

],
]);
}

Expand Down
Loading