@@ -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 ' => 8 ,
266258 'tokenCodes ' => [
267259 T_NAME_QUALIFIED ,
268260 T_COMMA ,
@@ -330,7 +322,6 @@ public function testAttributeAndLineComment()
330322 *
331323 * @param string $testMarker The comment which prefaces the target token in the test file.
332324 * @param int $position The token position (starting from T_FUNCTION) of T_ATTRIBUTE token.
333- * @param int $length The number of tokens between opener and closer.
334325 * @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
335326 *
336327 * @dataProvider dataAttributeOnParameters
@@ -341,10 +332,13 @@ public function testAttributeAndLineComment()
341332 *
342333 * @return void
343334 */
344- public function testAttributeOnParameters ($ testMarker , $ position , $ length , array $ tokenCodes )
335+ public function testAttributeOnParameters ($ testMarker , $ position , array $ tokenCodes )
345336 {
346337 $ tokens = $ this ->phpcsFile ->getTokens ();
347338
339+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
340+ $ length = (count ($ tokenCodes ) + 1 );
341+
348342 $ function = $ this ->getTargetToken ($ testMarker , T_FUNCTION );
349343 $ attribute = ($ function + $ position );
350344
@@ -389,15 +383,13 @@ public static function dataAttributeOnParameters()
389383 'parameter attribute; single, inline ' => [
390384 'testMarker ' => '/* testSingleAttributeOnParameter */ ' ,
391385 'position ' => 4 ,
392- 'length ' => 2 ,
393386 'tokenCodes ' => [
394387 T_STRING
395388 ],
396389 ],
397390 'parameter attribute; multiple comma separated, inline ' => [
398391 'testMarker ' => '/* testMultipleAttributesOnParameter */ ' ,
399392 'position ' => 4 ,
400- 'length ' => 10 ,
401393 'tokenCodes ' => [
402394 T_STRING ,
403395 T_COMMA ,
@@ -413,7 +405,6 @@ public static function dataAttributeOnParameters()
413405 'parameter attribute; single, multiline ' => [
414406 'testMarker ' => '/* testMultilineAttributesOnParameter */ ' ,
415407 'position ' => 4 ,
416- 'length ' => 13 ,
417408 'tokenCodes ' => [
418409 T_WHITESPACE ,
419410 T_WHITESPACE ,
@@ -438,7 +429,6 @@ public static function dataAttributeOnParameters()
438429 * Test that an attribute containing text which looks like a PHP close tag is tokenized correctly.
439430 *
440431 * @param string $testMarker The comment which prefaces the target token in the test file.
441- * @param int $length The number of tokens between opener and closer.
442432 * @param array<array<string>> $expectedTokensAttribute The codes of tokens inside the attributes.
443433 * @param array<int|string> $expectedTokensAfter The codes of tokens after the attributes.
444434 *
@@ -448,10 +438,13 @@ public static function dataAttributeOnParameters()
448438 *
449439 * @return void
450440 */
451- public function testAttributeContainingTextLookingLikeCloseTag ($ testMarker , $ length , array $ expectedTokensAttribute , array $ expectedTokensAfter )
441+ public function testAttributeContainingTextLookingLikeCloseTag ($ testMarker , array $ expectedTokensAttribute , array $ expectedTokensAfter )
452442 {
453443 $ tokens = $ this ->phpcsFile ->getTokens ();
454444
445+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
446+ $ length = count ($ expectedTokensAttribute );
447+
455448 $ attribute = $ this ->getTargetToken ($ testMarker , T_ATTRIBUTE );
456449
457450 $ this ->assertSame ('T_ATTRIBUTE ' , $ tokens [$ attribute ]['type ' ]);
@@ -496,7 +489,6 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
496489 return [
497490 'function attribute; string param with "?>" ' => [
498491 'testMarker ' => '/* testAttributeContainingTextLookingLikeCloseTag */ ' ,
499- 'length ' => 5 ,
500492 'expectedTokensAttribute ' => [
501493 [
502494 'T_STRING ' ,
@@ -533,7 +525,6 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
533525 ],
534526 'function attribute; string param with "?>"; multiline ' => [
535527 'testMarker ' => '/* testAttributeContainingMultilineTextLookingLikeCloseTag */ ' ,
536- 'length ' => 8 ,
537528 'expectedTokensAttribute ' => [
538529 [
539530 'T_STRING ' ,
@@ -643,11 +634,14 @@ public function testNestedAttributes()
643634 T_CLOSE_PARENTHESIS ,
644635 ];
645636
637+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
638+ $ outerAttributeLength = (count ($ tokenCodes ) + 1 );
639+
646640 $ attribute = $ this ->getTargetToken ('/* testNestedAttributes */ ' , T_ATTRIBUTE );
647641 $ this ->assertArrayHasKey ('attribute_closer ' , $ tokens [$ attribute ]);
648642
649643 $ closer = $ tokens [$ attribute ]['attribute_closer ' ];
650- $ this ->assertSame (($ attribute + 22 ), $ closer );
644+ $ this ->assertSame (($ attribute + $ outerAttributeLength ), $ closer );
651645
652646 $ this ->assertSame (T_ATTRIBUTE_END , $ tokens [$ closer ]['code ' ]);
653647
@@ -656,37 +650,39 @@ public function testNestedAttributes()
656650
657651 $ this ->assertArrayNotHasKey ('nested_attributes ' , $ tokens [$ attribute ]);
658652 $ this ->assertArrayHasKey ('nested_attributes ' , $ tokens [($ attribute + 6 )]);
659- $ this ->assertSame ([$ attribute => ($ attribute + 22 )], $ tokens [($ attribute + 6 )]['nested_attributes ' ]);
653+ $ this ->assertSame ([$ attribute => ($ attribute + $ outerAttributeLength )], $ tokens [($ attribute + 6 )]['nested_attributes ' ]);
660654
661- $ test = function (array $ tokens , $ length , $ nestedMap ) use ($ attribute ) {
655+ $ test = function (array $ tokens , $ outerAttributeLength , $ nestedMap ) use ($ attribute ) {
662656 foreach ($ tokens as $ token ) {
663657 $ this ->assertArrayHasKey ('attribute_closer ' , $ token );
664- $ this ->assertSame (($ attribute + $ length ), $ token ['attribute_closer ' ]);
658+ $ this ->assertSame (($ attribute + $ outerAttributeLength ), $ token ['attribute_closer ' ]);
665659 $ this ->assertSame ($ nestedMap , $ token ['nested_attributes ' ]);
666660 }
667661 };
668662
669- $ test (array_slice ($ tokens , ($ attribute + 1 ), 5 ), 22 , [$ attribute => $ attribute + 22 ]);
670- $ test (array_slice ($ tokens , ($ attribute + 6 ), 1 ), 6 + 5 , [$ attribute => $ attribute + 22 ]);
671-
672663 // Length here is 6 (nested attribute offset) + 5 (real length).
664+ $ innerAttributeLength = (6 + 5 );
665+
666+ $ test (array_slice ($ tokens , ($ attribute + 1 ), 5 ), $ outerAttributeLength , [$ attribute => $ attribute + $ outerAttributeLength ]);
667+ $ test (array_slice ($ tokens , ($ attribute + 6 ), 1 ), $ innerAttributeLength , [$ attribute => $ attribute + $ outerAttributeLength ]);
668+
673669 $ test (
674670 array_slice ($ tokens , ($ attribute + 7 ), 4 ),
675- 6 + 5 ,
671+ $ innerAttributeLength ,
676672 [
677- $ attribute => $ attribute + 22 ,
673+ $ attribute => $ attribute + $ outerAttributeLength ,
678674 $ attribute + 6 => $ attribute + 11 ,
679675 ]
680676 );
681677
682- $ test (array_slice ($ tokens , ($ attribute + 11 ), 1 ), 6 + 5 , [$ attribute => $ attribute + 22 ]);
683- $ test (array_slice ($ tokens , ($ attribute + 12 ), 10 ), 22 , [$ attribute => $ attribute + 22 ]);
678+ $ test (array_slice ($ tokens , ($ attribute + 11 ), 1 ), $ innerAttributeLength , [$ attribute => $ attribute + $ outerAttributeLength ]);
679+ $ test (array_slice ($ tokens , ($ attribute + 12 ), 10 ), $ outerAttributeLength , [$ attribute => $ attribute + $ outerAttributeLength ]);
684680
685681 $ map = array_map (
686682 static function ($ token ) {
687683 return $ token ['code ' ];
688684 },
689- array_slice ($ tokens , ($ attribute + 1 ), 21 )
685+ array_slice ($ tokens , ($ attribute + 1 ), ( $ outerAttributeLength - 1 ) )
690686 );
691687
692688 $ this ->assertSame ($ tokenCodes , $ map );
0 commit comments