Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
SpaceBeforeBracketSniff | |
0.00% |
0 / 21 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
register | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
process | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * http://www.gnu.org/copyleft/gpl.html |
18 | * |
19 | * @file |
20 | */ |
21 | |
22 | namespace MediaWiki\Sniffs\WhiteSpace; |
23 | |
24 | use PHP_CodeSniffer\Files\File; |
25 | use PHP_CodeSniffer\Sniffs\Sniff; |
26 | |
27 | /** |
28 | * Sniff to warn when there is a space between a variable expression and [ |
29 | * |
30 | * @author DannyS712 |
31 | */ |
32 | class SpaceBeforeBracketSniff implements Sniff { |
33 | |
34 | /** |
35 | * @inheritDoc |
36 | */ |
37 | public function register(): array { |
38 | return [ T_OPEN_SQUARE_BRACKET ]; |
39 | } |
40 | |
41 | /** |
42 | * @param File $phpcsFile |
43 | * @param int $stackPtr The current token index. |
44 | * @return void |
45 | */ |
46 | public function process( File $phpcsFile, $stackPtr ) { |
47 | $tokens = $phpcsFile->getTokens(); |
48 | |
49 | // Check if the open bracket is preceded by whitespace |
50 | $priorToken = $tokens[$stackPtr - 1]; |
51 | if ( $priorToken['code'] !== T_WHITESPACE ) { |
52 | return; |
53 | } |
54 | |
55 | // Skip newlines |
56 | $lastNonWhitespace = $phpcsFile->findPrevious( |
57 | T_WHITESPACE, |
58 | $stackPtr - 1, |
59 | null, |
60 | true |
61 | ); |
62 | if ( $lastNonWhitespace && |
63 | $tokens[$lastNonWhitespace]['line'] !== $tokens[$stackPtr]['line'] |
64 | ) { |
65 | return; |
66 | } |
67 | |
68 | $fix = $phpcsFile->addFixableWarning( |
69 | 'Do not include whitespace between variable expression and array offset', |
70 | $stackPtr - 1, |
71 | 'SpaceFound' |
72 | ); |
73 | |
74 | if ( $fix ) { |
75 | $phpcsFile->fixer->replaceToken( $stackPtr - 1, '' ); |
76 | } |
77 | } |
78 | } |