Skip to content

Commit 4806cf0

Browse files
committed
Refactor Scalar*Handlers into single ScalarHandler
1 parent d7482af commit 4806cf0

File tree

3 files changed

+20
-109
lines changed

3 files changed

+20
-109
lines changed

src/Analyser/Generator/ExprHandler/ScalarFloatHandler.php renamed to src/Analyser/Generator/ExprHandler/ScalarHandler.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,32 @@
55
use Generator;
66
use PhpParser\Node\Expr;
77
use PhpParser\Node\Scalar\Float_;
8+
use PhpParser\Node\Scalar\Int_;
9+
use PhpParser\Node\Scalar\String_;
810
use PhpParser\Node\Stmt;
911
use PHPStan\Analyser\ExpressionContext;
1012
use PHPStan\Analyser\Generator\ExprAnalysisResult;
1113
use PHPStan\Analyser\Generator\ExprHandler;
1214
use PHPStan\Analyser\Generator\GeneratorScope;
1315
use PHPStan\Analyser\SpecifiedTypes;
1416
use PHPStan\DependencyInjection\AutowiredService;
17+
use PHPStan\ShouldNotHappenException;
1518
use PHPStan\Type\Constant\ConstantFloatType;
19+
use PHPStan\Type\Constant\ConstantIntegerType;
20+
use PHPStan\Type\Constant\ConstantStringType;
1621

1722
/**
1823
* @implements ExprHandler<Float_>
1924
*/
2025
#[AutowiredService]
21-
final class ScalarFloatHandler implements ExprHandler
26+
final class ScalarHandler implements ExprHandler
2227
{
2328

2429
public function supports(Expr $expr): bool
2530
{
26-
return $expr instanceof Float_;
31+
return $expr instanceof Float_
32+
|| $expr instanceof Int_
33+
|| $expr instanceof String_;
2734
}
2835

2936
public function analyseExpr(
@@ -35,7 +42,17 @@ public function analyseExpr(
3542
): Generator
3643
{
3744
yield from [];
38-
$type = new ConstantFloatType($expr->value);
45+
46+
if ($expr instanceof Float_) {
47+
$type = new ConstantFloatType($expr->value);
48+
} elseif ($expr instanceof Int_) {
49+
$type = new ConstantIntegerType($expr->value);
50+
} elseif ($expr instanceof String_) {
51+
$type = new ConstantStringType($expr->value);
52+
} else {
53+
throw new ShouldNotHappenException();
54+
}
55+
3956
return new ExprAnalysisResult(
4057
$type,
4158
$type,

src/Analyser/Generator/ExprHandler/ScalarIntHandler.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/Analyser/Generator/ExprHandler/ScalarStringHandler.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)