Skip to content

Commit d9adf4c

Browse files
committed
add tests
1 parent d7482af commit d9adf4c

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/Analyser/Generator/ExprHandler/CastBoolHandler.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
use Generator;
66
use PhpParser\Node\Expr;
7+
use PhpParser\Node\Name\FullyQualified;
78
use PhpParser\Node\Stmt;
89
use PHPStan\Analyser\ExpressionContext;
910
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
1011
use PHPStan\Analyser\Generator\ExprAnalysisResult;
1112
use PHPStan\Analyser\Generator\ExprHandler;
1213
use PHPStan\Analyser\Generator\GeneratorScope;
13-
use PHPStan\Analyser\SpecifiedTypes;
14+
use PHPStan\Analyser\Generator\NoopNodeCallback;
1415
use PHPStan\DependencyInjection\AutowiredService;
1516

1617
/**
@@ -34,6 +35,7 @@ public function analyseExpr(
3435
): Generator
3536
{
3637
$exprResult = yield new ExprAnalysisRequest($stmt, $expr->expr, $scope, $context->enterDeep(), $alternativeNodeCallback);
38+
$specifiedExprResult = yield new ExprAnalysisRequest($stmt, new Expr\BinaryOp\Equal($expr->expr, new Expr\ConstFetch(new FullyQualified('true'))), $scope, $context->enterDeep(), new NoopNodeCallback());
3739

3840
return new ExprAnalysisResult(
3941
$exprResult->type->toBoolean(),
@@ -43,9 +45,9 @@ public function analyseExpr(
4345
isAlwaysTerminating: false,
4446
throwPoints: [],
4547
impurePoints: [],
46-
specifiedTruthyTypes: new SpecifiedTypes(),
47-
specifiedFalseyTypes: new SpecifiedTypes(),
48-
specifiedNullTypes: new SpecifiedTypes(),
48+
specifiedTruthyTypes: $specifiedExprResult->specifiedTruthyTypes,
49+
specifiedFalseyTypes: $specifiedExprResult->specifiedFalseyTypes,
50+
specifiedNullTypes: $specifiedExprResult->specifiedNullTypes,
4951
);
5052
}
5153

tests/PHPStan/Analyser/Generator/data/gnsr.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use function PHPStan\Testing\assertNativeType;
1010
use function PHPStan\Testing\assertType;
1111
use const PHP_VERSION_ID;
12+
use function PHPUnit\Framework\assertNull;
1213

1314
class Foo
1415
{
@@ -278,16 +279,15 @@ function doCast() {
278279
/**
279280
* @param '1' $b
280281
*/
281-
function doInterpolatedString(string $b) {
282+
function doInterpolatedString(string $b)
283+
{
282284
$a = '1';
283285

284286
assertType("'1'", "$a");
285287
assertNativeType("'1'", "$a");
286288
assertType("'1'", "$b");
287289
assertNativeType("string", "$b");
288290
}
289-
290-
291291
}
292292

293293
function (): void {
@@ -555,3 +555,43 @@ function (array $a): void {
555555

556556
assertType("array", $a);
557557
};
558+
559+
class CastNarrowing {
560+
/**
561+
* @param int $i2
562+
* @param mixed $m2
563+
*/
564+
public function doCastNarrowing(
565+
int $i, mixed $m,
566+
$i2, $m2
567+
): void
568+
{
569+
if ((bool) $i) {
570+
assertNativeType('int<min, -1>|int<1, max>', $i);
571+
} else {
572+
assertNativeType('0', $i);
573+
}
574+
assertNativeType('int', $i);
575+
576+
if ((bool) $i2) {
577+
assertType('int<min, -1>|int<1, max>', $i2);
578+
} else {
579+
assertType('0', $i2);
580+
}
581+
assertType('int', $i2);
582+
583+
if ((bool) $m) {
584+
assertNativeType("mixed~(0|0.0|''|'0'|array{}|false|null)", $m);
585+
} else {
586+
assertNativeType("0|0.0|''|'0'|array{}|false|null", $m);
587+
}
588+
assertNativeType('mixed', $m);
589+
590+
if ((bool) $m2) {
591+
assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $m2);
592+
} else {
593+
assertType("0|0.0|''|'0'|array{}|false|null", $m2);
594+
}
595+
assertType('mixed', $m2);
596+
}
597+
}

0 commit comments

Comments
 (0)