@@ -19,7 +19,6 @@ final class AttributesTest extends AbstractTokenizerTestCase
19
19
* Test that attributes are parsed correctly.
20
20
*
21
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
22
* @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
24
23
*
25
24
* @dataProvider dataAttribute
@@ -29,10 +28,13 @@ final class AttributesTest extends AbstractTokenizerTestCase
29
28
*
30
29
* @return void
31
30
*/
32
- public function testAttribute ($ testMarker , $ length , $ tokenCodes )
31
+ public function testAttribute ($ testMarker , $ tokenCodes )
33
32
{
34
33
$ tokens = $ this ->phpcsFile ->getTokens ();
35
34
35
+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
36
+ $ length = (count ($ tokenCodes ) + 1 );
37
+
36
38
$ attribute = $ this ->getTargetToken ($ testMarker , T_ATTRIBUTE );
37
39
$ this ->assertArrayHasKey ('attribute_closer ' , $ tokens [$ attribute ]);
38
40
@@ -71,14 +73,12 @@ public static function dataAttribute()
71
73
return [
72
74
'class attribute ' => [
73
75
'testMarker ' => '/* testAttribute */ ' ,
74
- 'length ' => 2 ,
75
76
'tokenCodes ' => [
76
77
T_STRING
77
78
],
78
79
],
79
80
'class attribute with param ' => [
80
81
'testMarker ' => '/* testAttributeWithParams */ ' ,
81
- 'length ' => 7 ,
82
82
'tokenCodes ' => [
83
83
T_STRING ,
84
84
T_OPEN_PARENTHESIS ,
@@ -90,7 +90,6 @@ public static function dataAttribute()
90
90
],
91
91
'class attribute with named param ' => [
92
92
'testMarker ' => '/* testAttributeWithNamedParam */ ' ,
93
- 'length ' => 10 ,
94
93
'tokenCodes ' => [
95
94
T_STRING ,
96
95
T_OPEN_PARENTHESIS ,
@@ -105,14 +104,12 @@ public static function dataAttribute()
105
104
],
106
105
'function attribute ' => [
107
106
'testMarker ' => '/* testAttributeOnFunction */ ' ,
108
- 'length ' => 2 ,
109
107
'tokenCodes ' => [
110
108
T_STRING
111
109
],
112
110
],
113
111
'function attribute with params ' => [
114
112
'testMarker ' => '/* testAttributeOnFunctionWithParams */ ' ,
115
- 'length ' => 17 ,
116
113
'tokenCodes ' => [
117
114
T_STRING ,
118
115
T_OPEN_PARENTHESIS ,
@@ -134,7 +131,6 @@ public static function dataAttribute()
134
131
],
135
132
'function attribute with arrow function as param ' => [
136
133
'testMarker ' => '/* testAttributeWithShortClosureParameter */ ' ,
137
- 'length ' => 17 ,
138
134
'tokenCodes ' => [
139
135
T_STRING ,
140
136
T_OPEN_PARENTHESIS ,
@@ -156,7 +152,6 @@ public static function dataAttribute()
156
152
],
157
153
'function attribute; multiple comma separated classes ' => [
158
154
'testMarker ' => '/* testAttributeGrouping */ ' ,
159
- 'length ' => 26 ,
160
155
'tokenCodes ' => [
161
156
T_STRING ,
162
157
T_COMMA ,
@@ -187,7 +182,6 @@ public static function dataAttribute()
187
182
],
188
183
'function attribute; multiple comma separated classes, one per line ' => [
189
184
'testMarker ' => '/* testAttributeMultiline */ ' ,
190
- 'length ' => 31 ,
191
185
'tokenCodes ' => [
192
186
T_WHITESPACE ,
193
187
T_WHITESPACE ,
@@ -223,7 +217,6 @@ public static function dataAttribute()
223
217
],
224
218
'function attribute; multiple comma separated classes, one per line, with comments ' => [
225
219
'testMarker ' => '/* testAttributeMultilineWithComment */ ' ,
226
- 'length ' => 34 ,
227
220
'tokenCodes ' => [
228
221
T_WHITESPACE ,
229
222
T_WHITESPACE ,
@@ -262,7 +255,6 @@ public static function dataAttribute()
262
255
],
263
256
'function attribute; using partially qualified and fully qualified class names ' => [
264
257
'testMarker ' => '/* testFqcnAttribute */ ' ,
265
- 'length ' => 8 ,
266
258
'tokenCodes ' => [
267
259
T_NAME_QUALIFIED ,
268
260
T_COMMA ,
@@ -330,7 +322,6 @@ public function testAttributeAndLineComment()
330
322
*
331
323
* @param string $testMarker The comment which prefaces the target token in the test file.
332
324
* @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.
334
325
* @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
335
326
*
336
327
* @dataProvider dataAttributeOnParameters
@@ -341,10 +332,13 @@ public function testAttributeAndLineComment()
341
332
*
342
333
* @return void
343
334
*/
344
- public function testAttributeOnParameters ($ testMarker , $ position , $ length , array $ tokenCodes )
335
+ public function testAttributeOnParameters ($ testMarker , $ position , array $ tokenCodes )
345
336
{
346
337
$ tokens = $ this ->phpcsFile ->getTokens ();
347
338
339
+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
340
+ $ length = (count ($ tokenCodes ) + 1 );
341
+
348
342
$ function = $ this ->getTargetToken ($ testMarker , T_FUNCTION );
349
343
$ attribute = ($ function + $ position );
350
344
@@ -389,15 +383,13 @@ public static function dataAttributeOnParameters()
389
383
'parameter attribute; single, inline ' => [
390
384
'testMarker ' => '/* testSingleAttributeOnParameter */ ' ,
391
385
'position ' => 4 ,
392
- 'length ' => 2 ,
393
386
'tokenCodes ' => [
394
387
T_STRING
395
388
],
396
389
],
397
390
'parameter attribute; multiple comma separated, inline ' => [
398
391
'testMarker ' => '/* testMultipleAttributesOnParameter */ ' ,
399
392
'position ' => 4 ,
400
- 'length ' => 10 ,
401
393
'tokenCodes ' => [
402
394
T_STRING ,
403
395
T_COMMA ,
@@ -413,7 +405,6 @@ public static function dataAttributeOnParameters()
413
405
'parameter attribute; single, multiline ' => [
414
406
'testMarker ' => '/* testMultilineAttributesOnParameter */ ' ,
415
407
'position ' => 4 ,
416
- 'length ' => 13 ,
417
408
'tokenCodes ' => [
418
409
T_WHITESPACE ,
419
410
T_WHITESPACE ,
@@ -438,7 +429,6 @@ public static function dataAttributeOnParameters()
438
429
* Test that an attribute containing text which looks like a PHP close tag is tokenized correctly.
439
430
*
440
431
* @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.
442
432
* @param array<array<string>> $expectedTokensAttribute The codes of tokens inside the attributes.
443
433
* @param array<int|string> $expectedTokensAfter The codes of tokens after the attributes.
444
434
*
@@ -448,10 +438,13 @@ public static function dataAttributeOnParameters()
448
438
*
449
439
* @return void
450
440
*/
451
- public function testAttributeContainingTextLookingLikeCloseTag ($ testMarker , $ length , array $ expectedTokensAttribute , array $ expectedTokensAfter )
441
+ public function testAttributeContainingTextLookingLikeCloseTag ($ testMarker , array $ expectedTokensAttribute , array $ expectedTokensAfter )
452
442
{
453
443
$ tokens = $ this ->phpcsFile ->getTokens ();
454
444
445
+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
446
+ $ length = count ($ expectedTokensAttribute );
447
+
455
448
$ attribute = $ this ->getTargetToken ($ testMarker , T_ATTRIBUTE );
456
449
457
450
$ this ->assertSame ('T_ATTRIBUTE ' , $ tokens [$ attribute ]['type ' ]);
@@ -496,7 +489,6 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
496
489
return [
497
490
'function attribute; string param with "?>" ' => [
498
491
'testMarker ' => '/* testAttributeContainingTextLookingLikeCloseTag */ ' ,
499
- 'length ' => 5 ,
500
492
'expectedTokensAttribute ' => [
501
493
[
502
494
'T_STRING ' ,
@@ -533,7 +525,6 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
533
525
],
534
526
'function attribute; string param with "?>"; multiline ' => [
535
527
'testMarker ' => '/* testAttributeContainingMultilineTextLookingLikeCloseTag */ ' ,
536
- 'length ' => 8 ,
537
528
'expectedTokensAttribute ' => [
538
529
[
539
530
'T_STRING ' ,
@@ -643,11 +634,14 @@ public function testNestedAttributes()
643
634
T_CLOSE_PARENTHESIS ,
644
635
];
645
636
637
+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
638
+ $ outerAttributeLength = (count ($ tokenCodes ) + 1 );
639
+
646
640
$ attribute = $ this ->getTargetToken ('/* testNestedAttributes */ ' , T_ATTRIBUTE );
647
641
$ this ->assertArrayHasKey ('attribute_closer ' , $ tokens [$ attribute ]);
648
642
649
643
$ closer = $ tokens [$ attribute ]['attribute_closer ' ];
650
- $ this ->assertSame (($ attribute + 22 ), $ closer );
644
+ $ this ->assertSame (($ attribute + $ outerAttributeLength ), $ closer );
651
645
652
646
$ this ->assertSame (T_ATTRIBUTE_END , $ tokens [$ closer ]['code ' ]);
653
647
@@ -656,37 +650,39 @@ public function testNestedAttributes()
656
650
657
651
$ this ->assertArrayNotHasKey ('nested_attributes ' , $ tokens [$ attribute ]);
658
652
$ 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 ' ]);
660
654
661
- $ test = function (array $ tokens , $ length , $ nestedMap ) use ($ attribute ) {
655
+ $ test = function (array $ tokens , $ outerAttributeLength , $ nestedMap ) use ($ attribute ) {
662
656
foreach ($ tokens as $ token ) {
663
657
$ this ->assertArrayHasKey ('attribute_closer ' , $ token );
664
- $ this ->assertSame (($ attribute + $ length ), $ token ['attribute_closer ' ]);
658
+ $ this ->assertSame (($ attribute + $ outerAttributeLength ), $ token ['attribute_closer ' ]);
665
659
$ this ->assertSame ($ nestedMap , $ token ['nested_attributes ' ]);
666
660
}
667
661
};
668
662
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
-
672
663
// 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
+
673
669
$ test (
674
670
array_slice ($ tokens , ($ attribute + 7 ), 4 ),
675
- 6 + 5 ,
671
+ $ innerAttributeLength ,
676
672
[
677
- $ attribute => $ attribute + 22 ,
673
+ $ attribute => $ attribute + $ outerAttributeLength ,
678
674
$ attribute + 6 => $ attribute + 11 ,
679
675
]
680
676
);
681
677
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 ]);
684
680
685
681
$ map = array_map (
686
682
static function ($ token ) {
687
683
return $ token ['code ' ];
688
684
},
689
- array_slice ($ tokens , ($ attribute + 1 ), 21 )
685
+ array_slice ($ tokens , ($ attribute + 1 ), ( $ outerAttributeLength - 1 ) )
690
686
);
691
687
692
688
$ this ->assertSame ($ tokenCodes , $ map );
0 commit comments