@@ -107,19 +107,23 @@ public static function dataMatchDefault()
107
107
* Note: Cases and default structures within a switch control structure *do* get case/default scope
108
108
* conditions.
109
109
*
110
- * @param string $testMarker The comment prefacing the target token.
111
- * @param int $openerOffset The expected offset of the scope opener in relation to the testMarker.
112
- * @param int $closerOffset The expected offset of the scope closer in relation to the testMarker.
113
- * @param int|null $conditionStop The expected offset in relation to the testMarker, at which tokens stop
114
- * having T_DEFAULT as a scope condition.
115
- * @param string $testContent The token content to look for.
110
+ * @param string $testMarker The comment prefacing the target token.
111
+ * @param int $openerOffset The expected offset of the scope opener in relation to the testMarker.
112
+ * @param int $closerOffset The expected offset of the scope closer in relation to the testMarker.
113
+ * @param int|null $conditionStop The expected offset in relation to the testMarker, at which tokens stop
114
+ * having T_DEFAULT as a scope condition.
115
+ * @param string $testContent The token content to look for.
116
+ * @param bool $sharedScopeCloser Whether to skip checking for the `scope_condition` of the
117
+ * scope closer. Needed when the default and switch
118
+ * structures share a scope closer. See
119
+ * https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/810.
116
120
*
117
121
* @dataProvider dataSwitchDefault
118
122
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
119
123
*
120
124
* @return void
121
125
*/
122
- public function testSwitchDefault ($ testMarker , $ openerOffset , $ closerOffset , $ conditionStop =null , $ testContent ='default ' )
126
+ public function testSwitchDefault ($ testMarker , $ openerOffset , $ closerOffset , $ conditionStop =null , $ testContent ='default ' , $ sharedScopeCloser = false )
123
127
{
124
128
$ tokens = $ this ->phpcsFile ->getTokens ();
125
129
@@ -146,13 +150,17 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
146
150
$ this ->assertSame ($ expectedScopeOpener , $ tokens [$ opener ]['scope_opener ' ], 'T_DEFAULT opener scope opener token incorrect ' );
147
151
$ this ->assertSame ($ expectedScopeCloser , $ tokens [$ opener ]['scope_closer ' ], 'T_DEFAULT opener scope closer token incorrect ' );
148
152
149
- $ closer = $ tokenArray ['scope_closer ' ];
150
- $ this ->assertArrayHasKey ('scope_condition ' , $ tokens [$ closer ], 'Closer scope condition is not set ' );
151
- $ this ->assertArrayHasKey ('scope_opener ' , $ tokens [$ closer ], 'Closer scope opener is not set ' );
152
- $ this ->assertArrayHasKey ('scope_closer ' , $ tokens [$ closer ], 'Closer scope closer is not set ' );
153
- $ this ->assertSame ($ token , $ tokens [$ closer ]['scope_condition ' ], 'Closer scope condition is not the T_DEFAULT token ' );
154
- $ this ->assertSame ($ expectedScopeOpener , $ tokens [$ closer ]['scope_opener ' ], 'T_DEFAULT closer scope opener token incorrect ' );
155
- $ this ->assertSame ($ expectedScopeCloser , $ tokens [$ closer ]['scope_closer ' ], 'T_DEFAULT closer scope closer token incorrect ' );
153
+ $ closer = $ expectedScopeCloser ;
154
+
155
+ if ($ sharedScopeCloser === false ) {
156
+ $ closer = $ tokenArray ['scope_closer ' ];
157
+ $ this ->assertArrayHasKey ('scope_condition ' , $ tokens [$ closer ], 'Closer scope condition is not set ' );
158
+ $ this ->assertArrayHasKey ('scope_opener ' , $ tokens [$ closer ], 'Closer scope opener is not set ' );
159
+ $ this ->assertArrayHasKey ('scope_closer ' , $ tokens [$ closer ], 'Closer scope closer is not set ' );
160
+ $ this ->assertSame ($ token , $ tokens [$ closer ]['scope_condition ' ], 'Closer scope condition is not the T_DEFAULT token ' );
161
+ $ this ->assertSame ($ expectedScopeOpener , $ tokens [$ closer ]['scope_opener ' ], 'T_DEFAULT closer scope opener token incorrect ' );
162
+ $ this ->assertSame ($ expectedScopeCloser , $ tokens [$ closer ]['scope_closer ' ], 'T_DEFAULT closer scope closer token incorrect ' );
163
+ }
156
164
157
165
if (($ opener + 1 ) !== $ closer ) {
158
166
$ end = $ closer ;
@@ -182,12 +190,12 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
182
190
public static function dataSwitchDefault ()
183
191
{
184
192
return [
185
- 'simple_switch_default ' => [
193
+ 'simple_switch_default ' => [
186
194
'testMarker ' => '/* testSimpleSwitchDefault */ ' ,
187
195
'openerOffset ' => 1 ,
188
196
'closerOffset ' => 4 ,
189
197
],
190
- 'simple_switch_default_with_curlies ' => [
198
+ 'simple_switch_default_with_curlies ' => [
191
199
// For a default structure with curly braces, the scope opener
192
200
// will be the open curly and the closer the close curly.
193
201
// However, scope conditions will not be set for open to close,
@@ -197,21 +205,34 @@ public static function dataSwitchDefault()
197
205
'closerOffset ' => 12 ,
198
206
'conditionStop ' => 6 ,
199
207
],
200
- 'switch_default_toplevel ' => [
208
+ 'switch_default_toplevel ' => [
201
209
'testMarker ' => '/* testSwitchDefault */ ' ,
202
210
'openerOffset ' => 1 ,
203
211
'closerOffset ' => 43 ,
204
212
],
205
- 'switch_default_nested_in_match_case ' => [
213
+ 'switch_default_nested_in_match_case ' => [
206
214
'testMarker ' => '/* testSwitchDefaultNestedInMatchCase */ ' ,
207
215
'openerOffset ' => 1 ,
208
216
'closerOffset ' => 20 ,
209
217
],
210
- 'switch_default_nested_in_match_default ' => [
218
+ 'switch_default_nested_in_match_default ' => [
211
219
'testMarker ' => '/* testSwitchDefaultNestedInMatchDefault */ ' ,
212
220
'openerOffset ' => 1 ,
213
221
'closerOffset ' => 18 ,
214
222
],
223
+ 'switch_and_default_sharing_scope_closer ' => [
224
+ 'testMarker ' => '/* testSwitchAndDefaultSharingScopeCloser */ ' ,
225
+ 'openerOffset ' => 1 ,
226
+ 'closerOffset ' => 10 ,
227
+ 'conditionStop ' => null ,
228
+ 'testContent ' => 'default ' ,
229
+ 'sharedScopeCloser ' => true ,
230
+ ],
231
+ 'switch_and_default_with_nested_if_with_and_without_braces ' => [
232
+ 'testMarker ' => '/* testSwitchDefaultNestedIfWithAndWithoutBraces */ ' ,
233
+ 'openerOffset ' => 1 ,
234
+ 'closerOffset ' => 48 ,
235
+ ],
215
236
];
216
237
217
238
}//end dataSwitchDefault()
0 commit comments