@@ -19,7 +19,6 @@ final class AttributesTest extends AbstractTokenizerTestCase
1919 * Test that attributes are parsed correctly.
2020 *
2121 * @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.
2322 * @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
2423 *
2524 * @dataProvider dataAttribute
@@ -29,10 +28,13 @@ final class AttributesTest extends AbstractTokenizerTestCase
2928 *
3029 * @return void
3130 */
32- public function testAttribute ($ testMarker , $ length , $ tokenCodes )
31+ public function testAttribute ($ testMarker , $ tokenCodes )
3332 {
3433 $ tokens = $ this ->phpcsFile ->getTokens ();
3534
35+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
36+ $ length = (count ($ tokenCodes ) + 1 );
37+
3638 $ attribute = $ this ->getTargetToken ($ testMarker , T_ATTRIBUTE );
3739 $ this ->assertArrayHasKey ('attribute_closer ' , $ tokens [$ attribute ]);
3840
@@ -71,14 +73,12 @@ public static function dataAttribute()
7173 return [
7274 'class attribute ' => [
7375 'testMarker ' => '/* testAttribute */ ' ,
74- 'length ' => 2 ,
7576 'tokenCodes ' => [
7677 T_STRING
7778 ],
7879 ],
7980 'class attribute with param ' => [
8081 'testMarker ' => '/* testAttributeWithParams */ ' ,
81- 'length ' => 7 ,
8282 'tokenCodes ' => [
8383 T_STRING ,
8484 T_OPEN_PARENTHESIS ,
@@ -90,7 +90,6 @@ public static function dataAttribute()
9090 ],
9191 'class attribute with named param ' => [
9292 'testMarker ' => '/* testAttributeWithNamedParam */ ' ,
93- 'length ' => 10 ,
9493 'tokenCodes ' => [
9594 T_STRING ,
9695 T_OPEN_PARENTHESIS ,
@@ -105,14 +104,12 @@ public static function dataAttribute()
105104 ],
106105 'function attribute ' => [
107106 'testMarker ' => '/* testAttributeOnFunction */ ' ,
108- 'length ' => 2 ,
109107 'tokenCodes ' => [
110108 T_STRING
111109 ],
112110 ],
113111 'function attribute with params ' => [
114112 'testMarker ' => '/* testAttributeOnFunctionWithParams */ ' ,
115- 'length ' => 17 ,
116113 'tokenCodes ' => [
117114 T_STRING ,
118115 T_OPEN_PARENTHESIS ,
@@ -134,7 +131,6 @@ public static function dataAttribute()
134131 ],
135132 'function attribute with arrow function as param ' => [
136133 'testMarker ' => '/* testAttributeWithShortClosureParameter */ ' ,
137- 'length ' => 17 ,
138134 'tokenCodes ' => [
139135 T_STRING ,
140136 T_OPEN_PARENTHESIS ,
@@ -156,7 +152,6 @@ public static function dataAttribute()
156152 ],
157153 'function attribute; multiple comma separated classes ' => [
158154 'testMarker ' => '/* testAttributeGrouping */ ' ,
159- 'length ' => 26 ,
160155 'tokenCodes ' => [
161156 T_STRING ,
162157 T_COMMA ,
@@ -187,7 +182,6 @@ public static function dataAttribute()
187182 ],
188183 'function attribute; multiple comma separated classes, one per line ' => [
189184 'testMarker ' => '/* testAttributeMultiline */ ' ,
190- 'length ' => 31 ,
191185 'tokenCodes ' => [
192186 T_WHITESPACE ,
193187 T_WHITESPACE ,
@@ -223,7 +217,6 @@ public static function dataAttribute()
223217 ],
224218 'function attribute; multiple comma separated classes, one per line, with comments ' => [
225219 'testMarker ' => '/* testAttributeMultilineWithComment */ ' ,
226- 'length ' => 34 ,
227220 'tokenCodes ' => [
228221 T_WHITESPACE ,
229222 T_WHITESPACE ,
@@ -262,7 +255,6 @@ public static function dataAttribute()
262255 ],
263256 'function attribute; using partially qualified and fully qualified class names ' => [
264257 'testMarker ' => '/* testFqcnAttribute */ ' ,
265- 'length ' => 13 ,
266258 'tokenCodes ' => [
267259 T_STRING ,
268260 T_NS_SEPARATOR ,
@@ -335,7 +327,6 @@ public function testAttributeAndLineComment()
335327 *
336328 * @param string $testMarker The comment which prefaces the target token in the test file.
337329 * @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.
339330 * @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
340331 *
341332 * @dataProvider dataAttributeOnParameters
@@ -346,10 +337,13 @@ public function testAttributeAndLineComment()
346337 *
347338 * @return void
348339 */
349- public function testAttributeOnParameters ($ testMarker , $ position , $ length , array $ tokenCodes )
340+ public function testAttributeOnParameters ($ testMarker , $ position , array $ tokenCodes )
350341 {
351342 $ tokens = $ this ->phpcsFile ->getTokens ();
352343
344+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
345+ $ length = (count ($ tokenCodes ) + 1 );
346+
353347 $ function = $ this ->getTargetToken ($ testMarker , T_FUNCTION );
354348 $ attribute = ($ function + $ position );
355349
@@ -394,15 +388,13 @@ public static function dataAttributeOnParameters()
394388 'parameter attribute; single, inline ' => [
395389 'testMarker ' => '/* testSingleAttributeOnParameter */ ' ,
396390 'position ' => 4 ,
397- 'length ' => 2 ,
398391 'tokenCodes ' => [
399392 T_STRING
400393 ],
401394 ],
402395 'parameter attribute; multiple comma separated, inline ' => [
403396 'testMarker ' => '/* testMultipleAttributesOnParameter */ ' ,
404397 'position ' => 4 ,
405- 'length ' => 10 ,
406398 'tokenCodes ' => [
407399 T_STRING ,
408400 T_COMMA ,
@@ -418,7 +410,6 @@ public static function dataAttributeOnParameters()
418410 'parameter attribute; single, multiline ' => [
419411 'testMarker ' => '/* testMultilineAttributesOnParameter */ ' ,
420412 'position ' => 4 ,
421- 'length ' => 13 ,
422413 'tokenCodes ' => [
423414 T_WHITESPACE ,
424415 T_WHITESPACE ,
@@ -443,7 +434,6 @@ public static function dataAttributeOnParameters()
443434 * Test that an attribute containing text which looks like a PHP close tag is tokenized correctly.
444435 *
445436 * @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.
447437 * @param array<array<string>> $expectedTokensAttribute The codes of tokens inside the attributes.
448438 * @param array<int|string> $expectedTokensAfter The codes of tokens after the attributes.
449439 *
@@ -453,10 +443,13 @@ public static function dataAttributeOnParameters()
453443 *
454444 * @return void
455445 */
456- public function testAttributeContainingTextLookingLikeCloseTag ($ testMarker , $ length , array $ expectedTokensAttribute , array $ expectedTokensAfter )
446+ public function testAttributeContainingTextLookingLikeCloseTag ($ testMarker , array $ expectedTokensAttribute , array $ expectedTokensAfter )
457447 {
458448 $ tokens = $ this ->phpcsFile ->getTokens ();
459449
450+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
451+ $ length = count ($ expectedTokensAttribute );
452+
460453 $ attribute = $ this ->getTargetToken ($ testMarker , T_ATTRIBUTE );
461454
462455 $ this ->assertSame ('T_ATTRIBUTE ' , $ tokens [$ attribute ]['type ' ]);
@@ -501,7 +494,6 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
501494 return [
502495 'function attribute; string param with "?>" ' => [
503496 'testMarker ' => '/* testAttributeContainingTextLookingLikeCloseTag */ ' ,
504- 'length ' => 5 ,
505497 'expectedTokensAttribute ' => [
506498 [
507499 'T_STRING ' ,
@@ -538,7 +530,6 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
538530 ],
539531 'function attribute; string param with "?>"; multiline ' => [
540532 'testMarker ' => '/* testAttributeContainingMultilineTextLookingLikeCloseTag */ ' ,
541- 'length ' => 8 ,
542533 'expectedTokensAttribute ' => [
543534 [
544535 'T_STRING ' ,
@@ -650,11 +641,14 @@ public function testNestedAttributes()
650641 T_CLOSE_PARENTHESIS ,
651642 ];
652643
644+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
645+ $ outerAttributeLength = (count ($ tokenCodes ) + 1 );
646+
653647 $ attribute = $ this ->getTargetToken ('/* testNestedAttributes */ ' , T_ATTRIBUTE );
654648 $ this ->assertArrayHasKey ('attribute_closer ' , $ tokens [$ attribute ]);
655649
656650 $ closer = $ tokens [$ attribute ]['attribute_closer ' ];
657- $ this ->assertSame (($ attribute + 24 ), $ closer );
651+ $ this ->assertSame (($ attribute + $ outerAttributeLength ), $ closer );
658652
659653 $ this ->assertSame (T_ATTRIBUTE_END , $ tokens [$ closer ]['code ' ]);
660654
@@ -663,37 +657,39 @@ public function testNestedAttributes()
663657
664658 $ this ->assertArrayNotHasKey ('nested_attributes ' , $ tokens [$ attribute ]);
665659 $ this ->assertArrayHasKey ('nested_attributes ' , $ tokens [($ attribute + 8 )]);
666- $ this ->assertSame ([$ attribute => ($ attribute + 24 )], $ tokens [($ attribute + 8 )]['nested_attributes ' ]);
660+ $ this ->assertSame ([$ attribute => ($ attribute + $ outerAttributeLength )], $ tokens [($ attribute + 8 )]['nested_attributes ' ]);
667661
668- $ test = function (array $ tokens , $ length , $ nestedMap ) use ($ attribute ) {
662+ $ test = function (array $ tokens , $ outerAttributeLength , $ nestedMap ) use ($ attribute ) {
669663 foreach ($ tokens as $ token ) {
670664 $ this ->assertArrayHasKey ('attribute_closer ' , $ token );
671- $ this ->assertSame (($ attribute + $ length ), $ token ['attribute_closer ' ]);
665+ $ this ->assertSame (($ attribute + $ outerAttributeLength ), $ token ['attribute_closer ' ]);
672666 $ this ->assertSame ($ nestedMap , $ token ['nested_attributes ' ]);
673667 }
674668 };
675669
676- $ test (array_slice ($ tokens , ($ attribute + 1 ), 7 ), 24 , [$ attribute => $ attribute + 24 ]);
677- $ test (array_slice ($ tokens , ($ attribute + 8 ), 1 ), 8 + 5 , [$ attribute => $ attribute + 24 ]);
678-
679670 // Length here is 8 (nested attribute offset) + 5 (real length).
671+ $ innerAttributeLength = (8 + 5 );
672+
673+ $ test (array_slice ($ tokens , ($ attribute + 1 ), 7 ), $ outerAttributeLength , [$ attribute => $ attribute + $ outerAttributeLength ]);
674+ $ test (array_slice ($ tokens , ($ attribute + 8 ), 1 ), $ innerAttributeLength , [$ attribute => $ attribute + $ outerAttributeLength ]);
675+
680676 $ test (
681677 array_slice ($ tokens , ($ attribute + 9 ), 4 ),
682- 8 + 5 ,
678+ $ innerAttributeLength ,
683679 [
684- $ attribute => $ attribute + 24 ,
680+ $ attribute => $ attribute + $ outerAttributeLength ,
685681 $ attribute + 8 => $ attribute + 13 ,
686682 ]
687683 );
688684
689- $ test (array_slice ($ tokens , ($ attribute + 13 ), 1 ), 8 + 5 , [$ attribute => $ attribute + 24 ]);
690- $ test (array_slice ($ tokens , ($ attribute + 14 ), 10 ), 24 , [$ attribute => $ attribute + 24 ]);
685+ $ test (array_slice ($ tokens , ($ attribute + 13 ), 1 ), $ innerAttributeLength , [$ attribute => $ attribute + $ outerAttributeLength ]);
686+ $ test (array_slice ($ tokens , ($ attribute + 14 ), 10 ), $ outerAttributeLength , [$ attribute => $ attribute + $ outerAttributeLength ]);
691687
692688 $ map = array_map (
693689 static function ($ token ) {
694690 return $ token ['code ' ];
695691 },
696- array_slice ($ tokens , ($ attribute + 1 ), 23 )
692+ array_slice ($ tokens , ($ attribute + 1 ), ( $ outerAttributeLength - 1 ) )
697693 );
698694
699695 $ this ->assertSame ($ tokenCodes , $ map );
0 commit comments