@@ -22,19 +22,20 @@ class DefaultKeywordTest extends AbstractMethodUnitTest
22
22
* Note: Cases and default structures within a match structure do *NOT* get case/default scope
23
23
* conditions, in contrast to case and default structures in switch control structures.
24
24
*
25
- * @param string $testMarker The comment prefacing the target token.
25
+ * @param string $testMarker The comment prefacing the target token.
26
+ * @param string $testContent The token content to look for.
26
27
*
27
28
* @dataProvider dataMatchDefault
28
29
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
29
30
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
30
31
*
31
32
* @return void
32
33
*/
33
- public function testMatchDefault ($ testMarker )
34
+ public function testMatchDefault ($ testMarker, $ testContent = ' default ' )
34
35
{
35
36
$ tokens = self ::$ phpcsFile ->getTokens ();
36
37
37
- $ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT ] );
38
+ $ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT , T_STRING ], $ testContent );
38
39
$ tokenArray = $ tokens [$ token ];
39
40
40
41
$ this ->assertSame (T_MATCH_DEFAULT , $ tokenArray ['code ' ], 'Token tokenized as ' .$ tokenArray ['type ' ].', not T_MATCH_DEFAULT (code) ' );
@@ -57,11 +58,36 @@ public function testMatchDefault($testMarker)
57
58
public function dataMatchDefault ()
58
59
{
59
60
return [
60
- 'simple_match_default ' => ['/* testSimpleMatchDefault */ ' ],
61
- 'match_default_in_switch_case_1 ' => ['/* testMatchDefaultNestedInSwitchCase1 */ ' ],
62
- 'match_default_in_switch_case_2 ' => ['/* testMatchDefaultNestedInSwitchCase2 */ ' ],
63
- 'match_default_in_switch_default ' => ['/* testMatchDefaultNestedInSwitchDefault */ ' ],
64
- 'match_default_containing_switch ' => ['/* testMatchDefault */ ' ],
61
+ 'simple_match_default ' => ['/* testSimpleMatchDefault */ ' ],
62
+ 'match_default_in_switch_case_1 ' => ['/* testMatchDefaultNestedInSwitchCase1 */ ' ],
63
+ 'match_default_in_switch_case_2 ' => ['/* testMatchDefaultNestedInSwitchCase2 */ ' ],
64
+ 'match_default_in_switch_default ' => ['/* testMatchDefaultNestedInSwitchDefault */ ' ],
65
+ 'match_default_containing_switch ' => ['/* testMatchDefault */ ' ],
66
+
67
+ 'match_default_with_nested_long_array_and_default_key ' => [
68
+ '/* testMatchDefaultWithNestedLongArrayWithClassConstantKey */ ' ,
69
+ 'DEFAULT ' ,
70
+ ],
71
+ 'match_default_with_nested_long_array_and_default_key_2 ' => [
72
+ '/* testMatchDefaultWithNestedLongArrayWithClassConstantKeyLevelDown */ ' ,
73
+ 'DEFAULT ' ,
74
+ ],
75
+ 'match_default_with_nested_short_array_and_default_key ' => [
76
+ '/* testMatchDefaultWithNestedShortArrayWithClassConstantKey */ ' ,
77
+ 'DEFAULT ' ,
78
+ ],
79
+ 'match_default_with_nested_short_array_and_default_key_2 ' => [
80
+ '/* testMatchDefaultWithNestedShortArrayWithClassConstantKeyLevelDown */ ' ,
81
+ 'DEFAULT ' ,
82
+ ],
83
+ 'match_default_in_long_array ' => [
84
+ '/* testMatchDefaultNestedInLongArray */ ' ,
85
+ 'DEFAULT ' ,
86
+ ],
87
+ 'match_default_in_short_array ' => [
88
+ '/* testMatchDefaultNestedInShortArray */ ' ,
89
+ 'DEFAULT ' ,
90
+ ],
65
91
];
66
92
67
93
}//end dataMatchDefault()
@@ -78,17 +104,18 @@ public function dataMatchDefault()
78
104
* @param int $openerOffset The expected offset of the scope opener in relation to the testMarker.
79
105
* @param int $closerOffset The expected offset of the scope closer in relation to the testMarker.
80
106
* @param int|null $conditionStop The expected offset at which tokens stop having T_DEFAULT as a scope condition.
107
+ * @param string $testContent The token content to look for.
81
108
*
82
109
* @dataProvider dataSwitchDefault
83
110
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
84
111
*
85
112
* @return void
86
113
*/
87
- public function testSwitchDefault ($ testMarker , $ openerOffset , $ closerOffset , $ conditionStop =null )
114
+ public function testSwitchDefault ($ testMarker , $ openerOffset , $ closerOffset , $ conditionStop =null , $ testContent = ' default ' )
88
115
{
89
116
$ tokens = self ::$ phpcsFile ->getTokens ();
90
117
91
- $ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT ] );
118
+ $ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT , T_STRING ], $ testContent );
92
119
$ tokenArray = $ tokens [$ token ];
93
120
$ expectedScopeOpener = ($ token + $ openerOffset );
94
121
$ expectedScopeCloser = ($ token + $ closerOffset );
@@ -182,4 +209,67 @@ public function dataSwitchDefault()
182
209
}//end dataSwitchDefault()
183
210
184
211
212
+ /**
213
+ * Verify that the retokenization of `T_DEFAULT` tokens in match constructs, doesn't negatively
214
+ * impact the tokenization of `T_STRING` tokens with the contents 'default' which aren't in
215
+ * actual fact the default keyword.
216
+ *
217
+ * @param string $testMarker The comment prefacing the target token.
218
+ * @param string $testContent The token content to look for.
219
+ *
220
+ * @dataProvider dataNotDefaultKeyword
221
+ * @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
222
+ *
223
+ * @return void
224
+ */
225
+ public function testNotDefaultKeyword ($ testMarker , $ testContent ='DEFAULT ' )
226
+ {
227
+ $ tokens = self ::$ phpcsFile ->getTokens ();
228
+
229
+ $ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT , T_STRING ], $ testContent );
230
+ $ tokenArray = $ tokens [$ token ];
231
+
232
+ $ this ->assertSame (T_STRING , $ tokenArray ['code ' ], 'Token tokenized as ' .$ tokenArray ['type ' ].', not T_STRING (code) ' );
233
+ $ this ->assertSame ('T_STRING ' , $ tokenArray ['type ' ], 'Token tokenized as ' .$ tokenArray ['type ' ].', not T_STRING (type) ' );
234
+
235
+ $ this ->assertArrayNotHasKey ('scope_condition ' , $ tokenArray , 'Scope condition is set ' );
236
+ $ this ->assertArrayNotHasKey ('scope_opener ' , $ tokenArray , 'Scope opener is set ' );
237
+ $ this ->assertArrayNotHasKey ('scope_closer ' , $ tokenArray , 'Scope closer is set ' );
238
+
239
+ }//end testNotDefaultKeyword()
240
+
241
+
242
+ /**
243
+ * Data provider.
244
+ *
245
+ * @see testNotDefaultKeyword()
246
+ *
247
+ * @return array
248
+ */
249
+ public function dataNotDefaultKeyword ()
250
+ {
251
+ return [
252
+ 'class-constant-as-short-array-key ' => ['/* testClassConstantAsShortArrayKey */ ' ],
253
+ 'class-property-as-short-array-key ' => ['/* testClassPropertyAsShortArrayKey */ ' ],
254
+ 'namespaced-constant-as-short-array-key ' => ['/* testNamespacedConstantAsShortArrayKey */ ' ],
255
+ 'fqn-global-constant-as-short-array-key ' => ['/* testFQNGlobalConstantAsShortArrayKey */ ' ],
256
+ 'class-constant-as-long-array-key ' => ['/* testClassConstantAsLongArrayKey */ ' ],
257
+ 'class-constant-as-yield-key ' => ['/* testClassConstantAsYieldKey */ ' ],
258
+
259
+ 'class-constant-as-long-array-key-nested-in-match ' => ['/* testClassConstantAsLongArrayKeyNestedInMatch */ ' ],
260
+ 'class-constant-as-long-array-key-nested-in-match-2 ' => ['/* testClassConstantAsLongArrayKeyNestedInMatchLevelDown */ ' ],
261
+ 'class-constant-as-short-array-key-nested-in-match ' => ['/* testClassConstantAsShortArrayKeyNestedInMatch */ ' ],
262
+ 'class-constant-as-short-array-key-nested-in-match-2 ' => ['/* testClassConstantAsShortArrayKeyNestedInMatchLevelDown */ ' ],
263
+ 'class-constant-as-long-array-key-with-nested-match ' => ['/* testClassConstantAsLongArrayKeyWithNestedMatch */ ' ],
264
+ 'class-constant-as-short-array-key-with-nested-match ' => ['/* testClassConstantAsShortArrayKeyWithNestedMatch */ ' ],
265
+
266
+ 'class-constant-in-switch-case ' => ['/* testClassConstantInSwitchCase */ ' ],
267
+ 'class-property-in-switch-case ' => ['/* testClassPropertyInSwitchCase */ ' ],
268
+ 'namespaced-constant-in-switch-case ' => ['/* testNamespacedConstantInSwitchCase */ ' ],
269
+ 'namespace-relative-constant-in-switch-case ' => ['/* testNamespaceRelativeConstantInSwitchCase */ ' ],
270
+ ];
271
+
272
+ }//end dataNotDefaultKeyword()
273
+
274
+
185
275
}//end class
0 commit comments