|
4 | 4 |
|
5 | 5 | use PhpParser\Node\Expr; |
6 | 6 | use PhpParser\Node\Expr\BinaryOp\BooleanAnd; |
| 7 | +use PhpParser\Node\Expr\BinaryOp\BooleanOr; |
7 | 8 | use PhpParser\Node\Expr\BinaryOp\LogicalAnd; |
| 9 | +use PhpParser\Node\Expr\BinaryOp\LogicalOr; |
8 | 10 | use PhpParser\Node\Stmt; |
9 | 11 | use PHPStan\Analyser\ExpressionContext; |
10 | 12 | use PHPStan\Analyser\ExpressionResult; |
11 | 13 | use PHPStan\Analyser\ExpressionResultStorage; |
12 | 14 | use PHPStan\Analyser\ExprHandler; |
13 | 15 | use PHPStan\Analyser\MutatingScope; |
14 | 16 | use PHPStan\Analyser\NodeScopeResolver; |
| 17 | +use PHPStan\Analyser\NoopNodeCallback; |
15 | 18 | use PHPStan\DependencyInjection\AutowiredService; |
16 | 19 | use PHPStan\Node\BooleanAndNode; |
| 20 | +use PHPStan\Type\BooleanType; |
| 21 | +use PHPStan\Type\Constant\ConstantBooleanType; |
17 | 22 | use PHPStan\Type\NeverType; |
| 23 | +use PHPStan\Type\Type; |
18 | 24 | use function array_merge; |
19 | 25 |
|
20 | 26 | /** |
|
24 | 30 | final class BooleanAndHandler implements ExprHandler |
25 | 31 | { |
26 | 32 |
|
| 33 | + private const BOOLEAN_EXPRESSION_MAX_PROCESS_DEPTH = 4; |
| 34 | + |
| 35 | + public function __construct( |
| 36 | + private NodeScopeResolver $nodeScopeResolver, |
| 37 | + ) |
| 38 | + { |
| 39 | + } |
| 40 | + |
27 | 41 | public function supports(Expr $expr): bool |
28 | 42 | { |
29 | 43 | return $expr instanceof BooleanAnd || $expr instanceof LogicalAnd; |
30 | 44 | } |
31 | 45 |
|
| 46 | + /** |
| 47 | + * @param BooleanAnd|LogicalAnd $expr |
| 48 | + */ |
| 49 | + public function resolveType(MutatingScope $scope, Expr $expr): Type |
| 50 | + { |
| 51 | + $leftBooleanType = $scope->getType($expr->left)->toBoolean(); |
| 52 | + if ($leftBooleanType->isFalse()->yes()) { |
| 53 | + return new ConstantBooleanType(false); |
| 54 | + } |
| 55 | + |
| 56 | + if (self::getBooleanExpressionDepth($expr->left) <= self::BOOLEAN_EXPRESSION_MAX_PROCESS_DEPTH) { |
| 57 | + $leftResult = $this->nodeScopeResolver->processExprNode(new Stmt\Expression($expr->left), $expr->left, $scope, new ExpressionResultStorage(), new NoopNodeCallback(), ExpressionContext::createDeep()); |
| 58 | + $rightBooleanType = $leftResult->getTruthyScope()->getType($expr->right)->toBoolean(); |
| 59 | + } else { |
| 60 | + $rightBooleanType = $scope->filterByTruthyValue($expr->left)->getType($expr->right)->toBoolean(); |
| 61 | + } |
| 62 | + |
| 63 | + if ($rightBooleanType->isFalse()->yes()) { |
| 64 | + return new ConstantBooleanType(false); |
| 65 | + } |
| 66 | + |
| 67 | + if ( |
| 68 | + $leftBooleanType->isTrue()->yes() |
| 69 | + && $rightBooleanType->isTrue()->yes() |
| 70 | + ) { |
| 71 | + return new ConstantBooleanType(true); |
| 72 | + } |
| 73 | + |
| 74 | + return new BooleanType(); |
| 75 | + } |
| 76 | + |
| 77 | + public static function getBooleanExpressionDepth(Expr $expr, int $depth = 0): int |
| 78 | + { |
| 79 | + while ( |
| 80 | + $expr instanceof BooleanOr |
| 81 | + || $expr instanceof LogicalOr |
| 82 | + || $expr instanceof BooleanAnd |
| 83 | + || $expr instanceof LogicalAnd |
| 84 | + ) { |
| 85 | + return self::getBooleanExpressionDepth($expr->left, $depth + 1); |
| 86 | + } |
| 87 | + |
| 88 | + return $depth; |
| 89 | + } |
| 90 | + |
32 | 91 | public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult |
33 | 92 | { |
34 | 93 | $leftResult = $nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep()); |
|
0 commit comments