-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathFileTypeMapperTest.php
More file actions
243 lines (206 loc) · 11.3 KB
/
FileTypeMapperTest.php
File metadata and controls
243 lines (206 loc) · 11.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
<?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 realpath;
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 testBug12639TraitPropertyPhpDocResolution(): void
{
self::createReflectionProvider();
/** @var FileTypeMapper $fileTypeMapper */
$fileTypeMapper = self::getContainer()->getByType(FileTypeMapper::class);
$classRealpath = realpath(__DIR__ . '/data/bug-12639-class.php');
if ($classRealpath === false) {
throw new ShouldNotHappenException();
}
$resolved = $fileTypeMapper->getResolvedPhpDoc(
$classRealpath,
'Bug12639Separate\OtherPlace\StandardAccount',
'Bug12639Separate\Policy\BaseAccount',
null,
'/**
* @var ObjectRefT<Account>
*/',
);
$varTags = $resolved->getVarTags();
$this->assertArrayHasKey(0, $varTags);
$this->assertSame(
'Bug12639Separate\Types\ObjectRefT<Bug12639Separate\Accounts\Account>',
$varTags[0]->getType()->describe(VerbosityLevel::precise()),
);
}
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()));
}
}