-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
PhpdocToPropertyTypeFixerTest.php
479 lines (449 loc) · 17.5 KB
/
PhpdocToPropertyTypeFixerTest.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\PhpdocToPropertyTypeFixer
*/
final class PhpdocToPropertyTypeFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $config = []): void
{
if (null !== $input && \PHP_VERSION_ID < 70400) {
$expected = $input;
$input = null;
}
$this->fixer->configure($config);
$this->doTest($expected, $input);
}
public function provideFixCases(): array
{
return [
'no phpdoc return' => [
'<?php class Foo { private $foo; }',
],
'invalid return' => [
'<?php class Foo { /** @var */ private $foo; }',
],
'invalid class 1' => [
'<?php class Foo { /** @var \9 */ private $foo; }',
],
'invalid class 2' => [
'<?php class Foo { /** @var \\Foo\\\\Bar */ private $foo; }',
],
'multiple returns' => [
'<?php
class Foo {
/**
* @var Bar
* @var Baz
*/
private $foo;
}
',
],
'non-root class' => [
'<?php class Foo { /** @var Bar */ private Bar $foo; }',
'<?php class Foo { /** @var Bar */ private $foo; }',
],
'non-root namespaced class' => [
'<?php class Foo { /** @var My\Bar */ private My\Bar $foo; }',
'<?php class Foo { /** @var My\Bar */ private $foo; }',
],
'root class' => [
'<?php class Foo { /** @var \My\Bar */ private \My\Bar $foo; }',
'<?php class Foo { /** @var \My\Bar */ private $foo; }',
],
'void' => [
'<?php class Foo { /** @var void */ private $foo; }',
],
'never' => [
'<?php class Foo { /** @var never */ private $foo; }',
],
'iterable' => [
'<?php class Foo { /** @var iterable */ private iterable $foo; }',
'<?php class Foo { /** @var iterable */ private $foo; }',
],
'object' => [
'<?php class Foo { /** @var object */ private object $foo; }',
'<?php class Foo { /** @var object */ private $foo; }',
],
'fix scalar types by default, int' => [
'<?php class Foo { /** @var int */ private int $foo; }',
'<?php class Foo { /** @var int */ private $foo; }',
],
'fix scalar types by default, float' => [
'<?php class Foo { /** @var float */ private float $foo; }',
'<?php class Foo { /** @var float */ private $foo; }',
],
'fix scalar types by default, string' => [
'<?php class Foo { /** @var string */ private string $foo; }',
'<?php class Foo { /** @var string */ private $foo; }',
],
'fix scalar types by default, bool' => [
'<?php class Foo { /** @var bool */ private bool $foo; }',
'<?php class Foo { /** @var bool */ private $foo; }',
],
'fix scalar types by default, false' => [
'<?php class Foo { /** @var false */ private bool $foo; }',
'<?php class Foo { /** @var false */ private $foo; }',
],
'fix scalar types by default, true' => [
'<?php class Foo { /** @var true */ private bool $foo; }',
'<?php class Foo { /** @var true */ private $foo; }',
],
'do not fix scalar types when configured as such' => [
'<?php class Foo { /** @var int */ private $foo; }',
null,
['scalar_types' => false],
],
'array native type' => [
'<?php class Foo { /** @var array */ private array $foo; }',
'<?php class Foo { /** @var array */ private $foo; }',
],
'callable type' => [
'<?php class Foo { /** @var callable */ private $foo; }',
],
'self accessor' => [
'<?php class Foo { /** @var self */ private self $foo; }',
'<?php class Foo { /** @var self */ private $foo; }',
],
'report static as self' => [
'<?php class Foo { /** @var static */ private self $foo; }',
'<?php class Foo { /** @var static */ private $foo; }',
],
'skip resource special type' => [
'<?php class Foo { /** @var resource */ private $foo; }',
],
'skip mixed special type' => [
'<?php class Foo { /** @var mixed */ private $foo; }',
],
'null alone cannot be a property type' => [
'<?php class Foo { /** @var null */ private $foo; }',
],
'skip mixed types' => [
'<?php class Foo { /** @var Foo|Bar */ private $foo; }',
],
'nullable type' => [
'<?php class Foo { /** @var null|Bar */ private ?Bar $foo; }',
'<?php class Foo { /** @var null|Bar */ private $foo; }',
],
'nullable type reverse order' => [
'<?php class Foo { /** @var Bar|null */ private ?Bar $foo; }',
'<?php class Foo { /** @var Bar|null */ private $foo; }',
],
'nullable native type' => [
'<?php class Foo { /** @var null|array */ private ?array $foo; }',
'<?php class Foo { /** @var null|array */ private $foo; }',
],
'skip mixed nullable types' => [
'<?php class Foo { /** @var null|Foo|Bar */ private $foo; }',
],
'generics' => [
'<?php class Foo { /** @var array<int, bool> */ private array $foo; }',
'<?php class Foo { /** @var array<int, bool> */ private $foo; }',
],
'array of types' => [
'<?php class Foo { /** @var Foo[] */ private array $foo; }',
'<?php class Foo { /** @var Foo[] */ private $foo; }',
],
'array of array of types' => [
'<?php class Foo { /** @var Foo[][] */ private array $foo; }',
'<?php class Foo { /** @var Foo[][] */ private $foo; }',
],
'nullable array of types' => [
'<?php class Foo { /** @var null|Foo[] */ private ?array $foo; }',
'<?php class Foo { /** @var null|Foo[] */ private $foo; }',
],
'comments' => [
'<?php
class Foo
{
// comment 0
/** @var Foo */ # comment 1
public/**/Foo $foo/**/;# comment 2
}
',
'<?php
class Foo
{
// comment 0
/** @var Foo */ # comment 1
public/**/$foo/**/;# comment 2
}
',
],
'array and traversable' => [
'<?php class Foo { /** @var array|Traversable */ private iterable $foo; }',
'<?php class Foo { /** @var array|Traversable */ private $foo; }',
],
'array and traversable with leading slash' => [
'<?php class Foo { /** @var array|\Traversable */ private iterable $foo; }',
'<?php class Foo { /** @var array|\Traversable */ private $foo; }',
],
'array and traversable in a namespace' => [
'<?php
namespace App;
class Foo {
/** @var array|Traversable */
private $foo;
}
',
],
'array and traversable with leading slash in a namespace' => [
'<?php
namespace App;
class Foo {
/** @var array|\Traversable */
private iterable $foo;
}
',
'<?php
namespace App;
class Foo {
/** @var array|\Traversable */
private $foo;
}
',
],
'array and imported traversable in a namespace' => [
'<?php
namespace App;
use Traversable;
class Foo {
/** @var array|Traversable */
private iterable $foo;
}
',
'<?php
namespace App;
use Traversable;
class Foo {
/** @var array|Traversable */
private $foo;
}
',
],
'array and object aliased as traversable in a namespace' => [
'<?php
namespace App;
use Bar as Traversable;
class Foo {
/** @var array|Traversable */
private $foo;
}
',
null,
],
'array of object and traversable' => [
'<?php class Foo { /** @var Foo[]|Traversable */ private iterable $foo; }',
'<?php class Foo { /** @var Foo[]|Traversable */ private $foo; }',
],
'array of object and iterable' => [
'<?php class Foo { /** @var Foo[]|iterable */ private iterable $foo; }',
'<?php class Foo { /** @var Foo[]|iterable */ private $foo; }',
],
'array of string and array of int' => [
'<?php class Foo { /** @var string[]|int[] */ private array $foo; }',
'<?php class Foo { /** @var string[]|int[] */ private $foo; }',
],
'trait' => [
'<?php trait Foo { /** @var int */ private int $foo; }',
'<?php trait Foo { /** @var int */ private $foo; }',
],
'static property' => [
'<?php class Foo { /** @var int */ private static int $foo; }',
'<?php class Foo { /** @var int */ private static $foo; }',
],
'static property reverse order' => [
'<?php class Foo { /** @var int */ static private int $foo; }',
'<?php class Foo { /** @var int */ static private $foo; }',
],
'var' => [
'<?php class Foo { /** @var int */ var int $foo; }',
'<?php class Foo { /** @var int */ var $foo; }',
],
'with default value' => [
'<?php class Foo { /** @var int */ public int $foo = 1; }',
'<?php class Foo { /** @var int */ public $foo = 1; }',
],
'multiple properties of the same type' => [
'<?php class Foo {
/**
* @var int $foo
* @var int $bar
*/
public int $foo, $bar;
}',
'<?php class Foo {
/**
* @var int $foo
* @var int $bar
*/
public $foo, $bar;
}',
],
'multiple properties of different types' => [
'<?php class Foo {
/**
* @var int $foo
* @var string $bar
*/
public $foo, $bar;
}',
],
'single property with different annotations' => [
'<?php class Foo {
/**
* @var int $foo
* @var string $foo
*/
public $foo;
}',
],
'multiple properties with missing annotation' => [
'<?php class Foo {
/**
* @var int $foo
*/
public $foo, $bar;
}',
],
'multiple properties with annotation without name' => [
'<?php class Foo {
/**
* @var int
* @var int $bar
*/
public $foo, $bar;
}',
],
'multiple properties with annotation without name reverse order' => [
'<?php class Foo {
/**
* @var int $foo
* @var int
*/
public $foo, $bar;
}',
],
'multiple properties with extra annotations' => [
'<?php class Foo {
/**
* @var string
* @var int $foo
* @var int $bar
* @var int
*/
public int $foo, $bar;
}',
'<?php class Foo {
/**
* @var string
* @var int $foo
* @var int $bar
* @var int
*/
public $foo, $bar;
}',
],
'abstract method' => [
'<?php abstract class Foo {
/** @var Bar */ private Bar $foo;
public abstract function getFoo();
}',
'<?php abstract class Foo {
/** @var Bar */ private $foo;
public abstract function getFoo();
}',
],
'great number of properties' => [
'<?php class Foo {
/** @var string */
private string $foo1;
/** @var string */
private string $foo2;
/** @var int */
private int $foo3;
/** @var string */
private string $foo4;
/** @var string */
private string $foo5;
/** @var string */
private string $foo6;
/** @var string */
private string $foo7;
/** @var int */
private int $foo8;
/** @var string */
private string $foo9;
/** @var int|null */
private ?int $foo10;
}',
'<?php class Foo {
/** @var string */
private $foo1;
/** @var string */
private $foo2;
/** @var int */
private $foo3;
/** @var string */
private $foo4;
/** @var string */
private $foo5;
/** @var string */
private $foo6;
/** @var string */
private $foo7;
/** @var int */
private $foo8;
/** @var string */
private $foo9;
/** @var int|null */
private $foo10;
}',
],
'anonymous class' => [
'<?php new class { /** @var int */ private int $foo; };',
'<?php new class { /** @var int */ private $foo; };',
],
'intersection types' => [
'<?php class Foo { /** @var Bar&Baz */ private $x; }',
],
'very long class name before ampersand' => [
'<?php class Foo { /** @var Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar&Baz */ private $x; }',
],
'very long class name after ampersand' => [
'<?php class Foo { /** @var Bar&Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz */ private $x; }',
],
];
}
/**
* @dataProvider provideFix81Cases
* @requires PHP 8.1
*/
public function testFix81(string $expected): void
{
$this->doTest($expected);
}
public function provideFix81Cases(): \Generator
{
yield 'readonly properties are always typed, make sure the fixer does not crash' => [
'<?php class Foo { /** @var int */ private readonly string $foo; }',
];
}
}