Skip to content

Commit 3bd2d8b

Browse files
staabmondrejmirtes
authored andcommitted
Implement UnaryPlusHandler
1 parent 6cf0ab2 commit 3bd2d8b

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Stmt;
8+
use PHPStan\Analyser\ExpressionContext;
9+
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
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+
16+
/**
17+
* @implements ExprHandler<Expr\UnaryPlus>
18+
*/
19+
#[AutowiredService]
20+
final class UnaryPlusHandler implements ExprHandler
21+
{
22+
23+
public function supports(Expr $expr): bool
24+
{
25+
return $expr instanceof Expr\UnaryPlus;
26+
}
27+
28+
public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator
29+
{
30+
$result = yield new ExprAnalysisRequest($stmt, $expr->expr, $scope, $context->enterDeep(), $alternativeNodeCallback);
31+
32+
return new ExprAnalysisResult(
33+
$result->type->toNumber(),
34+
$result->nativeType->toNumber(),
35+
$result->scope,
36+
hasYield: $result->hasYield,
37+
isAlwaysTerminating: $result->isAlwaysTerminating,
38+
throwPoints: $result->throwPoints,
39+
impurePoints: $result->impurePoints,
40+
specifiedTruthyTypes: new SpecifiedTypes(),
41+
specifiedFalseyTypes: new SpecifiedTypes(),
42+
specifiedNullTypes: new SpecifiedTypes(),
43+
);
44+
}
45+
46+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@ public function doConcat($a, $b, string $c, string $d): void
191191
assertNativeType('string', $c . $d);
192192
}
193193

194+
function doUnaryPlus(int $i) {
195+
$a = '1';
196+
197+
assertType('1', +$a);
198+
assertNativeType('1', +$a);
199+
assertType('int', +$i);
200+
assertNativeType('int', +$i);
201+
}
202+
194203
/**
195204
* @param int $a
196205
* @param int $b

0 commit comments

Comments
 (0)