9
9
10
10
namespace PHP_CodeSniffer \Tests \Core \Tokenizers \PHP ;
11
11
12
+ use PHP_CodeSniffer \Util \Tokens ;
12
13
use PHP_CodeSniffer \Tests \Core \Tokenizers \AbstractTokenizerTestCase ;
13
14
14
15
final class AttributesTest extends AbstractTokenizerTestCase
@@ -18,8 +19,9 @@ final class AttributesTest extends AbstractTokenizerTestCase
18
19
/**
19
20
* Test that attributes are parsed correctly.
20
21
*
21
- * @param string $testMarker The comment which prefaces the target token in the test file.
22
- * @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
22
+ * @param string $testMarker The comment which prefaces the target token in the test file.
23
+ * @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
24
+ * @param int|string $expectedFirstAfter The expected token code for the first token after the attribute.
23
25
*
24
26
* @dataProvider dataAttribute
25
27
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
@@ -28,7 +30,7 @@ final class AttributesTest extends AbstractTokenizerTestCase
28
30
*
29
31
* @return void
30
32
*/
31
- public function testAttribute ($ testMarker , $ tokenCodes )
33
+ public function testAttribute ($ testMarker , $ tokenCodes, $ expectedFirstAfter )
32
34
{
33
35
$ tokens = $ this ->phpcsFile ->getTokens ();
34
36
@@ -58,6 +60,9 @@ function ($token) use ($attribute, $length) {
58
60
59
61
$ this ->assertSame ($ tokenCodes , $ map );
60
62
63
+ $ actualFirstAfter = $ this ->phpcsFile ->findNext (Tokens::$ emptyTokens , ($ closer + 1 ), null , true );
64
+ $ this ->assertSame ($ expectedFirstAfter , $ tokens [$ actualFirstAfter ]['code ' ]);
65
+
61
66
}//end testAttribute()
62
67
63
68
@@ -72,25 +77,27 @@ public static function dataAttribute()
72
77
{
73
78
return [
74
79
'class attribute ' => [
75
- 'testMarker ' => '/* testAttribute */ ' ,
76
- 'tokenCodes ' => [
80
+ 'testMarker ' => '/* testAttribute */ ' ,
81
+ 'tokenCodes ' => [
77
82
T_STRING ,
78
83
],
84
+ 'expectedFirstAfter ' => T_CLASS ,
79
85
],
80
86
'class attribute with param ' => [
81
- 'testMarker ' => '/* testAttributeWithParams */ ' ,
82
- 'tokenCodes ' => [
87
+ 'testMarker ' => '/* testAttributeWithParams */ ' ,
88
+ 'tokenCodes ' => [
83
89
T_STRING ,
84
90
T_OPEN_PARENTHESIS ,
85
91
T_STRING ,
86
92
T_DOUBLE_COLON ,
87
93
T_STRING ,
88
94
T_CLOSE_PARENTHESIS ,
89
95
],
96
+ 'expectedFirstAfter ' => T_CLASS ,
90
97
],
91
98
'class attribute with named param ' => [
92
- 'testMarker ' => '/* testAttributeWithNamedParam */ ' ,
93
- 'tokenCodes ' => [
99
+ 'testMarker ' => '/* testAttributeWithNamedParam */ ' ,
100
+ 'tokenCodes ' => [
94
101
T_STRING ,
95
102
T_OPEN_PARENTHESIS ,
96
103
T_PARAM_NAME ,
@@ -101,16 +108,18 @@ public static function dataAttribute()
101
108
T_STRING ,
102
109
T_CLOSE_PARENTHESIS ,
103
110
],
111
+ 'expectedFirstAfter ' => T_CLASS ,
104
112
],
105
113
'function attribute ' => [
106
- 'testMarker ' => '/* testAttributeOnFunction */ ' ,
107
- 'tokenCodes ' => [
114
+ 'testMarker ' => '/* testAttributeOnFunction */ ' ,
115
+ 'tokenCodes ' => [
108
116
T_STRING ,
109
117
],
118
+ 'expectedFirstAfter ' => T_FUNCTION ,
110
119
],
111
120
'function attribute with params ' => [
112
- 'testMarker ' => '/* testAttributeOnFunctionWithParams */ ' ,
113
- 'tokenCodes ' => [
121
+ 'testMarker ' => '/* testAttributeOnFunctionWithParams */ ' ,
122
+ 'tokenCodes ' => [
114
123
T_STRING ,
115
124
T_OPEN_PARENTHESIS ,
116
125
T_CONSTANT_ENCAPSED_STRING ,
@@ -128,10 +137,11 @@ public static function dataAttribute()
128
137
T_CLOSE_SHORT_ARRAY ,
129
138
T_CLOSE_PARENTHESIS ,
130
139
],
140
+ 'expectedFirstAfter ' => T_FUNCTION ,
131
141
],
132
142
'function attribute with arrow function as param ' => [
133
- 'testMarker ' => '/* testAttributeWithShortClosureParameter */ ' ,
134
- 'tokenCodes ' => [
143
+ 'testMarker ' => '/* testAttributeWithShortClosureParameter */ ' ,
144
+ 'tokenCodes ' => [
135
145
T_STRING ,
136
146
T_OPEN_PARENTHESIS ,
137
147
T_STATIC ,
@@ -149,10 +159,11 @@ public static function dataAttribute()
149
159
T_VARIABLE ,
150
160
T_CLOSE_PARENTHESIS ,
151
161
],
162
+ 'expectedFirstAfter ' => T_FUNCTION ,
152
163
],
153
164
'function attribute; multiple comma separated classes ' => [
154
- 'testMarker ' => '/* testAttributeGrouping */ ' ,
155
- 'tokenCodes ' => [
165
+ 'testMarker ' => '/* testAttributeGrouping */ ' ,
166
+ 'tokenCodes ' => [
156
167
T_STRING ,
157
168
T_COMMA ,
158
169
T_WHITESPACE ,
@@ -179,10 +190,11 @@ public static function dataAttribute()
179
190
T_CLOSE_SHORT_ARRAY ,
180
191
T_CLOSE_PARENTHESIS ,
181
192
],
193
+ 'expectedFirstAfter ' => T_FUNCTION ,
182
194
],
183
195
'function attribute; multiple comma separated classes, one per line ' => [
184
- 'testMarker ' => '/* testAttributeMultiline */ ' ,
185
- 'tokenCodes ' => [
196
+ 'testMarker ' => '/* testAttributeMultiline */ ' ,
197
+ 'tokenCodes ' => [
186
198
T_WHITESPACE ,
187
199
T_WHITESPACE ,
188
200
T_STRING ,
@@ -214,10 +226,11 @@ public static function dataAttribute()
214
226
T_CLOSE_PARENTHESIS ,
215
227
T_WHITESPACE ,
216
228
],
229
+ 'expectedFirstAfter ' => T_FUNCTION ,
217
230
],
218
231
'function attribute; multiple comma separated classes, one per line, with comments ' => [
219
- 'testMarker ' => '/* testAttributeMultilineWithComment */ ' ,
220
- 'tokenCodes ' => [
232
+ 'testMarker ' => '/* testAttributeMultilineWithComment */ ' ,
233
+ 'tokenCodes ' => [
221
234
T_WHITESPACE ,
222
235
T_WHITESPACE ,
223
236
T_STRING ,
@@ -252,10 +265,11 @@ public static function dataAttribute()
252
265
T_CLOSE_PARENTHESIS ,
253
266
T_WHITESPACE ,
254
267
],
268
+ 'expectedFirstAfter ' => T_FUNCTION ,
255
269
],
256
270
'function attribute; using partially qualified and fully qualified class names ' => [
257
- 'testMarker ' => '/* testFqcnAttribute */ ' ,
258
- 'tokenCodes ' => [
271
+ 'testMarker ' => '/* testFqcnAttribute */ ' ,
272
+ 'tokenCodes ' => [
259
273
T_STRING ,
260
274
T_NS_SEPARATOR ,
261
275
T_STRING ,
@@ -269,6 +283,7 @@ public static function dataAttribute()
269
283
T_CONSTANT_ENCAPSED_STRING ,
270
284
T_CLOSE_PARENTHESIS ,
271
285
],
286
+ 'expectedFirstAfter ' => T_FUNCTION ,
272
287
],
273
288
];
274
289
@@ -325,9 +340,11 @@ public function testAttributeAndLineComment()
325
340
/**
326
341
* Test that attributes on function declaration parameters are parsed correctly.
327
342
*
328
- * @param string $testMarker The comment which prefaces the target token in the test file.
329
- * @param int $position The token position (starting from T_FUNCTION) of T_ATTRIBUTE token.
330
- * @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
343
+ * @param string $testMarker The comment which prefaces the target token in the test file.
344
+ * @param int $position The token position (starting from T_FUNCTION) of T_ATTRIBUTE token.
345
+ * @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
346
+ * @param int|string $expectedFirstAfter The expected token code for the first token after the attribute.
347
+ * @param string $expectedFirstAfterContent The expected token code for the first token after the attribute.
331
348
*
332
349
* @dataProvider dataAttributeOnParameters
333
350
*
@@ -337,7 +354,7 @@ public function testAttributeAndLineComment()
337
354
*
338
355
* @return void
339
356
*/
340
- public function testAttributeOnParameters ($ testMarker , $ position , array $ tokenCodes )
357
+ public function testAttributeOnParameters ($ testMarker , $ position , array $ tokenCodes, $ expectedFirstAfter , $ expectedFirstAfterContent )
341
358
{
342
359
$ tokens = $ this ->phpcsFile ->getTokens ();
343
360
@@ -354,8 +371,8 @@ public function testAttributeOnParameters($testMarker, $position, array $tokenCo
354
371
355
372
$ closer = $ tokens [$ attribute ]['attribute_closer ' ];
356
373
$ this ->assertSame (T_WHITESPACE , $ tokens [($ closer + 1 )]['code ' ]);
357
- $ this ->assertSame (T_STRING , $ tokens [($ closer + 2 )]['code ' ]);
358
- $ this ->assertSame (' int ' , $ tokens [($ closer + 2 )]['content ' ]);
374
+ $ this ->assertSame ($ expectedFirstAfter , $ tokens [($ closer + 2 )]['code ' ]);
375
+ $ this ->assertSame ($ expectedFirstAfterContent , $ tokens [($ closer + 2 )]['content ' ]);
359
376
360
377
$ this ->assertSame (T_VARIABLE , $ tokens [($ closer + 4 )]['code ' ]);
361
378
$ this ->assertSame ('$param ' , $ tokens [($ closer + 4 )]['content ' ]);
@@ -386,16 +403,18 @@ public static function dataAttributeOnParameters()
386
403
{
387
404
return [
388
405
'parameter attribute; single, inline ' => [
389
- 'testMarker ' => '/* testSingleAttributeOnParameter */ ' ,
390
- 'position ' => 4 ,
391
- 'tokenCodes ' => [
406
+ 'testMarker ' => '/* testSingleAttributeOnParameter */ ' ,
407
+ 'position ' => 4 ,
408
+ 'tokenCodes ' => [
392
409
T_STRING ,
393
410
],
411
+ 'expectedFirstAfter ' => T_STRING ,
412
+ 'expectedFirstAfterContent ' => 'int ' ,
394
413
],
395
414
'parameter attribute; multiple comma separated, inline ' => [
396
- 'testMarker ' => '/* testMultipleAttributesOnParameter */ ' ,
397
- 'position ' => 4 ,
398
- 'tokenCodes ' => [
415
+ 'testMarker ' => '/* testMultipleAttributesOnParameter */ ' ,
416
+ 'position ' => 4 ,
417
+ 'tokenCodes ' => [
399
418
T_STRING ,
400
419
T_COMMA ,
401
420
T_WHITESPACE ,
@@ -406,11 +425,13 @@ public static function dataAttributeOnParameters()
406
425
T_CONSTANT_ENCAPSED_STRING ,
407
426
T_CLOSE_PARENTHESIS ,
408
427
],
428
+ 'expectedFirstAfter ' => T_STRING ,
429
+ 'expectedFirstAfterContent ' => 'array ' ,
409
430
],
410
431
'parameter attribute; single, multiline ' => [
411
- 'testMarker ' => '/* testMultilineAttributesOnParameter */ ' ,
412
- 'position ' => 4 ,
413
- 'tokenCodes ' => [
432
+ 'testMarker ' => '/* testMultilineAttributesOnParameter */ ' ,
433
+ 'position ' => 4 ,
434
+ 'tokenCodes ' => [
414
435
T_WHITESPACE ,
415
436
T_WHITESPACE ,
416
437
T_STRING ,
@@ -424,6 +445,8 @@ public static function dataAttributeOnParameters()
424
445
T_WHITESPACE ,
425
446
T_WHITESPACE ,
426
447
],
448
+ 'expectedFirstAfter ' => T_NULL ,
449
+ 'expectedFirstAfterContent ' => 'null ' ,
427
450
],
428
451
];
429
452
0 commit comments