|
7 | 7 | use PhpParser\Node\Expr\BinaryOp; |
8 | 8 | use PhpParser\Node\Expr\BinaryOp\BooleanAnd; |
9 | 9 | use PhpParser\Node\Expr\BinaryOp\BooleanOr; |
| 10 | +use PhpParser\Node\Expr\Variable; |
10 | 11 | use PhpParser\Node\Stmt; |
11 | 12 | use PHPStan\Analyser\ExpressionContext; |
12 | 13 | use PHPStan\Analyser\ExpressionResult; |
|
15 | 16 | use PHPStan\Analyser\InternalThrowPoint; |
16 | 17 | use PHPStan\Analyser\MutatingScope; |
17 | 18 | use PHPStan\Analyser\NodeScopeResolver; |
| 19 | +use PHPStan\Analyser\RicherScopeGetTypeHelper; |
18 | 20 | use PHPStan\DependencyInjection\AutowiredService; |
| 21 | +use PHPStan\Php\PhpVersion; |
| 22 | +use PHPStan\Reflection\InitializerExprTypeResolver; |
| 23 | +use PHPStan\ShouldNotHappenException; |
| 24 | +use PHPStan\Type\BooleanType; |
| 25 | +use PHPStan\Type\Constant\ConstantBooleanType; |
19 | 26 | use PHPStan\Type\Constant\ConstantIntegerType; |
20 | 27 | use PHPStan\Type\ObjectType; |
| 28 | +use PHPStan\Type\Type; |
21 | 29 | use function array_merge; |
| 30 | +use function get_class; |
| 31 | +use function is_string; |
| 32 | +use function sprintf; |
22 | 33 |
|
23 | 34 | /** |
24 | 35 | * @implements ExprHandler<BinaryOp> |
|
27 | 38 | final class BinaryOpHandler implements ExprHandler |
28 | 39 | { |
29 | 40 |
|
| 41 | + public function __construct( |
| 42 | + private InitializerExprTypeResolver $initializerExprTypeResolver, |
| 43 | + private RicherScopeGetTypeHelper $richerScopeGetTypeHelper, |
| 44 | + private PhpVersion $phpVersion, |
| 45 | + ) |
| 46 | + { |
| 47 | + } |
| 48 | + |
30 | 49 | public function supports(Expr $expr): bool |
31 | 50 | { |
32 | 51 | return $expr instanceof BinaryOp |
@@ -70,4 +89,127 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex |
70 | 89 | ); |
71 | 90 | } |
72 | 91 |
|
| 92 | + /** |
| 93 | + * @param BinaryOp $expr |
| 94 | + */ |
| 95 | + public function resolveType(MutatingScope $scope, Expr $expr): Type |
| 96 | + { |
| 97 | + $getType = static fn (Expr $expr): Type => $scope->getType($expr); |
| 98 | + |
| 99 | + if ($expr instanceof BinaryOp\Smaller) { |
| 100 | + return $scope->getType($expr->left)->isSmallerThan($scope->getType($expr->right), $this->phpVersion)->toBooleanType(); |
| 101 | + } |
| 102 | + |
| 103 | + if ($expr instanceof BinaryOp\SmallerOrEqual) { |
| 104 | + return $scope->getType($expr->left)->isSmallerThanOrEqual($scope->getType($expr->right), $this->phpVersion)->toBooleanType(); |
| 105 | + } |
| 106 | + |
| 107 | + if ($expr instanceof BinaryOp\Greater) { |
| 108 | + return $scope->getType($expr->right)->isSmallerThan($scope->getType($expr->left), $this->phpVersion)->toBooleanType(); |
| 109 | + } |
| 110 | + |
| 111 | + if ($expr instanceof BinaryOp\GreaterOrEqual) { |
| 112 | + return $scope->getType($expr->right)->isSmallerThanOrEqual($scope->getType($expr->left), $this->phpVersion)->toBooleanType(); |
| 113 | + } |
| 114 | + |
| 115 | + if ($expr instanceof BinaryOp\Equal) { |
| 116 | + if ( |
| 117 | + $expr->left instanceof Variable |
| 118 | + && is_string($expr->left->name) |
| 119 | + && $expr->right instanceof Variable |
| 120 | + && is_string($expr->right->name) |
| 121 | + && $expr->left->name === $expr->right->name |
| 122 | + ) { |
| 123 | + return new ConstantBooleanType(true); |
| 124 | + } |
| 125 | + |
| 126 | + $leftType = $scope->getType($expr->left); |
| 127 | + $rightType = $scope->getType($expr->right); |
| 128 | + |
| 129 | + return $this->initializerExprTypeResolver->resolveEqualType($leftType, $rightType)->type; |
| 130 | + } |
| 131 | + |
| 132 | + if ($expr instanceof BinaryOp\NotEqual) { |
| 133 | + return $scope->getType(new Expr\BooleanNot(new BinaryOp\Equal($expr->left, $expr->right))); |
| 134 | + } |
| 135 | + |
| 136 | + if ($expr instanceof BinaryOp\Identical) { |
| 137 | + return $this->richerScopeGetTypeHelper->getIdenticalResult($scope, $expr)->type; |
| 138 | + } |
| 139 | + |
| 140 | + if ($expr instanceof BinaryOp\NotIdentical) { |
| 141 | + return $this->richerScopeGetTypeHelper->getNotIdenticalResult($scope, $expr)->type; |
| 142 | + } |
| 143 | + |
| 144 | + if ($expr instanceof BinaryOp\LogicalXor) { |
| 145 | + $leftBooleanType = $scope->getType($expr->left)->toBoolean(); |
| 146 | + $rightBooleanType = $scope->getType($expr->right)->toBoolean(); |
| 147 | + |
| 148 | + if ( |
| 149 | + $leftBooleanType instanceof ConstantBooleanType |
| 150 | + && $rightBooleanType instanceof ConstantBooleanType |
| 151 | + ) { |
| 152 | + return new ConstantBooleanType( |
| 153 | + $leftBooleanType->getValue() xor $rightBooleanType->getValue(), |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + return new BooleanType(); |
| 158 | + } |
| 159 | + |
| 160 | + if ($expr instanceof BinaryOp\Spaceship) { |
| 161 | + return $this->initializerExprTypeResolver->getSpaceshipType($expr->left, $expr->right, $getType); |
| 162 | + } |
| 163 | + |
| 164 | + if ($expr instanceof BinaryOp\Concat) { |
| 165 | + return $this->initializerExprTypeResolver->getConcatType($expr->left, $expr->right, $getType); |
| 166 | + } |
| 167 | + |
| 168 | + if ($expr instanceof BinaryOp\BitwiseAnd) { |
| 169 | + return $this->initializerExprTypeResolver->getBitwiseAndType($expr->left, $expr->right, $getType); |
| 170 | + } |
| 171 | + |
| 172 | + if ($expr instanceof BinaryOp\BitwiseOr) { |
| 173 | + return $this->initializerExprTypeResolver->getBitwiseOrType($expr->left, $expr->right, $getType); |
| 174 | + } |
| 175 | + |
| 176 | + if ($expr instanceof BinaryOp\BitwiseXor) { |
| 177 | + return $this->initializerExprTypeResolver->getBitwiseXorType($expr->left, $expr->right, $getType); |
| 178 | + } |
| 179 | + |
| 180 | + if ($expr instanceof BinaryOp\Div) { |
| 181 | + return $this->initializerExprTypeResolver->getDivType($expr->left, $expr->right, $getType); |
| 182 | + } |
| 183 | + |
| 184 | + if ($expr instanceof BinaryOp\Mod) { |
| 185 | + return $this->initializerExprTypeResolver->getModType($expr->left, $expr->right, $getType); |
| 186 | + } |
| 187 | + |
| 188 | + if ($expr instanceof BinaryOp\Plus) { |
| 189 | + return $this->initializerExprTypeResolver->getPlusType($expr->left, $expr->right, $getType); |
| 190 | + } |
| 191 | + |
| 192 | + if ($expr instanceof BinaryOp\Minus) { |
| 193 | + return $this->initializerExprTypeResolver->getMinusType($expr->left, $expr->right, $getType); |
| 194 | + } |
| 195 | + |
| 196 | + if ($expr instanceof BinaryOp\Mul) { |
| 197 | + return $this->initializerExprTypeResolver->getMulType($expr->left, $expr->right, $getType); |
| 198 | + } |
| 199 | + |
| 200 | + if ($expr instanceof BinaryOp\Pow) { |
| 201 | + return $this->initializerExprTypeResolver->getPowType($expr->left, $expr->right, $getType); |
| 202 | + } |
| 203 | + |
| 204 | + if ($expr instanceof BinaryOp\ShiftLeft) { |
| 205 | + return $this->initializerExprTypeResolver->getShiftLeftType($expr->left, $expr->right, $getType); |
| 206 | + } |
| 207 | + |
| 208 | + if ($expr instanceof BinaryOp\ShiftRight) { |
| 209 | + return $this->initializerExprTypeResolver->getShiftRightType($expr->left, $expr->right, $getType); |
| 210 | + } |
| 211 | + |
| 212 | + throw new ShouldNotHappenException(sprintf('Unhandled %s', get_class($expr))); |
| 213 | + } |
| 214 | + |
73 | 215 | } |
0 commit comments