From 36f905c84417ef28b5c401fdb0d0351971b039cb Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 29 Nov 2025 13:13:35 +0100 Subject: [PATCH] Implement BinaryMinusHandler --- .../ExprHandler/BinaryMinusHandler.php | 53 +++++++++++++++++++ .../InitializerExprTypeResolver.php | 5 ++ .../PHPStan/Analyser/Generator/data/gnsr.php | 15 ++++++ 3 files changed, 73 insertions(+) create mode 100644 src/Analyser/Generator/ExprHandler/BinaryMinusHandler.php diff --git a/src/Analyser/Generator/ExprHandler/BinaryMinusHandler.php b/src/Analyser/Generator/ExprHandler/BinaryMinusHandler.php new file mode 100644 index 0000000000..4297eeaec9 --- /dev/null +++ b/src/Analyser/Generator/ExprHandler/BinaryMinusHandler.php @@ -0,0 +1,53 @@ + + */ +#[AutowiredService] +final class BinaryMinusHandler implements ExprHandler +{ + + public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver) + { + } + + public function supports(Expr $expr): bool + { + return $expr instanceof Expr\BinaryOp\Minus; + } + + 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->getMinusTypeFromTypes($expr->left, $expr->right, $leftResult->type, $rightResult->type), + $this->initializerExprTypeResolver->getMinusTypeFromTypes($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 55c47a0a5b..e9175d7cff 100644 --- a/src/Reflection/InitializerExprTypeResolver.php +++ b/src/Reflection/InitializerExprTypeResolver.php @@ -1525,6 +1525,11 @@ public function getMinusType(Expr $left, Expr $right, callable $getTypeCallback) $leftType = $getTypeCallback($left); $rightType = $getTypeCallback($right); + return $this->getMinusTypeFromTypes($left, $right, $leftType, $rightType); + } + + public function getMinusTypeFromTypes(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 04f374a1b5..723a07fc69 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 doMinus($a, $b, int $c, int $d): void + { + assertType('int', $a - $b); + assertNativeType('(float|int)', $a - $b); + assertType('0', 1 - 1); + assertNativeType('0', 1 - 1); + assertType('int', $c - $d); + assertNativeType('int', $c - $d); + } + } function (): void {