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