forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooleanOrHandler.php
More file actions
96 lines (81 loc) · 3.59 KB
/
BooleanOrHandler.php
File metadata and controls
96 lines (81 loc) · 3.59 KB
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
<?php declare(strict_types = 1);
namespace PHPStan\Analyser\ExprHandler;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\LogicalOr;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\ExpressionResult;
use PHPStan\Analyser\ExpressionResultStorage;
use PHPStan\Analyser\ExprHandler;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\NoopNodeCallback;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\BooleanOrNode;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Type;
use function array_merge;
/**
* @implements ExprHandler<BooleanOr|LogicalOr>
*/
#[AutowiredService]
final class BooleanOrHandler implements ExprHandler
{
private const BOOLEAN_EXPRESSION_MAX_PROCESS_DEPTH = 4;
public function __construct(
private NodeScopeResolver $nodeScopeResolver,
)
{
}
public function supports(Expr $expr): bool
{
return $expr instanceof BooleanOr || $expr instanceof LogicalOr;
}
public function resolveType(MutatingScope $scope, Expr $expr): Type
{
$leftBooleanType = $scope->getType($expr->left)->toBoolean();
if ($leftBooleanType->isTrue()->yes()) {
return new ConstantBooleanType(true);
}
if (BooleanAndHandler::getBooleanExpressionDepth($expr->left) <= self::BOOLEAN_EXPRESSION_MAX_PROCESS_DEPTH) {
$leftResult = $this->nodeScopeResolver->processExprNode(new Stmt\Expression($expr->left), $expr->left, $scope, new ExpressionResultStorage(), new NoopNodeCallback(), ExpressionContext::createDeep());
$rightBooleanType = $leftResult->getFalseyScope()->getType($expr->right)->toBoolean();
} else {
$rightBooleanType = $scope->filterByFalseyValue($expr->left)->getType($expr->right)->toBoolean();
}
if ($rightBooleanType->isTrue()->yes()) {
return new ConstantBooleanType(true);
}
if (
$leftBooleanType->isFalse()->yes()
&& $rightBooleanType->isFalse()->yes()
) {
return new ConstantBooleanType(false);
}
return new BooleanType();
}
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
{
$leftResult = $nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep());
$leftFalseyScope = $leftResult->getFalseyScope();
$rightResult = $nodeScopeResolver->processExprNode($stmt, $expr->right, $leftFalseyScope, $storage, $nodeCallback, $context);
$rightExprType = $rightResult->getScope()->getType($expr->right);
if ($rightExprType->isExplicitNever()->yes()) {
$leftMergedWithRightScope = $leftResult->getTruthyScope();
} else {
$leftMergedWithRightScope = $leftResult->getScope()->mergeWith($rightResult->getScope());
}
$nodeScopeResolver->callNodeCallbackWithExpression($nodeCallback, new BooleanOrNode($expr, $leftFalseyScope), $scope, $storage, $context);
return new ExpressionResult(
$leftMergedWithRightScope,
hasYield: $leftResult->hasYield() || $rightResult->hasYield(),
isAlwaysTerminating: $leftResult->isAlwaysTerminating(),
throwPoints: array_merge($leftResult->getThrowPoints(), $rightResult->getThrowPoints()),
impurePoints: array_merge($leftResult->getImpurePoints(), $rightResult->getImpurePoints()),
truthyScopeCallback: static fn (): MutatingScope => $leftMergedWithRightScope->filterByTruthyValue($expr),
falseyScopeCallback: static fn (): MutatingScope => $rightResult->getScope()->filterByFalseyValue($expr->right),
);
}
}