-
Notifications
You must be signed in to change notification settings - Fork 426
/
BitwiseFlagHelper.php
107 lines (86 loc) · 2.79 KB
/
BitwiseFlagHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BitwiseOr;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantIntegerType;
final class BitwiseFlagHelper
{
public function __construct(private ReflectionProvider $reflectionProvider)
{
}
/**
* @param non-empty-string $constName
*/
public function bitwiseOrContainsConstant(Expr $expr, Scope $scope, string $constName): TrinaryLogic
{
if ($expr instanceof ConstFetch) {
if (((string) $expr->name) === $constName) {
return TrinaryLogic::createYes();
}
$resolveConstantName = $this->reflectionProvider->resolveConstantName($expr->name, $scope);
if ($resolveConstantName !== null) {
if ($resolveConstantName === $constName) {
return TrinaryLogic::createYes();
}
return TrinaryLogic::createNo();
}
}
if ($expr instanceof BitwiseOr) {
return TrinaryLogic::createFromBoolean($this->bitwiseOrContainsConstant($expr->left, $scope, $constName)->yes() ||
$this->bitwiseOrContainsConstant($expr->right, $scope, $constName)->yes());
}
$fqcn = new FullyQualified($constName);
if ($this->reflectionProvider->hasConstant($fqcn, $scope)) {
$constant = $this->reflectionProvider->getConstant($fqcn, $scope);
$valueType = $constant->getValueType();
if ($valueType instanceof ConstantIntegerType) {
return $this->exprContainsIntFlag($expr, $scope, $valueType->getValue());
}
}
return TrinaryLogic::createNo();
}
private function exprContainsIntFlag(Expr $expr, Scope $scope, int $flag): TrinaryLogic
{
$exprType = $scope->getType($expr);
if ($exprType instanceof UnionType) {
$allTypesContainFlag = true;
$someTypesContainFlag = false;
foreach ($exprType->getTypes() as $type) {
$containsFlag = $this->typeContainsIntFlag($type, $flag);
if (!$containsFlag->yes()) {
$allTypesContainFlag = false;
}
if (!$containsFlag->yes() && !$containsFlag->maybe()) {
continue;
}
$someTypesContainFlag = true;
}
if ($allTypesContainFlag) {
return TrinaryLogic::createYes();
}
if ($someTypesContainFlag) {
return TrinaryLogic::createMaybe();
}
return TrinaryLogic::createNo();
}
return $this->typeContainsIntFlag($exprType, $flag);
}
private function typeContainsIntFlag(Type $type, int $flag): TrinaryLogic
{
if ($type instanceof ConstantIntegerType) {
if (($type->getValue() & $flag) === $flag) {
return TrinaryLogic::createYes();
}
return TrinaryLogic::createNo();
}
if ($type->isInteger()->yes() || $type instanceof MixedType) {
return TrinaryLogic::createMaybe();
}
return TrinaryLogic::createNo();
}
}