@@ -109,19 +109,23 @@ public static function dataMatchDefault()
109
109
* Note: Cases and default structures within a switch control structure *do* get case/default scope
110
110
* conditions.
111
111
*
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.
118
122
*
119
123
* @dataProvider dataSwitchDefault
120
124
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
121
125
*
122
126
* @return void
123
127
*/
124
- public function testSwitchDefault ($ testMarker , $ openerOffset , $ closerOffset , $ conditionStop =null , $ testContent ='default ' )
128
+ public function testSwitchDefault ($ testMarker , $ openerOffset , $ closerOffset , $ conditionStop =null , $ testContent ='default ' , $ sharedScopeCloser = false )
125
129
{
126
130
$ tokens = $ this ->phpcsFile ->getTokens ();
127
131
@@ -148,13 +152,17 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
148
152
$ this ->assertSame ($ expectedScopeOpener , $ tokens [$ opener ]['scope_opener ' ], 'T_DEFAULT opener scope opener token incorrect ' );
149
153
$ this ->assertSame ($ expectedScopeCloser , $ tokens [$ opener ]['scope_closer ' ], 'T_DEFAULT opener scope closer token incorrect ' );
150
154
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
+ }
158
166
159
167
if (($ opener + 1 ) !== $ closer ) {
160
168
$ end = $ closer ;
@@ -184,12 +192,12 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
184
192
public static function dataSwitchDefault ()
185
193
{
186
194
return [
187
- 'simple_switch_default ' => [
195
+ 'simple_switch_default ' => [
188
196
'testMarker ' => '/* testSimpleSwitchDefault */ ' ,
189
197
'openerOffset ' => 1 ,
190
198
'closerOffset ' => 4 ,
191
199
],
192
- 'simple_switch_default_with_curlies ' => [
200
+ 'simple_switch_default_with_curlies ' => [
193
201
// For a default structure with curly braces, the scope opener
194
202
// will be the open curly and the closer the close curly.
195
203
// However, scope conditions will not be set for open to close,
@@ -199,21 +207,34 @@ public static function dataSwitchDefault()
199
207
'closerOffset ' => 12 ,
200
208
'conditionStop ' => 6 ,
201
209
],
202
- 'switch_default_toplevel ' => [
210
+ 'switch_default_toplevel ' => [
203
211
'testMarker ' => '/* testSwitchDefault */ ' ,
204
212
'openerOffset ' => 1 ,
205
213
'closerOffset ' => 43 ,
206
214
],
207
- 'switch_default_nested_in_match_case ' => [
215
+ 'switch_default_nested_in_match_case ' => [
208
216
'testMarker ' => '/* testSwitchDefaultNestedInMatchCase */ ' ,
209
217
'openerOffset ' => 1 ,
210
218
'closerOffset ' => 20 ,
211
219
],
212
- 'switch_default_nested_in_match_default ' => [
220
+ 'switch_default_nested_in_match_default ' => [
213
221
'testMarker ' => '/* testSwitchDefaultNestedInMatchDefault */ ' ,
214
222
'openerOffset ' => 1 ,
215
223
'closerOffset ' => 18 ,
216
224
],
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
+ ],
217
238
];
218
239
219
240
}//end dataSwitchDefault()
0 commit comments