From 7c1366885601a0975c7bcd168ad783874d125bee Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 1 Dec 2025 07:45:04 +0100 Subject: [PATCH] Implement UnaryPlusHandler --- .../ExprHandler/UnaryPlusHandler.php | 46 +++++++++++++++++++ .../PHPStan/Analyser/Generator/data/gnsr.php | 9 ++++ 2 files changed, 55 insertions(+) create mode 100644 src/Analyser/Generator/ExprHandler/UnaryPlusHandler.php diff --git a/src/Analyser/Generator/ExprHandler/UnaryPlusHandler.php b/src/Analyser/Generator/ExprHandler/UnaryPlusHandler.php new file mode 100644 index 0000000000..d563c7094a --- /dev/null +++ b/src/Analyser/Generator/ExprHandler/UnaryPlusHandler.php @@ -0,0 +1,46 @@ + + */ +#[AutowiredService] +final class UnaryPlusHandler implements ExprHandler +{ + + public function supports(Expr $expr): bool + { + return $expr instanceof Expr\UnaryPlus; + } + + public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator + { + $result = yield new ExprAnalysisRequest($stmt, $expr->expr, $scope, $context->enterDeep(), $alternativeNodeCallback); + + return new ExprAnalysisResult( + $result->type->toNumber(), + $result->nativeType->toNumber(), + $result->scope, + hasYield: $result->hasYield, + isAlwaysTerminating: $result->isAlwaysTerminating, + throwPoints: $result->throwPoints, + impurePoints: $result->impurePoints, + specifiedTruthyTypes: new SpecifiedTypes(), + specifiedFalseyTypes: new SpecifiedTypes(), + specifiedNullTypes: new SpecifiedTypes(), + ); + } + +} diff --git a/tests/PHPStan/Analyser/Generator/data/gnsr.php b/tests/PHPStan/Analyser/Generator/data/gnsr.php index a142ab7bed..d25f728f0b 100644 --- a/tests/PHPStan/Analyser/Generator/data/gnsr.php +++ b/tests/PHPStan/Analyser/Generator/data/gnsr.php @@ -191,6 +191,15 @@ public function doConcat($a, $b, string $c, string $d): void assertNativeType('string', $c . $d); } + function doUnaryPlus(int $i) { + $a = '1'; + + assertType('1', +$a); + assertNativeType('1', +$a); + assertType('int', +$i); + assertNativeType('int', +$i); + } + /** * @param int $a * @param int $b