11
11
12
12
use WordPressCS \WordPress \Sniff ;
13
13
use PHP_CodeSniffer \Util \Tokens ;
14
- use PHPCSUtils \Utils \UseStatements ;
15
14
16
15
/**
17
16
* Enforces spacing around logical operators and assignments, based upon Squiz code.
23
22
* @since 0.3.0 This sniff now has the ability to fix most errors it flags.
24
23
* @since 0.7.0 This class now extends the WordPressCS native `Sniff` class.
25
24
* @since 0.13.0 Class name changed: this class is now namespaced.
25
+ * @since 3.0.0 Checks related to function declarations have been removed from this sniff.
26
26
*
27
27
* Last synced with base class 2017-01-15 at commit b024ad84656c37ef5733c6998ebc1e60957b2277.
28
28
* Note: This class has diverged quite far from the original. All the same, checking occasionally
@@ -52,33 +52,19 @@ final class ControlStructureSpacingSniff extends Sniff {
52
52
*/
53
53
public $ space_before_colon = 'required ' ;
54
54
55
- /**
56
- * How many spaces should be between a T_CLOSURE and T_OPEN_PARENTHESIS.
57
- *
58
- * `function[*]() {...}`
59
- *
60
- * @since 0.7.0
61
- *
62
- * @var int
63
- */
64
- public $ spaces_before_closure_open_paren = -1 ;
65
-
66
55
/**
67
56
* Tokens for which to ignore extra space on the inside of parenthesis.
68
57
*
69
- * For functions, this is already checked by the Squiz.Functions.FunctionDeclarationArgumentSpacing sniff.
70
58
* For do / else / try, there are no parenthesis, so skip it.
71
59
*
72
60
* @since 0.11.0
73
61
*
74
62
* @var array
75
63
*/
76
64
private $ ignore_extra_space_after_open_paren = array (
77
- \T_FUNCTION => true ,
78
- \T_CLOSURE => true ,
79
- \T_DO => true ,
80
- \T_ELSE => true ,
81
- \T_TRY => true ,
65
+ \T_DO => true ,
66
+ \T_ELSE => true ,
67
+ \T_TRY => true ,
82
68
);
83
69
84
70
/**
@@ -96,9 +82,6 @@ public function register() {
96
82
\T_DO ,
97
83
\T_ELSE ,
98
84
\T_ELSEIF ,
99
- \T_FUNCTION ,
100
- \T_CLOSURE ,
101
- \T_USE ,
102
85
\T_TRY ,
103
86
\T_CATCH ,
104
87
);
@@ -112,18 +95,8 @@ public function register() {
112
95
* @return void
113
96
*/
114
97
public function process_token ( $ stackPtr ) {
115
- $ this ->spaces_before_closure_open_paren = (int ) $ this ->spaces_before_closure_open_paren ;
116
-
117
- if ( \T_USE === $ this ->tokens [ $ stackPtr ]['code ' ]
118
- && UseStatements::isClosureUse ( $ this ->phpcsFile , $ stackPtr ) === false
119
- ) {
120
- return ;
121
- }
122
-
123
98
if ( isset ( $ this ->tokens [ ( $ stackPtr + 1 ) ] ) && \T_WHITESPACE !== $ this ->tokens [ ( $ stackPtr + 1 ) ]['code ' ]
124
99
&& ! ( \T_ELSE === $ this ->tokens [ $ stackPtr ]['code ' ] && \T_COLON === $ this ->tokens [ ( $ stackPtr + 1 ) ]['code ' ] )
125
- && ! ( \T_CLOSURE === $ this ->tokens [ $ stackPtr ]['code ' ]
126
- && 0 >= $ this ->spaces_before_closure_open_paren )
127
100
) {
128
101
$ error = 'Space after opening control structure is required ' ;
129
102
$ fix = $ this ->phpcsFile ->addFixableError ( $ error , $ stackPtr , 'NoSpaceAfterStructureOpen ' );
@@ -133,12 +106,8 @@ public function process_token( $stackPtr ) {
133
106
}
134
107
}
135
108
136
- if ( ! isset ( $ this ->tokens [ $ stackPtr ]['scope_closer ' ] ) ) {
137
-
138
- if ( \T_USE === $ this ->tokens [ $ stackPtr ]['code ' ] ) {
139
- $ scopeOpener = $ this ->phpcsFile ->findNext ( \T_OPEN_CURLY_BRACKET , ( $ stackPtr + 1 ) );
140
- $ scopeCloser = $ this ->tokens [ $ scopeOpener ]['scope_closer ' ];
141
- } elseif ( \T_WHILE !== $ this ->tokens [ $ stackPtr ]['code ' ] ) {
109
+ if ( ! isset ( $ this ->tokens [ $ stackPtr ]['scope_opener ' ], $ this ->tokens [ $ stackPtr ]['scope_closer ' ] ) ) {
110
+ if ( \T_WHILE !== $ this ->tokens [ $ stackPtr ]['code ' ] ) {
142
111
return ;
143
112
}
144
113
} else {
@@ -174,102 +143,15 @@ public function process_token( $stackPtr ) {
174
143
175
144
$ parenthesisOpener = $ this ->phpcsFile ->findNext ( Tokens::$ emptyTokens , ( $ stackPtr + 1 ), null , true );
176
145
177
- // If this is a function declaration.
178
- if ( \T_FUNCTION === $ this ->tokens [ $ stackPtr ]['code ' ] ) {
179
-
180
- if ( \T_STRING === $ this ->tokens [ $ parenthesisOpener ]['code ' ] ) {
181
-
182
- $ function_name_ptr = $ parenthesisOpener ;
183
-
184
- } elseif ( \T_BITWISE_AND === $ this ->tokens [ $ parenthesisOpener ]['code ' ] ) {
185
-
186
- // This function returns by reference, i.e. `function &function_name() {}`.
187
- $ parenthesisOpener = $ this ->phpcsFile ->findNext (
188
- Tokens::$ emptyTokens ,
189
- ( $ parenthesisOpener + 1 ),
190
- null ,
191
- true
192
- );
193
- $ function_name_ptr = $ parenthesisOpener ;
194
- }
195
-
196
- if ( isset ( $ function_name_ptr ) ) {
197
- $ parenthesisOpener = $ this ->phpcsFile ->findNext (
198
- Tokens::$ emptyTokens ,
199
- ( $ parenthesisOpener + 1 ),
200
- null ,
201
- true
202
- );
203
-
204
- // Checking space between name and open parentheses, i.e. `function my_function[*](...) {}`.
205
- if ( ( $ function_name_ptr + 1 ) !== $ parenthesisOpener ) {
206
-
207
- $ error = 'Space between function name and opening parenthesis is prohibited. ' ;
208
- $ fix = $ this ->phpcsFile ->addFixableError (
209
- $ error ,
210
- $ stackPtr ,
211
- 'SpaceBeforeFunctionOpenParenthesis ' ,
212
- array ( $ this ->tokens [ ( $ function_name_ptr + 1 ) ]['content ' ] )
213
- );
214
-
215
- if ( true === $ fix ) {
216
- $ this ->phpcsFile ->fixer ->replaceToken ( ( $ function_name_ptr + 1 ), '' );
217
- }
218
- }
219
- }
220
- } elseif ( \T_CLOSURE === $ this ->tokens [ $ stackPtr ]['code ' ] ) {
221
-
222
- // Check if there is a use () statement.
223
- if ( isset ( $ this ->tokens [ $ parenthesisOpener ]['parenthesis_closer ' ] ) ) {
224
-
225
- $ usePtr = $ this ->phpcsFile ->findNext (
226
- Tokens::$ emptyTokens ,
227
- ( $ this ->tokens [ $ parenthesisOpener ]['parenthesis_closer ' ] + 1 ),
228
- null ,
229
- true ,
230
- null ,
231
- true
232
- );
233
-
234
- // If it is, we set that as the "scope opener".
235
- if ( \T_USE === $ this ->tokens [ $ usePtr ]['code ' ] ) {
236
- $ scopeOpener = $ usePtr ;
237
- }
238
- }
239
- }
240
-
241
146
if ( \T_COLON !== $ this ->tokens [ $ parenthesisOpener ]['code ' ]
242
- && \ T_FUNCTION !== $ this -> tokens [ $ stackPtr ][ ' code ' ]
147
+ && ( $ stackPtr + 1 ) === $ parenthesisOpener
243
148
) {
149
+ // Checking space between keyword and open parenthesis, i.e. `if[*](...) {}`.
150
+ $ error = 'No space before opening parenthesis is prohibited ' ;
151
+ $ fix = $ this ->phpcsFile ->addFixableError ( $ error , $ stackPtr , 'NoSpaceBeforeOpenParenthesis ' );
244
152
245
- if ( \T_CLOSURE === $ this ->tokens [ $ stackPtr ]['code ' ]
246
- && 0 === $ this ->spaces_before_closure_open_paren
247
- ) {
248
-
249
- if ( ( $ stackPtr + 1 ) !== $ parenthesisOpener ) {
250
- // Checking space between keyword and open parenthesis, i.e. `function[*](...) {}`.
251
- $ error = 'Space before closure opening parenthesis is prohibited ' ;
252
- $ fix = $ this ->phpcsFile ->addFixableError ( $ error , $ stackPtr , 'SpaceBeforeClosureOpenParenthesis ' );
253
-
254
- if ( true === $ fix ) {
255
- $ this ->phpcsFile ->fixer ->replaceToken ( ( $ stackPtr + 1 ), '' );
256
- }
257
- }
258
- } elseif (
259
- (
260
- \T_CLOSURE !== $ this ->tokens [ $ stackPtr ]['code ' ]
261
- || 1 === $ this ->spaces_before_closure_open_paren
262
- )
263
- && ( $ stackPtr + 1 ) === $ parenthesisOpener
264
- ) {
265
-
266
- // Checking space between keyword and open parenthesis, i.e. `if[*](...) {}`.
267
- $ error = 'No space before opening parenthesis is prohibited ' ;
268
- $ fix = $ this ->phpcsFile ->addFixableError ( $ error , $ stackPtr , 'NoSpaceBeforeOpenParenthesis ' );
269
-
270
- if ( true === $ fix ) {
271
- $ this ->phpcsFile ->fixer ->addContent ( $ stackPtr , ' ' );
272
- }
153
+ if ( true === $ fix ) {
154
+ $ this ->phpcsFile ->fixer ->addContent ( $ stackPtr , ' ' );
273
155
}
274
156
}
275
157
@@ -292,7 +174,7 @@ public function process_token( $stackPtr ) {
292
174
293
175
if ( \T_CLOSE_PARENTHESIS !== $ this ->tokens [ ( $ parenthesisOpener + 1 ) ]['code ' ] ) {
294
176
if ( \T_WHITESPACE !== $ this ->tokens [ ( $ parenthesisOpener + 1 ) ]['code ' ] ) {
295
- // Checking space directly after the open parenthesis, i.e. `$value = my_function ([*]...)`.
177
+ // Checking space directly after the open parenthesis, i.e. `if ([*]...) {} `.
296
178
$ error = 'No space after opening parenthesis is prohibited ' ;
297
179
$ fix = $ this ->phpcsFile ->addFixableError ( $ error , $ stackPtr , 'NoSpaceAfterOpenParenthesis ' );
298
180
@@ -351,11 +233,6 @@ public function process_token( $stackPtr ) {
351
233
}
352
234
353
235
if ( \T_WHITESPACE !== $ this ->tokens [ ( $ parenthesisCloser + 1 ) ]['code ' ]
354
- && ! ( // Do NOT flag : immediately following ) for return types declarations.
355
- \T_COLON === $ this ->tokens [ ( $ parenthesisCloser + 1 ) ]['code ' ]
356
- && ( isset ( $ this ->tokens [ $ parenthesisCloser ]['parenthesis_owner ' ] ) === false
357
- || in_array ( $ this ->tokens [ $ this ->tokens [ $ parenthesisCloser ]['parenthesis_owner ' ] ]['code ' ], array ( \T_FUNCTION , \T_CLOSURE ), true ) )
358
- )
359
236
&& ( isset ( $ scopeOpener ) && \T_COLON !== $ this ->tokens [ $ scopeOpener ]['code ' ] )
360
237
) {
361
238
$ error = 'Space between opening control structure and closing parenthesis is required ' ;
@@ -367,10 +244,7 @@ public function process_token( $stackPtr ) {
367
244
}
368
245
}
369
246
370
- // Ignore this for function declarations. Handled by the OpeningFunctionBraceKernighanRitchie sniff.
371
- if ( \T_FUNCTION !== $ this ->tokens [ $ stackPtr ]['code ' ]
372
- && \T_CLOSURE !== $ this ->tokens [ $ stackPtr ]['code ' ]
373
- && isset ( $ this ->tokens [ $ parenthesisOpener ]['parenthesis_owner ' ] )
247
+ if ( isset ( $ this ->tokens [ $ parenthesisOpener ]['parenthesis_owner ' ] )
374
248
&& ( isset ( $ scopeOpener )
375
249
&& $ this ->tokens [ $ parenthesisCloser ]['line ' ] !== $ this ->tokens [ $ scopeOpener ]['line ' ] )
376
250
) {
@@ -392,7 +266,6 @@ public function process_token( $stackPtr ) {
392
266
} elseif ( \T_WHITESPACE === $ this ->tokens [ ( $ parenthesisCloser + 1 ) ]['code ' ]
393
267
&& ' ' !== $ this ->tokens [ ( $ parenthesisCloser + 1 ) ]['content ' ]
394
268
) {
395
-
396
269
// Checking space between the close parenthesis and the open brace, i.e. `if (...) [*]{}`.
397
270
$ error = 'Expected exactly one space between closing parenthesis and opening control structure; "%s" found. ' ;
398
271
$ fix = $ this ->phpcsFile ->addFixableError (
0 commit comments