diff --git a/src/Analyser/Generator/ExprHandler/BinaryDivHandler.php b/src/Analyser/Generator/ExprHandler/BinaryDivHandler.php new file mode 100644 index 0000000000..07b7a76796 --- /dev/null +++ b/src/Analyser/Generator/ExprHandler/BinaryDivHandler.php @@ -0,0 +1,53 @@ + + */ +#[AutowiredService] +final class BinaryDivHandler implements ExprHandler +{ + + public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver) + { + } + + public function supports(Expr $expr): bool + { + return $expr instanceof Expr\BinaryOp\Div; + } + + public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator + { + $leftResult = yield new ExprAnalysisRequest($stmt, $expr->left, $scope, $context, $alternativeNodeCallback); + $rightResult = yield new ExprAnalysisRequest($stmt, $expr->right, $leftResult->scope, $context, $alternativeNodeCallback); + + return new ExprAnalysisResult( + $this->initializerExprTypeResolver->getDivTypeFromTypes($expr->left, $expr->right, $leftResult->type, $rightResult->type), + $this->initializerExprTypeResolver->getDivTypeFromTypes($expr->left, $expr->right, $leftResult->nativeType, $rightResult->nativeType), + $rightResult->scope, + hasYield: $leftResult->hasYield || $rightResult->hasYield, + isAlwaysTerminating: $leftResult->isAlwaysTerminating || $rightResult->isAlwaysTerminating, + throwPoints: array_merge($leftResult->throwPoints, $rightResult->throwPoints), + impurePoints: array_merge($leftResult->impurePoints, $rightResult->impurePoints), + specifiedTruthyTypes: new SpecifiedTypes(), + specifiedFalseyTypes: new SpecifiedTypes(), + specifiedNullTypes: new SpecifiedTypes(), + ); + } + +} diff --git a/src/Reflection/InitializerExprTypeResolver.php b/src/Reflection/InitializerExprTypeResolver.php index e9175d7cff..60be07e9f4 100644 --- a/src/Reflection/InitializerExprTypeResolver.php +++ b/src/Reflection/InitializerExprTypeResolver.php @@ -1174,6 +1174,11 @@ public function getDivType(Expr $left, Expr $right, callable $getTypeCallback): $leftType = $getTypeCallback($left); $rightType = $getTypeCallback($right); + return $this->getDivTypeFromTypes($left, $right, $leftType, $rightType); + } + + public function getDivTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type + { $leftTypes = $leftType->getConstantScalarTypes(); $rightTypes = $rightType->getConstantScalarTypes(); $leftTypesCount = count($leftTypes); diff --git a/tests/PHPStan/Analyser/Generator/data/gnsr.php b/tests/PHPStan/Analyser/Generator/data/gnsr.php index 723a07fc69..f6948ab716 100644 --- a/tests/PHPStan/Analyser/Generator/data/gnsr.php +++ b/tests/PHPStan/Analyser/Generator/data/gnsr.php @@ -37,6 +37,21 @@ public function doPlus($a, $b, int $c, int $d): void assertNativeType('int', $c + $d); } + /** + * @param int $a + * @param int $b + * @return void + */ + public function doDiv($a, $b, int $c, int $d): void + { + assertType('(float|int)', $a / $b); + assertNativeType('(float|int)', $a / $b); + assertType('1', 1 / 1); + assertNativeType('1', 1 / 1); + assertType('(float|int)', $c / $d); + assertNativeType('(float|int)', $c / $d); + } + /** * @param int $a * @param int $b