forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTypeMapperTest.php
More file actions
290 lines (247 loc) · 13.3 KB
/
FileTypeMapperTest.php
File metadata and controls
290 lines (247 loc) · 13.3 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
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use DependentPhpDocs\Foo;
use PHPStan\PhpDoc\Tag\ReturnTag;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\PHPStanTestCase;
use RuntimeException;
use function file_put_contents;
use function microtime;
use function realpath;
use function sys_get_temp_dir;
use function tempnam;
use function unlink;
class FileTypeMapperTest extends PHPStanTestCase
{
public function testGetResolvedPhpDoc(): void
{
/** @var FileTypeMapper $fileTypeMapper */
$fileTypeMapper = self::getContainer()->getByType(FileTypeMapper::class);
$resolvedA = $fileTypeMapper->getResolvedPhpDoc(__DIR__ . '/data/annotations.php', 'TestAnnotations\\Foo', null, null, '/**
* @property int | float $numericBazBazProperty
* @property X $singleLetterObjectName
*
* @method void simpleMethod()
* @method string returningMethod()
* @method ?float returningNullableScalar()
* @method ?\stdClass returningNullableObject()
* @method void complicatedParameters(string $a, ?int|?float|?\stdClass $b, \stdClass $c = null, string|?int $d)
* @method Image rotate(float $angle, $backgroundColor)
* @method int | float paramMultipleTypesWithExtraSpaces(string | null $string, stdClass | null $object)
*/');
$this->assertCount(0, $resolvedA->getVarTags());
$this->assertCount(0, $resolvedA->getParamTags());
$this->assertCount(2, $resolvedA->getPropertyTags());
$this->assertArrayHasKey('numericBazBazProperty', $resolvedA->getPropertyTags());
$this->assertNull($resolvedA->getReturnTag());
$this->assertNotNull($resolvedA->getPropertyTags()['numericBazBazProperty']->getReadableType());
$this->assertNotNull($resolvedA->getPropertyTags()['numericBazBazProperty']->getWritableType());
$this->assertSame('float|int', $resolvedA->getPropertyTags()['numericBazBazProperty']->getReadableType()->describe(VerbosityLevel::precise()));
$this->assertSame('float|int', $resolvedA->getPropertyTags()['numericBazBazProperty']->getWritableType()->describe(VerbosityLevel::precise()));
$this->assertArrayHasKey('singleLetterObjectName', $resolvedA->getPropertyTags());
$this->assertNotNull($resolvedA->getPropertyTags()['singleLetterObjectName']->getReadableType());
$this->assertNotNull($resolvedA->getPropertyTags()['singleLetterObjectName']->getWritableType());
$this->assertSame('TestAnnotations\\X', $resolvedA->getPropertyTags()['singleLetterObjectName']->getReadableType()->describe(VerbosityLevel::precise()));
$this->assertSame('TestAnnotations\\X', $resolvedA->getPropertyTags()['singleLetterObjectName']->getWritableType()->describe(VerbosityLevel::precise()));
$this->assertCount(6, $resolvedA->getMethodTags());
$this->assertArrayNotHasKey('complicatedParameters', $resolvedA->getMethodTags()); // ambiguous parameter types
$this->assertArrayHasKey('simpleMethod', $resolvedA->getMethodTags());
$simpleMethod = $resolvedA->getMethodTags()['simpleMethod'];
$this->assertSame('void', $simpleMethod->getReturnType()->describe(VerbosityLevel::precise()));
$this->assertFalse($simpleMethod->isStatic());
$this->assertCount(0, $simpleMethod->getParameters());
$this->assertArrayHasKey('returningMethod', $resolvedA->getMethodTags());
$returningMethod = $resolvedA->getMethodTags()['returningMethod'];
$this->assertSame('string', $returningMethod->getReturnType()->describe(VerbosityLevel::precise()));
$this->assertFalse($returningMethod->isStatic());
$this->assertCount(0, $returningMethod->getParameters());
$this->assertArrayHasKey('returningNullableScalar', $resolvedA->getMethodTags());
$returningNullableScalar = $resolvedA->getMethodTags()['returningNullableScalar'];
$this->assertSame('float|null', $returningNullableScalar->getReturnType()->describe(VerbosityLevel::precise()));
$this->assertFalse($returningNullableScalar->isStatic());
$this->assertCount(0, $returningNullableScalar->getParameters());
$this->assertArrayHasKey('returningNullableObject', $resolvedA->getMethodTags());
$returningNullableObject = $resolvedA->getMethodTags()['returningNullableObject'];
$this->assertSame('stdClass|null', $returningNullableObject->getReturnType()->describe(VerbosityLevel::precise()));
$this->assertFalse($returningNullableObject->isStatic());
$this->assertCount(0, $returningNullableObject->getParameters());
$this->assertArrayHasKey('rotate', $resolvedA->getMethodTags());
$rotate = $resolvedA->getMethodTags()['rotate'];
$this->assertSame('TestAnnotations\\Image', $rotate->getReturnType()->describe(VerbosityLevel::precise()));
$this->assertFalse($rotate->isStatic());
$this->assertCount(2, $rotate->getParameters());
$this->assertArrayHasKey('angle', $rotate->getParameters());
$this->assertSame('float', $rotate->getParameters()['angle']->getType()->describe(VerbosityLevel::precise()));
$this->assertTrue($rotate->getParameters()['angle']->passedByReference()->no());
$this->assertFalse($rotate->getParameters()['angle']->isOptional());
$this->assertFalse($rotate->getParameters()['angle']->isVariadic());
$this->assertArrayHasKey('backgroundColor', $rotate->getParameters());
$this->assertSame('mixed', $rotate->getParameters()['backgroundColor']->getType()->describe(VerbosityLevel::precise()));
$this->assertTrue($rotate->getParameters()['backgroundColor']->passedByReference()->no());
$this->assertFalse($rotate->getParameters()['backgroundColor']->isOptional());
$this->assertFalse($rotate->getParameters()['backgroundColor']->isVariadic());
$this->assertArrayHasKey('paramMultipleTypesWithExtraSpaces', $resolvedA->getMethodTags());
$paramMultipleTypesWithExtraSpaces = $resolvedA->getMethodTags()['paramMultipleTypesWithExtraSpaces'];
$this->assertSame('float|int', $paramMultipleTypesWithExtraSpaces->getReturnType()->describe(VerbosityLevel::precise()));
$this->assertFalse($paramMultipleTypesWithExtraSpaces->isStatic());
$this->assertCount(2, $paramMultipleTypesWithExtraSpaces->getParameters());
$this->assertArrayHasKey('string', $paramMultipleTypesWithExtraSpaces->getParameters());
$this->assertSame('string|null', $paramMultipleTypesWithExtraSpaces->getParameters()['string']->getType()->describe(VerbosityLevel::precise()));
$this->assertTrue($paramMultipleTypesWithExtraSpaces->getParameters()['string']->passedByReference()->no());
$this->assertFalse($paramMultipleTypesWithExtraSpaces->getParameters()['string']->isOptional());
$this->assertFalse($paramMultipleTypesWithExtraSpaces->getParameters()['string']->isVariadic());
$this->assertArrayHasKey('object', $paramMultipleTypesWithExtraSpaces->getParameters());
$this->assertSame('TestAnnotations\\stdClass|null', $paramMultipleTypesWithExtraSpaces->getParameters()['object']->getType()->describe(VerbosityLevel::precise()));
$this->assertTrue($paramMultipleTypesWithExtraSpaces->getParameters()['object']->passedByReference()->no());
$this->assertFalse($paramMultipleTypesWithExtraSpaces->getParameters()['object']->isOptional());
$this->assertFalse($paramMultipleTypesWithExtraSpaces->getParameters()['object']->isVariadic());
}
public function testFileWithDependentPhpDocs(): void
{
/** @var FileTypeMapper $fileTypeMapper */
$fileTypeMapper = self::getContainer()->getByType(FileTypeMapper::class);
$realpath = realpath(__DIR__ . '/data/dependent-phpdocs.php');
if ($realpath === false) {
throw new ShouldNotHappenException();
}
$resolved = $fileTypeMapper->getResolvedPhpDoc(
$realpath,
Foo::class,
null,
'addPages',
'/** @param Foo[]|Foo|\Iterator $pages */',
);
$this->assertCount(1, $resolved->getParamTags());
$this->assertArrayHasKey('pages', $resolved->getParamTags());
$this->assertSame(
'(DependentPhpDocs\Foo&iterable<DependentPhpDocs\Foo>)|(iterable<DependentPhpDocs\Foo>&Iterator)',
$resolved->getParamTags()['pages']->getType()->describe(VerbosityLevel::precise()),
);
}
public function testFileThrowsPhpDocs(): void
{
/** @var FileTypeMapper $fileTypeMapper */
$fileTypeMapper = self::getContainer()->getByType(FileTypeMapper::class);
$realpath = realpath(__DIR__ . '/data/throws-phpdocs.php');
if ($realpath === false) {
throw new ShouldNotHappenException();
}
$resolved = $fileTypeMapper->getResolvedPhpDoc($realpath, \ThrowsPhpDocs\Foo::class, null, 'throwRuntimeException', '/**
* @throws RuntimeException
*/');
$this->assertNotNull($resolved->getThrowsTag());
$this->assertSame(
RuntimeException::class,
$resolved->getThrowsTag()->getType()->describe(VerbosityLevel::precise()),
);
$resolved = $fileTypeMapper->getResolvedPhpDoc($realpath, \ThrowsPhpDocs\Foo::class, null, 'throwRuntimeAndLogicException', '/**
* @throws RuntimeException|LogicException
*/');
$this->assertNotNull($resolved->getThrowsTag());
$this->assertSame(
'LogicException|RuntimeException',
$resolved->getThrowsTag()->getType()->describe(VerbosityLevel::precise()),
);
$resolved = $fileTypeMapper->getResolvedPhpDoc($realpath, \ThrowsPhpDocs\Foo::class, null, 'throwRuntimeAndLogicException2', '/**
* @throws RuntimeException
* @throws LogicException
*/');
$this->assertNotNull($resolved->getThrowsTag());
$this->assertSame(
'LogicException|RuntimeException',
$resolved->getThrowsTag()->getType()->describe(VerbosityLevel::precise()),
);
}
public function testFileWithCyclicPhpDocs(): void
{
self::createReflectionProvider();
/** @var FileTypeMapper $fileTypeMapper */
$fileTypeMapper = self::getContainer()->getByType(FileTypeMapper::class);
$realpath = realpath(__DIR__ . '/data/cyclic-phpdocs.php');
if ($realpath === false) {
throw new ShouldNotHappenException();
}
$resolved = $fileTypeMapper->getResolvedPhpDoc(
$realpath,
\CyclicPhpDocs\Foo::class,
null,
'getIterator',
'/** @return iterable<Foo> | Foo */',
);
/** @var ReturnTag $returnTag */
$returnTag = $resolved->getReturnTag();
$this->assertSame('CyclicPhpDocs\Foo|iterable<CyclicPhpDocs\Foo>', $returnTag->getType()->describe(VerbosityLevel::precise()));
}
public function testLargeStubFileLazyPhpDocParsing(): void
{
/** @var FileTypeMapper $fileTypeMapper */
$fileTypeMapper = self::getContainer()->getByType(FileTypeMapper::class);
// Generate a large stub file with many PHPDoc comments, simulating
// WordPress-style stubs where a single file declares many functions.
$tmpFile = tempnam(sys_get_temp_dir(), 'phpstan_stub_perf_');
if ($tmpFile === false) {
throw new ShouldNotHappenException();
}
$tmpFile .= '.php';
try {
$code = "<?php declare(strict_types = 1);\n\nnamespace StubPhpDocPerformance;\n\n";
// 10000 functions with complex PHPDocs but no template/type-alias tags
for ($i = 1; $i <= 10000; $i++) {
$code .= "/**\n";
$code .= " * @param array<string, array<int, string>> \$param1\n";
$code .= " * @param callable(int, string): array<string, mixed> \$param2\n";
$code .= " * @param list<array{id: int, name: string, data: array<string, mixed>}> \$param3\n";
$code .= " * @return array<int, array{key: string, value: mixed, metadata: array<string, string>}>\n";
$code .= " */\n";
$code .= "function stub_{$i}(array \$param1, callable \$param2, array \$param3): array {}\n\n";
}
// A class with @template - this PHPDoc must still be parsed
$code .= "/**\n * @template T\n */\n";
$code .= "class GenericContainer\n{\n";
$code .= " /** @var T */\n private \$value;\n\n";
$code .= " /** @param T \$value */\n";
$code .= " public function __construct(\$value) { \$this->value = \$value; }\n\n";
$code .= " /** @return T */\n";
$code .= " public function getValue() { return \$this->value; }\n";
$code .= "}\n";
file_put_contents($tmpFile, $code);
$start = microtime(true);
// Resolve the template class - should work despite 10000 other PHPDocs in file
$resolved = $fileTypeMapper->getResolvedPhpDoc(
$tmpFile,
'StubPhpDocPerformance\\GenericContainer',
null,
'getValue',
'/** @return T */',
);
$elapsed = microtime(true) - $start;
$returnTag = $resolved->getReturnTag();
$this->assertNotNull($returnTag);
$this->assertSame(
'T (class StubPhpDocPerformance\GenericContainer, parameter)',
$returnTag->getType()->describe(VerbosityLevel::precise()),
);
// With lazy PHPDoc parsing, only PHPDocs with @template or type-alias
// tags are parsed during name scope map creation. Without the optimization,
// all 10000+ PHPDocs must be parsed, taking >2 seconds on typical hardware.
$this->assertLessThan(
3.0,
$elapsed,
'FileTypeMapper should skip PHPDoc parsing for entries without template/type-alias tags',
);
} finally {
@unlink($tmpFile);
}
}
public function testFilesWithIdenticalPhpDocsUsingDifferentAliases(): void
{
/** @var FileTypeMapper $fileTypeMapper */
$fileTypeMapper = self::getContainer()->getByType(FileTypeMapper::class);
$doc1 = $fileTypeMapper->getResolvedPhpDoc(__DIR__ . '/data/alias-collision1.php', null, null, null, '/** @var Foo $x */');
$doc2 = $fileTypeMapper->getResolvedPhpDoc(__DIR__ . '/data/alias-collision2.php', null, null, null, '/** @var Foo $x */');
$this->assertArrayHasKey('x', $doc1->getVarTags());
$this->assertSame('AliasCollisionNamespace1\Foo', $doc1->getVarTags()['x']->getType()->describe(VerbosityLevel::precise()));
$this->assertArrayHasKey('x', $doc2->getVarTags());
$this->assertSame('AliasCollisionNamespace2\Foo', $doc2->getVarTags()['x']->getType()->describe(VerbosityLevel::precise()));
}
}