@@ -18,9 +18,9 @@ final class AttributesTest extends AbstractMethodUnitTest
18
18
/**
19
19
* Test that attributes are parsed correctly.
20
20
*
21
- * @param string $testMarker The comment which prefaces the target token in the test file.
22
- * @param int $length The number of tokens between opener and closer.
23
- * @param array $tokenCodes The codes of tokens inside the attributes.
21
+ * @param string $testMarker The comment which prefaces the target token in the test file.
22
+ * @param int $length The number of tokens between opener and closer.
23
+ * @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
24
24
*
25
25
* @dataProvider dataAttribute
26
26
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
@@ -64,20 +64,22 @@ function ($token) use ($attribute, $length) {
64
64
*
65
65
* @see testAttribute()
66
66
*
67
- * @return array
67
+ * @return array<string, array<string, string|int|array<int|string>>>
68
68
*/
69
69
public static function dataAttribute ()
70
70
{
71
71
return [
72
- [
73
- '/* testAttribute */ ' ,
74
- 2 ,
75
- [ T_STRING ],
72
+ 'class attribute ' => [
73
+ 'testMarker ' => '/* testAttribute */ ' ,
74
+ 'length ' => 2 ,
75
+ 'tokenCodes ' => [
76
+ T_STRING
77
+ ],
76
78
],
77
- [
78
- '/* testAttributeWithParams */ ' ,
79
- 7 ,
80
- [
79
+ ' class attribute with param ' => [
80
+ 'testMarker ' => ' /* testAttributeWithParams */ ' ,
81
+ ' length ' => 7 ,
82
+ ' tokenCodes ' => [
81
83
T_STRING ,
82
84
T_OPEN_PARENTHESIS ,
83
85
T_STRING ,
@@ -86,10 +88,10 @@ public static function dataAttribute()
86
88
T_CLOSE_PARENTHESIS ,
87
89
],
88
90
],
89
- [
90
- '/* testAttributeWithNamedParam */ ' ,
91
- 10 ,
92
- [
91
+ ' class attribute with named param ' => [
92
+ 'testMarker ' => ' /* testAttributeWithNamedParam */ ' ,
93
+ ' length ' => 10 ,
94
+ ' tokenCodes ' => [
93
95
T_STRING ,
94
96
T_OPEN_PARENTHESIS ,
95
97
T_PARAM_NAME ,
@@ -101,15 +103,17 @@ public static function dataAttribute()
101
103
T_CLOSE_PARENTHESIS ,
102
104
],
103
105
],
104
- [
105
- '/* testAttributeOnFunction */ ' ,
106
- 2 ,
107
- [ T_STRING ],
106
+ 'function attribute ' => [
107
+ 'testMarker ' => '/* testAttributeOnFunction */ ' ,
108
+ 'length ' => 2 ,
109
+ 'tokenCodes ' => [
110
+ T_STRING
111
+ ],
108
112
],
109
- [
110
- '/* testAttributeOnFunctionWithParams */ ' ,
111
- 17 ,
112
- [
113
+ ' function attribute with params ' => [
114
+ 'testMarker ' => ' /* testAttributeOnFunctionWithParams */ ' ,
115
+ ' length ' => 17 ,
116
+ ' tokenCodes ' => [
113
117
T_STRING ,
114
118
T_OPEN_PARENTHESIS ,
115
119
T_CONSTANT_ENCAPSED_STRING ,
@@ -128,10 +132,10 @@ public static function dataAttribute()
128
132
T_CLOSE_PARENTHESIS ,
129
133
],
130
134
],
131
- [
132
- '/* testAttributeWithShortClosureParameter */ ' ,
133
- 17 ,
134
- [
135
+ ' function attribute with arrow function as param ' => [
136
+ 'testMarker ' => ' /* testAttributeWithShortClosureParameter */ ' ,
137
+ ' length ' => 17 ,
138
+ ' tokenCodes ' => [
135
139
T_STRING ,
136
140
T_OPEN_PARENTHESIS ,
137
141
T_STATIC ,
@@ -150,10 +154,10 @@ public static function dataAttribute()
150
154
T_CLOSE_PARENTHESIS ,
151
155
],
152
156
],
153
- [
154
- '/* testAttributeGrouping */ ' ,
155
- 26 ,
156
- [
157
+ ' function attribute; multiple comma separated classes ' => [
158
+ 'testMarker ' => ' /* testAttributeGrouping */ ' ,
159
+ ' length ' => 26 ,
160
+ ' tokenCodes ' => [
157
161
T_STRING ,
158
162
T_COMMA ,
159
163
T_WHITESPACE ,
@@ -181,10 +185,10 @@ public static function dataAttribute()
181
185
T_CLOSE_PARENTHESIS ,
182
186
],
183
187
],
184
- [
185
- '/* testAttributeMultiline */ ' ,
186
- 31 ,
187
- [
188
+ ' function attribute; multiple comma separated classes, one per line ' => [
189
+ 'testMarker ' => ' /* testAttributeMultiline */ ' ,
190
+ ' length ' => 31 ,
191
+ ' tokenCodes ' => [
188
192
T_WHITESPACE ,
189
193
T_WHITESPACE ,
190
194
T_STRING ,
@@ -217,10 +221,49 @@ public static function dataAttribute()
217
221
T_WHITESPACE ,
218
222
],
219
223
],
220
- [
221
- '/* testFqcnAttribute */ ' ,
222
- 13 ,
223
- [
224
+ 'function attribute; multiple comma separated classes, one per line, with comments ' => [
225
+ 'testMarker ' => '/* testAttributeMultilineWithComment */ ' ,
226
+ 'length ' => 34 ,
227
+ 'tokenCodes ' => [
228
+ T_WHITESPACE ,
229
+ T_WHITESPACE ,
230
+ T_STRING ,
231
+ T_COMMA ,
232
+ T_WHITESPACE ,
233
+ T_COMMENT ,
234
+ T_WHITESPACE ,
235
+ T_STRING ,
236
+ T_OPEN_PARENTHESIS ,
237
+ T_COMMENT ,
238
+ T_WHITESPACE ,
239
+ T_CONSTANT_ENCAPSED_STRING ,
240
+ T_CLOSE_PARENTHESIS ,
241
+ T_COMMA ,
242
+ T_WHITESPACE ,
243
+ T_WHITESPACE ,
244
+ T_STRING ,
245
+ T_OPEN_PARENTHESIS ,
246
+ T_CONSTANT_ENCAPSED_STRING ,
247
+ T_COMMA ,
248
+ T_WHITESPACE ,
249
+ T_PARAM_NAME ,
250
+ T_COLON ,
251
+ T_WHITESPACE ,
252
+ T_OPEN_SHORT_ARRAY ,
253
+ T_CONSTANT_ENCAPSED_STRING ,
254
+ T_WHITESPACE ,
255
+ T_DOUBLE_ARROW ,
256
+ T_WHITESPACE ,
257
+ T_CONSTANT_ENCAPSED_STRING ,
258
+ T_CLOSE_SHORT_ARRAY ,
259
+ T_CLOSE_PARENTHESIS ,
260
+ T_WHITESPACE ,
261
+ ],
262
+ ],
263
+ 'function attribute; using partially qualified and fully qualified class names ' => [
264
+ 'testMarker ' => '/* testFqcnAttribute */ ' ,
265
+ 'length ' => 13 ,
266
+ 'tokenCodes ' => [
224
267
T_STRING ,
225
268
T_NS_SEPARATOR ,
226
269
T_STRING ,
@@ -290,10 +333,10 @@ public function testAttributeAndLineComment()
290
333
/**
291
334
* Test that attributes on function declaration parameters are parsed correctly.
292
335
*
293
- * @param string $testMarker The comment which prefaces the target token in the test file.
294
- * @param int $position The token position (starting from T_FUNCTION) of T_ATTRIBUTE token.
295
- * @param int $length The number of tokens between opener and closer.
296
- * @param array $tokenCodes The codes of tokens inside the attributes.
336
+ * @param string $testMarker The comment which prefaces the target token in the test file.
337
+ * @param int $position The token position (starting from T_FUNCTION) of T_ATTRIBUTE token.
338
+ * @param int $length The number of tokens between opener and closer.
339
+ * @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
297
340
*
298
341
* @dataProvider dataAttributeOnParameters
299
342
*
@@ -343,22 +386,24 @@ function ($token) use ($attribute, $length) {
343
386
*
344
387
* @see testAttributeOnParameters()
345
388
*
346
- * @return array
389
+ * @return array<string, array<string, string|int|array<int|string>>>
347
390
*/
348
391
public static function dataAttributeOnParameters ()
349
392
{
350
393
return [
351
- [
352
- '/* testSingleAttributeOnParameter */ ' ,
353
- 4 ,
354
- 2 ,
355
- [T_STRING ],
394
+ 'parameter attribute; single, inline ' => [
395
+ 'testMarker ' => '/* testSingleAttributeOnParameter */ ' ,
396
+ 'position ' => 4 ,
397
+ 'length ' => 2 ,
398
+ 'tokenCodes ' => [
399
+ T_STRING
400
+ ],
356
401
],
357
- [
358
- '/* testMultipleAttributesOnParameter */ ' ,
359
- 4 ,
360
- 10 ,
361
- [
402
+ ' parameter attribute; multiple comma separated, inline ' => [
403
+ 'testMarker ' => ' /* testMultipleAttributesOnParameter */ ' ,
404
+ ' position ' => 4 ,
405
+ ' length ' => 10 ,
406
+ ' tokenCodes ' => [
362
407
T_STRING ,
363
408
T_COMMA ,
364
409
T_WHITESPACE ,
@@ -370,11 +415,11 @@ public static function dataAttributeOnParameters()
370
415
T_CLOSE_PARENTHESIS ,
371
416
],
372
417
],
373
- [
374
- '/* testMultilineAttributesOnParameter */ ' ,
375
- 4 ,
376
- 13 ,
377
- [
418
+ ' parameter attribute; single, multiline ' => [
419
+ 'testMarker ' => ' /* testMultilineAttributesOnParameter */ ' ,
420
+ ' position ' => 4 ,
421
+ ' length ' => 13 ,
422
+ ' tokenCodes ' => [
378
423
T_WHITESPACE ,
379
424
T_WHITESPACE ,
380
425
T_STRING ,
@@ -397,10 +442,10 @@ public static function dataAttributeOnParameters()
397
442
/**
398
443
* Test that an attribute containing text which looks like a PHP close tag is tokenized correctly.
399
444
*
400
- * @param string $testMarker The comment which prefaces the target token in the test file.
401
- * @param int $length The number of tokens between opener and closer.
402
- * @param array $expectedTokensAttribute The codes of tokens inside the attributes.
403
- * @param array $expectedTokensAfter The codes of tokens after the attributes.
445
+ * @param string $testMarker The comment which prefaces the target token in the test file.
446
+ * @param int $length The number of tokens between opener and closer.
447
+ * @param array<array<string>> $expectedTokensAttribute The codes of tokens inside the attributes.
448
+ * @param array<int|string> $expectedTokensAfter The codes of tokens after the attributes.
404
449
*
405
450
* @covers PHP_CodeSniffer\Tokenizers\PHP::parsePhpAttribute
406
451
*
@@ -449,15 +494,15 @@ public function testAttributeContainingTextLookingLikeCloseTag($testMarker, $len
449
494
*
450
495
* @see dataAttributeOnTextLookingLikeCloseTag()
451
496
*
452
- * @return array
497
+ * @return array<string, array<string, string|int|array<array<string>>|array<int|string>>>
453
498
*/
454
499
public static function dataAttributeOnTextLookingLikeCloseTag ()
455
500
{
456
501
return [
457
- [
458
- '/* testAttributeContainingTextLookingLikeCloseTag */ ' ,
459
- 5 ,
460
- [
502
+ ' function attribute; string param with "?>" ' => [
503
+ 'testMarker ' => ' /* testAttributeContainingTextLookingLikeCloseTag */ ' ,
504
+ ' length ' => 5 ,
505
+ ' expectedTokensAttribute ' => [
461
506
[
462
507
'T_STRING ' ,
463
508
'DeprecationReason ' ,
@@ -479,7 +524,7 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
479
524
'] ' ,
480
525
],
481
526
],
482
- [
527
+ ' expectedTokensAfter ' => [
483
528
T_WHITESPACE ,
484
529
T_FUNCTION ,
485
530
T_WHITESPACE ,
@@ -491,10 +536,10 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
491
536
T_CLOSE_CURLY_BRACKET ,
492
537
],
493
538
],
494
- [
495
- '/* testAttributeContainingMultilineTextLookingLikeCloseTag */ ' ,
496
- 8 ,
497
- [
539
+ ' function attribute; string param with "?>"; multiline ' => [
540
+ 'testMarker ' => ' /* testAttributeContainingMultilineTextLookingLikeCloseTag */ ' ,
541
+ ' length ' => 8 ,
542
+ ' expectedTokensAttribute ' => [
498
543
[
499
544
'T_STRING ' ,
500
545
'DeprecationReason ' ,
@@ -528,7 +573,7 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
528
573
'] ' ,
529
574
],
530
575
],
531
- [
576
+ ' expectedTokensAfter ' => [
532
577
T_WHITESPACE ,
533
578
T_FUNCTION ,
534
579
T_WHITESPACE ,
0 commit comments