-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathTernaryHandler.php
More file actions
130 lines (112 loc) · 4.65 KB
/
TernaryHandler.php
File metadata and controls
130 lines (112 loc) · 4.65 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php declare(strict_types = 1);
namespace PHPStan\Analyser\ExprHandler;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Ternary;
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\Type\Type;
use PHPStan\Type\TypeCombinator;
use function array_merge;
/**
* @implements ExprHandler<Ternary>
*/
#[AutowiredService]
final class TernaryHandler implements ExprHandler
{
public function __construct(
private NodeScopeResolver $nodeScopeResolver,
)
{
}
public function supports(Expr $expr): bool
{
return $expr instanceof Ternary;
}
public function resolveType(MutatingScope $scope, Expr $expr): Type
{
$condResult = $this->nodeScopeResolver->processExprNode(new Stmt\Expression($expr->cond), $expr->cond, $scope, new ExpressionResultStorage(), new NoopNodeCallback(), ExpressionContext::createDeep());
if ($expr->if === null) {
$conditionType = $scope->getType($expr->cond);
$booleanConditionType = $conditionType->toBoolean();
if ($booleanConditionType->isTrue()->yes()) {
return $condResult->getTruthyScope()->getType($expr->cond);
}
if ($booleanConditionType->isFalse()->yes()) {
return $condResult->getFalseyScope()->getType($expr->else);
}
return TypeCombinator::union(
TypeCombinator::removeFalsey($condResult->getTruthyScope()->getType($expr->cond)),
$condResult->getFalseyScope()->getType($expr->else),
);
}
$booleanConditionType = $scope->getType($expr->cond)->toBoolean();
if ($booleanConditionType->isTrue()->yes()) {
return $condResult->getTruthyScope()->getType($expr->if);
}
if ($booleanConditionType->isFalse()->yes()) {
return $condResult->getFalseyScope()->getType($expr->else);
}
return TypeCombinator::union(
$condResult->getTruthyScope()->getType($expr->if),
$condResult->getFalseyScope()->getType($expr->else),
);
}
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
{
$ternaryCondResult = $nodeScopeResolver->processExprNode($stmt, $expr->cond, $scope, $storage, $nodeCallback, $context->enterDeep());
$throwPoints = $ternaryCondResult->getThrowPoints();
$impurePoints = $ternaryCondResult->getImpurePoints();
$ifTrueScope = $ternaryCondResult->getTruthyScope();
$ifFalseScope = $ternaryCondResult->getFalseyScope();
$ifTrueType = null;
if ($expr->if === null) {
$elseResult = $nodeScopeResolver->processExprNode($stmt, $expr->else, $ifFalseScope, $storage, $nodeCallback, $context);
$throwPoints = array_merge($throwPoints, $elseResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $elseResult->getImpurePoints());
$ifFalseScope = $elseResult->getScope();
} else {
$ifResult = $nodeScopeResolver->processExprNode($stmt, $expr->if, $ifTrueScope, $storage, $nodeCallback, $context);
$throwPoints = array_merge($throwPoints, $ifResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $ifResult->getImpurePoints());
$ifTrueScope = $ifResult->getScope();
$ifTrueType = $ifTrueScope->getType($expr->if);
$elseResult = $nodeScopeResolver->processExprNode($stmt, $expr->else, $ifFalseScope, $storage, $nodeCallback, $context);
$throwPoints = array_merge($throwPoints, $elseResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $elseResult->getImpurePoints());
$ifFalseScope = $elseResult->getScope();
}
$condType = $scope->getType($expr->cond);
if ($condType->isTrue()->yes()) {
$finalScope = $ifTrueScope;
} elseif ($condType->isFalse()->yes()) {
$finalScope = $ifFalseScope;
} else {
if ($ifTrueType !== null && $ifTrueType->isExplicitNever()->yes()) {
$finalScope = $ifFalseScope;
} else {
$ifFalseType = $ifFalseScope->getType($expr->else);
if ($ifFalseType->isExplicitNever()->yes()) {
$finalScope = $ifTrueScope;
} else {
$finalScope = $ifTrueScope->mergeWith($ifFalseScope);
}
}
}
return new ExpressionResult(
$finalScope,
hasYield: $ternaryCondResult->hasYield(),
isAlwaysTerminating: $ternaryCondResult->isAlwaysTerminating(),
throwPoints: $throwPoints,
impurePoints: $impurePoints,
truthyScopeCallback: static fn (): MutatingScope => $finalScope->filterByTruthyValue($expr),
falseyScopeCallback: static fn (): MutatingScope => $finalScope->filterByFalseyValue($expr),
);
}
}