14
14
final class RecurseScopeMapDefaultKeywordConditionsTest extends AbstractTokenizerTestCase
15
15
{
16
16
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
+
17
31
18
32
/**
19
33
* Test that match "default" tokens does not get scope indexes.
@@ -123,30 +137,30 @@ public static function dataMatchDefault()
123
137
* Note: Cases and default structures within a switch control structure *do* get case/default scope
124
138
* conditions.
125
139
*
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.
136
150
*
137
151
* @dataProvider dataSwitchDefault
138
152
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
139
153
*
140
154
* @return void
141
155
*/
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 )
143
157
{
144
158
$ tokens = $ this ->phpcsFile ->getTokens ();
145
159
146
160
$ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT , T_STRING ], $ testContent );
147
161
$ 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 ] );
150
164
151
165
// Make sure we're looking at the right token.
152
166
$ this ->assertSame (
@@ -190,32 +204,32 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
190
204
$ this ->assertArrayHasKey (
191
205
'scope_condition ' ,
192
206
$ tokens [$ opener ],
193
- sprintf ('Opener scope condition is not set. Marker: %s. ' , $ testMarker )
207
+ sprintf ('Opener scope condition is not set. Marker: %s. ' , $ openerMarker )
194
208
);
195
209
$ this ->assertArrayHasKey (
196
210
'scope_opener ' ,
197
211
$ tokens [$ opener ],
198
- sprintf ('Opener scope opener is not set. Marker: %s. ' , $ testMarker )
212
+ sprintf ('Opener scope opener is not set. Marker: %s. ' , $ openerMarker )
199
213
);
200
214
$ this ->assertArrayHasKey (
201
215
'scope_closer ' ,
202
216
$ tokens [$ opener ],
203
- sprintf ('Opener scope closer is not set. Marker: %s. ' , $ testMarker )
217
+ sprintf ('Opener scope closer is not set. Marker: %s. ' , $ openerMarker )
204
218
);
205
219
$ this ->assertSame (
206
220
$ token ,
207
221
$ 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 )
209
223
);
210
224
$ this ->assertSame (
211
225
$ expectedScopeOpener ,
212
226
$ 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 )
214
228
);
215
229
$ this ->assertSame (
216
230
$ expectedScopeCloser ,
217
231
$ 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 )
219
233
);
220
234
221
235
$ closer = $ expectedScopeCloser ;
@@ -225,39 +239,39 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
225
239
$ this ->assertArrayHasKey (
226
240
'scope_condition ' ,
227
241
$ tokens [$ closer ],
228
- sprintf ('Closer scope condition is not set. Marker: %s. ' , $ testMarker )
242
+ sprintf ('Closer scope condition is not set. Marker: %s. ' , $ closerMarker )
229
243
);
230
244
$ this ->assertArrayHasKey (
231
245
'scope_opener ' ,
232
246
$ tokens [$ closer ],
233
- sprintf ('Closer scope opener is not set. Marker: %s. ' , $ testMarker )
247
+ sprintf ('Closer scope opener is not set. Marker: %s. ' , $ closerMarker )
234
248
);
235
249
$ this ->assertArrayHasKey (
236
250
'scope_closer ' ,
237
251
$ tokens [$ closer ],
238
- sprintf ('Closer scope closer is not set. Marker: %s. ' , $ testMarker )
252
+ sprintf ('Closer scope closer is not set. Marker: %s. ' , $ closerMarker )
239
253
);
240
254
$ this ->assertSame (
241
255
$ token ,
242
256
$ 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 )
244
258
);
245
259
$ this ->assertSame (
246
260
$ expectedScopeOpener ,
247
261
$ 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 )
249
263
);
250
264
$ this ->assertSame (
251
265
$ expectedScopeCloser ,
252
266
$ 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 )
254
268
);
255
269
}//end if
256
270
257
271
if (($ opener + 1 ) !== $ closer ) {
258
272
$ 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 );
261
275
}
262
276
263
277
for ($ i = ($ opener + 1 ); $ i < $ end ; $ i ++) {
@@ -267,7 +281,7 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
267
281
sprintf ('T_DEFAULT condition not added for token belonging to the T_DEFAULT structure. Marker: %s. ' , $ testMarker )
268
282
);
269
283
}
270
- }
284
+ }//end if
271
285
272
286
}//end testSwitchDefault()
273
287
@@ -284,46 +298,46 @@ public static function dataSwitchDefault()
284
298
return [
285
299
'simple_switch_default ' => [
286
300
'testMarker ' => '/* testSimpleSwitchDefault */ ' ,
287
- 'openerOffset ' => 1 ,
288
- 'closerOffset ' => 4 ,
301
+ 'openerMarker ' => ' /* testSimpleSwitchDefault */ ' ,
302
+ 'closerMarker ' => ' /* testSimpleSwitchDefault */ ' ,
289
303
],
290
304
'simple_switch_default_with_curlies ' => [
291
305
// For a default structure with curly braces, the scope opener
292
306
// will be the open curly and the closer the close curly.
293
307
// However, scope conditions will not be set for open to close,
294
308
// 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 */ ' ,
299
313
],
300
314
'switch_default_toplevel ' => [
301
315
'testMarker ' => '/* testSwitchDefault */ ' ,
302
- 'openerOffset ' => 1 ,
303
- 'closerOffset ' => 43 ,
316
+ 'openerMarker ' => ' /* testSwitchDefault */ ' ,
317
+ 'closerMarker ' => ' /* testSwitchDefaultCloserMarker */ ' ,
304
318
],
305
319
'switch_default_nested_in_match_case ' => [
306
320
'testMarker ' => '/* testSwitchDefaultNestedInMatchCase */ ' ,
307
- 'openerOffset ' => 1 ,
308
- 'closerOffset ' => 20 ,
321
+ 'openerMarker ' => ' /* testSwitchDefaultNestedInMatchCase */ ' ,
322
+ 'closerMarker ' => ' /* testSwitchDefaultNestedInMatchCase */ ' ,
309
323
],
310
324
'switch_default_nested_in_match_default ' => [
311
325
'testMarker ' => '/* testSwitchDefaultNestedInMatchDefault */ ' ,
312
- 'openerOffset ' => 1 ,
313
- 'closerOffset ' => 18 ,
326
+ 'openerMarker ' => ' /* testSwitchDefaultNestedInMatchDefault */ ' ,
327
+ 'closerMarker ' => ' /* testSwitchDefaultNestedInMatchDefault */ ' ,
314
328
],
315
329
'switch_and_default_sharing_scope_closer ' => [
316
330
'testMarker ' => '/* testSwitchAndDefaultSharingScopeCloser */ ' ,
317
- 'openerOffset ' => 1 ,
318
- 'closerOffset ' => 10 ,
331
+ 'openerMarker ' => ' /* testSwitchAndDefaultSharingScopeCloser */ ' ,
332
+ 'closerMarker ' => ' /* testSwitchAndDefaultSharingScopeCloserScopeCloser */ ' ,
319
333
'conditionStop ' => null ,
320
334
'testContent ' => 'default ' ,
321
335
'sharedScopeCloser ' => true ,
322
336
],
323
337
'switch_and_default_with_nested_if_with_and_without_braces ' => [
324
338
'testMarker ' => '/* testSwitchDefaultNestedIfWithAndWithoutBraces */ ' ,
325
- 'openerOffset ' => 1 ,
326
- 'closerOffset ' => 48 ,
339
+ 'openerMarker ' => ' /* testSwitchDefaultNestedIfWithAndWithoutBraces */ ' ,
340
+ 'closerMarker ' => ' /* testSwitchDefaultNestedIfWithAndWithoutBracesScopeCloser */ ' ,
327
341
],
328
342
];
329
343
0 commit comments