@@ -125,29 +125,39 @@ 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 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.
137137 *
138138 * @dataProvider dataSwitchDefault
139139 * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
140140 *
141141 * @return void
142142 */
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+ ) {
145146 $ tokens = $ this ->phpcsFile ->getTokens ();
146147
147148 $ token = $ this ->getTargetToken ($ testMarker , [T_MATCH_DEFAULT , T_DEFAULT , T_STRING ], $ testContent );
148149 $ 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 ]);
151161
152162 // Make sure we're looking at the right token.
153163 $ this ->assertSame (
@@ -179,86 +189,86 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
179189 $ this ->assertSame (
180190 $ expectedScopeOpener ,
181191 $ 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 )
183193 );
184194 $ this ->assertSame (
185195 $ expectedScopeCloser ,
186196 $ 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 )
188198 );
189199
190200 $ opener = $ tokenArray ['scope_opener ' ];
191201 $ this ->assertArrayHasKey (
192202 'scope_condition ' ,
193203 $ tokens [$ opener ],
194- sprintf ('Opener scope condition is not set. Marker: %s. ' , $ testMarker )
204+ sprintf ('Opener scope condition is not set. Marker: %s. ' , $ testOpenerMarker )
195205 );
196206 $ this ->assertArrayHasKey (
197207 'scope_opener ' ,
198208 $ tokens [$ opener ],
199- sprintf ('Opener scope opener is not set. Marker: %s. ' , $ testMarker )
209+ sprintf ('Opener scope opener is not set. Marker: %s. ' , $ testOpenerMarker )
200210 );
201211 $ this ->assertArrayHasKey (
202212 'scope_closer ' ,
203213 $ tokens [$ opener ],
204- sprintf ('Opener scope closer is not set. Marker: %s. ' , $ testMarker )
214+ sprintf ('Opener scope closer is not set. Marker: %s. ' , $ testOpenerMarker )
205215 );
206216 $ this ->assertSame (
207217 $ token ,
208218 $ 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 )
210220 );
211221 $ this ->assertSame (
212222 $ expectedScopeOpener ,
213223 $ 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 )
215225 );
216226 $ this ->assertSame (
217227 $ expectedScopeCloser ,
218228 $ 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 )
220230 );
221231
222232 if ($ sharedScopeCloser === false ) {
223233 $ closer = $ tokenArray ['scope_closer ' ];
224234 $ this ->assertArrayHasKey (
225235 'scope_condition ' ,
226236 $ tokens [$ closer ],
227- sprintf ('Closer scope condition is not set. Marker: %s. ' , $ testMarker )
237+ sprintf ('Closer scope condition is not set. Marker: %s. ' , $ testCloserMarker )
228238 );
229239 $ this ->assertArrayHasKey (
230240 'scope_opener ' ,
231241 $ tokens [$ closer ],
232- sprintf ('Closer scope opener is not set. Marker: %s. ' , $ testMarker )
242+ sprintf ('Closer scope opener is not set. Marker: %s. ' , $ testCloserMarker )
233243 );
234244 $ this ->assertArrayHasKey (
235245 'scope_closer ' ,
236246 $ tokens [$ closer ],
237- sprintf ('Closer scope closer is not set. Marker: %s. ' , $ testMarker )
247+ sprintf ('Closer scope closer is not set. Marker: %s. ' , $ testCloserMarker )
238248 );
239249 $ this ->assertSame (
240250 $ token ,
241251 $ 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 )
243253 );
244254 $ this ->assertSame (
245255 $ expectedScopeOpener ,
246256 $ 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 )
248258 );
249259 $ this ->assertSame (
250260 $ expectedScopeCloser ,
251261 $ 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 )
253263 );
254264 } else {
255265 $ closer = $ expectedScopeCloser ;
256266 }//end if
257267
258268 if (($ opener + 1 ) !== $ closer ) {
259269 $ end = $ closer ;
260- if (isset ($ conditionStop ) === true ) {
261- $ end = ( $ opener + $ conditionStop );
270+ if (isset ($ conditionStopMarker ) === true ) {
271+ $ end = $ this -> getTargetToken ( $ conditionStopMarker , [ T_RETURN ] );
262272 }
263273
264274 for ($ i = ($ opener + 1 ); $ i < $ end ; $ i ++) {
@@ -284,47 +294,47 @@ public static function dataSwitchDefault()
284294 {
285295 return [
286296 'simple_switch_default ' => [
287- 'testMarker ' => '/* testSimpleSwitchDefault */ ' ,
288- 'openerOffset ' => 1 ,
289- 'closerOffset ' => 4 ,
297+ 'testMarker ' => '/* testSimpleSwitchDefault */ ' ,
298+ 'testOpenerMarker ' => null ,
299+ 'testCloserMarker ' => null ,
290300 ],
291301 'simple_switch_default_with_curlies ' => [
292302 // For a default structure with curly braces, the scope opener
293303 // will be the open curly and the closer the close curly.
294304 // However, scope conditions will not be set for open to close,
295305 // 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 */ ' ,
300310 ],
301311 'switch_default_toplevel ' => [
302- 'testMarker ' => '/* testSwitchDefault */ ' ,
303- 'openerOffset ' => 1 ,
304- 'closerOffset ' => 43 ,
312+ 'testMarker ' => '/* testSwitchDefault */ ' ,
313+ 'testOpenerMarker ' => null ,
314+ 'testCloserMarker ' => ' /* testSwitchDefaultCloserMarker */ ' ,
305315 ],
306316 'switch_default_nested_in_match_case ' => [
307- 'testMarker ' => '/* testSwitchDefaultNestedInMatchCase */ ' ,
308- 'openerOffset ' => 1 ,
309- 'closerOffset ' => 20 ,
317+ 'testMarker ' => '/* testSwitchDefaultNestedInMatchCase */ ' ,
318+ 'testOpenerMarker ' => null ,
319+ 'testCloserMarker ' => null ,
310320 ],
311321 'switch_default_nested_in_match_default ' => [
312- 'testMarker ' => '/* testSwitchDefaultNestedInMatchDefault */ ' ,
313- 'openerOffset ' => 1 ,
314- 'closerOffset ' => 18 ,
322+ 'testMarker ' => '/* testSwitchDefaultNestedInMatchDefault */ ' ,
323+ 'testOpenerMarker ' => null ,
324+ 'testCloserMarker ' => null ,
315325 ],
316326 'switch_and_default_sharing_scope_closer ' => [
317327 'testMarker ' => '/* testSwitchAndDefaultSharingScopeCloser */ ' ,
318- 'openerOffset ' => 1 ,
319- 'closerOffset ' => 10 ,
328+ 'testOpenerMarker ' => null ,
329+ 'testCloserMarker ' => ' /* testSwitchAndDefaultSharingScopeCloserScopeCloser */ ' ,
320330 'conditionStop ' => null ,
321331 'testContent ' => 'default ' ,
322332 'sharedScopeCloser ' => true ,
323333 ],
324334 '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 */ ' ,
328338 ],
329339 ];
330340
0 commit comments