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
53 changes: 53 additions & 0 deletions src/Analyser/Generator/ExprHandler/ScalarFloatHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser\Generator\ExprHandler;

use Generator;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\Float_;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\Generator\ExprAnalysisResult;
use PHPStan\Analyser\Generator\ExprHandler;
use PHPStan\Analyser\Generator\GeneratorScope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Type\Constant\ConstantFloatType;

/**
* @implements ExprHandler<Float_>
*/
#[AutowiredService]
final class ScalarFloatHandler implements ExprHandler
{

public function supports(Expr $expr): bool
{
return $expr instanceof Float_;
}

public function analyseExpr(
Stmt $stmt,
Expr $expr,
GeneratorScope $scope,
ExpressionContext $context,
?callable $alternativeNodeCallback,
): Generator
{
yield from [];
$type = new ConstantFloatType($expr->value);
return new ExprAnalysisResult(
$type,
$type,
$scope,
hasYield: false,
isAlwaysTerminating: false,
throwPoints: [],
impurePoints: [],
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
);
}

}
3 changes: 3 additions & 0 deletions tests/PHPStan/Analyser/Generator/data/gnsr.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ function doCast() {
assertType('1.0', (double) $a);
assertType('true', (bool) $a);
assertType("'1'", (string) $a);

$f = 1.1;
assertType('1', (int) $f);
}

/**
Expand Down
Loading