forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStubValidator.php
More file actions
302 lines (279 loc) · 14 KB
/
StubValidator.php
File metadata and controls
302 lines (279 loc) · 14 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
<?php declare(strict_types = 1);
namespace PHPStan\PhpDoc;
use PHPStan\Analyser\Error;
use PHPStan\Analyser\FileAnalyser;
use PHPStan\Analyser\InternalError;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Collectors\Registry as CollectorRegistry;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\DependencyInjection\Container;
use PHPStan\DependencyInjection\DerivativeContainerFactory;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\Reflection\Php\PhpClassReflectionExtension;
use PHPStan\Reflection\PhpVersionStaticAccessor;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
use PHPStan\Rules\Classes\DuplicateClassDeclarationRule;
use PHPStan\Rules\Classes\DuplicateDeclarationHelper;
use PHPStan\Rules\Classes\DuplicateDeclarationRule;
use PHPStan\Rules\Classes\ExistingClassesInClassImplementsRule;
use PHPStan\Rules\Classes\ExistingClassesInInterfaceExtendsRule;
use PHPStan\Rules\Classes\ExistingClassInClassExtendsRule;
use PHPStan\Rules\Classes\ExistingClassInTraitUseRule;
use PHPStan\Rules\Classes\LocalTypeAliasesCheck;
use PHPStan\Rules\Classes\LocalTypeAliasesRule;
use PHPStan\Rules\Classes\LocalTypeTraitAliasesRule;
use PHPStan\Rules\Classes\LocalTypeTraitUseAliasesRule;
use PHPStan\Rules\Classes\MethodTagCheck;
use PHPStan\Rules\Classes\MethodTagRule;
use PHPStan\Rules\Classes\MethodTagTraitRule;
use PHPStan\Rules\Classes\MethodTagTraitUseRule;
use PHPStan\Rules\Classes\MixinCheck;
use PHPStan\Rules\Classes\MixinRule;
use PHPStan\Rules\Classes\MixinTraitRule;
use PHPStan\Rules\Classes\MixinTraitUseRule;
use PHPStan\Rules\Classes\PropertyTagCheck;
use PHPStan\Rules\Classes\PropertyTagRule;
use PHPStan\Rules\Classes\PropertyTagTraitRule;
use PHPStan\Rules\Classes\PropertyTagTraitUseRule;
use PHPStan\Rules\ClassNameCheck;
use PHPStan\Rules\DirectRegistry as DirectRuleRegistry;
use PHPStan\Rules\FunctionDefinitionCheck;
use PHPStan\Rules\Functions\DuplicateFunctionDeclarationRule;
use PHPStan\Rules\Functions\MissingFunctionParameterTypehintRule;
use PHPStan\Rules\Functions\MissingFunctionReturnTypehintRule;
use PHPStan\Rules\Generics\ClassAncestorsRule;
use PHPStan\Rules\Generics\ClassTemplateTypeRule;
use PHPStan\Rules\Generics\CrossCheckInterfacesHelper;
use PHPStan\Rules\Generics\EnumAncestorsRule;
use PHPStan\Rules\Generics\EnumTemplateTypeRule;
use PHPStan\Rules\Generics\FunctionSignatureVarianceRule;
use PHPStan\Rules\Generics\FunctionTemplateTypeRule;
use PHPStan\Rules\Generics\GenericAncestorsCheck;
use PHPStan\Rules\Generics\GenericObjectTypeCheck;
use PHPStan\Rules\Generics\InterfaceAncestorsRule;
use PHPStan\Rules\Generics\InterfaceTemplateTypeRule;
use PHPStan\Rules\Generics\MethodSignatureVarianceRule;
use PHPStan\Rules\Generics\MethodTagTemplateTypeCheck;
use PHPStan\Rules\Generics\MethodTagTemplateTypeRule;
use PHPStan\Rules\Generics\MethodTagTemplateTypeTraitRule;
use PHPStan\Rules\Generics\MethodTemplateTypeRule;
use PHPStan\Rules\Generics\PropertyVarianceRule;
use PHPStan\Rules\Generics\TemplateTypeCheck;
use PHPStan\Rules\Generics\TraitTemplateTypeRule;
use PHPStan\Rules\Generics\UsedTraitsRule;
use PHPStan\Rules\Generics\VarianceCheck;
use PHPStan\Rules\Methods\ExistingClassesInTypehintsRule;
use PHPStan\Rules\Methods\MethodParameterComparisonHelper;
use PHPStan\Rules\Methods\MethodPrototypeFinder;
use PHPStan\Rules\Methods\MethodSignatureRule;
use PHPStan\Rules\Methods\MethodVisibilityComparisonHelper;
use PHPStan\Rules\Methods\MissingMethodParameterTypehintRule;
use PHPStan\Rules\Methods\MissingMethodReturnTypehintRule;
use PHPStan\Rules\Methods\MissingMethodSelfOutTypeRule;
use PHPStan\Rules\Methods\OverridingMethodRule;
use PHPStan\Rules\Methods\ParentMethodHelper;
use PHPStan\Rules\MissingTypehintCheck;
use PHPStan\Rules\PhpDoc\AssertRuleHelper;
use PHPStan\Rules\PhpDoc\ConditionalReturnTypeRuleHelper;
use PHPStan\Rules\PhpDoc\FunctionAssertRule;
use PHPStan\Rules\PhpDoc\FunctionConditionalReturnTypeRule;
use PHPStan\Rules\PhpDoc\GenericCallableRuleHelper;
use PHPStan\Rules\PhpDoc\IncompatibleClassConstantPhpDocTypeRule;
use PHPStan\Rules\PhpDoc\IncompatibleParamImmediatelyInvokedCallableRule;
use PHPStan\Rules\PhpDoc\IncompatiblePhpDocTypeCheck;
use PHPStan\Rules\PhpDoc\IncompatiblePhpDocTypeRule;
use PHPStan\Rules\PhpDoc\IncompatiblePropertyPhpDocTypeRule;
use PHPStan\Rules\PhpDoc\IncompatibleSelfOutTypeRule;
use PHPStan\Rules\PhpDoc\InvalidPhpDocTagValueRule;
use PHPStan\Rules\PhpDoc\InvalidPHPStanDocTagRule;
use PHPStan\Rules\PhpDoc\InvalidThrowsPhpDocValueRule;
use PHPStan\Rules\PhpDoc\MethodAssertRule;
use PHPStan\Rules\PhpDoc\MethodConditionalReturnTypeRule;
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
use PHPStan\Rules\Properties\ExistingClassesInPropertiesRule;
use PHPStan\Rules\Properties\MissingPropertyTypehintRule;
use PHPStan\Rules\Registry as RuleRegistry;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\ObjectType;
use Throwable;
use function array_fill_keys;
use function count;
use function sprintf;
#[AutowiredService]
final class StubValidator
{
public function __construct(
private DerivativeContainerFactory $derivativeContainerFactory,
)
{
}
/**
* @param string[] $stubFiles
* @return list<Error>
*/
public function validate(array $stubFiles, bool $debug): array
{
if (count($stubFiles) === 0) {
return [];
}
$originalReflectionProvider = ReflectionProviderStaticAccessor::getInstance();
$originalPhpVersion = PhpVersionStaticAccessor::getInstance();
try {
$container = $this->derivativeContainerFactory->create([
__DIR__ . '/../../conf/config.stubValidator.neon',
]);
$ruleRegistry = $this->getRuleRegistry($container);
$collectorRegistry = $this->getCollectorRegistry();
$fileAnalyser = $container->getByType(FileAnalyser::class);
$nodeScopeResolver = $container->getByType(NodeScopeResolver::class);
$nodeScopeResolver->setAnalysedFiles($stubFiles);
$pathRoutingParser = $container->getService('pathRoutingParser');
$pathRoutingParser->setAnalysedFiles($stubFiles);
$analysedFiles = array_fill_keys($stubFiles, true);
$errors = [];
foreach ($stubFiles as $stubFile) {
try {
$tmpErrors = $fileAnalyser->analyseFile(
$stubFile,
$analysedFiles,
$ruleRegistry,
$collectorRegistry,
static function (): void {
},
)->getErrors();
foreach ($tmpErrors as $tmpError) {
$errors[] = $tmpError->withoutTip()->doNotIgnore();
}
} catch (Throwable $e) {
if ($debug) {
throw $e;
}
$internalErrorMessage = sprintf('Internal error: %s', $e->getMessage());
$errors[] = (new Error($internalErrorMessage, $stubFile, canBeIgnored: $e))
->withIdentifier('phpstan.internal')
->withMetadata([
InternalError::STACK_TRACE_METADATA_KEY => InternalError::prepareTrace($e),
InternalError::STACK_TRACE_AS_STRING_METADATA_KEY => $e->getTraceAsString(),
]);
}
}
} finally {
ReflectionProviderStaticAccessor::registerInstance($originalReflectionProvider);
PhpVersionStaticAccessor::registerInstance($originalPhpVersion);
ObjectType::resetCaches();
}
return $errors;
}
private function getRuleRegistry(Container $container): RuleRegistry
{
$fileTypeMapper = $container->getByType(FileTypeMapper::class);
$genericObjectTypeCheck = $container->getByType(GenericObjectTypeCheck::class);
$genericAncestorsCheck = $container->getByType(GenericAncestorsCheck::class);
$templateTypeCheck = $container->getByType(TemplateTypeCheck::class);
$varianceCheck = $container->getByType(VarianceCheck::class);
$reflectionProvider = $container->getByType(ReflectionProvider::class);
$classNameCheck = $container->getByType(ClassNameCheck::class);
$functionDefinitionCheck = $container->getByType(FunctionDefinitionCheck::class);
$missingTypehintCheck = $container->getByType(MissingTypehintCheck::class);
$unresolvableTypeHelper = $container->getByType(UnresolvableTypeHelper::class);
$crossCheckInterfacesHelper = $container->getByType(CrossCheckInterfacesHelper::class);
$phpVersion = $container->getByType(PhpVersion::class);
$localTypeAliasesCheck = $container->getByType(LocalTypeAliasesCheck::class);
$phpClassReflectionExtension = $container->getByType(PhpClassReflectionExtension::class);
$genericCallableRuleHelper = $container->getByType(GenericCallableRuleHelper::class);
$methodTagTemplateTypeCheck = $container->getByType(MethodTagTemplateTypeCheck::class);
$mixinCheck = $container->getByType(MixinCheck::class);
$discoveringSymbolsTip = $container->getParameter('tips')['discoveringSymbols'];
$methodTagCheck = new MethodTagCheck($reflectionProvider, $classNameCheck, $genericObjectTypeCheck, $missingTypehintCheck, $unresolvableTypeHelper, checkClassCaseSensitivity: true, checkMissingTypehints: true, discoveringSymbolsTip: $discoveringSymbolsTip);
$propertyTagCheck = new PropertyTagCheck($reflectionProvider, $classNameCheck, $genericObjectTypeCheck, $missingTypehintCheck, $unresolvableTypeHelper, checkClassCaseSensitivity: true, checkMissingTypehints: true, discoveringSymbolsTip: $discoveringSymbolsTip);
$reflector = $container->getService('stubReflector');
$relativePathHelper = $container->getService('simpleRelativePathHelper');
$assertRuleHelper = $container->getByType(AssertRuleHelper::class);
$conditionalReturnTypeRuleHelper = $container->getByType(ConditionalReturnTypeRuleHelper::class);
$parentMethodHelper = $container->getByType(ParentMethodHelper::class);
$rules = [
// level 0
new ExistingClassesInClassImplementsRule($classNameCheck, $reflectionProvider, $discoveringSymbolsTip),
new ExistingClassesInInterfaceExtendsRule($classNameCheck, $reflectionProvider, $discoveringSymbolsTip),
new ExistingClassInClassExtendsRule($classNameCheck, $reflectionProvider, $discoveringSymbolsTip),
new ExistingClassInTraitUseRule($classNameCheck, $reflectionProvider, $discoveringSymbolsTip),
new ExistingClassesInTypehintsRule($functionDefinitionCheck),
new \PHPStan\Rules\Functions\ExistingClassesInTypehintsRule($functionDefinitionCheck),
new ExistingClassesInPropertiesRule($reflectionProvider, $classNameCheck, $unresolvableTypeHelper, $phpVersion, checkClassCaseSensitivity: true, checkThisOnly: false, discoveringSymbolsTip: $discoveringSymbolsTip),
new OverridingMethodRule(
$phpVersion,
new MethodSignatureRule($parentMethodHelper, reportMaybes: true, reportStatic: true),
true,
new MethodParameterComparisonHelper($phpVersion),
new MethodVisibilityComparisonHelper(),
new MethodPrototypeFinder($phpVersion, $phpClassReflectionExtension),
$container->getParameter('checkMissingOverrideMethodAttribute'),
),
new DuplicateDeclarationRule(new DuplicateDeclarationHelper()),
new LocalTypeAliasesRule($localTypeAliasesCheck),
new LocalTypeTraitAliasesRule($localTypeAliasesCheck, $reflectionProvider),
new LocalTypeTraitUseAliasesRule($localTypeAliasesCheck),
// level 2
new ClassAncestorsRule($genericAncestorsCheck, $crossCheckInterfacesHelper),
new ClassTemplateTypeRule($templateTypeCheck),
new FunctionTemplateTypeRule($fileTypeMapper, $templateTypeCheck),
new FunctionSignatureVarianceRule($varianceCheck),
new InterfaceAncestorsRule($genericAncestorsCheck, $crossCheckInterfacesHelper),
new InterfaceTemplateTypeRule($templateTypeCheck),
new MethodTemplateTypeRule($fileTypeMapper, $templateTypeCheck),
new MethodTagTemplateTypeRule($methodTagTemplateTypeCheck),
new MethodSignatureVarianceRule($varianceCheck),
new TraitTemplateTypeRule($fileTypeMapper, $templateTypeCheck),
new IncompatiblePhpDocTypeRule($fileTypeMapper, new IncompatiblePhpDocTypeCheck($genericObjectTypeCheck, $unresolvableTypeHelper, $genericCallableRuleHelper)),
new IncompatiblePropertyPhpDocTypeRule($genericObjectTypeCheck, $unresolvableTypeHelper, $genericCallableRuleHelper),
new InvalidPhpDocTagValueRule(
$container->getByType(Lexer::class),
$container->getByType(PhpDocParser::class),
),
new IncompatibleParamImmediatelyInvokedCallableRule($fileTypeMapper),
new IncompatibleSelfOutTypeRule($unresolvableTypeHelper, $genericObjectTypeCheck),
new IncompatibleClassConstantPhpDocTypeRule($genericObjectTypeCheck, $unresolvableTypeHelper),
new InvalidPHPStanDocTagRule(
$container->getByType(Lexer::class),
$container->getByType(PhpDocParser::class),
),
new InvalidThrowsPhpDocValueRule($fileTypeMapper),
new MixinTraitRule($mixinCheck, $reflectionProvider),
new MixinRule($mixinCheck),
new MixinTraitUseRule($mixinCheck),
new MethodTagRule($methodTagCheck),
new MethodTagTraitRule($methodTagCheck, $reflectionProvider),
new MethodTagTraitUseRule($methodTagCheck),
new MethodTagTemplateTypeTraitRule($methodTagTemplateTypeCheck, $reflectionProvider),
new PropertyTagRule($propertyTagCheck),
new PropertyTagTraitRule($propertyTagCheck, $reflectionProvider),
new PropertyTagTraitUseRule($propertyTagCheck),
new EnumAncestorsRule($genericAncestorsCheck, $crossCheckInterfacesHelper),
new EnumTemplateTypeRule(),
new PropertyVarianceRule($varianceCheck),
new UsedTraitsRule($fileTypeMapper, $genericAncestorsCheck),
new FunctionAssertRule($assertRuleHelper),
new MethodAssertRule($assertRuleHelper),
new FunctionConditionalReturnTypeRule($conditionalReturnTypeRuleHelper),
new MethodConditionalReturnTypeRule($conditionalReturnTypeRuleHelper),
// level 6
new MissingFunctionParameterTypehintRule($missingTypehintCheck),
new MissingFunctionReturnTypehintRule($missingTypehintCheck),
new MissingMethodParameterTypehintRule($missingTypehintCheck),
new MissingMethodReturnTypehintRule($missingTypehintCheck),
new MissingPropertyTypehintRule($missingTypehintCheck),
new MissingMethodSelfOutTypeRule($missingTypehintCheck),
// duplicate stubs
new DuplicateClassDeclarationRule($reflector, $relativePathHelper),
new DuplicateFunctionDeclarationRule($reflector, $relativePathHelper),
];
return new DirectRuleRegistry($rules);
}
private function getCollectorRegistry(): CollectorRegistry
{
return new CollectorRegistry([]);
}
}