@@ -125,30 +125,40 @@ public static function dataMatchDefault()
125125 * Note: Cases and default structures within a switch control structure *do* get case/default scope
126126 * conditions.
127127 *
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.
138138 *
139139 * @dataProvider dataSwitchDefault
140140 * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
141141 *
142142 * @return void
143143 */
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+ ) {
146147 $ tokens = $ this ->phpcsFile ->getTokens ();
147148
148149 $ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT , T_STRING ], $ testContent );
149150 $ 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 ]);
152162
153163 // Make sure we're looking at the right token.
154164 $ this ->assertSame (
@@ -180,86 +190,86 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
180190 $ this ->assertSame (
181191 $ expectedScopeOpener ,
182192 $ 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 )
184194 );
185195 $ this ->assertSame (
186196 $ expectedScopeCloser ,
187197 $ 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 )
189199 );
190200
191201 $ opener = $ tokenArray ['scope_opener ' ];
192202 $ this ->assertArrayHasKey (
193203 'scope_condition ' ,
194204 $ tokens [$ opener ],
195- sprintf ('Opener scope condition is not set. Marker: %s. ' , $ testMarker )
205+ sprintf ('Opener scope condition is not set. Marker: %s. ' , $ testOpenerMarker )
196206 );
197207 $ this ->assertArrayHasKey (
198208 'scope_opener ' ,
199209 $ tokens [$ opener ],
200- sprintf ('Opener scope opener is not set. Marker: %s. ' , $ testMarker )
210+ sprintf ('Opener scope opener is not set. Marker: %s. ' , $ testOpenerMarker )
201211 );
202212 $ this ->assertArrayHasKey (
203213 'scope_closer ' ,
204214 $ tokens [$ opener ],
205- sprintf ('Opener scope closer is not set. Marker: %s. ' , $ testMarker )
215+ sprintf ('Opener scope closer is not set. Marker: %s. ' , $ testOpenerMarker )
206216 );
207217 $ this ->assertSame (
208218 $ token ,
209219 $ 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 )
211221 );
212222 $ this ->assertSame (
213223 $ expectedScopeOpener ,
214224 $ 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 )
216226 );
217227 $ this ->assertSame (
218228 $ expectedScopeCloser ,
219229 $ 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 )
221231 );
222232
223233 if ($ sharedScopeCloser === false ) {
224234 $ closer = $ tokenArray ['scope_closer ' ];
225235 $ this ->assertArrayHasKey (
226236 'scope_condition ' ,
227237 $ tokens [$ closer ],
228- sprintf ('Closer scope condition is not set. Marker: %s. ' , $ testMarker )
238+ sprintf ('Closer scope condition is not set. Marker: %s. ' , $ testCloserMarker )
229239 );
230240 $ this ->assertArrayHasKey (
231241 'scope_opener ' ,
232242 $ tokens [$ closer ],
233- sprintf ('Closer scope opener is not set. Marker: %s. ' , $ testMarker )
243+ sprintf ('Closer scope opener is not set. Marker: %s. ' , $ testCloserMarker )
234244 );
235245 $ this ->assertArrayHasKey (
236246 'scope_closer ' ,
237247 $ tokens [$ closer ],
238- sprintf ('Closer scope closer is not set. Marker: %s. ' , $ testMarker )
248+ sprintf ('Closer scope closer is not set. Marker: %s. ' , $ testCloserMarker )
239249 );
240250 $ this ->assertSame (
241251 $ token ,
242252 $ 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 )
244254 );
245255 $ this ->assertSame (
246256 $ expectedScopeOpener ,
247257 $ 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 )
249259 );
250260 $ this ->assertSame (
251261 $ expectedScopeCloser ,
252262 $ 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 )
254264 );
255265 } else {
256266 $ closer = $ expectedScopeCloser ;
257267 }//end if
258268
259269 if (($ opener + 1 ) !== $ closer ) {
260270 $ 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 );
263273 }
264274
265275 for ($ i = ($ opener + 1 ); $ i < $ end ; $ i ++) {
@@ -285,47 +295,47 @@ public static function dataSwitchDefault()
285295 {
286296 return [
287297 'simple_switch_default ' => [
288- 'testMarker ' => '/* testSimpleSwitchDefault */ ' ,
289- 'openerOffset ' => 1 ,
290- 'closerOffset ' => 4 ,
298+ 'testMarker ' => '/* testSimpleSwitchDefault */ ' ,
299+ 'testOpenerMarker ' => null ,
300+ 'testCloserMarker ' => null ,
291301 ],
292302 'simple_switch_default_with_curlies ' => [
293303 // For a default structure with curly braces, the scope opener
294304 // will be the open curly and the closer the close curly.
295305 // However, scope conditions will not be set for open to close,
296306 // 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 */ ' ,
301311 ],
302312 'switch_default_toplevel ' => [
303- 'testMarker ' => '/* testSwitchDefault */ ' ,
304- 'openerOffset ' => 1 ,
305- 'closerOffset ' => 43 ,
313+ 'testMarker ' => '/* testSwitchDefault */ ' ,
314+ 'testOpenerMarker ' => null ,
315+ 'testCloserMarker ' => ' /* testSwitchDefaultCloserMarker */ ' ,
306316 ],
307317 'switch_default_nested_in_match_case ' => [
308- 'testMarker ' => '/* testSwitchDefaultNestedInMatchCase */ ' ,
309- 'openerOffset ' => 1 ,
310- 'closerOffset ' => 20 ,
318+ 'testMarker ' => '/* testSwitchDefaultNestedInMatchCase */ ' ,
319+ 'testOpenerMarker ' => null ,
320+ 'testCloserMarker ' => null ,
311321 ],
312322 'switch_default_nested_in_match_default ' => [
313- 'testMarker ' => '/* testSwitchDefaultNestedInMatchDefault */ ' ,
314- 'openerOffset ' => 1 ,
315- 'closerOffset ' => 18 ,
323+ 'testMarker ' => '/* testSwitchDefaultNestedInMatchDefault */ ' ,
324+ 'testOpenerMarker ' => null ,
325+ 'testCloserMarker ' => null ,
316326 ],
317327 'switch_and_default_sharing_scope_closer ' => [
318328 'testMarker ' => '/* testSwitchAndDefaultSharingScopeCloser */ ' ,
319- 'openerOffset ' => 1 ,
320- 'closerOffset ' => 10 ,
329+ 'testOpenerMarker ' => null ,
330+ 'testCloserMarker ' => ' /* testSwitchAndDefaultSharingScopeCloserScopeCloser */ ' ,
321331 'conditionStop ' => null ,
322332 'testContent ' => 'default ' ,
323333 'sharedScopeCloser ' => true ,
324334 ],
325335 '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 */ ' ,
329339 ],
330340 ];
331341
0 commit comments