-
Notifications
You must be signed in to change notification settings - Fork 571
Expand file tree
/
Copy pathArrayDimFetchHandler.php
More file actions
145 lines (128 loc) · 4.71 KB
/
ArrayDimFetchHandler.php
File metadata and controls
145 lines (128 loc) · 4.71 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
<?php declare(strict_types = 1);
namespace PHPStan\Analyser\ExprHandler;
use ArrayAccess;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\ExpressionResult;
use PHPStan\Analyser\ExpressionResultFactory;
use PHPStan\Analyser\ExpressionResultStorage;
use PHPStan\Analyser\ExprHandler;
use PHPStan\Analyser\ExprHandler\Helper\NullsafeShortCircuitingHelper;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\NoopNodeCallback;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use function array_merge;
/**
* @implements ExprHandler<ArrayDimFetch>
*/
#[AutowiredService]
final class ArrayDimFetchHandler implements ExprHandler
{
public function __construct(
private ExpressionResultFactory $expressionResultFactory,
)
{
}
public function supports(Expr $expr): bool
{
return $expr instanceof ArrayDimFetch;
}
public function resolveType(MutatingScope $scope, Expr $expr): Type
{
if ($expr->dim === null) {
return new NeverType();
}
$offsetAccessibleType = $scope->getType($expr->var);
if (
!$offsetAccessibleType->isArray()->yes()
&& (new ObjectType(ArrayAccess::class))->isSuperTypeOf($offsetAccessibleType)->yes()
) {
return NullsafeShortCircuitingHelper::getType(
$scope,
$expr->var,
$scope->getType(
new MethodCall(
$expr->var,
new Identifier('offsetGet'),
[
new Arg($expr->dim),
],
),
),
);
}
$offsetType = $scope->getType($expr->dim);
return NullsafeShortCircuitingHelper::getType(
$scope,
$expr->var,
$offsetAccessibleType->getOffsetValueType($offsetType),
);
}
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
{
if ($expr->dim === null) {
$varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep());
$scope = $varResult->getScope();
return $this->expressionResultFactory->create(
$expr,
$scope,
typeCallback: static fn () => new NeverType(),
hasYield: $varResult->hasYield(),
isAlwaysTerminating: $varResult->isAlwaysTerminating(),
throwPoints: $varResult->getThrowPoints(),
impurePoints: $varResult->getImpurePoints(),
truthyScopeCallback: static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
falseyScopeCallback: static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
);
}
$dimResult = $nodeScopeResolver->processExprNode($stmt, $expr->dim, $scope, $storage, $nodeCallback, $context->enterDeep());
$varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $dimResult->getScope(), $storage, $nodeCallback, $context->enterDeep());
$throwPoints = array_merge($dimResult->getThrowPoints(), $varResult->getThrowPoints());
$impurePoints = array_merge($dimResult->getImpurePoints(), $varResult->getImpurePoints());
$scope = $varResult->getScope();
$offsetGetResult = $nodeScopeResolver->processExprNode(
$stmt,
new MethodCall($expr->var, new Identifier('offsetGet'), [new Arg($expr->dim)]),
$scope,
new ExpressionResultStorage(),
new NoopNodeCallback(),
$context->enterDeep(),
);
$varType = $varResult->getType();
if (!$varType->isArray()->yes() && !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->no()) {
$throwPoints = array_merge($throwPoints, $offsetGetResult->getThrowPoints());
}
return $this->expressionResultFactory->create(
$expr,
$scope,
typeCallback: static function (Expr $uninteresting, MutatingScope $scope) use ($varResult, $dimResult, $offsetGetResult): Type {
$varType = $varResult->getTypeForScope($scope);
if ($varType instanceof NeverType) {
return $varType;
}
if (
!$varType->isArray()->yes()
&& (new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->yes()
) {
return $offsetGetResult->getTypeForScope($scope);
}
return $varType->getOffsetValueType($dimResult->getTypeForScope($scope));
},
hasYield: $dimResult->hasYield() || $varResult->hasYield(),
isAlwaysTerminating: $dimResult->isAlwaysTerminating() || $varResult->isAlwaysTerminating(),
throwPoints: $throwPoints,
impurePoints: $impurePoints,
truthyScopeCallback: static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
falseyScopeCallback: static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
);
}
}