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