Skip to content

Commit b64d77e

Browse files
staabmondrejmirtes
authored andcommitted
Implement ScalarFloatHandler
1 parent 0feb183 commit b64d77e

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\Generator\ExprHandler;
4+
5+
use Generator;
6+
use PhpParser\Node\Expr;
7+
use PhpParser\Node\Scalar\Float_;
8+
use PhpParser\Node\Stmt;
9+
use PHPStan\Analyser\ExpressionContext;
10+
use PHPStan\Analyser\Generator\ExprAnalysisResult;
11+
use PHPStan\Analyser\Generator\ExprHandler;
12+
use PHPStan\Analyser\Generator\GeneratorScope;
13+
use PHPStan\Analyser\SpecifiedTypes;
14+
use PHPStan\DependencyInjection\AutowiredService;
15+
use PHPStan\Type\Constant\ConstantFloatType;
16+
17+
/**
18+
* @implements ExprHandler<Float_>
19+
*/
20+
#[AutowiredService]
21+
final class ScalarFloatHandler implements ExprHandler
22+
{
23+
24+
public function supports(Expr $expr): bool
25+
{
26+
return $expr instanceof Float_;
27+
}
28+
29+
public function analyseExpr(
30+
Stmt $stmt,
31+
Expr $expr,
32+
GeneratorScope $scope,
33+
ExpressionContext $context,
34+
?callable $alternativeNodeCallback,
35+
): Generator
36+
{
37+
yield from [];
38+
$type = new ConstantFloatType($expr->value);
39+
return new ExprAnalysisResult(
40+
$type,
41+
$type,
42+
$scope,
43+
hasYield: false,
44+
isAlwaysTerminating: false,
45+
throwPoints: [],
46+
impurePoints: [],
47+
specifiedTruthyTypes: new SpecifiedTypes(),
48+
specifiedFalseyTypes: new SpecifiedTypes(),
49+
specifiedNullTypes: new SpecifiedTypes(),
50+
);
51+
}
52+
53+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ function doCast() {
269269
assertType('1.0', (double) $a);
270270
assertType('true', (bool) $a);
271271
assertType("'1'", (string) $a);
272+
273+
$f = 1.1;
274+
assertType('1', (int) $f);
272275
}
273276

274277
/**

0 commit comments

Comments
 (0)