1414final class RecurseScopeMapDefaultKeywordConditionsTest extends AbstractTokenizerTestCase
1515{
1616
17+ /**
18+ * Condition stop tokens when `default` is used with curlies.
19+ *
20+ * @var array<int>
21+ */
22+ protected $ conditionStopTokens = [
23+ T_BREAK ,
24+ T_CONTINUE ,
25+ T_EXIT ,
26+ T_GOTO ,
27+ T_RETURN ,
28+ T_THROW ,
29+ ];
30+
1731
1832 /**
1933 * Test that match "default" tokens does not get scope indexes.
@@ -123,30 +137,30 @@ public static function dataMatchDefault()
123137 * Note: Cases and default structures within a switch control structure *do* get case/default scope
124138 * conditions.
125139 *
126- * @param string $testMarker The comment prefacing the target token.
127- * @param int $openerOffset The expected offset of the scope opener in relation to the testMarker .
128- * @param int $closerOffset The expected offset of the scope closer in relation to the testMarker .
129- * @param int |null $conditionStop The expected offset in relation to the testMarker, at which tokens stop
130- * having T_DEFAULT as a scope condition.
131- * @param string $testContent The token content to look for.
132- * @param bool $sharedScopeCloser Whether to skip checking for the `scope_condition` of the
133- * scope closer. Needed when the default and switch
134- * structures share a scope closer. See
135- * https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/810.
140+ * @param string $testMarker The comment prefacing the target token.
141+ * @param string $openerMarker The comment prefacing the scope opener token .
142+ * @param string $closerMarker The comment prefacing the scope closer token .
143+ * @param string |null $conditionStopMarker The expected offset in relation to the testMarker, after which tokens stop
144+ * having T_DEFAULT as a scope condition.
145+ * @param string $testContent The token content to look for.
146+ * @param bool $sharedScopeCloser Whether to skip checking for the `scope_condition` of the
147+ * scope closer. Needed when the default and switch
148+ * structures share a scope closer. See
149+ * https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/810.
136150 *
137151 * @dataProvider dataSwitchDefault
138152 * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
139153 *
140154 * @return void
141155 */
142- public function testSwitchDefault ($ testMarker , $ openerOffset , $ closerOffset , $ conditionStop =null , $ testContent ='default ' , $ sharedScopeCloser =false )
156+ public function testSwitchDefault ($ testMarker , $ openerMarker , $ closerMarker , $ conditionStopMarker =null , $ testContent ='default ' , $ sharedScopeCloser =false )
143157 {
144158 $ tokens = $ this ->phpcsFile ->getTokens ();
145159
146160 $ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT , T_STRING ], $ testContent );
147161 $ tokenArray = $ tokens [$ token ];
148- $ expectedScopeOpener = ( $ token + $ openerOffset );
149- $ expectedScopeCloser = ( $ token + $ closerOffset );
162+ $ expectedScopeOpener = $ this -> getTargetToken ( $ openerMarker , [ T_COLON , T_OPEN_CURLY_BRACKET , T_SEMICOLON ] );
163+ $ expectedScopeCloser = $ this -> getTargetToken ( $ closerMarker , [ T_BREAK , T_CLOSE_CURLY_BRACKET , T_RETURN , T_ENDSWITCH ] );
150164
151165 // Make sure we're looking at the right token.
152166 $ this ->assertSame (
@@ -190,32 +204,32 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
190204 $ this ->assertArrayHasKey (
191205 'scope_condition ' ,
192206 $ tokens [$ opener ],
193- sprintf ('Opener scope condition is not set. Marker: %s. ' , $ testMarker )
207+ sprintf ('Opener scope condition is not set. Marker: %s. ' , $ openerMarker )
194208 );
195209 $ this ->assertArrayHasKey (
196210 'scope_opener ' ,
197211 $ tokens [$ opener ],
198- sprintf ('Opener scope opener is not set. Marker: %s. ' , $ testMarker )
212+ sprintf ('Opener scope opener is not set. Marker: %s. ' , $ openerMarker )
199213 );
200214 $ this ->assertArrayHasKey (
201215 'scope_closer ' ,
202216 $ tokens [$ opener ],
203- sprintf ('Opener scope closer is not set. Marker: %s. ' , $ testMarker )
217+ sprintf ('Opener scope closer is not set. Marker: %s. ' , $ openerMarker )
204218 );
205219 $ this ->assertSame (
206220 $ token ,
207221 $ tokens [$ opener ]['scope_condition ' ],
208- sprintf ('Opener scope condition is not the T_DEFAULT token. Marker: %s. ' , $ testMarker )
222+ sprintf ('Opener scope condition is not the T_DEFAULT token. Marker: %s. ' , $ openerMarker )
209223 );
210224 $ this ->assertSame (
211225 $ expectedScopeOpener ,
212226 $ tokens [$ opener ]['scope_opener ' ],
213- sprintf ('T_DEFAULT opener scope opener token incorrect. Marker: %s. ' , $ testMarker )
227+ sprintf ('T_DEFAULT opener scope opener token incorrect. Marker: %s. ' , $ openerMarker )
214228 );
215229 $ this ->assertSame (
216230 $ expectedScopeCloser ,
217231 $ tokens [$ opener ]['scope_closer ' ],
218- sprintf ('T_DEFAULT opener scope closer token incorrect. Marker: %s. ' , $ testMarker )
232+ sprintf ('T_DEFAULT opener scope closer token incorrect. Marker: %s. ' , $ openerMarker )
219233 );
220234
221235 $ closer = $ expectedScopeCloser ;
@@ -225,39 +239,39 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
225239 $ this ->assertArrayHasKey (
226240 'scope_condition ' ,
227241 $ tokens [$ closer ],
228- sprintf ('Closer scope condition is not set. Marker: %s. ' , $ testMarker )
242+ sprintf ('Closer scope condition is not set. Marker: %s. ' , $ closerMarker )
229243 );
230244 $ this ->assertArrayHasKey (
231245 'scope_opener ' ,
232246 $ tokens [$ closer ],
233- sprintf ('Closer scope opener is not set. Marker: %s. ' , $ testMarker )
247+ sprintf ('Closer scope opener is not set. Marker: %s. ' , $ closerMarker )
234248 );
235249 $ this ->assertArrayHasKey (
236250 'scope_closer ' ,
237251 $ tokens [$ closer ],
238- sprintf ('Closer scope closer is not set. Marker: %s. ' , $ testMarker )
252+ sprintf ('Closer scope closer is not set. Marker: %s. ' , $ closerMarker )
239253 );
240254 $ this ->assertSame (
241255 $ token ,
242256 $ tokens [$ closer ]['scope_condition ' ],
243- sprintf ('Closer scope condition is not the T_DEFAULT token. Marker: %s. ' , $ testMarker )
257+ sprintf ('Closer scope condition is not the T_DEFAULT token. Marker: %s. ' , $ closerMarker )
244258 );
245259 $ this ->assertSame (
246260 $ expectedScopeOpener ,
247261 $ tokens [$ closer ]['scope_opener ' ],
248- sprintf ('T_DEFAULT closer scope opener token incorrect. Marker: %s. ' , $ testMarker )
262+ sprintf ('T_DEFAULT closer scope opener token incorrect. Marker: %s. ' , $ closerMarker )
249263 );
250264 $ this ->assertSame (
251265 $ expectedScopeCloser ,
252266 $ tokens [$ closer ]['scope_closer ' ],
253- sprintf ('T_DEFAULT closer scope closer token incorrect. Marker: %s. ' , $ testMarker )
267+ sprintf ('T_DEFAULT closer scope closer token incorrect. Marker: %s. ' , $ closerMarker )
254268 );
255269 }//end if
256270
257271 if (($ opener + 1 ) !== $ closer ) {
258272 $ end = $ closer ;
259- if (isset ($ conditionStop ) === true ) {
260- $ end = ($ token + $ conditionStop + 1 );
273+ if (isset ($ conditionStopMarker ) === true ) {
274+ $ end = ( $ this -> getTargetToken ( $ conditionStopMarker , $ this -> conditionStopTokens ) + 1 );
261275 }
262276
263277 for ($ i = ($ opener + 1 ); $ i < $ end ; $ i ++) {
@@ -267,7 +281,7 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
267281 sprintf ('T_DEFAULT condition not added for token belonging to the T_DEFAULT structure. Marker: %s. ' , $ testMarker )
268282 );
269283 }
270- }
284+ }//end if
271285
272286 }//end testSwitchDefault()
273287
@@ -284,46 +298,46 @@ public static function dataSwitchDefault()
284298 return [
285299 'simple_switch_default ' => [
286300 'testMarker ' => '/* testSimpleSwitchDefault */ ' ,
287- 'openerOffset ' => 1 ,
288- 'closerOffset ' => 4 ,
301+ 'openerMarker ' => ' /* testSimpleSwitchDefault */ ' ,
302+ 'closerMarker ' => ' /* testSimpleSwitchDefault */ ' ,
289303 ],
290304 'simple_switch_default_with_curlies ' => [
291305 // For a default structure with curly braces, the scope opener
292306 // will be the open curly and the closer the close curly.
293307 // However, scope conditions will not be set for open to close,
294308 // but only for the open token up to the "break/return/continue" etc.
295- 'testMarker ' => '/* testSimpleSwitchDefaultWithCurlies */ ' ,
296- 'openerOffset ' => 3 ,
297- 'closerOffset ' => 12 ,
298- 'conditionStop ' => 6 ,
309+ 'testMarker ' => '/* testSimpleSwitchDefaultWithCurlies */ ' ,
310+ 'openerMarker ' => ' /* testSimpleSwitchDefaultWithCurliesScopeOpener */ ' ,
311+ 'closerMarker ' => ' /* testSimpleSwitchDefaultWithCurliesScopeCloser */ ' ,
312+ 'conditionStopMarker ' => ' /* testSimpleSwitchDefaultWithCurliesConditionStop */ ' ,
299313 ],
300314 'switch_default_toplevel ' => [
301315 'testMarker ' => '/* testSwitchDefault */ ' ,
302- 'openerOffset ' => 1 ,
303- 'closerOffset ' => 43 ,
316+ 'openerMarker ' => ' /* testSwitchDefault */ ' ,
317+ 'closerMarker ' => ' /* testSwitchDefaultCloserMarker */ ' ,
304318 ],
305319 'switch_default_nested_in_match_case ' => [
306320 'testMarker ' => '/* testSwitchDefaultNestedInMatchCase */ ' ,
307- 'openerOffset ' => 1 ,
308- 'closerOffset ' => 20 ,
321+ 'openerMarker ' => ' /* testSwitchDefaultNestedInMatchCase */ ' ,
322+ 'closerMarker ' => ' /* testSwitchDefaultNestedInMatchCase */ ' ,
309323 ],
310324 'switch_default_nested_in_match_default ' => [
311325 'testMarker ' => '/* testSwitchDefaultNestedInMatchDefault */ ' ,
312- 'openerOffset ' => 1 ,
313- 'closerOffset ' => 18 ,
326+ 'openerMarker ' => ' /* testSwitchDefaultNestedInMatchDefault */ ' ,
327+ 'closerMarker ' => ' /* testSwitchDefaultNestedInMatchDefault */ ' ,
314328 ],
315329 'switch_and_default_sharing_scope_closer ' => [
316330 'testMarker ' => '/* testSwitchAndDefaultSharingScopeCloser */ ' ,
317- 'openerOffset ' => 1 ,
318- 'closerOffset ' => 10 ,
331+ 'openerMarker ' => ' /* testSwitchAndDefaultSharingScopeCloser */ ' ,
332+ 'closerMarker ' => ' /* testSwitchAndDefaultSharingScopeCloserScopeCloser */ ' ,
319333 'conditionStop ' => null ,
320334 'testContent ' => 'default ' ,
321335 'sharedScopeCloser ' => true ,
322336 ],
323337 'switch_and_default_with_nested_if_with_and_without_braces ' => [
324338 'testMarker ' => '/* testSwitchDefaultNestedIfWithAndWithoutBraces */ ' ,
325- 'openerOffset ' => 1 ,
326- 'closerOffset ' => 48 ,
339+ 'openerMarker ' => ' /* testSwitchDefaultNestedIfWithAndWithoutBraces */ ' ,
340+ 'closerMarker ' => ' /* testSwitchDefaultNestedIfWithAndWithoutBracesScopeCloser */ ' ,
327341 ],
328342 ];
329343
0 commit comments