forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayKeyExistsFunctionTypeSpecifyingExtension.php
More file actions
157 lines (137 loc) · 4.05 KB
/
ArrayKeyExistsFunctionTypeSpecifyingExtension.php
File metadata and controls
157 lines (137 loc) · 4.05 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
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\TypeCombinator;
use function count;
use function in_array;
#[AutowiredService]
final class ArrayKeyExistsFunctionTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
{
private TypeSpecifier $typeSpecifier;
public function __construct(
private PhpVersion $phpVersion,
)
{
}
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}
public function isFunctionSupported(
FunctionReflection $functionReflection,
FuncCall $node,
TypeSpecifierContext $context,
): bool
{
return in_array($functionReflection->getName(), ['array_key_exists', 'key_exists'], true)
&& !$context->null();
}
public function specifyTypes(
FunctionReflection $functionReflection,
FuncCall $node,
Scope $scope,
TypeSpecifierContext $context,
): SpecifiedTypes
{
$args = $node->getArgs();
if (count($args) < 2) {
return new SpecifiedTypes();
}
$key = $args[0]->value;
$array = $args[1]->value;
$keyType = $scope->getType($key)->toArrayKey();
$arrayType = $scope->getType($array);
if (
!$keyType instanceof ConstantIntegerType
&& !$keyType instanceof ConstantStringType
) {
if ($context->true()) {
$specifiedTypes = new SpecifiedTypes();
$nonEmptyType = $arrayType->isArray()->yes()
? new NonEmptyArrayType()
: TypeCombinator::intersect(new ArrayType(new MixedType(), new MixedType()), new NonEmptyArrayType());
$specifiedTypes = $specifiedTypes->unionWith($this->typeSpecifier->create(
$array,
$nonEmptyType,
$context,
$scope,
));
if ($arrayType->isIterableAtLeastOnce()->no()) {
return $specifiedTypes;
}
$arrayKeyType = $arrayType->getIterableKeyType();
if ($keyType->isString()->yes()) {
$arrayKeyType = $arrayKeyType->toString();
} elseif ($keyType->isString()->maybe()) {
$arrayKeyType = TypeCombinator::union($arrayKeyType, $arrayKeyType->toString());
}
$specifiedTypes = $specifiedTypes->unionWith($this->typeSpecifier->create(
$key,
$arrayKeyType,
$context,
$scope,
));
$arrayDimFetch = new ArrayDimFetch(
$array,
$key,
);
return $specifiedTypes->unionWith($this->typeSpecifier->create(
$arrayDimFetch,
$arrayType->getIterableValueType(),
$context,
$scope,
))->setRootExpr(new Identical($arrayDimFetch, new ConstFetch(new Name('__PHPSTAN_FAUX_CONSTANT'))));
}
return new SpecifiedTypes();
}
if ($context->true()) {
$type = new IntersectionType([
new ArrayType(new MixedType(), new MixedType()),
new HasOffsetType($keyType),
]);
} elseif ($this->phpVersion->throwsValueErrorForInternalFunctions()) {
$specifiedTypes = $this->typeSpecifier->create(
$array,
new HasOffsetType($keyType),
$context,
$scope,
);
$type = new ArrayType(new MixedType(), new MixedType());
$type = $type->unsetOffset($keyType);
return $specifiedTypes->unionWith($this->typeSpecifier->create(
$array,
$type,
$context->negate(),
$scope,
));
} else {
$type = new HasOffsetType($keyType);
}
return $this->typeSpecifier->create(
$array,
$type,
$context,
$scope,
);
}
}