|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\Annotations\TypeParser; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Annotations\Metadata\Type\FloatType; |
|
8
|
|
|
use Doctrine\Annotations\Metadata\Type\IntegerType; |
|
9
|
|
|
use Doctrine\Annotations\Metadata\Type\IntersectionType; |
|
10
|
|
|
use Doctrine\Annotations\Metadata\Type\ListType; |
|
11
|
|
|
use Doctrine\Annotations\Metadata\Type\MapType; |
|
12
|
|
|
use Doctrine\Annotations\Metadata\Type\MixedType; |
|
13
|
|
|
use Doctrine\Annotations\Metadata\Type\NullType; |
|
14
|
|
|
use Doctrine\Annotations\Metadata\Type\ObjectType; |
|
15
|
|
|
use Doctrine\Annotations\Metadata\Type\StringType; |
|
16
|
|
|
use Doctrine\Annotations\Metadata\Type\Type; |
|
17
|
|
|
use Doctrine\Annotations\Metadata\Type\UnionType; |
|
18
|
|
|
use Doctrine\Annotations\Parser\Reference\FallbackReferenceResolver; |
|
19
|
|
|
use Doctrine\Annotations\TypeParser\PHPStanTypeParser; |
|
20
|
|
|
use Doctrine\Annotations\TypeParser\TypeParser; |
|
21
|
|
|
use PHPStan\PhpDocParser\Lexer\Lexer; |
|
22
|
|
|
use PHPStan\PhpDocParser\Parser\ConstExprParser; |
|
23
|
|
|
use PHPStan\PhpDocParser\Parser\PhpDocParser; |
|
24
|
|
|
use PHPStan\PhpDocParser\Parser\TypeParser as BaseTypeParser; |
|
25
|
|
|
|
|
26
|
|
|
final class PHPStanTypeParserTest extends TypeParserTest |
|
27
|
|
|
{ |
|
28
|
|
|
protected function createParser() : TypeParser |
|
29
|
|
|
{ |
|
30
|
|
|
return new PHPStanTypeParser( |
|
31
|
|
|
new Lexer(), |
|
32
|
|
|
new PhpDocParser(new BaseTypeParser(), new ConstExprParser()), |
|
33
|
|
|
new FallbackReferenceResolver() |
|
34
|
|
|
); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return string[][]|Type[][] |
|
39
|
|
|
*/ |
|
40
|
|
|
public function validPropertyTypesProvider() : iterable |
|
41
|
|
|
{ |
|
42
|
|
|
yield 'empty' => [ |
|
|
|
|
|
|
43
|
|
|
<<<'PHPDOC' |
|
44
|
|
|
/** */ |
|
45
|
|
|
PHPDOC |
|
46
|
|
|
, |
|
47
|
|
|
new MixedType(), |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
yield 'only comment' => [ |
|
51
|
|
|
<<<'PHPDOC' |
|
52
|
|
|
/** Hello world */ |
|
53
|
|
|
PHPDOC |
|
54
|
|
|
, |
|
55
|
|
|
new MixedType(), |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
yield 'no type' => [ |
|
59
|
|
|
<<<'PHPDOC' |
|
60
|
|
|
/** @var */ |
|
61
|
|
|
PHPDOC |
|
62
|
|
|
, |
|
63
|
|
|
new MixedType(), |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
yield 'int' => [ |
|
67
|
|
|
<<<'PHPDOC' |
|
68
|
|
|
/** @var int */ |
|
69
|
|
|
PHPDOC |
|
70
|
|
|
, |
|
71
|
|
|
new IntegerType(), |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
yield 'int with description' => [ |
|
75
|
|
|
<<<'PHPDOC' |
|
76
|
|
|
/** @var int Hello world */ |
|
77
|
|
|
PHPDOC |
|
78
|
|
|
, |
|
79
|
|
|
new IntegerType(), |
|
80
|
|
|
]; |
|
81
|
|
|
|
|
82
|
|
|
yield '?int' => [ |
|
83
|
|
|
<<<'PHPDOC' |
|
84
|
|
|
/** @var ?int */ |
|
85
|
|
|
PHPDOC |
|
86
|
|
|
, |
|
87
|
|
|
new UnionType(new IntegerType(), new NullType()), |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
|
|
yield 'int|null' => [ |
|
91
|
|
|
<<<'PHPDOC' |
|
92
|
|
|
/** @var int|null */ |
|
93
|
|
|
PHPDOC |
|
94
|
|
|
, |
|
95
|
|
|
new UnionType(new IntegerType(), new NullType()), |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
yield 'null|int' => [ |
|
99
|
|
|
<<<'PHPDOC' |
|
100
|
|
|
/** @var null|int */ |
|
101
|
|
|
PHPDOC |
|
102
|
|
|
, |
|
103
|
|
|
new UnionType(new NullType(), new IntegerType()), |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
|
|
yield 'int[]' => [ |
|
107
|
|
|
<<<'PHPDOC' |
|
108
|
|
|
/** @var int[] */ |
|
109
|
|
|
PHPDOC |
|
110
|
|
|
, |
|
111
|
|
|
new ListType(new IntegerType()), |
|
112
|
|
|
]; |
|
113
|
|
|
|
|
114
|
|
|
yield 'int[]|null' => [ |
|
115
|
|
|
<<<'PHPDOC' |
|
116
|
|
|
/** @var int[]|null */ |
|
117
|
|
|
PHPDOC |
|
118
|
|
|
, |
|
119
|
|
|
new UnionType(new ListType(new IntegerType()), new NullType()), |
|
120
|
|
|
]; |
|
121
|
|
|
|
|
122
|
|
|
yield 'array' => [ |
|
123
|
|
|
<<<'PHPDOC' |
|
124
|
|
|
/** @var array */ |
|
125
|
|
|
PHPDOC |
|
126
|
|
|
, |
|
127
|
|
|
new MapType(new UnionType(new IntegerType(), new StringType()), new MixedType()), |
|
128
|
|
|
]; |
|
129
|
|
|
|
|
130
|
|
|
yield 'array<int>' => [ |
|
131
|
|
|
<<<'PHPDOC' |
|
132
|
|
|
/** @var array<int> */ |
|
133
|
|
|
PHPDOC |
|
134
|
|
|
, |
|
135
|
|
|
new ListType(new IntegerType()), |
|
136
|
|
|
]; |
|
137
|
|
|
|
|
138
|
|
|
yield 'array<int>|null' => [ |
|
139
|
|
|
<<<'PHPDOC' |
|
140
|
|
|
/** @var array<int>|null */ |
|
141
|
|
|
PHPDOC |
|
142
|
|
|
, |
|
143
|
|
|
new UnionType(new ListType(new IntegerType()), new NullType()), |
|
144
|
|
|
]; |
|
145
|
|
|
|
|
146
|
|
|
yield 'array<int, string>' => [ |
|
147
|
|
|
<<<'PHPDOC' |
|
148
|
|
|
/** @var array<int, string> */ |
|
149
|
|
|
PHPDOC |
|
150
|
|
|
, |
|
151
|
|
|
new MapType(new IntegerType(), new StringType()), |
|
152
|
|
|
]; |
|
153
|
|
|
|
|
154
|
|
|
yield 'array<int, string>|null' => [ |
|
155
|
|
|
<<<'PHPDOC' |
|
156
|
|
|
/** @var array<int, string>|null */ |
|
157
|
|
|
PHPDOC |
|
158
|
|
|
, |
|
159
|
|
|
new UnionType(new MapType(new IntegerType(), new StringType()), new NullType()), |
|
160
|
|
|
]; |
|
161
|
|
|
|
|
162
|
|
|
yield 'int[][]' => [ |
|
163
|
|
|
<<<'PHPDOC' |
|
164
|
|
|
/** @var int[][] */ |
|
165
|
|
|
PHPDOC |
|
166
|
|
|
, |
|
167
|
|
|
new ListType(new ListType(new IntegerType())), |
|
168
|
|
|
]; |
|
169
|
|
|
|
|
170
|
|
|
yield 'int[][]|null' => [ |
|
171
|
|
|
<<<'PHPDOC' |
|
172
|
|
|
/** @var int[][]|null */ |
|
173
|
|
|
PHPDOC |
|
174
|
|
|
, |
|
175
|
|
|
new UnionType(new ListType(new ListType(new IntegerType())), new NullType()), |
|
176
|
|
|
]; |
|
177
|
|
|
|
|
178
|
|
|
yield 'int[]string[]' => [ |
|
179
|
|
|
<<<'PHPDOC' |
|
180
|
|
|
/** @var int[]|string[] */ |
|
181
|
|
|
PHPDOC |
|
182
|
|
|
, |
|
183
|
|
|
new UnionType(new ListType(new IntegerType()), new ListType(new StringType())), |
|
184
|
|
|
]; |
|
185
|
|
|
|
|
186
|
|
|
yield 'SomeType' => [ |
|
187
|
|
|
<<<'PHPDOC' |
|
188
|
|
|
/** @var SomeType */ |
|
189
|
|
|
PHPDOC |
|
190
|
|
|
, |
|
191
|
|
|
new ObjectType('SomeType'), |
|
192
|
|
|
]; |
|
193
|
|
|
|
|
194
|
|
|
yield '(SomeClass&SomeInterface)|null' => [ |
|
195
|
|
|
<<<'PHPDOC' |
|
196
|
|
|
/** @var (SomeClass&SomeInterface)|null */ |
|
197
|
|
|
PHPDOC |
|
198
|
|
|
, |
|
199
|
|
|
new UnionType( |
|
200
|
|
|
new IntersectionType( |
|
201
|
|
|
new ObjectType('SomeClass'), |
|
202
|
|
|
new ObjectType('SomeInterface') |
|
203
|
|
|
), |
|
204
|
|
|
new NullType() |
|
205
|
|
|
), |
|
206
|
|
|
]; |
|
207
|
|
|
|
|
208
|
|
|
yield 'SomeClass|(AnotherClass&(AnotherFooInterface|AnotherBarInterface))|null' => [ |
|
209
|
|
|
<<<'PHPDOC' |
|
210
|
|
|
/** @var SomeClass|(AnotherClass&(AnotherFooInterface|AnotherBarInterface))|null */ |
|
211
|
|
|
PHPDOC |
|
212
|
|
|
, |
|
213
|
|
|
new UnionType( |
|
214
|
|
|
new ObjectType('SomeClass'), |
|
215
|
|
|
new IntersectionType( |
|
216
|
|
|
new ObjectType('AnotherClass'), |
|
217
|
|
|
new UnionType( |
|
218
|
|
|
new ObjectType('AnotherFooInterface'), |
|
219
|
|
|
new ObjectType('AnotherBarInterface') |
|
220
|
|
|
) |
|
221
|
|
|
), |
|
222
|
|
|
new NullType() |
|
223
|
|
|
), |
|
224
|
|
|
]; |
|
225
|
|
|
|
|
226
|
|
|
yield 'FooBar alias' => [ |
|
227
|
|
|
<<<'PHPDOC' |
|
228
|
|
|
/** @var FooBar */ |
|
229
|
|
|
PHPDOC |
|
230
|
|
|
, |
|
231
|
|
|
new ObjectType('FooBaz'), |
|
232
|
|
|
]; |
|
233
|
|
|
|
|
234
|
|
|
yield 'double' => [ |
|
235
|
|
|
<<<'PHPDOC' |
|
236
|
|
|
/** @var double */ |
|
237
|
|
|
PHPDOC |
|
238
|
|
|
, |
|
239
|
|
|
new FloatType(), |
|
240
|
|
|
]; |
|
241
|
|
|
|
|
242
|
|
|
yield 'real' => [ |
|
243
|
|
|
<<<'PHPDOC' |
|
244
|
|
|
/** @var real */ |
|
245
|
|
|
PHPDOC |
|
246
|
|
|
, |
|
247
|
|
|
new FloatType(), |
|
248
|
|
|
]; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|