Skip to content

Commit 452bf6b

Browse files
authored
Merge pull request #201 from PHPCSStandards/php-8.0/tests-set-up-to-work-with-identifier-tokens
PHP 8.0 | Tests: set up to work with PHP 8.0 identifier name tokens
2 parents ae73a76 + 12d512c commit 452bf6b

File tree

11 files changed

+127
-57
lines changed

11 files changed

+127
-57
lines changed

Tests/BackCompat/BCFile/FindEndOfStatementTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ public function testUseGroup()
163163
$start = $this->getTargetToken('/* testUseGroup */', T_USE);
164164
$found = BCFile::findEndOfStatement(self::$phpcsFile, $start);
165165

166-
$this->assertSame(($start + 23), $found);
166+
$expected = parent::usesPhp8NameTokens() ? ($start + 21) : ($start + 23);
167+
168+
$this->assertSame($expected, $found);
167169
}
168170

169171
/**

Tests/BackCompat/BCFile/GetMemberPropertiesTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public function testGetMemberProperties($identifier, $expected)
8282
*/
8383
public function dataGetMemberProperties()
8484
{
85+
$php8Names = parent::usesPhp8NameTokens();
86+
8587
return [
8688
[
8789
'/* testVar */',
@@ -498,7 +500,7 @@ public function dataGetMemberProperties()
498500
'scope_specified' => true,
499501
'is_static' => false,
500502
'type' => '\MyNamespace\MyClass',
501-
'type_token' => -5, // Offset from the T_VARIABLE token.
503+
'type_token' => ($php8Names === true) ? -2 : -5, // Offset from the T_VARIABLE token.
502504
'type_end_token' => -2, // Offset from the T_VARIABLE token.
503505
'nullable_type' => false,
504506
],
@@ -522,7 +524,7 @@ public function dataGetMemberProperties()
522524
'scope_specified' => true,
523525
'is_static' => false,
524526
'type' => '?Folder\ClassName',
525-
'type_token' => -4, // Offset from the T_VARIABLE token.
527+
'type_token' => ($php8Names === true) ? -2 : -4, // Offset from the T_VARIABLE token.
526528
'type_end_token' => -2, // Offset from the T_VARIABLE token.
527529
'nullable_type' => true,
528530
],
@@ -534,7 +536,7 @@ public function dataGetMemberProperties()
534536
'scope_specified' => true,
535537
'is_static' => false,
536538
'type' => '\MyNamespace\MyClass\Foo',
537-
'type_token' => -18, // Offset from the T_VARIABLE token.
539+
'type_token' => ($php8Names === true) ? -15 : -18, // Offset from the T_VARIABLE token.
538540
'type_end_token' => -2, // Offset from the T_VARIABLE token.
539541
'nullable_type' => false,
540542
],

Tests/BackCompat/BCFile/GetMethodParametersTest.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ public function testSelfTypeHint()
277277
*/
278278
public function testNullableTypeHint()
279279
{
280+
$php8Names = parent::usesPhp8NameTokens();
281+
280282
$expected = [];
281283
$expected[0] = [
282284
'token' => 7, // Offset from the T_FUNCTION token.
@@ -294,7 +296,7 @@ public function testNullableTypeHint()
294296
];
295297

296298
$expected[1] = [
297-
'token' => 14, // Offset from the T_FUNCTION token.
299+
'token' => ($php8Names === true) ? 13 : 14, // Offset from the T_FUNCTION token.
298300
'name' => '$var2',
299301
'content' => '?\bar $var2',
300302
'pass_by_reference' => false,
@@ -303,7 +305,7 @@ public function testNullableTypeHint()
303305
'variadic_token' => false,
304306
'type_hint' => '?\bar',
305307
'type_hint_token' => 11, // Offset from the T_FUNCTION token.
306-
'type_hint_end_token' => 12, // Offset from the T_FUNCTION token.
308+
'type_hint_end_token' => ($php8Names === true) ? 11 : 12, // Offset from the T_FUNCTION token.
307309
'nullable_type' => true,
308310
'comma_token' => false,
309311
];
@@ -743,9 +745,11 @@ public function testVariadicFunctionClassType()
743745
*/
744746
public function testNameSpacedTypeDeclaration()
745747
{
748+
$php8Names = parent::usesPhp8NameTokens();
749+
746750
$expected = [];
747751
$expected[0] = [
748-
'token' => 12, // Offset from the T_FUNCTION token.
752+
'token' => ($php8Names === true) ? 7 : 12, // Offset from the T_FUNCTION token.
749753
'name' => '$a',
750754
'content' => '\Package\Sub\ClassName $a',
751755
'pass_by_reference' => false,
@@ -754,21 +758,21 @@ public function testNameSpacedTypeDeclaration()
754758
'variadic_token' => false,
755759
'type_hint' => '\Package\Sub\ClassName',
756760
'type_hint_token' => 5, // Offset from the T_FUNCTION token.
757-
'type_hint_end_token' => 10, // Offset from the T_FUNCTION token.
761+
'type_hint_end_token' => ($php8Names === true) ? 5 : 10, // Offset from the T_FUNCTION token.
758762
'nullable_type' => false,
759-
'comma_token' => 13, // Offset from the T_FUNCTION token.
763+
'comma_token' => ($php8Names === true) ? 8 : 13, // Offset from the T_FUNCTION token.
760764
];
761765
$expected[1] = [
762-
'token' => 20, // Offset from the T_FUNCTION token.
766+
'token' => ($php8Names === true) ? 13 : 20, // Offset from the T_FUNCTION token.
763767
'name' => '$b',
764768
'content' => '?Sub\AnotherClass $b',
765769
'pass_by_reference' => false,
766770
'reference_token' => false,
767771
'variable_length' => false,
768772
'variadic_token' => false,
769773
'type_hint' => '?Sub\AnotherClass',
770-
'type_hint_token' => 16, // Offset from the T_FUNCTION token.
771-
'type_hint_end_token' => 18, // Offset from the T_FUNCTION token.
774+
'type_hint_token' => ($php8Names === true) ? 11 : 16, // Offset from the T_FUNCTION token.
775+
'type_hint_end_token' => ($php8Names === true) ? 11 : 18, // Offset from the T_FUNCTION token.
772776
'nullable_type' => true,
773777
'comma_token' => false,
774778
];
@@ -1135,9 +1139,11 @@ public function testArrowFunctionWithAllTypes()
11351139
*/
11361140
public function testMessyDeclaration()
11371141
{
1142+
$php8Names = parent::usesPhp8NameTokens();
1143+
11381144
$expected = [];
11391145
$expected[0] = [
1140-
'token' => 25, // Offset from the T_FUNCTION token.
1146+
'token' => ($php8Names === true) ? 24 : 25, // Offset from the T_FUNCTION token.
11411147
'name' => '$a',
11421148
'content' => '// comment
11431149
?\MyNS /* comment */
@@ -1149,17 +1155,17 @@ public function testMessyDeclaration()
11491155
'variadic_token' => false,
11501156
'type_hint' => '?\MyNS\SubCat\MyClass',
11511157
'type_hint_token' => 9,
1152-
'type_hint_end_token' => 23,
1158+
'type_hint_end_token' => ($php8Names === true) ? 22 : 23,
11531159
'nullable_type' => true,
1154-
'comma_token' => 26, // Offset from the T_FUNCTION token.
1160+
'comma_token' => ($php8Names === true) ? 25 : 26, // Offset from the T_FUNCTION token.
11551161
];
11561162
$expected[1] = [
1157-
'token' => 29, // Offset from the T_FUNCTION token.
1163+
'token' => ($php8Names === true) ? 28 : 29, // Offset from the T_FUNCTION token.
11581164
'name' => '$b',
11591165
'content' => "\$b /* test */ = /* test */ 'default' /* test*/",
11601166
'default' => "'default' /* test*/",
1161-
'default_token' => 37, // Offset from the T_FUNCTION token.
1162-
'default_equal_token' => 33, // Offset from the T_FUNCTION token.
1167+
'default_token' => ($php8Names === true) ? 36 : 37, // Offset from the T_FUNCTION token.
1168+
'default_equal_token' => ($php8Names === true) ? 32 : 33, // Offset from the T_FUNCTION token.
11631169
'pass_by_reference' => false,
11641170
'reference_token' => false,
11651171
'variable_length' => false,
@@ -1168,22 +1174,22 @@ public function testMessyDeclaration()
11681174
'type_hint_token' => false,
11691175
'type_hint_end_token' => false,
11701176
'nullable_type' => false,
1171-
'comma_token' => 40, // Offset from the T_FUNCTION token.
1177+
'comma_token' => ($php8Names === true) ? 39 : 40, // Offset from the T_FUNCTION token.
11721178
];
11731179
$expected[2] = [
1174-
'token' => 62, // Offset from the T_FUNCTION token.
1180+
'token' => ($php8Names === true) ? 61 : 62, // Offset from the T_FUNCTION token.
11751181
'name' => '$c',
11761182
'content' => '// phpcs:ignore Stnd.Cat.Sniff -- For reasons.
11771183
? /*comment*/
11781184
bool // phpcs:disable Stnd.Cat.Sniff -- For reasons.
11791185
& /*test*/ ... /* phpcs:ignore */ $c',
11801186
'pass_by_reference' => true,
1181-
'reference_token' => 54, // Offset from the T_FUNCTION token.
1187+
'reference_token' => ($php8Names === true) ? 53 : 54, // Offset from the T_FUNCTION token.
11821188
'variable_length' => true,
1183-
'variadic_token' => 58, // Offset from the T_FUNCTION token.
1189+
'variadic_token' => ($php8Names === true) ? 57 : 58, // Offset from the T_FUNCTION token.
11841190
'type_hint' => '?bool',
1185-
'type_hint_token' => 50, // Offset from the T_FUNCTION token.
1186-
'type_hint_end_token' => 50, // Offset from the T_FUNCTION token.
1191+
'type_hint_token' => ($php8Names === true) ? 49 : 50, // Offset from the T_FUNCTION token.
1192+
'type_hint_end_token' => ($php8Names === true) ? 49 : 50, // Offset from the T_FUNCTION token.
11871193
'nullable_type' => true,
11881194
'comma_token' => false,
11891195
];

Tests/Utils/Conditions/GetConditionTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ public function testGetConditionMultipleTypes()
103103
*/
104104
public function testNonConditionalTokenGetFirstLast()
105105
{
106-
$stackPtr = $this->getTargetToken('/* testStartPoint */', \T_STRING);
106+
$targetType = \T_STRING;
107+
if (parent::usesPhp8NameTokens() === true) {
108+
$targetType = \T_NAME_QUALIFIED;
109+
}
110+
111+
$stackPtr = $this->getTargetToken('/* testStartPoint */', $targetType);
107112

108113
$result = Conditions::getFirstCondition(self::$phpcsFile, $stackPtr);
109114
$this->assertFalse($result, 'Failed asserting that getFirstCondition() on non conditional token returns false');

Tests/Utils/ControlStructures/GetCaughtExceptionsTest.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public function testGetCaughtExceptions($testMarker, $expected)
9595
*/
9696
public function dataGetCaughtExceptions()
9797
{
98+
$php8Names = parent::usesPhp8NameTokens();
99+
98100
return [
99101
'single-name-only' => [
100102
'target' => '/* testSingleCatchNameOnly */',
@@ -112,7 +114,7 @@ public function dataGetCaughtExceptions()
112114
[
113115
'type' => '\RuntimeException',
114116
'type_token' => 3,
115-
'type_end_token' => 4,
117+
'type_end_token' => ($php8Names === true) ? 3 : 4,
116118
],
117119
],
118120
],
@@ -122,7 +124,7 @@ public function dataGetCaughtExceptions()
122124
[
123125
'type' => 'MyNS\RuntimeException',
124126
'type_token' => 4,
125-
'type_end_token' => 6,
127+
'type_end_token' => ($php8Names === true) ? 4 : 6,
126128
],
127129
],
128130
],
@@ -132,7 +134,7 @@ public function dataGetCaughtExceptions()
132134
[
133135
'type' => '\MyNS\RuntimeException',
134136
'type_token' => 4,
135-
'type_end_token' => 7,
137+
'type_end_token' => ($php8Names === true) ? 4 : 7,
136138
],
137139
],
138140
],
@@ -142,7 +144,7 @@ public function dataGetCaughtExceptions()
142144
[
143145
'type' => 'My\NS\Sub\RuntimeException',
144146
'type_token' => 4,
145-
'type_end_token' => 15,
147+
'type_end_token' => ($php8Names === true) ? 13 : 15,
146148
],
147149
],
148150
],
@@ -152,7 +154,7 @@ public function dataGetCaughtExceptions()
152154
[
153155
'type' => 'namespace\RuntimeException',
154156
'type_token' => 4,
155-
'type_end_token' => 6,
157+
'type_end_token' => ($php8Names === true) ? 4 : 6,
156158
],
157159
],
158160
],
@@ -183,17 +185,17 @@ public function dataGetCaughtExceptions()
183185
[
184186
'type' => '\NS\RuntimeException',
185187
'type_token' => 3,
186-
'type_end_token' => 6,
188+
'type_end_token' => ($php8Names === true) ? 3 : 6,
187189
],
188190
[
189191
'type' => 'My\ParseErrorException',
190-
'type_token' => 10,
191-
'type_end_token' => 12,
192+
'type_token' => ($php8Names === true) ? 7 : 10,
193+
'type_end_token' => ($php8Names === true) ? 7 : 12,
192194
],
193195
[
194196
'type' => 'namespace\AnotherException',
195-
'type_token' => 16,
196-
'type_end_token' => 20,
197+
'type_token' => ($php8Names === true) ? 11 : 16,
198+
'type_end_token' => ($php8Names === true) ? 15 : 20,
197199
],
198200
],
199201
],

Tests/Utils/FunctionDeclarations/GetParametersDiffTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ public function testPHP8UnionTypesSimpleWithBitwiseOrInDefault()
119119
*/
120120
public function testPHP8UnionTypesTwoClasses()
121121
{
122+
$php8Names = parent::usesPhp8NameTokens();
123+
122124
$expected = [];
123125
$expected[0] = [
124-
'token' => 11, // Offset from the T_FUNCTION token.
126+
'token' => ($php8Names === true) ? 8 : 11, // Offset from the T_FUNCTION token.
125127
'name' => '$var',
126128
'content' => 'MyClassA|\Package\MyClassB $var',
127129
'pass_by_reference' => false,
@@ -130,7 +132,7 @@ public function testPHP8UnionTypesTwoClasses()
130132
'variadic_token' => false,
131133
'type_hint' => 'MyClassA|\Package\MyClassB',
132134
'type_hint_token' => 4, // Offset from the T_FUNCTION token.
133-
'type_hint_end_token' => 9, // Offset from the T_FUNCTION token.
135+
'type_hint_end_token' => ($php8Names === true) ? 6 : 9, // Offset from the T_FUNCTION token.
134136
'nullable_type' => false,
135137
'comma_token' => false,
136138
];
@@ -636,9 +638,11 @@ public function testPHP8ConstructorPropertyPromotionAbstractMethod()
636638
*/
637639
public function testNamespaceOperatorTypeHint()
638640
{
641+
$php8Names = parent::usesPhp8NameTokens();
642+
639643
$expected = [];
640644
$expected[0] = [
641-
'token' => 9, // Offset from the T_FUNCTION token.
645+
'token' => ($php8Names === true) ? 7 : 9, // Offset from the T_FUNCTION token.
642646
'name' => '$var1',
643647
'content' => '?namespace\Name $var1',
644648
'pass_by_reference' => false,
@@ -647,7 +651,7 @@ public function testNamespaceOperatorTypeHint()
647651
'variadic_token' => false,
648652
'type_hint' => '?namespace\Name',
649653
'type_hint_token' => 5, // Offset from the T_FUNCTION token.
650-
'type_hint_end_token' => 7, // Offset from the T_FUNCTION token.
654+
'type_hint_end_token' => ($php8Names === true) ? 5 : 7, // Offset from the T_FUNCTION token.
651655
'nullable_type' => true,
652656
'comma_token' => false,
653657
];

Tests/Utils/FunctionDeclarations/GetPropertiesDiffTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ public function testMessyPhpcsAnnotationsStaticClosure()
9494
*/
9595
public function testReturnTypeEndTokenIndex()
9696
{
97+
$php8Names = parent::usesPhp8NameTokens();
98+
9799
$expected = [
98100
'scope' => 'public',
99101
'scope_specified' => false,
100102
'return_type' => '?\MyNamespace\MyClass\Foo',
101103
'return_type_token' => 8, // Offset from the T_FUNCTION token.
102-
'return_type_end_token' => 20, // Offset from the T_FUNCTION token.
104+
'return_type_end_token' => ($php8Names === true) ? 17 : 20, // Offset from the T_FUNCTION token.
103105
'nullable_return_type' => true,
104106
'is_abstract' => false,
105107
'is_final' => false,
@@ -140,12 +142,14 @@ public function testPHP8UnionTypesSimple()
140142
*/
141143
public function testPHP8UnionTypesTwoClasses()
142144
{
145+
$php8Names = parent::usesPhp8NameTokens();
146+
143147
$expected = [
144148
'scope' => 'public',
145149
'scope_specified' => false,
146150
'return_type' => 'MyClassA|\Package\MyClassB',
147151
'return_type_token' => 6, // Offset from the T_FUNCTION token.
148-
'return_type_end_token' => 11, // Offset from the T_FUNCTION token.
152+
'return_type_end_token' => ($php8Names === true) ? 8 : 11, // Offset from the T_FUNCTION token.
149153
'nullable_return_type' => false,
150154
'is_abstract' => false,
151155
'is_final' => false,
@@ -374,12 +378,14 @@ public function testPHP8DuplicateTypeInUnionWhitespaceAndComment()
374378
*/
375379
public function testNamespaceOperatorTypeHint()
376380
{
381+
$php8Names = parent::usesPhp8NameTokens();
382+
377383
$expected = [
378384
'scope' => 'public',
379385
'scope_specified' => false,
380386
'return_type' => '?namespace\Name',
381387
'return_type_token' => 9, // Offset from the T_FUNCTION token.
382-
'return_type_end_token' => 11, // Offset from the T_FUNCTION token.
388+
'return_type_end_token' => ($php8Names === true) ? 9 : 11, // Offset from the T_FUNCTION token.
383389
'nullable_return_type' => true,
384390
'is_abstract' => false,
385391
'is_final' => false,

0 commit comments

Comments
 (0)