@@ -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 ' => 13 ,
266
258
'tokenCodes ' => [
267
259
T_STRING ,
268
260
T_NS_SEPARATOR ,
@@ -335,7 +327,6 @@ public function testAttributeAndLineComment()
335
327
*
336
328
* @param string $testMarker The comment which prefaces the target token in the test file.
337
329
* @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.
339
330
* @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
340
331
*
341
332
* @dataProvider dataAttributeOnParameters
@@ -346,10 +337,13 @@ public function testAttributeAndLineComment()
346
337
*
347
338
* @return void
348
339
*/
349
- public function testAttributeOnParameters ($ testMarker , $ position , $ length , array $ tokenCodes )
340
+ public function testAttributeOnParameters ($ testMarker , $ position , array $ tokenCodes )
350
341
{
351
342
$ tokens = $ this ->phpcsFile ->getTokens ();
352
343
344
+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
345
+ $ length = (count ($ tokenCodes ) + 1 );
346
+
353
347
$ function = $ this ->getTargetToken ($ testMarker , T_FUNCTION );
354
348
$ attribute = ($ function + $ position );
355
349
@@ -394,15 +388,13 @@ public static function dataAttributeOnParameters()
394
388
'parameter attribute; single, inline ' => [
395
389
'testMarker ' => '/* testSingleAttributeOnParameter */ ' ,
396
390
'position ' => 4 ,
397
- 'length ' => 2 ,
398
391
'tokenCodes ' => [
399
392
T_STRING
400
393
],
401
394
],
402
395
'parameter attribute; multiple comma separated, inline ' => [
403
396
'testMarker ' => '/* testMultipleAttributesOnParameter */ ' ,
404
397
'position ' => 4 ,
405
- 'length ' => 10 ,
406
398
'tokenCodes ' => [
407
399
T_STRING ,
408
400
T_COMMA ,
@@ -418,7 +410,6 @@ public static function dataAttributeOnParameters()
418
410
'parameter attribute; single, multiline ' => [
419
411
'testMarker ' => '/* testMultilineAttributesOnParameter */ ' ,
420
412
'position ' => 4 ,
421
- 'length ' => 13 ,
422
413
'tokenCodes ' => [
423
414
T_WHITESPACE ,
424
415
T_WHITESPACE ,
@@ -443,7 +434,6 @@ public static function dataAttributeOnParameters()
443
434
* Test that an attribute containing text which looks like a PHP close tag is tokenized correctly.
444
435
*
445
436
* @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.
447
437
* @param array<array<string>> $expectedTokensAttribute The codes of tokens inside the attributes.
448
438
* @param array<int|string> $expectedTokensAfter The codes of tokens after the attributes.
449
439
*
@@ -453,10 +443,13 @@ public static function dataAttributeOnParameters()
453
443
*
454
444
* @return void
455
445
*/
456
- public function testAttributeContainingTextLookingLikeCloseTag ($ testMarker , $ length , array $ expectedTokensAttribute , array $ expectedTokensAfter )
446
+ public function testAttributeContainingTextLookingLikeCloseTag ($ testMarker , array $ expectedTokensAttribute , array $ expectedTokensAfter )
457
447
{
458
448
$ tokens = $ this ->phpcsFile ->getTokens ();
459
449
450
+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
451
+ $ length = count ($ expectedTokensAttribute );
452
+
460
453
$ attribute = $ this ->getTargetToken ($ testMarker , T_ATTRIBUTE );
461
454
462
455
$ this ->assertSame ('T_ATTRIBUTE ' , $ tokens [$ attribute ]['type ' ]);
@@ -501,7 +494,6 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
501
494
return [
502
495
'function attribute; string param with "?>" ' => [
503
496
'testMarker ' => '/* testAttributeContainingTextLookingLikeCloseTag */ ' ,
504
- 'length ' => 5 ,
505
497
'expectedTokensAttribute ' => [
506
498
[
507
499
'T_STRING ' ,
@@ -538,7 +530,6 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
538
530
],
539
531
'function attribute; string param with "?>"; multiline ' => [
540
532
'testMarker ' => '/* testAttributeContainingMultilineTextLookingLikeCloseTag */ ' ,
541
- 'length ' => 8 ,
542
533
'expectedTokensAttribute ' => [
543
534
[
544
535
'T_STRING ' ,
@@ -650,11 +641,14 @@ public function testNestedAttributes()
650
641
T_CLOSE_PARENTHESIS ,
651
642
];
652
643
644
+ // Calculate the number of tokens between opener and closer (excluding the opener, including the closer).
645
+ $ outerAttributeLength = (count ($ tokenCodes ) + 1 );
646
+
653
647
$ attribute = $ this ->getTargetToken ('/* testNestedAttributes */ ' , T_ATTRIBUTE );
654
648
$ this ->assertArrayHasKey ('attribute_closer ' , $ tokens [$ attribute ]);
655
649
656
650
$ closer = $ tokens [$ attribute ]['attribute_closer ' ];
657
- $ this ->assertSame (($ attribute + 24 ), $ closer );
651
+ $ this ->assertSame (($ attribute + $ outerAttributeLength ), $ closer );
658
652
659
653
$ this ->assertSame (T_ATTRIBUTE_END , $ tokens [$ closer ]['code ' ]);
660
654
@@ -663,37 +657,39 @@ public function testNestedAttributes()
663
657
664
658
$ this ->assertArrayNotHasKey ('nested_attributes ' , $ tokens [$ attribute ]);
665
659
$ 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 ' ]);
667
661
668
- $ test = function (array $ tokens , $ length , $ nestedMap ) use ($ attribute ) {
662
+ $ test = function (array $ tokens , $ outerAttributeLength , $ nestedMap ) use ($ attribute ) {
669
663
foreach ($ tokens as $ token ) {
670
664
$ this ->assertArrayHasKey ('attribute_closer ' , $ token );
671
- $ this ->assertSame (($ attribute + $ length ), $ token ['attribute_closer ' ]);
665
+ $ this ->assertSame (($ attribute + $ outerAttributeLength ), $ token ['attribute_closer ' ]);
672
666
$ this ->assertSame ($ nestedMap , $ token ['nested_attributes ' ]);
673
667
}
674
668
};
675
669
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
-
679
670
// 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
+
680
676
$ test (
681
677
array_slice ($ tokens , ($ attribute + 9 ), 4 ),
682
- 8 + 5 ,
678
+ $ innerAttributeLength ,
683
679
[
684
- $ attribute => $ attribute + 24 ,
680
+ $ attribute => $ attribute + $ outerAttributeLength ,
685
681
$ attribute + 8 => $ attribute + 13 ,
686
682
]
687
683
);
688
684
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 ]);
691
687
692
688
$ map = array_map (
693
689
static function ($ token ) {
694
690
return $ token ['code ' ];
695
691
},
696
- array_slice ($ tokens , ($ attribute + 1 ), 23 )
692
+ array_slice ($ tokens , ($ attribute + 1 ), ( $ outerAttributeLength - 1 ) )
697
693
);
698
694
699
695
$ this ->assertSame ($ tokenCodes , $ map );
0 commit comments