-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathNonNullabilityHelper.php
More file actions
167 lines (144 loc) · 5.81 KB
/
NonNullabilityHelper.php
File metadata and controls
167 lines (144 loc) · 5.81 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php declare(strict_types = 1);
namespace PHPStan\Analyser\ExprHandler\Helper;
use Closure;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PHPStan\Analyser\EnsuredNonNullabilityResult;
use PHPStan\Analyser\EnsuredNonNullabilityResultExpression;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\TrinaryLogic;
use PHPStan\Type\TypeCombinator;
#[AutowiredService]
final class NonNullabilityHelper
{
public function ensureShallowNonNullability(MutatingScope $scope, Scope $originalScope, Expr $exprToSpecify): EnsuredNonNullabilityResult
{
$exprType = $scope->getType($exprToSpecify);
$isNull = $exprType->isNull();
if ($isNull->yes()) {
return new EnsuredNonNullabilityResult($scope, []);
}
$hasExpressionType = $originalScope->hasExpressionType($exprToSpecify);
$exprTypeWithoutNull = TypeCombinator::removeNull($exprType);
if ($exprType->equals($exprTypeWithoutNull)) {
$originalExprType = $originalScope->getType($exprToSpecify);
if (!$originalExprType->equals($exprTypeWithoutNull)) {
$originalNativeType = $originalScope->getNativeType($exprToSpecify);
return new EnsuredNonNullabilityResult($scope, [
new EnsuredNonNullabilityResultExpression($exprToSpecify, $originalExprType, $originalNativeType, $hasExpressionType),
]);
}
return new EnsuredNonNullabilityResult($scope, []);
}
$specifiedExpressions = [];
// When narrowing an ArrayDimFetch, specifyExpressionType also recursively
// narrows the parent array's offset type via intersection with HasOffsetValueType.
// To properly revert this, we must also save and restore the parent expression's type.
if ($exprToSpecify instanceof Expr\ArrayDimFetch && $exprToSpecify->dim !== null) {
$parentExpr = $exprToSpecify->var;
$specifiedExpressions[] = new EnsuredNonNullabilityResultExpression(
$parentExpr,
$scope->getType($parentExpr),
$scope->getNativeType($parentExpr),
$originalScope->hasExpressionType($parentExpr),
);
}
// keep certainty
$certainty = TrinaryLogic::createYes();
if (!$hasExpressionType->no()) {
$certainty = $hasExpressionType;
}
$nativeType = $scope->getNativeType($exprToSpecify);
$specifiedExpressions[] = new EnsuredNonNullabilityResultExpression($exprToSpecify, $exprType, $nativeType, $certainty);
$scope = $scope->specifyExpressionType(
$exprToSpecify,
$exprTypeWithoutNull,
TypeCombinator::removeNull($nativeType),
TrinaryLogic::createYes(),
);
return new EnsuredNonNullabilityResult(
$scope,
$specifiedExpressions,
);
}
public function ensureNonNullability(MutatingScope $scope, Expr $expr): EnsuredNonNullabilityResult
{
$specifiedExpressions = [];
$originalScope = $scope;
$scope = $this->lookForExpressionCallback($scope, $expr, function ($scope, $expr) use (&$specifiedExpressions, $originalScope) {
$result = $this->ensureShallowNonNullability($scope, $originalScope, $expr);
foreach ($result->getSpecifiedExpressions() as $specifiedExpression) {
$specifiedExpressions[] = $specifiedExpression;
}
return $result->getScope();
});
return new EnsuredNonNullabilityResult($scope, $specifiedExpressions);
}
/**
* @param EnsuredNonNullabilityResultExpression[] $specifiedExpressions
*/
public function revertNonNullability(MutatingScope $scope, array $specifiedExpressions): MutatingScope
{
foreach ($specifiedExpressions as $specifiedExpressionResult) {
if ($specifiedExpressionResult->getCertainty()->no()) {
$scope = $scope->invalidateExpression($specifiedExpressionResult->getExpression());
continue;
}
$scope = $scope->specifyExpressionType(
$specifiedExpressionResult->getExpression(),
$specifiedExpressionResult->getOriginalType(),
$specifiedExpressionResult->getOriginalNativeType(),
$specifiedExpressionResult->getCertainty(),
);
}
return $scope;
}
/**
* Walk a nullsafe chain (NullsafePropertyFetch/NullsafeMethodCall) and narrow
* each intermediate var to non-null. Used so that method call arguments can see
* the narrowed types without affecting the scope during var processing.
*/
public function narrowNullsafeVarChain(MutatingScope $scope, Expr $expr): EnsuredNonNullabilityResult
{
$specifiedExpressions = [];
$currentExpr = $expr;
while ($currentExpr instanceof Expr\NullsafePropertyFetch || $currentExpr instanceof Expr\NullsafeMethodCall) {
$result = $this->ensureShallowNonNullability($scope, $scope, $currentExpr->var);
$scope = $result->getScope();
foreach ($result->getSpecifiedExpressions() as $specifiedExpression) {
$specifiedExpressions[] = $specifiedExpression;
}
$currentExpr = $currentExpr->var;
}
return new EnsuredNonNullabilityResult($scope, $specifiedExpressions);
}
/**
* @param Closure(MutatingScope, Expr): MutatingScope $callback
*/
private function lookForExpressionCallback(MutatingScope $scope, Expr $expr, Closure $callback): MutatingScope
{
if (!$expr instanceof ArrayDimFetch || $expr->dim !== null) {
$scope = $callback($scope, $expr);
}
if ($expr instanceof ArrayDimFetch) {
$scope = $this->lookForExpressionCallback($scope, $expr->var, $callback);
} elseif ($expr instanceof PropertyFetch || $expr instanceof Expr\NullsafePropertyFetch) {
$scope = $this->lookForExpressionCallback($scope, $expr->var, $callback);
} elseif ($expr instanceof StaticPropertyFetch && $expr->class instanceof Expr) {
$scope = $this->lookForExpressionCallback($scope, $expr->class, $callback);
} elseif ($expr instanceof List_) {
foreach ($expr->items as $item) {
if ($item === null) {
continue;
}
$scope = $this->lookForExpressionCallback($scope, $item->value, $callback);
}
}
return $scope;
}
}