@@ -125,29 +125,39 @@ public static function dataMatchDefault()
125
125
* Note: Cases and default structures within a switch control structure *do* get case/default scope
126
126
* conditions.
127
127
*
128
- * @param string $testMarker The comment prefacing the target token.
129
- * @param int $openerOffset The expected offset of the scope opener in relation to the testMarker .
130
- * @param int $closerOffset The expected offset of the scope closer in relation to the testMarker .
131
- * @param int |null $conditionStop The expected offset at which tokens stop having T_DEFAULT as a scope condition.
132
- * @param string $testContent The token content to look for.
133
- * @param bool $sharedScopeCloser Whether to skip checking for the `scope_condition` of the
134
- * scope closer. Needed when the default and switch
135
- * structures share a scope closer. See
136
- * https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/810.
128
+ * @param string $testMarker The comment prefacing the target token.
129
+ * @param string|null $testOpenerMarker The comment prefacing the scope opener token .
130
+ * @param string|null $testCloserMarker The comment prefacing the scope closer token .
131
+ * @param string |null $conditionStopMarker The expected offset at which tokens stop having T_DEFAULT as a scope condition.
132
+ * @param string $testContent The token content to look for.
133
+ * @param bool $sharedScopeCloser Whether to skip checking for the `scope_condition` of the
134
+ * scope closer. Needed when the default and switch
135
+ * structures share a scope closer. See
136
+ * https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/810.
137
137
*
138
138
* @dataProvider dataSwitchDefault
139
139
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
140
140
*
141
141
* @return void
142
142
*/
143
- public function testSwitchDefault ($ testMarker , $ openerOffset , $ closerOffset , $ conditionStop =null , $ testContent ='default ' , $ sharedScopeCloser =false )
144
- {
143
+ public function testSwitchDefault (
144
+ $ testMarker , $ testOpenerMarker =null , $ testCloserMarker =null , $ conditionStopMarker =null , $ testContent ='default ' , $ sharedScopeCloser =false
145
+ ) {
145
146
$ tokens = $ this ->phpcsFile ->getTokens ();
146
147
147
148
$ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT , T_STRING ], $ testContent );
148
149
$ tokenArray = $ tokens [$ token ];
149
- $ expectedScopeOpener = ($ token + $ openerOffset );
150
- $ expectedScopeCloser = ($ token + $ closerOffset );
150
+
151
+ if ($ testOpenerMarker === null ) {
152
+ $ testOpenerMarker = $ testMarker ;
153
+ }
154
+
155
+ if ($ testCloserMarker === null ) {
156
+ $ testCloserMarker = $ testMarker ;
157
+ }
158
+
159
+ $ expectedScopeOpener = $ this ->getTargetToken ($ testOpenerMarker , [T_COLON , T_OPEN_CURLY_BRACKET , T_SEMICOLON ]);
160
+ $ expectedScopeCloser = $ this ->getTargetToken ($ testCloserMarker , [T_BREAK , T_CLOSE_CURLY_BRACKET , T_RETURN , T_ENDSWITCH ]);
151
161
152
162
// Make sure we're looking at the right token.
153
163
$ this ->assertSame (
@@ -179,86 +189,86 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
179
189
$ this ->assertSame (
180
190
$ expectedScopeOpener ,
181
191
$ tokenArray ['scope_opener ' ],
182
- sprintf ('Scope opener of the T_DEFAULT token incorrect. Marker: %s. ' , $ testMarker )
192
+ sprintf ('Scope opener of the T_DEFAULT token incorrect. Marker: %s. ' , $ testOpenerMarker )
183
193
);
184
194
$ this ->assertSame (
185
195
$ expectedScopeCloser ,
186
196
$ tokenArray ['scope_closer ' ],
187
- sprintf ('Scope closer of the T_DEFAULT token incorrect. Marker: %s. ' , $ testMarker )
197
+ sprintf ('Scope closer of the T_DEFAULT token incorrect. Marker: %s. ' , $ testCloserMarker )
188
198
);
189
199
190
200
$ opener = $ tokenArray ['scope_opener ' ];
191
201
$ this ->assertArrayHasKey (
192
202
'scope_condition ' ,
193
203
$ tokens [$ opener ],
194
- sprintf ('Opener scope condition is not set. Marker: %s. ' , $ testMarker )
204
+ sprintf ('Opener scope condition is not set. Marker: %s. ' , $ testOpenerMarker )
195
205
);
196
206
$ this ->assertArrayHasKey (
197
207
'scope_opener ' ,
198
208
$ tokens [$ opener ],
199
- sprintf ('Opener scope opener is not set. Marker: %s. ' , $ testMarker )
209
+ sprintf ('Opener scope opener is not set. Marker: %s. ' , $ testOpenerMarker )
200
210
);
201
211
$ this ->assertArrayHasKey (
202
212
'scope_closer ' ,
203
213
$ tokens [$ opener ],
204
- sprintf ('Opener scope closer is not set. Marker: %s. ' , $ testMarker )
214
+ sprintf ('Opener scope closer is not set. Marker: %s. ' , $ testOpenerMarker )
205
215
);
206
216
$ this ->assertSame (
207
217
$ token ,
208
218
$ tokens [$ opener ]['scope_condition ' ],
209
- sprintf ('Opener scope condition is not the T_DEFAULT token. Marker: %s. ' , $ testMarker )
219
+ sprintf ('Opener scope condition is not the T_DEFAULT token. Marker: %s. ' , $ testOpenerMarker )
210
220
);
211
221
$ this ->assertSame (
212
222
$ expectedScopeOpener ,
213
223
$ tokens [$ opener ]['scope_opener ' ],
214
- sprintf ('T_DEFAULT opener scope opener token incorrect. Marker: %s. ' , $ testMarker )
224
+ sprintf ('T_DEFAULT opener scope opener token incorrect. Marker: %s. ' , $ testOpenerMarker )
215
225
);
216
226
$ this ->assertSame (
217
227
$ expectedScopeCloser ,
218
228
$ tokens [$ opener ]['scope_closer ' ],
219
- sprintf ('T_DEFAULT opener scope closer token incorrect. Marker: %s. ' , $ testMarker )
229
+ sprintf ('T_DEFAULT opener scope closer token incorrect. Marker: %s. ' , $ testOpenerMarker )
220
230
);
221
231
222
232
if ($ sharedScopeCloser === false ) {
223
233
$ closer = $ tokenArray ['scope_closer ' ];
224
234
$ this ->assertArrayHasKey (
225
235
'scope_condition ' ,
226
236
$ tokens [$ closer ],
227
- sprintf ('Closer scope condition is not set. Marker: %s. ' , $ testMarker )
237
+ sprintf ('Closer scope condition is not set. Marker: %s. ' , $ testCloserMarker )
228
238
);
229
239
$ this ->assertArrayHasKey (
230
240
'scope_opener ' ,
231
241
$ tokens [$ closer ],
232
- sprintf ('Closer scope opener is not set. Marker: %s. ' , $ testMarker )
242
+ sprintf ('Closer scope opener is not set. Marker: %s. ' , $ testCloserMarker )
233
243
);
234
244
$ this ->assertArrayHasKey (
235
245
'scope_closer ' ,
236
246
$ tokens [$ closer ],
237
- sprintf ('Closer scope closer is not set. Marker: %s. ' , $ testMarker )
247
+ sprintf ('Closer scope closer is not set. Marker: %s. ' , $ testCloserMarker )
238
248
);
239
249
$ this ->assertSame (
240
250
$ token ,
241
251
$ tokens [$ closer ]['scope_condition ' ],
242
- sprintf ('Closer scope condition is not the T_DEFAULT token. Marker: %s. ' , $ testMarker )
252
+ sprintf ('Closer scope condition is not the T_DEFAULT token. Marker: %s. ' , $ testCloserMarker )
243
253
);
244
254
$ this ->assertSame (
245
255
$ expectedScopeOpener ,
246
256
$ tokens [$ closer ]['scope_opener ' ],
247
- sprintf ('T_DEFAULT closer scope opener token incorrect. Marker: %s. ' , $ testMarker )
257
+ sprintf ('T_DEFAULT closer scope opener token incorrect. Marker: %s. ' , $ testCloserMarker )
248
258
);
249
259
$ this ->assertSame (
250
260
$ expectedScopeCloser ,
251
261
$ tokens [$ closer ]['scope_closer ' ],
252
- sprintf ('T_DEFAULT closer scope closer token incorrect. Marker: %s. ' , $ testMarker )
262
+ sprintf ('T_DEFAULT closer scope closer token incorrect. Marker: %s. ' , $ testCloserMarker )
253
263
);
254
264
} else {
255
265
$ closer = $ expectedScopeCloser ;
256
266
}//end if
257
267
258
268
if (($ opener + 1 ) !== $ closer ) {
259
269
$ end = $ closer ;
260
- if (isset ($ conditionStop ) === true ) {
261
- $ end = ( $ opener + $ conditionStop );
270
+ if (isset ($ conditionStopMarker ) === true ) {
271
+ $ end = $ this -> getTargetToken ( $ conditionStopMarker , [ T_RETURN ] );
262
272
}
263
273
264
274
for ($ i = ($ opener + 1 ); $ i < $ end ; $ i ++) {
@@ -284,47 +294,47 @@ public static function dataSwitchDefault()
284
294
{
285
295
return [
286
296
'simple_switch_default ' => [
287
- 'testMarker ' => '/* testSimpleSwitchDefault */ ' ,
288
- 'openerOffset ' => 1 ,
289
- 'closerOffset ' => 4 ,
297
+ 'testMarker ' => '/* testSimpleSwitchDefault */ ' ,
298
+ 'testOpenerMarker ' => null ,
299
+ 'testCloserMarker ' => null ,
290
300
],
291
301
'simple_switch_default_with_curlies ' => [
292
302
// For a default structure with curly braces, the scope opener
293
303
// will be the open curly and the closer the close curly.
294
304
// However, scope conditions will not be set for open to close,
295
305
// but only for the open token up to the "break/return/continue" etc.
296
- 'testMarker ' => '/* testSimpleSwitchDefaultWithCurlies */ ' ,
297
- 'openerOffset ' => 3 ,
298
- 'closerOffset ' => 12 ,
299
- 'conditionStop ' => 3 ,
306
+ 'testMarker ' => '/* testSimpleSwitchDefaultWithCurlies */ ' ,
307
+ 'testOpenerMarker ' => ' /* testSimpleSwitchDefaultWithCurliesScopeOpener */ ' ,
308
+ 'testCloserMarker ' => ' /* testSimpleSwitchDefaultWithCurliesScopeCloser */ ' ,
309
+ 'conditionStopMarker ' => ' /* testSimpleSwitchDefaultWithCurliesConditionStop */ ' ,
300
310
],
301
311
'switch_default_toplevel ' => [
302
- 'testMarker ' => '/* testSwitchDefault */ ' ,
303
- 'openerOffset ' => 1 ,
304
- 'closerOffset ' => 43 ,
312
+ 'testMarker ' => '/* testSwitchDefault */ ' ,
313
+ 'testOpenerMarker ' => null ,
314
+ 'testCloserMarker ' => ' /* testSwitchDefaultCloserMarker */ ' ,
305
315
],
306
316
'switch_default_nested_in_match_case ' => [
307
- 'testMarker ' => '/* testSwitchDefaultNestedInMatchCase */ ' ,
308
- 'openerOffset ' => 1 ,
309
- 'closerOffset ' => 20 ,
317
+ 'testMarker ' => '/* testSwitchDefaultNestedInMatchCase */ ' ,
318
+ 'testOpenerMarker ' => null ,
319
+ 'testCloserMarker ' => null ,
310
320
],
311
321
'switch_default_nested_in_match_default ' => [
312
- 'testMarker ' => '/* testSwitchDefaultNestedInMatchDefault */ ' ,
313
- 'openerOffset ' => 1 ,
314
- 'closerOffset ' => 18 ,
322
+ 'testMarker ' => '/* testSwitchDefaultNestedInMatchDefault */ ' ,
323
+ 'testOpenerMarker ' => null ,
324
+ 'testCloserMarker ' => null ,
315
325
],
316
326
'switch_and_default_sharing_scope_closer ' => [
317
327
'testMarker ' => '/* testSwitchAndDefaultSharingScopeCloser */ ' ,
318
- 'openerOffset ' => 1 ,
319
- 'closerOffset ' => 10 ,
328
+ 'testOpenerMarker ' => null ,
329
+ 'testCloserMarker ' => ' /* testSwitchAndDefaultSharingScopeCloserScopeCloser */ ' ,
320
330
'conditionStop ' => null ,
321
331
'testContent ' => 'default ' ,
322
332
'sharedScopeCloser ' => true ,
323
333
],
324
334
'switch_and_default_with_nested_if_with_and_without_braces ' => [
325
- 'testMarker ' => '/* testSwitchDefaultNestedIfWithAndWithoutBraces */ ' ,
326
- 'openerOffset ' => 1 ,
327
- 'closerOffset ' => 48 ,
335
+ 'testMarker ' => '/* testSwitchDefaultNestedIfWithAndWithoutBraces */ ' ,
336
+ 'testOpenerMarker ' => null ,
337
+ 'testCloserMarker ' => ' /* testSwitchDefaultNestedIfWithAndWithoutBracesScopeCloser */ ' ,
328
338
],
329
339
];
330
340
0 commit comments