Repository navigation
Expand file tree
/
Copy pathPhpDocsResolver.php
More file actions
303 lines (271 loc) · 10.7 KB
/
Copy pathPhpDocsResolver.php
File metadata and controls
303 lines (271 loc) · 10.7 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\PhpDoc\Tag\VarTag;
use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\ClassReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Turbo\ShadowedByTurboExtension;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\StaticType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;
use function array_key_exists;
use function array_map;
use function count;
use function is_string;
use function sprintf;
use function strtolower;
use function trim;
#[AutowiredService]
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/PhpDocsResolver.cpp')]
final class PhpDocsResolver
{
public function __construct(
private FileTypeMapper $fileTypeMapper,
private PhpDocInheritanceResolver $phpDocInheritanceResolver,
)
{
}
/**
* @return array{TemplateTypeMap, array<string, Type>, array<string, bool>, array<string, Type>, ?Type, ?Type, ?string, bool, bool, bool, bool|null, bool, bool, string|null, Assertions, ?Type, array<string, Type>, array<(string|int), VarTag>, bool, ?ResolvedPhpDocBlock, array<string, bool>, array<string, bool>}
*/
public function getPhpDocs(Scope $scope, Node\FunctionLike|Node\Stmt\Property $node): array
{
$templateTypeMap = TemplateTypeMap::createEmpty();
$phpDocParameterTypes = [];
$phpDocImmediatelyInvokedCallableParameters = [];
$phpDocClosureThisTypeParameters = [];
$phpDocReturnType = null;
$phpDocThrowType = null;
$deprecatedDescription = null;
$isDeprecated = false;
$isInternal = false;
$isFinal = false;
$isPure = null;
$isAllowedPrivateMutation = false;
$acceptsNamedArguments = true;
$isReadOnly = $scope->isInClass() && $scope->getClassReflection()->isImmutable();
$asserts = Assertions::createEmpty();
$selfOutType = null;
$docComment = $node->getDocComment() !== null
? $node->getDocComment()->getText()
: null;
$file = $scope->getFile();
$class = $scope->isInClass() ? $scope->getClassReflection()->getName() : null;
$trait = $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null;
$resolvedPhpDoc = null;
$functionName = null;
$phpDocParameterOutTypes = [];
$phpDocPureUnlessCallableIsImpureParameters = [];
$phpDocPureUnlessParameterPassedParameters = [];
if ($node instanceof Node\Stmt\ClassMethod) {
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}
$functionName = $node->name->name;
$positionalParameterNames = array_map(static function (Node\Param $param): string {
if (!$param->var instanceof Variable || !is_string($param->var->name)) {
throw new ShouldNotHappenException();
}
return $param->var->name;
}, $node->getParams());
$currentResolvedPhpDoc = null;
if ($docComment !== null) {
$currentResolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
$file,
$class,
$trait,
$node->name->name,
$docComment,
);
}
$methodNameForInheritance = $node->getAttribute('originalTraitMethodName') ?? $node->name->name;
$resolvedPhpDoc = $this->phpDocInheritanceResolver->resolvePhpDocForMethod(
$scope->getClassReflection(),
$methodNameForInheritance,
$currentResolvedPhpDoc,
$positionalParameterNames,
);
if ($node->name->toLowerString() === '__construct') {
foreach ($node->params as $param) {
if ($param->flags === 0) {
continue;
}
if ($param->getDocComment() === null) {
continue;
}
if (
!$param->var instanceof Variable
|| !is_string($param->var->name)
) {
throw new ShouldNotHappenException();
}
$paramPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
$file,
$class,
$trait,
'__construct',
$param->getDocComment()->getText(),
);
$varTags = $paramPhpDoc->getVarTags();
if (isset($varTags[0]) && count($varTags) === 1) {
$phpDocType = $varTags[0]->getType();
} elseif (isset($varTags[$param->var->name])) {
$phpDocType = $varTags[$param->var->name]->getType();
} else {
continue;
}
$phpDocParameterTypes[$param->var->name] = $phpDocType;
}
}
} elseif ($node instanceof Node\Stmt\Function_) {
$functionName = trim($scope->getNamespace() . '\\' . $node->name->name, '\\');
} elseif ($node instanceof Node\PropertyHook) {
$propertyName = $node->getAttribute('propertyName');
if ($propertyName !== null) {
$functionName = sprintf('$%s::%s', $propertyName, $node->name->toString());
}
}
if ($docComment !== null && $resolvedPhpDoc === null) {
$resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
$file,
$class,
$trait,
$functionName,
$docComment,
);
}
$varTags = [];
if ($resolvedPhpDoc !== null) {
$templateTypeMap = $resolvedPhpDoc->getTemplateTypeMap();
$phpDocImmediatelyInvokedCallableParameters = $resolvedPhpDoc->getParamsImmediatelyInvokedCallable();
foreach ($resolvedPhpDoc->getParamTags() as $paramName => $paramTag) {
if (array_key_exists($paramName, $phpDocParameterTypes)) {
continue;
}
$paramType = $paramTag->getType();
if ($scope->isInClass()) {
$paramType = $this->transformStaticType($scope->getClassReflection(), $paramType);
}
$phpDocParameterTypes[$paramName] = $paramType;
}
foreach ($resolvedPhpDoc->getParamClosureThisTags() as $paramName => $paramClosureThisTag) {
if (array_key_exists($paramName, $phpDocClosureThisTypeParameters)) {
continue;
}
$paramClosureThisType = $paramClosureThisTag->getType();
if ($scope->isInClass()) {
$paramClosureThisType = $this->transformStaticType($scope->getClassReflection(), $paramClosureThisType);
}
$phpDocClosureThisTypeParameters[$paramName] = $paramClosureThisType;
}
foreach ($resolvedPhpDoc->getParamOutTags() as $paramName => $paramOutTag) {
$phpDocParameterOutTypes[$paramName] = $paramOutTag->getType();
}
if ($node instanceof Node\FunctionLike) {
$nativeReturnType = $scope->getFunctionType($node->getReturnType(), false, false);
$phpDocReturnType = $this->getPhpDocReturnType($resolvedPhpDoc, $nativeReturnType);
if ($phpDocReturnType !== null && $scope->isInClass()) {
$phpDocReturnType = $this->transformStaticType($scope->getClassReflection(), $phpDocReturnType);
}
}
$phpDocThrowType = $resolvedPhpDoc->getThrowsTag() !== null ? $resolvedPhpDoc->getThrowsTag()->getType() : null;
$deprecatedDescription = $resolvedPhpDoc->getDeprecatedTag() !== null ? $resolvedPhpDoc->getDeprecatedTag()->getMessage() : null;
$isDeprecated = $resolvedPhpDoc->isDeprecated();
$isInternal = $resolvedPhpDoc->isInternal();
$isFinal = $resolvedPhpDoc->isFinal();
$isPure = $resolvedPhpDoc->isPure();
$isAllowedPrivateMutation = $resolvedPhpDoc->isAllowedPrivateMutation();
$acceptsNamedArguments = $resolvedPhpDoc->acceptsNamedArguments();
$isReadOnly = $isReadOnly || $resolvedPhpDoc->isReadOnly();
$asserts = Assertions::createFromResolvedPhpDocBlock($resolvedPhpDoc);
$selfOutType = $resolvedPhpDoc->getSelfOutTag() !== null ? $resolvedPhpDoc->getSelfOutTag()->getType() : null;
$varTags = $resolvedPhpDoc->getVarTags();
$phpDocPureUnlessCallableIsImpureParameters = $resolvedPhpDoc->getParamsPureUnlessCallableIsImpure();
$phpDocPureUnlessParameterPassedParameters = $resolvedPhpDoc->getParamsPureUnlessParameterPassed();
}
if ($acceptsNamedArguments && $scope->isInClass()) {
$acceptsNamedArguments = $scope->getClassReflection()->acceptsNamedArguments();
}
// A @pure-unless-* tag on the method, written or inherited, states its purity
// just like @phpstan-pure does, so the class-level tags do not override it.
if (
$isPure === null
&& $phpDocPureUnlessCallableIsImpureParameters === []
&& $phpDocPureUnlessParameterPassedParameters === []
&& $node instanceof Node\FunctionLike
&& $scope->isInClass()
) {
// a set hook has no return type node of its own, but it always returns
// void - the class-level @phpstan-pure must not make it pure
$isSetHook = $node instanceof Node\PropertyHook && $node->name->toLowerString() === 'set';
$classResolvedPhpDoc = $scope->getClassReflection()->getResolvedPhpDoc();
if ($classResolvedPhpDoc !== null && $classResolvedPhpDoc->areAllMethodsPure()) {
if (
strtolower($functionName ?? '') === '__construct'
|| (
!$isSetHook
&& ($phpDocReturnType === null || !$phpDocReturnType->isVoid()->yes())
&& !$scope->getFunctionType($node->getReturnType(), false, false)->isVoid()->yes()
)
) {
$isPure = true;
}
} elseif ($classResolvedPhpDoc !== null && $classResolvedPhpDoc->areAllMethodsImpure()) {
$isPure = false;
}
}
return [$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, $isReadOnly, $docComment, $asserts, $selfOutType, $phpDocParameterOutTypes, $varTags, $isAllowedPrivateMutation, $resolvedPhpDoc, $phpDocPureUnlessCallableIsImpureParameters, $phpDocPureUnlessParameterPassedParameters];
}
private function getPhpDocReturnType(ResolvedPhpDocBlock $resolvedPhpDoc, Type $nativeReturnType): ?Type
{
$returnTag = $resolvedPhpDoc->getReturnTag();
if ($returnTag === null) {
return null;
}
$phpDocReturnType = $returnTag->getType();
if ($returnTag->isExplicit()) {
return $phpDocReturnType;
}
if ($nativeReturnType->isSuperTypeOf(TemplateTypeHelper::resolveToBounds($phpDocReturnType))->yes()) {
return $phpDocReturnType;
}
if ($phpDocReturnType instanceof UnionType) {
$types = [];
foreach ($phpDocReturnType->getTypes() as $innerType) {
if (!$nativeReturnType->isSuperTypeOf($innerType)->yes()) {
continue;
}
$types[] = $innerType;
}
if (count($types) === 0) {
return null;
}
return TypeCombinator::union(...$types);
}
return null;
}
private function transformStaticType(ClassReflection $declaringClass, Type $type): Type
{
return TypeTraverser::mapMemoized($type, static function (Type $type, callable $traverse) use ($declaringClass): Type {
if ($type instanceof StaticType) {
$changedType = $type->changeBaseClass($declaringClass);
if ($declaringClass->isFinal() && !$type instanceof ThisType) {
$changedType = $changedType->getStaticObjectType();
}
return $traverse($changedType);
}
return $traverse($type);
});
}
}