|
| 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\Expr\BinaryOp\Plus; |
| 8 | +use PhpParser\Node\Stmt; |
| 9 | +use PHPStan\Analyser\ExpressionContext; |
| 10 | +use PHPStan\Analyser\Generator\ExprAnalysisRequest; |
| 11 | +use PHPStan\Analyser\Generator\ExprAnalysisResult; |
| 12 | +use PHPStan\Analyser\Generator\ExprHandler; |
| 13 | +use PHPStan\Analyser\Generator\GeneratorScope; |
| 14 | +use PHPStan\Analyser\SpecifiedTypes; |
| 15 | +use PHPStan\DependencyInjection\AutowiredService; |
| 16 | +use PHPStan\Reflection\InitializerExprTypeResolver; |
| 17 | +use function array_merge; |
| 18 | + |
| 19 | +/** |
| 20 | + * @implements ExprHandler<Plus> |
| 21 | + */ |
| 22 | +#[AutowiredService] |
| 23 | +final class BinaryPlusHandler implements ExprHandler |
| 24 | +{ |
| 25 | + |
| 26 | + public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + public function supports(Expr $expr): bool |
| 31 | + { |
| 32 | + return $expr instanceof Plus; |
| 33 | + } |
| 34 | + |
| 35 | + public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator |
| 36 | + { |
| 37 | + $leftResult = yield new ExprAnalysisRequest($stmt, $expr->left, $scope, $context, $alternativeNodeCallback); |
| 38 | + $rightResult = yield new ExprAnalysisRequest($stmt, $expr->right, $leftResult->scope, $context, $alternativeNodeCallback); |
| 39 | + |
| 40 | + return new ExprAnalysisResult( |
| 41 | + $this->initializerExprTypeResolver->getPlusTypeFromTypes($expr->left, $expr->right, $leftResult->type, $rightResult->type), |
| 42 | + $this->initializerExprTypeResolver->getPlusTypeFromTypes($expr->left, $expr->right, $leftResult->nativeType, $rightResult->nativeType), |
| 43 | + $rightResult->scope, |
| 44 | + hasYield: $leftResult->hasYield || $rightResult->hasYield, |
| 45 | + isAlwaysTerminating: $leftResult->isAlwaysTerminating || $rightResult->isAlwaysTerminating, |
| 46 | + throwPoints: array_merge($leftResult->throwPoints, $rightResult->throwPoints), |
| 47 | + impurePoints: array_merge($leftResult->impurePoints, $rightResult->impurePoints), |
| 48 | + specifiedTruthyTypes: new SpecifiedTypes(), |
| 49 | + specifiedFalseyTypes: new SpecifiedTypes(), |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | +} |
0 commit comments