-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RexFunctionsDynamicReturnTypeExtension.php
157 lines (132 loc) · 4.16 KB
/
RexFunctionsDynamicReturnTypeExtension.php
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 rexstan;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use rex_request;
use function count;
use function in_array;
final class RexFunctionsDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension, DynamicStaticMethodReturnTypeExtension
{
public function getClass(): string
{
return rex_request::class;
}
public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return in_array(
strtolower($methodReflection->getName()),
['get', 'post', 'request', 'server', 'session', 'cookie', 'files', 'env'],
true
);
}
public function getTypeFromStaticMethodCall(
MethodReflection $methodReflection,
StaticCall $methodCall,
Scope $scope
): ?Type {
return $this->getType($methodCall->getArgs(), $scope);
}
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), ['rex_get', 'rex_post', 'rex_request', 'rex_server', 'rex_session', 'rex_cookie', 'rex_files', 'rex_env'], true);
}
public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope
): ?Type {
return $this->getType($functionCall->getArgs(), $scope);
}
/**
* @param Arg[] $args
*/
private function getType(array $args, Scope $scope): ?Type
{
if (count($args) < 2) {
return null;
}
$defaultArgType = new ConstantStringType('');
if (count($args) >= 3) {
$defaultArgType = $scope->getType($args[2]->value);
}
$typeStrings = $scope->getType($args[1]->value)->getConstantStrings();
if (count($typeStrings) > 0) {
$results = [];
foreach ($typeStrings as $typeString) {
$resolvedType = $this->resolveTypeFromString($typeString->getValue());
if ($resolvedType === null) {
return null;
}
$results[] = $resolvedType;
}
return TypeCombinator::union($defaultArgType, ...$results);
}
return null;
}
private function resolveTypeFromString(string $vartype): ?Type
{
if (in_array($vartype, [
'bool',
'boolean',
], true)) {
return new BooleanType();
}
if (in_array($vartype, [
'int',
'integer',
], true)) {
return new IntegerType();
}
if (in_array($vartype, [
'double',
'float',
'real',
], true)) {
return new FloatType();
}
if (in_array($vartype, [
'string',
], true)) {
return new StringType();
}
if (in_array($vartype, [
'object',
], true)) {
return new ObjectWithoutClassType();
}
if (in_array($vartype, [
'array',
], true)) {
return new ArrayType(new MixedType(), new MixedType());
}
if (preg_match('/^array\[(.+)\]$/', $vartype, $match) === 1) {
$valueType = $this->resolveTypeFromString($match[1]);
if ($valueType === null) {
throw new ShouldNotHappenException();
}
return new ArrayType(
new MixedType(),
$valueType
);
}
return null;
}
}