forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssetHandler.php
More file actions
130 lines (113 loc) · 3.9 KB
/
IssetHandler.php
File metadata and controls
130 lines (113 loc) · 3.9 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 ArrayAccess;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\ExpressionResult;
use PHPStan\Analyser\ExpressionResultStorage;
use PHPStan\Analyser\ExprHandler;
use PHPStan\Analyser\ExprHandler\Helper\NonNullabilityHelper;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\NoopNodeCallback;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use function array_merge;
use function array_reverse;
/**
* @implements ExprHandler<Isset_>
*/
#[AutowiredService]
final class IssetHandler implements ExprHandler
{
public function __construct(
private NonNullabilityHelper $nonNullabilityHelper,
)
{
}
public function supports(Expr $expr): bool
{
return $expr instanceof Isset_;
}
public function resolveType(MutatingScope $scope, Expr $expr): Type
{
$issetResult = true;
foreach ($expr->vars as $var) {
$result = $scope->issetCheck($var, static function (Type $type): ?bool {
$isNull = $type->isNull();
if ($isNull->maybe()) {
return null;
}
return !$isNull->yes();
});
if ($result !== null) {
if (!$result) {
return new ConstantBooleanType($result);
}
continue;
}
$issetResult = $result;
}
if ($issetResult === null) {
return new BooleanType();
}
return new ConstantBooleanType($issetResult);
}
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
{
$hasYield = false;
$throwPoints = [];
$impurePoints = [];
$nonNullabilityResults = [];
$isAlwaysTerminating = false;
foreach ($expr->vars as $var) {
$nonNullabilityResult = $this->nonNullabilityHelper->ensureNonNullability($scope, $var);
$scope = $nodeScopeResolver->lookForSetAllowedUndefinedExpressions($nonNullabilityResult->getScope(), $var);
$result = $nodeScopeResolver->processExprNode($stmt, $var, $scope, $storage, $nodeCallback, $context->enterDeep());
$scope = $result->getScope();
$hasYield = $hasYield || $result->hasYield();
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
$impurePoints = array_merge($impurePoints, $result->getImpurePoints());
$isAlwaysTerminating = $isAlwaysTerminating || $result->isAlwaysTerminating();
$nonNullabilityResults[] = $nonNullabilityResult;
if (!($var instanceof ArrayDimFetch)) {
continue;
}
$varType = $scope->getType($var->var);
if ($varType->isArray()->yes() || (new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->no()) {
continue;
}
$throwPoints = array_merge($throwPoints, $nodeScopeResolver->processExprNode(
$stmt,
new MethodCall(new TypeExpr($varType), 'offsetExists'),
$scope,
$storage,
new NoopNodeCallback(),
$context,
)->getThrowPoints());
}
foreach (array_reverse($expr->vars) as $var) {
$scope = $nodeScopeResolver->lookForUnsetAllowedUndefinedExpressions($scope, $var);
}
foreach (array_reverse($nonNullabilityResults) as $nonNullabilityResult) {
$scope = $this->nonNullabilityHelper->revertNonNullability($scope, $nonNullabilityResult->getSpecifiedExpressions());
}
return new ExpressionResult(
$scope,
hasYield: $hasYield,
isAlwaysTerminating: $isAlwaysTerminating,
throwPoints: $throwPoints,
impurePoints: $impurePoints,
truthyScopeCallback: static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
falseyScopeCallback: static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
);
}
}