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