Skip to content

Commit 9eb83b7

Browse files
staabmondrejmirtes
authored andcommitted
Implement BitwiseOrHandler
1 parent 0d92315 commit 9eb83b7

File tree

3 files changed

+73
-0
lines changed

3 files changed

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

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,11 @@ public function getBitwiseOrType(Expr $left, Expr $right, callable $getTypeCallb
10281028
$leftType = $getTypeCallback($left);
10291029
$rightType = $getTypeCallback($right);
10301030

1031+
return $this->getBitwiseOrTypeFromTypes($leftType, $rightType);
1032+
}
1033+
1034+
public function getBitwiseOrTypeFromTypes(Type $leftType, Type $rightType): Type
1035+
{
10311036
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
10321037
return $this->getNeverType($leftType, $rightType);
10331038
}

tests/PHPStan/Analyser/Generator/data/gnsr.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ public function doBitwiseAnd($a, $b, int $c, int $d): void
102102
assertNativeType('int', $c & $d);
103103
}
104104

105+
/**
106+
* @param int $a
107+
* @param int $b
108+
* @return void
109+
*/
110+
public function doBitwiseOr($a, $b, int $c, int $d): void
111+
{
112+
assertType('int', $a | $b);
113+
assertNativeType('int', $a | $b);
114+
assertType('1', 1 | 1);
115+
assertNativeType('1', 1 | 1);
116+
assertType('int', $c | $d);
117+
assertNativeType('int', $c | $d);
118+
}
119+
105120
/**
106121
* @param int $a
107122
* @param int $b

0 commit comments

Comments
 (0)