diff --git a/src/Analyser/Generator/ExprHandler/ScalarFloatHandler.php b/src/Analyser/Generator/ExprHandler/ScalarHandler.php similarity index 62% rename from src/Analyser/Generator/ExprHandler/ScalarFloatHandler.php rename to src/Analyser/Generator/ExprHandler/ScalarHandler.php index 8067e6e249..6b1f5ba638 100644 --- a/src/Analyser/Generator/ExprHandler/ScalarFloatHandler.php +++ b/src/Analyser/Generator/ExprHandler/ScalarHandler.php @@ -5,6 +5,8 @@ use Generator; use PhpParser\Node\Expr; use PhpParser\Node\Scalar\Float_; +use PhpParser\Node\Scalar\Int_; +use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt; use PHPStan\Analyser\ExpressionContext; use PHPStan\Analyser\Generator\ExprAnalysisResult; @@ -12,18 +14,23 @@ use PHPStan\Analyser\Generator\GeneratorScope; use PHPStan\Analyser\SpecifiedTypes; use PHPStan\DependencyInjection\AutowiredService; +use PHPStan\ShouldNotHappenException; use PHPStan\Type\Constant\ConstantFloatType; +use PHPStan\Type\Constant\ConstantIntegerType; +use PHPStan\Type\Constant\ConstantStringType; /** * @implements ExprHandler */ #[AutowiredService] -final class ScalarFloatHandler implements ExprHandler +final class ScalarHandler implements ExprHandler { public function supports(Expr $expr): bool { - return $expr instanceof Float_; + return $expr instanceof Float_ + || $expr instanceof Int_ + || $expr instanceof String_; } public function analyseExpr( @@ -35,7 +42,17 @@ public function analyseExpr( ): Generator { yield from []; - $type = new ConstantFloatType($expr->value); + + if ($expr instanceof Float_) { + $type = new ConstantFloatType($expr->value); + } elseif ($expr instanceof Int_) { + $type = new ConstantIntegerType($expr->value); + } elseif ($expr instanceof String_) { + $type = new ConstantStringType($expr->value); + } else { + throw new ShouldNotHappenException(); + } + return new ExprAnalysisResult( $type, $type, diff --git a/src/Analyser/Generator/ExprHandler/ScalarIntHandler.php b/src/Analyser/Generator/ExprHandler/ScalarIntHandler.php deleted file mode 100644 index 3888fb8156..0000000000 --- a/src/Analyser/Generator/ExprHandler/ScalarIntHandler.php +++ /dev/null @@ -1,53 +0,0 @@ - - */ -#[AutowiredService] -final class ScalarIntHandler implements ExprHandler -{ - - public function supports(Expr $expr): bool - { - return $expr instanceof Int_; - } - - public function analyseExpr( - Stmt $stmt, - Expr $expr, - GeneratorScope $scope, - ExpressionContext $context, - ?callable $alternativeNodeCallback, - ): Generator - { - yield from []; - $type = new ConstantIntegerType($expr->value); - return new ExprAnalysisResult( - $type, - $type, - $scope, - hasYield: false, - isAlwaysTerminating: false, - throwPoints: [], - impurePoints: [], - specifiedTruthyTypes: new SpecifiedTypes(), - specifiedFalseyTypes: new SpecifiedTypes(), - specifiedNullTypes: new SpecifiedTypes(), - ); - } - -} diff --git a/src/Analyser/Generator/ExprHandler/ScalarStringHandler.php b/src/Analyser/Generator/ExprHandler/ScalarStringHandler.php deleted file mode 100644 index b91bb13d7a..0000000000 --- a/src/Analyser/Generator/ExprHandler/ScalarStringHandler.php +++ /dev/null @@ -1,53 +0,0 @@ - - */ -#[AutowiredService] -final class ScalarStringHandler implements ExprHandler -{ - - public function supports(Expr $expr): bool - { - return $expr instanceof String_; - } - - public function analyseExpr( - Stmt $stmt, - Expr $expr, - GeneratorScope $scope, - ExpressionContext $context, - ?callable $alternativeNodeCallback, - ): Generator - { - yield from []; - $type = new ConstantStringType($expr->value); - return new ExprAnalysisResult( - $type, - $type, - $scope, - hasYield: false, - isAlwaysTerminating: false, - throwPoints: [], - impurePoints: [], - specifiedTruthyTypes: new SpecifiedTypes(), - specifiedFalseyTypes: new SpecifiedTypes(), - specifiedNullTypes: new SpecifiedTypes(), - ); - } - -}