|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Analyser\Generator\ExprHandler; |
| 4 | + |
| 5 | +use DivisionByZeroError; |
| 6 | +use Generator; |
| 7 | +use PhpParser\Node\Expr; |
| 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\Generator\InternalThrowPoint; |
| 15 | +use PHPStan\Analyser\SpecifiedTypes; |
| 16 | +use PHPStan\DependencyInjection\AutowiredService; |
| 17 | +use PHPStan\Reflection\InitializerExprTypeResolver; |
| 18 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 19 | +use PHPStan\Type\ObjectType; |
| 20 | +use function array_merge; |
| 21 | + |
| 22 | +/** |
| 23 | + * @implements ExprHandler<Expr\BinaryOp\Mod> |
| 24 | + */ |
| 25 | +#[AutowiredService] |
| 26 | +final class BinaryModHandler implements ExprHandler |
| 27 | +{ |
| 28 | + |
| 29 | + public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver) |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + public function supports(Expr $expr): bool |
| 34 | + { |
| 35 | + return $expr instanceof Expr\BinaryOp\Mod; |
| 36 | + } |
| 37 | + |
| 38 | + public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator |
| 39 | + { |
| 40 | + $leftResult = yield new ExprAnalysisRequest($stmt, $expr->left, $scope, $context, $alternativeNodeCallback); |
| 41 | + $rightResult = yield new ExprAnalysisRequest($stmt, $expr->right, $leftResult->scope, $context, $alternativeNodeCallback); |
| 42 | + |
| 43 | + $throwPoints = array_merge($leftResult->throwPoints, $rightResult->throwPoints); |
| 44 | + if ( |
| 45 | + !$rightResult->type->toNumber()->isSuperTypeOf(new ConstantIntegerType(0))->no() |
| 46 | + ) { |
| 47 | + $throwPoints[] = InternalThrowPoint::createExplicit($scope, new ObjectType(DivisionByZeroError::class), $expr, false); |
| 48 | + } |
| 49 | + |
| 50 | + return new ExprAnalysisResult( |
| 51 | + $this->initializerExprTypeResolver->getModTypeFromTypes($expr->left, $expr->right, $leftResult->type, $rightResult->type), |
| 52 | + $this->initializerExprTypeResolver->getModTypeFromTypes($expr->left, $expr->right, $leftResult->nativeType, $rightResult->nativeType), |
| 53 | + $rightResult->scope, |
| 54 | + hasYield: $leftResult->hasYield || $rightResult->hasYield, |
| 55 | + isAlwaysTerminating: $leftResult->isAlwaysTerminating || $rightResult->isAlwaysTerminating, |
| 56 | + throwPoints: $throwPoints, |
| 57 | + impurePoints: array_merge($leftResult->impurePoints, $rightResult->impurePoints), |
| 58 | + specifiedTruthyTypes: new SpecifiedTypes(), |
| 59 | + specifiedFalseyTypes: new SpecifiedTypes(), |
| 60 | + specifiedNullTypes: new SpecifiedTypes(), |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments