@@ -22,19 +22,20 @@ class DefaultKeywordTest extends AbstractMethodUnitTest
2222 * Note: Cases and default structures within a match structure do *NOT* get case/default scope
2323 * conditions, in contrast to case and default structures in switch control structures.
2424 *
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.
2627 *
2728 * @dataProvider dataMatchDefault
2829 * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
2930 * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
3031 *
3132 * @return void
3233 */
33- public function testMatchDefault ($ testMarker )
34+ public function testMatchDefault ($ testMarker, $ testContent = ' default ' )
3435 {
3536 $ tokens = self ::$ phpcsFile ->getTokens ();
3637
37- $ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT ] );
38+ $ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT , T_STRING ], $ testContent );
3839 $ tokenArray = $ tokens [$ token ];
3940
4041 $ 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)
5758 public function dataMatchDefault ()
5859 {
5960 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+ ],
6591 ];
6692
6793 }//end dataMatchDefault()
@@ -78,17 +104,18 @@ public function dataMatchDefault()
78104 * @param int $openerOffset The expected offset of the scope opener in relation to the testMarker.
79105 * @param int $closerOffset The expected offset of the scope closer in relation to the testMarker.
80106 * @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.
81108 *
82109 * @dataProvider dataSwitchDefault
83110 * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
84111 *
85112 * @return void
86113 */
87- public function testSwitchDefault ($ testMarker , $ openerOffset , $ closerOffset , $ conditionStop =null )
114+ public function testSwitchDefault ($ testMarker , $ openerOffset , $ closerOffset , $ conditionStop =null , $ testContent = ' default ' )
88115 {
89116 $ tokens = self ::$ phpcsFile ->getTokens ();
90117
91- $ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT ] );
118+ $ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT , T_STRING ], $ testContent );
92119 $ tokenArray = $ tokens [$ token ];
93120 $ expectedScopeOpener = ($ token + $ openerOffset );
94121 $ expectedScopeCloser = ($ token + $ closerOffset );
@@ -182,4 +209,67 @@ public function dataSwitchDefault()
182209 }//end dataSwitchDefault()
183210
184211
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+
185275}//end class
0 commit comments