Skip to content
Merged
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
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
20 changes: 13 additions & 7 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 @@ -63,8 +63,14 @@ function castInt($x, string $s, bool $b) {
assertType('false', $b);
}

if ((int) $s) {
assertType('string', $s);
} else {
assertType('string', $s);
}

if ((int) strpos($s, 'xy')) {
assertType('non-falsy-string', $s);
assertType('string', $s);
} else {
assertType('string', $s);
}
Expand All @@ -73,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 All @@ -85,8 +91,8 @@ function castFloat($x, string $s, bool $b) {
}

if ((float) $s) {
assertType('non-falsy-string', $s);
assertType('string', $s);
} else {
assertType("''|'0'", $s);
assertType("string", $s);
}
}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Comparison/ElseIfConstantConditionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;

/**
* @extends RuleTestCase<ElseIfConstantConditionRule>
Expand Down Expand Up @@ -121,4 +122,19 @@ public function testReportPhpDoc(): void
]);
}

public function testBug11674(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0');
}

$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-11674.php'], [
[
'Elseif condition is always false.',
28,
],
]);
}

}
40 changes: 40 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-11674.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1); // lint >= 8.0

namespace Bug11674;

class Test {

private ?string $param;

function show() : void {
if ((int) $this->param) {
echo 1;
} elseif ($this->param) {
echo 2;
}
}

function show2() : void {
if ((float) $this->param) {
echo 1;
} elseif ($this->param) {
echo 2;
}
}

function show3() : void {
if ((bool) $this->param) {
echo 1;
} elseif ($this->param) {
echo 2;
}
}

function show4() : void {
if ((string) $this->param) {
echo 1;
} elseif ($this->param) {
echo 2;
}
}
}
Loading