99
1010namespace PHP_CodeSniffer \Tests \Core \Tokenizers \PHP ;
1111
12+ use PHP_CodeSniffer \Util \Tokens ;
1213use PHP_CodeSniffer \Tests \Core \Tokenizers \AbstractTokenizerTestCase ;
1314
1415final class AttributesTest extends AbstractTokenizerTestCase
@@ -18,8 +19,9 @@ final class AttributesTest extends AbstractTokenizerTestCase
1819 /**
1920 * Test that attributes are parsed correctly.
2021 *
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.
2325 *
2426 * @dataProvider dataAttribute
2527 * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
@@ -28,7 +30,7 @@ final class AttributesTest extends AbstractTokenizerTestCase
2830 *
2931 * @return void
3032 */
31- public function testAttribute ($ testMarker , $ tokenCodes )
33+ public function testAttribute ($ testMarker , $ tokenCodes, $ expectedFirstAfter )
3234 {
3335 $ tokens = $ this ->phpcsFile ->getTokens ();
3436
@@ -58,6 +60,9 @@ function ($token) use ($attribute, $length) {
5860
5961 $ this ->assertSame ($ tokenCodes , $ map );
6062
63+ $ actualFirstAfter = $ this ->phpcsFile ->findNext (Tokens::$ emptyTokens , ($ closer + 1 ), null , true );
64+ $ this ->assertSame ($ expectedFirstAfter , $ tokens [$ actualFirstAfter ]['code ' ]);
65+
6166 }//end testAttribute()
6267
6368
@@ -72,25 +77,27 @@ public static function dataAttribute()
7277 {
7378 return [
7479 'class attribute ' => [
75- 'testMarker ' => '/* testAttribute */ ' ,
76- 'tokenCodes ' => [
80+ 'testMarker ' => '/* testAttribute */ ' ,
81+ 'tokenCodes ' => [
7782 T_STRING ,
7883 ],
84+ 'expectedFirstAfter ' => T_CLASS ,
7985 ],
8086 'class attribute with param ' => [
81- 'testMarker ' => '/* testAttributeWithParams */ ' ,
82- 'tokenCodes ' => [
87+ 'testMarker ' => '/* testAttributeWithParams */ ' ,
88+ 'tokenCodes ' => [
8389 T_STRING ,
8490 T_OPEN_PARENTHESIS ,
8591 T_STRING ,
8692 T_DOUBLE_COLON ,
8793 T_STRING ,
8894 T_CLOSE_PARENTHESIS ,
8995 ],
96+ 'expectedFirstAfter ' => T_CLASS ,
9097 ],
9198 'class attribute with named param ' => [
92- 'testMarker ' => '/* testAttributeWithNamedParam */ ' ,
93- 'tokenCodes ' => [
99+ 'testMarker ' => '/* testAttributeWithNamedParam */ ' ,
100+ 'tokenCodes ' => [
94101 T_STRING ,
95102 T_OPEN_PARENTHESIS ,
96103 T_PARAM_NAME ,
@@ -101,16 +108,18 @@ public static function dataAttribute()
101108 T_STRING ,
102109 T_CLOSE_PARENTHESIS ,
103110 ],
111+ 'expectedFirstAfter ' => T_CLASS ,
104112 ],
105113 'function attribute ' => [
106- 'testMarker ' => '/* testAttributeOnFunction */ ' ,
107- 'tokenCodes ' => [
114+ 'testMarker ' => '/* testAttributeOnFunction */ ' ,
115+ 'tokenCodes ' => [
108116 T_STRING ,
109117 ],
118+ 'expectedFirstAfter ' => T_FUNCTION ,
110119 ],
111120 'function attribute with params ' => [
112- 'testMarker ' => '/* testAttributeOnFunctionWithParams */ ' ,
113- 'tokenCodes ' => [
121+ 'testMarker ' => '/* testAttributeOnFunctionWithParams */ ' ,
122+ 'tokenCodes ' => [
114123 T_STRING ,
115124 T_OPEN_PARENTHESIS ,
116125 T_CONSTANT_ENCAPSED_STRING ,
@@ -128,10 +137,11 @@ public static function dataAttribute()
128137 T_CLOSE_SHORT_ARRAY ,
129138 T_CLOSE_PARENTHESIS ,
130139 ],
140+ 'expectedFirstAfter ' => T_FUNCTION ,
131141 ],
132142 'function attribute with arrow function as param ' => [
133- 'testMarker ' => '/* testAttributeWithShortClosureParameter */ ' ,
134- 'tokenCodes ' => [
143+ 'testMarker ' => '/* testAttributeWithShortClosureParameter */ ' ,
144+ 'tokenCodes ' => [
135145 T_STRING ,
136146 T_OPEN_PARENTHESIS ,
137147 T_STATIC ,
@@ -149,10 +159,11 @@ public static function dataAttribute()
149159 T_VARIABLE ,
150160 T_CLOSE_PARENTHESIS ,
151161 ],
162+ 'expectedFirstAfter ' => T_FUNCTION ,
152163 ],
153164 'function attribute; multiple comma separated classes ' => [
154- 'testMarker ' => '/* testAttributeGrouping */ ' ,
155- 'tokenCodes ' => [
165+ 'testMarker ' => '/* testAttributeGrouping */ ' ,
166+ 'tokenCodes ' => [
156167 T_STRING ,
157168 T_COMMA ,
158169 T_WHITESPACE ,
@@ -179,10 +190,11 @@ public static function dataAttribute()
179190 T_CLOSE_SHORT_ARRAY ,
180191 T_CLOSE_PARENTHESIS ,
181192 ],
193+ 'expectedFirstAfter ' => T_FUNCTION ,
182194 ],
183195 'function attribute; multiple comma separated classes, one per line ' => [
184- 'testMarker ' => '/* testAttributeMultiline */ ' ,
185- 'tokenCodes ' => [
196+ 'testMarker ' => '/* testAttributeMultiline */ ' ,
197+ 'tokenCodes ' => [
186198 T_WHITESPACE ,
187199 T_WHITESPACE ,
188200 T_STRING ,
@@ -214,10 +226,11 @@ public static function dataAttribute()
214226 T_CLOSE_PARENTHESIS ,
215227 T_WHITESPACE ,
216228 ],
229+ 'expectedFirstAfter ' => T_FUNCTION ,
217230 ],
218231 'function attribute; multiple comma separated classes, one per line, with comments ' => [
219- 'testMarker ' => '/* testAttributeMultilineWithComment */ ' ,
220- 'tokenCodes ' => [
232+ 'testMarker ' => '/* testAttributeMultilineWithComment */ ' ,
233+ 'tokenCodes ' => [
221234 T_WHITESPACE ,
222235 T_WHITESPACE ,
223236 T_STRING ,
@@ -252,10 +265,11 @@ public static function dataAttribute()
252265 T_CLOSE_PARENTHESIS ,
253266 T_WHITESPACE ,
254267 ],
268+ 'expectedFirstAfter ' => T_FUNCTION ,
255269 ],
256270 'function attribute; using partially qualified and fully qualified class names ' => [
257- 'testMarker ' => '/* testFqcnAttribute */ ' ,
258- 'tokenCodes ' => [
271+ 'testMarker ' => '/* testFqcnAttribute */ ' ,
272+ 'tokenCodes ' => [
259273 T_STRING ,
260274 T_NS_SEPARATOR ,
261275 T_STRING ,
@@ -269,6 +283,7 @@ public static function dataAttribute()
269283 T_CONSTANT_ENCAPSED_STRING ,
270284 T_CLOSE_PARENTHESIS ,
271285 ],
286+ 'expectedFirstAfter ' => T_FUNCTION ,
272287 ],
273288 ];
274289
@@ -325,9 +340,11 @@ public function testAttributeAndLineComment()
325340 /**
326341 * Test that attributes on function declaration parameters are parsed correctly.
327342 *
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.
331348 *
332349 * @dataProvider dataAttributeOnParameters
333350 *
@@ -337,7 +354,7 @@ public function testAttributeAndLineComment()
337354 *
338355 * @return void
339356 */
340- public function testAttributeOnParameters ($ testMarker , $ position , array $ tokenCodes )
357+ public function testAttributeOnParameters ($ testMarker , $ position , array $ tokenCodes, $ expectedFirstAfter , $ expectedFirstAfterContent )
341358 {
342359 $ tokens = $ this ->phpcsFile ->getTokens ();
343360
@@ -354,8 +371,8 @@ public function testAttributeOnParameters($testMarker, $position, array $tokenCo
354371
355372 $ closer = $ tokens [$ attribute ]['attribute_closer ' ];
356373 $ 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 ' ]);
359376
360377 $ this ->assertSame (T_VARIABLE , $ tokens [($ closer + 4 )]['code ' ]);
361378 $ this ->assertSame ('$param ' , $ tokens [($ closer + 4 )]['content ' ]);
@@ -386,16 +403,18 @@ public static function dataAttributeOnParameters()
386403 {
387404 return [
388405 'parameter attribute; single, inline ' => [
389- 'testMarker ' => '/* testSingleAttributeOnParameter */ ' ,
390- 'position ' => 4 ,
391- 'tokenCodes ' => [
406+ 'testMarker ' => '/* testSingleAttributeOnParameter */ ' ,
407+ 'position ' => 4 ,
408+ 'tokenCodes ' => [
392409 T_STRING ,
393410 ],
411+ 'expectedFirstAfter ' => T_STRING ,
412+ 'expectedFirstAfterContent ' => 'int ' ,
394413 ],
395414 'parameter attribute; multiple comma separated, inline ' => [
396- 'testMarker ' => '/* testMultipleAttributesOnParameter */ ' ,
397- 'position ' => 4 ,
398- 'tokenCodes ' => [
415+ 'testMarker ' => '/* testMultipleAttributesOnParameter */ ' ,
416+ 'position ' => 4 ,
417+ 'tokenCodes ' => [
399418 T_STRING ,
400419 T_COMMA ,
401420 T_WHITESPACE ,
@@ -406,11 +425,13 @@ public static function dataAttributeOnParameters()
406425 T_CONSTANT_ENCAPSED_STRING ,
407426 T_CLOSE_PARENTHESIS ,
408427 ],
428+ 'expectedFirstAfter ' => T_STRING ,
429+ 'expectedFirstAfterContent ' => 'array ' ,
409430 ],
410431 'parameter attribute; single, multiline ' => [
411- 'testMarker ' => '/* testMultilineAttributesOnParameter */ ' ,
412- 'position ' => 4 ,
413- 'tokenCodes ' => [
432+ 'testMarker ' => '/* testMultilineAttributesOnParameter */ ' ,
433+ 'position ' => 4 ,
434+ 'tokenCodes ' => [
414435 T_WHITESPACE ,
415436 T_WHITESPACE ,
416437 T_STRING ,
@@ -424,6 +445,8 @@ public static function dataAttributeOnParameters()
424445 T_WHITESPACE ,
425446 T_WHITESPACE ,
426447 ],
448+ 'expectedFirstAfter ' => T_NULL ,
449+ 'expectedFirstAfterContent ' => 'null ' ,
427450 ],
428451 ];
429452
0 commit comments