-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathRuleTestCase.php
227 lines (201 loc) · 7.15 KB
/
RuleTestCase.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
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
<?php declare(strict_types = 1);
namespace PHPStan\Testing;
use PhpParser\Node;
use PHPStan\Analyser\Analyser;
use PHPStan\Analyser\AnalyserResultFinalizer;
use PHPStan\Analyser\Error;
use PHPStan\Analyser\FileAnalyser;
use PHPStan\Analyser\InternalError;
use PHPStan\Analyser\LocalIgnoresProcessor;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\RuleErrorTransformer;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Collectors\Collector;
use PHPStan\Collectors\Registry as CollectorRegistry;
use PHPStan\Dependency\DependencyResolver;
use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\ParameterOutTypeExtensionProvider;
use PHPStan\File\FileHelper;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
use PHPStan\PhpDoc\StubPhpDocProvider;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Reflection\SignatureMap\SignatureMapProvider;
use PHPStan\Rules\DirectRegistry as DirectRuleRegistry;
use PHPStan\Rules\Properties\DirectReadWritePropertiesExtensionProvider;
use PHPStan\Rules\Properties\ReadWritePropertiesExtension;
use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\ParameterClosureTypeHelper;
use function array_map;
use function count;
use function implode;
use function sprintf;
/**
* @api
* @template TRule of Rule
*/
abstract class RuleTestCase extends PHPStanTestCase
{
private ?Analyser $analyser = null;
/**
* @phpstan-return TRule
*/
abstract protected function getRule(): Rule;
/**
* @return array<Collector<Node, mixed>>
*/
protected function getCollectors(): array
{
return [];
}
/**
* @return ReadWritePropertiesExtension[]
*/
protected function getReadWritePropertiesExtensions(): array
{
return [];
}
protected function getTypeSpecifier(): TypeSpecifier
{
return self::getContainer()->getService('typeSpecifier');
}
private function getAnalyser(DirectRuleRegistry $ruleRegistry): Analyser
{
if ($this->analyser === null) {
$collectorRegistry = new CollectorRegistry($this->getCollectors());
$reflectionProvider = $this->createReflectionProvider();
$typeSpecifier = $this->getTypeSpecifier();
$readWritePropertiesExtensions = $this->getReadWritePropertiesExtensions();
$nodeScopeResolver = new NodeScopeResolver(
$reflectionProvider,
self::getContainer()->getByType(InitializerExprTypeResolver::class),
self::getReflector(),
self::getClassReflectionExtensionRegistryProvider(),
self::getContainer()->getByType(ParameterOutTypeExtensionProvider::class),
$this->getParser(),
self::getContainer()->getByType(FileTypeMapper::class),
self::getContainer()->getByType(StubPhpDocProvider::class),
self::getContainer()->getByType(PhpVersion::class),
self::getContainer()->getByType(SignatureMapProvider::class),
self::getContainer()->getByType(PhpDocInheritanceResolver::class),
self::getContainer()->getByType(FileHelper::class),
$typeSpecifier,
self::getContainer()->getByType(DynamicThrowTypeExtensionProvider::class),
$readWritePropertiesExtensions !== [] ? new DirectReadWritePropertiesExtensionProvider($readWritePropertiesExtensions) : self::getContainer()->getByType(ReadWritePropertiesExtensionProvider::class),
self::getContainer()->getByType(ParameterClosureTypeHelper::class),
self::createScopeFactory($reflectionProvider, $typeSpecifier),
$this->shouldPolluteScopeWithLoopInitialAssignments(),
$this->shouldPolluteScopeWithAlwaysIterableForeach(),
[],
[],
self::getContainer()->getParameter('universalObjectCratesClasses'),
self::getContainer()->getParameter('exceptions')['implicitThrows'],
$this->shouldTreatPhpDocTypesAsCertain(),
self::getContainer()->getParameter('featureToggles')['detectDeadTypeInMultiCatch'],
self::getContainer()->getParameter('featureToggles')['paramOutType'],
self::getContainer()->getParameter('featureToggles')['preciseMissingReturn'],
self::getContainer()->getParameter('featureToggles')['explicitThrow'],
);
$fileAnalyser = new FileAnalyser(
$this->createScopeFactory($reflectionProvider, $typeSpecifier),
$nodeScopeResolver,
$this->getParser(),
self::getContainer()->getByType(DependencyResolver::class),
new RuleErrorTransformer(),
new LocalIgnoresProcessor(),
);
$this->analyser = new Analyser(
$fileAnalyser,
$ruleRegistry,
$collectorRegistry,
$nodeScopeResolver,
50,
);
}
return $this->analyser;
}
/**
* @param string[] $files
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrors
*/
public function analyse(array $files, array $expectedErrors): void
{
$actualErrors = $this->gatherAnalyserErrors($files);
$strictlyTypedSprintf = static function (int $line, string $message, ?string $tip): string {
$message = sprintf('%02d: %s', $line, $message);
if ($tip !== null) {
$message .= "\n 💡 " . $tip;
}
return $message;
};
$expectedErrors = array_map(
static fn (array $error): string => $strictlyTypedSprintf($error[1], $error[0], $error[2] ?? null),
$expectedErrors,
);
$actualErrors = array_map(
static function (Error $error) use ($strictlyTypedSprintf): string {
$line = $error->getLine();
if ($line === null) {
return $strictlyTypedSprintf(-1, $error->getMessage(), $error->getTip());
}
return $strictlyTypedSprintf($line, $error->getMessage(), $error->getTip());
},
$actualErrors,
);
$this->assertSame(implode("\n", $expectedErrors) . "\n", implode("\n", $actualErrors) . "\n");
}
/**
* @param string[] $files
* @return list<Error>
*/
public function gatherAnalyserErrors(array $files): array
{
$ruleRegistry = new DirectRuleRegistry([
$this->getRule(),
]);
$files = array_map([$this->getFileHelper(), 'normalizePath'], $files);
$analyserResult = $this->getAnalyser($ruleRegistry)->analyse(
$files,
null,
null,
true,
);
if (count($analyserResult->getInternalErrors()) > 0) {
$this->fail(implode("\n", array_map(static fn (InternalError $internalError) => $internalError->getMessage(), $analyserResult->getInternalErrors())));
}
if ($this->shouldFailOnPhpErrors() && count($analyserResult->getAllPhpErrors()) > 0) {
$this->fail(implode("\n", array_map(
static fn (Error $error): string => sprintf('%s on %s:%d', $error->getMessage(), $error->getFile(), $error->getLine()),
$analyserResult->getAllPhpErrors(),
)));
}
$finalizer = new AnalyserResultFinalizer(
$ruleRegistry,
new RuleErrorTransformer(),
$this->createScopeFactory($this->createReflectionProvider(), $this->getTypeSpecifier()),
new LocalIgnoresProcessor(),
true,
);
return $finalizer->finalize($analyserResult, false, true)->getAnalyserResult()->getUnorderedErrors();
}
protected function shouldPolluteScopeWithLoopInitialAssignments(): bool
{
return false;
}
protected function shouldPolluteScopeWithAlwaysIterableForeach(): bool
{
return true;
}
protected function shouldFailOnPhpErrors(): bool
{
return true;
}
public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../conf/bleedingEdge.neon',
];
}
}