From 83497796a3bccd7d44d890eed4a424daaaf1b478 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 29 Nov 2025 15:15:23 +0100 Subject: [PATCH] Implement BitwiseOrHandler --- .../ExprHandler/BitwiseOrHandler.php | 53 +++++++++++++++++++ .../InitializerExprTypeResolver.php | 5 ++ .../PHPStan/Analyser/Generator/data/gnsr.php | 15 ++++++ 3 files changed, 73 insertions(+) create mode 100644 src/Analyser/Generator/ExprHandler/BitwiseOrHandler.php diff --git a/src/Analyser/Generator/ExprHandler/BitwiseOrHandler.php b/src/Analyser/Generator/ExprHandler/BitwiseOrHandler.php new file mode 100644 index 0000000000..e51a235c8c --- /dev/null +++ b/src/Analyser/Generator/ExprHandler/BitwiseOrHandler.php @@ -0,0 +1,53 @@ + + */ +#[AutowiredService] +final class BitwiseOrHandler implements ExprHandler +{ + + public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver) + { + } + + public function supports(Expr $expr): bool + { + return $expr instanceof Expr\BinaryOp\BitwiseOr; + } + + 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->getBitwiseOrTypeFromTypes($leftResult->type, $rightResult->type), + $this->initializerExprTypeResolver->getBitwiseOrTypeFromTypes($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 27ceac0a4a..3b4e617672 100644 --- a/src/Reflection/InitializerExprTypeResolver.php +++ b/src/Reflection/InitializerExprTypeResolver.php @@ -1028,6 +1028,11 @@ public function getBitwiseOrType(Expr $left, Expr $right, callable $getTypeCallb $leftType = $getTypeCallback($left); $rightType = $getTypeCallback($right); + return $this->getBitwiseOrTypeFromTypes($leftType, $rightType); + } + + public function getBitwiseOrTypeFromTypes(Type $leftType, Type $rightType): Type + { if ($leftType instanceof NeverType || $rightType instanceof NeverType) { return $this->getNeverType($leftType, $rightType); } diff --git a/tests/PHPStan/Analyser/Generator/data/gnsr.php b/tests/PHPStan/Analyser/Generator/data/gnsr.php index b915536172..4f0efcf4b0 100644 --- a/tests/PHPStan/Analyser/Generator/data/gnsr.php +++ b/tests/PHPStan/Analyser/Generator/data/gnsr.php @@ -82,6 +82,21 @@ public function doBitwiseAnd($a, $b, int $c, int $d): void assertNativeType('int', $c & $d); } + /** + * @param int $a + * @param int $b + * @return void + */ + public function doBitwiseOr($a, $b, int $c, int $d): void + { + assertType('int', $a | $b); + assertNativeType('int', $a | $b); + assertType('1', 1 | 1); + assertNativeType('1', 1 | 1); + assertType('int', $c | $d); + assertNativeType('int', $c | $d); + } + /** * @param int $a * @param int $b