@@ -109,19 +109,23 @@ public static function dataMatchDefault()
109109 * Note: Cases and default structures within a switch control structure *do* get case/default scope
110110 * conditions.
111111 *
112- * @param string $testMarker The comment prefacing the target token.
113- * @param int $openerOffset The expected offset of the scope opener in relation to the testMarker.
114- * @param int $closerOffset The expected offset of the scope closer in relation to the testMarker.
115- * @param int|null $conditionStop The expected offset in relation to the testMarker, at which tokens stop
116- * having T_DEFAULT as a scope condition.
117- * @param string $testContent The token content to look for.
112+ * @param string $testMarker The comment prefacing the target token.
113+ * @param int $openerOffset The expected offset of the scope opener in relation to the testMarker.
114+ * @param int $closerOffset The expected offset of the scope closer in relation to the testMarker.
115+ * @param int|null $conditionStop The expected offset in relation to the testMarker, at which tokens stop
116+ * having T_DEFAULT as a scope condition.
117+ * @param string $testContent The token content to look for.
118+ * @param bool $sharedScopeCloser Whether to skip checking for the `scope_condition` of the
119+ * scope closer. Needed when the default and switch
120+ * structures share a scope closer. See
121+ * https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/810.
118122 *
119123 * @dataProvider dataSwitchDefault
120124 * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
121125 *
122126 * @return void
123127 */
124- public function testSwitchDefault ($ testMarker , $ openerOffset , $ closerOffset , $ conditionStop =null , $ testContent ='default ' )
128+ public function testSwitchDefault ($ testMarker , $ openerOffset , $ closerOffset , $ conditionStop =null , $ testContent ='default ' , $ sharedScopeCloser = false )
125129 {
126130 $ tokens = $ this ->phpcsFile ->getTokens ();
127131
@@ -148,13 +152,17 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
148152 $ this ->assertSame ($ expectedScopeOpener , $ tokens [$ opener ]['scope_opener ' ], 'T_DEFAULT opener scope opener token incorrect ' );
149153 $ this ->assertSame ($ expectedScopeCloser , $ tokens [$ opener ]['scope_closer ' ], 'T_DEFAULT opener scope closer token incorrect ' );
150154
151- $ closer = $ tokenArray ['scope_closer ' ];
152- $ this ->assertArrayHasKey ('scope_condition ' , $ tokens [$ closer ], 'Closer scope condition is not set ' );
153- $ this ->assertArrayHasKey ('scope_opener ' , $ tokens [$ closer ], 'Closer scope opener is not set ' );
154- $ this ->assertArrayHasKey ('scope_closer ' , $ tokens [$ closer ], 'Closer scope closer is not set ' );
155- $ this ->assertSame ($ token , $ tokens [$ closer ]['scope_condition ' ], 'Closer scope condition is not the T_DEFAULT token ' );
156- $ this ->assertSame ($ expectedScopeOpener , $ tokens [$ closer ]['scope_opener ' ], 'T_DEFAULT closer scope opener token incorrect ' );
157- $ this ->assertSame ($ expectedScopeCloser , $ tokens [$ closer ]['scope_closer ' ], 'T_DEFAULT closer scope closer token incorrect ' );
155+ $ closer = $ expectedScopeCloser ;
156+
157+ if ($ sharedScopeCloser === false ) {
158+ $ closer = $ tokenArray ['scope_closer ' ];
159+ $ this ->assertArrayHasKey ('scope_condition ' , $ tokens [$ closer ], 'Closer scope condition is not set ' );
160+ $ this ->assertArrayHasKey ('scope_opener ' , $ tokens [$ closer ], 'Closer scope opener is not set ' );
161+ $ this ->assertArrayHasKey ('scope_closer ' , $ tokens [$ closer ], 'Closer scope closer is not set ' );
162+ $ this ->assertSame ($ token , $ tokens [$ closer ]['scope_condition ' ], 'Closer scope condition is not the T_DEFAULT token ' );
163+ $ this ->assertSame ($ expectedScopeOpener , $ tokens [$ closer ]['scope_opener ' ], 'T_DEFAULT closer scope opener token incorrect ' );
164+ $ this ->assertSame ($ expectedScopeCloser , $ tokens [$ closer ]['scope_closer ' ], 'T_DEFAULT closer scope closer token incorrect ' );
165+ }
158166
159167 if (($ opener + 1 ) !== $ closer ) {
160168 $ end = $ closer ;
@@ -184,12 +192,12 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
184192 public static function dataSwitchDefault ()
185193 {
186194 return [
187- 'simple_switch_default ' => [
195+ 'simple_switch_default ' => [
188196 'testMarker ' => '/* testSimpleSwitchDefault */ ' ,
189197 'openerOffset ' => 1 ,
190198 'closerOffset ' => 4 ,
191199 ],
192- 'simple_switch_default_with_curlies ' => [
200+ 'simple_switch_default_with_curlies ' => [
193201 // For a default structure with curly braces, the scope opener
194202 // will be the open curly and the closer the close curly.
195203 // However, scope conditions will not be set for open to close,
@@ -199,21 +207,34 @@ public static function dataSwitchDefault()
199207 'closerOffset ' => 12 ,
200208 'conditionStop ' => 6 ,
201209 ],
202- 'switch_default_toplevel ' => [
210+ 'switch_default_toplevel ' => [
203211 'testMarker ' => '/* testSwitchDefault */ ' ,
204212 'openerOffset ' => 1 ,
205213 'closerOffset ' => 43 ,
206214 ],
207- 'switch_default_nested_in_match_case ' => [
215+ 'switch_default_nested_in_match_case ' => [
208216 'testMarker ' => '/* testSwitchDefaultNestedInMatchCase */ ' ,
209217 'openerOffset ' => 1 ,
210218 'closerOffset ' => 20 ,
211219 ],
212- 'switch_default_nested_in_match_default ' => [
220+ 'switch_default_nested_in_match_default ' => [
213221 'testMarker ' => '/* testSwitchDefaultNestedInMatchDefault */ ' ,
214222 'openerOffset ' => 1 ,
215223 'closerOffset ' => 18 ,
216224 ],
225+ 'switch_and_default_sharing_scope_closer ' => [
226+ 'testMarker ' => '/* testSwitchAndDefaultSharingScopeCloser */ ' ,
227+ 'openerOffset ' => 1 ,
228+ 'closerOffset ' => 10 ,
229+ 'conditionStop ' => null ,
230+ 'testContent ' => 'default ' ,
231+ 'sharedScopeCloser ' => true ,
232+ ],
233+ 'switch_and_default_with_nested_if_with_and_without_braces ' => [
234+ 'testMarker ' => '/* testSwitchDefaultNestedIfWithAndWithoutBraces */ ' ,
235+ 'openerOffset ' => 1 ,
236+ 'closerOffset ' => 48 ,
237+ ],
217238 ];
218239
219240 }//end dataSwitchDefault()
0 commit comments