-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathFunctionClosingBraceSpaceSniff.php
164 lines (137 loc) · 5.85 KB
/
FunctionClosingBraceSpaceSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* Checks that there is one empty line before the closing brace of a function.
*
* @author Greg Sherwood <[email protected]>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
class FunctionClosingBraceSpaceSniff implements Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = [
'PHP',
'JS',
];
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [
T_FUNCTION,
T_CLOSURE,
];
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
// Probably an interface method.
return;
}
$closeBrace = $tokens[$stackPtr]['scope_closer'];
$prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBrace - 1), null, true);
// Special case for empty JS functions.
if ($phpcsFile->tokenizerType === 'JS' && $prevContent === $tokens[$stackPtr]['scope_opener']) {
// In this case, the opening and closing brace must be
// right next to each other.
if ($tokens[$stackPtr]['scope_closer'] !== ($tokens[$stackPtr]['scope_opener'] + 1)) {
$error = 'The opening and closing braces of empty functions must be directly next to each other; e.g., function () {}';
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'SpacingBetween');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($tokens[$stackPtr]['scope_opener'] + 1); $i < $closeBrace; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}
}
return;
}
$nestedFunction = false;
if ($phpcsFile->hasCondition($stackPtr, [T_FUNCTION, T_CLOSURE]) === true
|| isset($tokens[$stackPtr]['nested_parenthesis']) === true
) {
$nestedFunction = true;
}
$braceLine = $tokens[$closeBrace]['line'];
$prevLine = $tokens[$prevContent]['line'];
$found = ($braceLine - $prevLine - 1);
if ($nestedFunction === true) {
if ($found < 0) {
$error = 'Closing brace of nested function must be on a new line';
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'ContentBeforeClose');
if ($fix === true) {
$phpcsFile->fixer->addNewlineBefore($closeBrace);
}
} else if ($found > 0) {
$error = 'Expected 0 blank lines before closing brace of nested function; %s found';
$data = [$found];
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'SpacingBeforeNestedClose', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
$changeMade = false;
for ($i = ($prevContent + 1); $i < $closeBrace; $i++) {
// Try and maintain indentation.
if ($tokens[$i]['line'] === ($braceLine - 1)) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
$changeMade = true;
}
// Special case for when the last content contains the newline
// token as well, like with a comment.
if ($changeMade === false) {
$phpcsFile->fixer->replaceToken(($prevContent + 1), '');
}
$phpcsFile->fixer->endChangeset();
}//end if
}//end if
} else {
if ($found !== 1) {
if ($found < 0) {
$found = 0;
}
$error = 'Expected 1 blank line before closing function brace; %s found';
$data = [$found];
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'SpacingBeforeClose', $data);
if ($fix === true) {
if ($found > 1) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($prevContent + 1); $i < ($closeBrace - 1); $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->replaceToken($i, $phpcsFile->eolChar);
$phpcsFile->fixer->endChangeset();
} else {
// Try and maintain indentation.
if ($tokens[($closeBrace - 1)]['code'] === T_WHITESPACE) {
$phpcsFile->fixer->addNewlineBefore($closeBrace - 1);
} else {
$phpcsFile->fixer->addNewlineBefore($closeBrace);
}
}
}
}//end if
}//end if
}//end process()
}//end class