@@ -46,6 +46,7 @@ public function register()
46
46
return [
47
47
T_FUNCTION ,
48
48
T_CLOSURE ,
49
+ T_FN ,
49
50
];
50
51
51
52
}//end register()
@@ -75,7 +76,9 @@ public function process(File $phpcsFile, $stackPtr)
75
76
$ openBracket = $ tokens [$ stackPtr ]['parenthesis_opener ' ];
76
77
$ closeBracket = $ tokens [$ stackPtr ]['parenthesis_closer ' ];
77
78
78
- if (strtolower ($ tokens [$ stackPtr ]['content ' ]) === 'function ' ) {
79
+ if (strtolower ($ tokens [$ stackPtr ]['content ' ]) === 'function '
80
+ || strtolower ($ tokens [$ stackPtr ]['content ' ]) === 'fn '
81
+ ) {
79
82
// Must be one space after the FUNCTION keyword.
80
83
if ($ tokens [($ stackPtr + 1 )]['content ' ] === $ phpcsFile ->eolChar ) {
81
84
$ spaces = 'newline ' ;
@@ -86,9 +89,13 @@ public function process(File $phpcsFile, $stackPtr)
86
89
}
87
90
88
91
if ($ spaces !== 1 ) {
89
- $ error = 'Expected 1 space after FUNCTION keyword; %s found ' ;
90
- $ data = [$ spaces ];
91
- $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'SpaceAfterFunction ' , $ data );
92
+ $ error = 'Expected 1 space after %s keyword; %s found ' ;
93
+ $ data = [
94
+ strtoupper ($ tokens [$ stackPtr ]['content ' ]),
95
+ $ spaces ,
96
+ ];
97
+
98
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'SpaceAfterFunction ' , $ data );
92
99
if ($ fix === true ) {
93
100
if ($ spaces === 0 ) {
94
101
$ phpcsFile ->fixer ->addContent ($ stackPtr , ' ' );
@@ -99,9 +106,9 @@ public function process(File $phpcsFile, $stackPtr)
99
106
}
100
107
}//end if
101
108
102
- // Must be no space before the opening parenthesis. For closures, this is
103
- // enforced by the previous check because there is no content between the keywords
104
- // and the opening parenthesis.
109
+ // Must be no space before the opening parenthesis. For closures and arrow functions,
110
+ // this is enforced by the previous check because there is no content between
111
+ // the keywords and the opening parenthesis.
105
112
// Unfinished closures are tokenized as T_FUNCTION however, and can be excluded
106
113
// by checking for the scope_opener.
107
114
if ($ tokens [$ stackPtr ]['code ' ] === T_FUNCTION
@@ -257,6 +264,10 @@ public function isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tok
257
264
*/
258
265
public function processSingleLineDeclaration ($ phpcsFile , $ stackPtr , $ tokens )
259
266
{
267
+ if ($ tokens [$ stackPtr ]['code ' ] === T_FN ) {
268
+ return ;
269
+ }
270
+
260
271
if ($ tokens [$ stackPtr ]['code ' ] === T_CLOSURE ) {
261
272
$ sniff = new OpeningFunctionBraceKernighanRitchieSniff ();
262
273
} else {
@@ -300,12 +311,20 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
300
311
// The opening brace needs to be one space away from the closing parenthesis.
301
312
$ opener = $ tokens [$ stackPtr ]['scope_opener ' ];
302
313
if ($ tokens [$ opener ]['line ' ] !== $ tokens [$ closeBracket ]['line ' ]) {
303
- $ error = 'The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line ' ;
304
- $ fix = $ phpcsFile ->addFixableError ($ error , $ opener , 'NewlineBeforeOpenBrace ' );
314
+ $ error = 'The closing parenthesis and the %s of a multi-line function declaration must be on the same line ' ;
315
+ $ code = 'NewlineBeforeOpenBrace ' ;
316
+ $ data = ['opening brace ' ];
317
+
318
+ if ($ tokens [$ stackPtr ]['code ' ] === T_FN ) {
319
+ $ code = 'NewlineBeforeArrow ' ;
320
+ $ data = ['arrow ' ];
321
+ }
322
+
323
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ opener , $ code , $ data );
305
324
if ($ fix === true ) {
306
325
$ prev = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ opener - 1 ), $ closeBracket , true );
307
326
$ phpcsFile ->fixer ->beginChangeset ();
308
- $ phpcsFile ->fixer ->addContent ($ prev , ' { ' );
327
+ $ phpcsFile ->fixer ->addContent ($ prev , ' ' . $ tokens [ $ opener ][ ' content ' ] );
309
328
310
329
// If the opener is on a line by itself, removing it will create
311
330
// an empty line, so just remove the entire line instead.
@@ -340,8 +359,18 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
340
359
}
341
360
342
361
if ($ length !== 1 ) {
343
- $ error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found %s spaces ' ;
344
- $ fix = $ phpcsFile ->addFixableError ($ error , ($ opener - 1 ), 'SpaceBeforeOpenBrace ' , [$ length ]);
362
+ $ error = 'There must be a single space between the closing parenthesis and the %s of a multi-line function declaration; found %s spaces ' ;
363
+ $ code = 'SpaceBeforeOpenBrace ' ;
364
+ $ data = ['opening brace ' ];
365
+
366
+ if ($ tokens [$ stackPtr ]['code ' ] === T_FN ) {
367
+ $ code = 'SpaceBeforeArrow ' ;
368
+ $ data = ['arrow ' ];
369
+ }
370
+
371
+ $ data [] = $ length ;
372
+
373
+ $ fix = $ phpcsFile ->addFixableError ($ error , ($ opener - 1 ), $ code , $ data );
345
374
if ($ fix === true ) {
346
375
if ($ length === 0 ) {
347
376
$ phpcsFile ->fixer ->addContentBefore ($ opener , ' ' );
0 commit comments