Skip to content
Draft
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
10 changes: 6 additions & 4 deletions src/Analyser/Generator/ExprHandler/CastBoolHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use Generator;
use PhpParser\Node\Expr;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
use PHPStan\Analyser\Generator\ExprAnalysisResult;
use PHPStan\Analyser\Generator\ExprHandler;
use PHPStan\Analyser\Generator\GeneratorScope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\Generator\NoopNodeCallback;
use PHPStan\DependencyInjection\AutowiredService;

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

return new ExprAnalysisResult(
$exprResult->type->toBoolean(),
Expand All @@ -43,9 +45,9 @@ public function analyseExpr(
isAlwaysTerminating: false,
throwPoints: [],
impurePoints: [],
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
specifiedTruthyTypes: $specifiedExprResult->specifiedTruthyTypes,
specifiedFalseyTypes: $specifiedExprResult->specifiedFalseyTypes,
specifiedNullTypes: $specifiedExprResult->specifiedNullTypes,
);
}

Expand Down
10 changes: 7 additions & 3 deletions src/Analyser/Generator/ExprHandler/CastDoubleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use Generator;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
use PHPStan\Analyser\Generator\ExprAnalysisResult;
use PHPStan\Analyser\Generator\ExprHandler;
use PHPStan\Analyser\Generator\GeneratorScope;
use PHPStan\Analyser\Generator\NoopNodeCallback;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\DependencyInjection\AutowiredService;

Expand All @@ -34,6 +37,7 @@ public function analyseExpr(
): Generator
{
$exprResult = yield new ExprAnalysisRequest($stmt, $expr->expr, $scope, $context->enterDeep(), $alternativeNodeCallback);
$specifiedExprResult = yield new ExprAnalysisRequest($stmt, new Expr\BinaryOp\Equal($expr->expr, new DNumber(0.0)), $scope, $context->enterDeep(), new NoopNodeCallback());

return new ExprAnalysisResult(
$exprResult->type->toFloat(),
Expand All @@ -43,9 +47,9 @@ public function analyseExpr(
isAlwaysTerminating: false,
throwPoints: [],
impurePoints: [],
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
specifiedTruthyTypes: $specifiedExprResult->specifiedTruthyTypes,
specifiedFalseyTypes: $specifiedExprResult->specifiedFalseyTypes,
specifiedNullTypes: $specifiedExprResult->specifiedNullTypes,
);
}

Expand Down
10 changes: 7 additions & 3 deletions src/Analyser/Generator/ExprHandler/CastIntHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
use Generator;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Cast\Int_;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
use PHPStan\Analyser\Generator\ExprAnalysisResult;
use PHPStan\Analyser\Generator\ExprHandler;
use PHPStan\Analyser\Generator\GeneratorScope;
use PHPStan\Analyser\Generator\NoopNodeCallback;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\DependencyInjection\AutowiredService;

Expand All @@ -35,6 +38,7 @@ public function analyseExpr(
): Generator
{
$exprResult = yield new ExprAnalysisRequest($stmt, $expr->expr, $scope, $context->enterDeep(), $alternativeNodeCallback);
$specifiedExprResult = yield new ExprAnalysisRequest($stmt, new Expr\BinaryOp\Equal($expr->expr, new LNumber(0)), $scope, $context->enterDeep(), new NoopNodeCallback());

return new ExprAnalysisResult(
$exprResult->type->toInteger(),
Expand All @@ -44,9 +48,9 @@ public function analyseExpr(
isAlwaysTerminating: false,
throwPoints: [],
impurePoints: [],
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
specifiedTruthyTypes: $specifiedExprResult->specifiedTruthyTypes,
specifiedFalseyTypes: $specifiedExprResult->specifiedFalseyTypes,
specifiedNullTypes: $specifiedExprResult->specifiedNullTypes,
);
}

Expand Down
10 changes: 7 additions & 3 deletions src/Analyser/Generator/ExprHandler/CastStringHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use Generator;
use PhpParser\Node\Expr;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
use PHPStan\Analyser\Generator\ExprAnalysisResult;
use PHPStan\Analyser\Generator\ExprHandler;
use PHPStan\Analyser\Generator\GeneratorScope;
use PHPStan\Analyser\Generator\NoopNodeCallback;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\DependencyInjection\AutowiredService;

Expand All @@ -34,6 +37,7 @@ public function analyseExpr(
): Generator
{
$exprResult = yield new ExprAnalysisRequest($stmt, $expr->expr, $scope, $context->enterDeep(), $alternativeNodeCallback);
$specifiedExprResult = yield new ExprAnalysisRequest($stmt, new Expr\BinaryOp\Equal($expr->expr, new String_('')), $scope, $context->enterDeep(), new NoopNodeCallback());

return new ExprAnalysisResult(
$exprResult->type->toString(),
Expand All @@ -43,9 +47,9 @@ public function analyseExpr(
isAlwaysTerminating: false,
throwPoints: [],
impurePoints: [],
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
specifiedTruthyTypes: $specifiedExprResult->specifiedTruthyTypes,
specifiedFalseyTypes: $specifiedExprResult->specifiedFalseyTypes,
specifiedNullTypes: $specifiedExprResult->specifiedNullTypes,
);
}

Expand Down
61 changes: 61 additions & 0 deletions tests/PHPStan/Analyser/Generator/data/gnsr.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,64 @@ function (array $a): void {

assertType("array", $a);
};

class CastNarrowing {
/**
* @param int $i2
* @param mixed $m2
*/
public function doCastNarrowing(
int $i, mixed $m,
$i2, $m2
): void
{
if ((bool) $i) {
assertNativeType('int<min, -1>|int<1, max>', $i);
} else {
assertNativeType('0', $i);
}
assertNativeType('int', $i);

if ((bool) $i2) {
assertType('int<min, -1>|int<1, max>', $i2);
} else {
assertType('0', $i2);
}
assertType('int', $i2);

if ((bool) $m) {
assertNativeType("mixed~(0|0.0|''|'0'|array{}|false|null)", $m);
} else {
assertNativeType("0|0.0|''|'0'|array{}|false|null", $m);
}
assertNativeType('mixed', $m);

if ((bool) $m2) {
assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $m2);
} else {
assertType("0|0.0|''|'0'|array{}|false|null", $m2);
}
assertType('mixed', $m2);

if ((string) $m2) {
assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $m2);
} else {
assertType("0|0.0|''|'0'|array{}|false|null", $m2);
}
assertType('mixed', $m2);

if ((int) $m2) {
assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $m2);
} else {
assertType("0|0.0|''|'0'|array{}|false|null", $m2);
}
assertType('mixed', $m2);

if ((float) $m2) {
assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $m2);
} else {
assertType("0|0.0|''|'0'|array{}|false|null", $m2);
}
assertType('mixed', $m2);
}
}
Loading