Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
MisleadingGlobalNamesSniff | |
0.00% |
0 / 27 |
|
0.00% |
0 / 2 |
156 | |
0.00% |
0 / 1 |
register | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
process | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
132 |
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\VariableAnalysis; |
23 | |
24 | use PHP_CodeSniffer\Files\File; |
25 | use PHP_CodeSniffer\Sniffs\Sniff; |
26 | |
27 | /** |
28 | * Detect variables named like $wg[A-Z]* that do not refer to globals. |
29 | * |
30 | * This sniff does not apply to uses of $GLOBALS |
31 | * |
32 | * @author DannyS712 |
33 | */ |
34 | class MisleadingGlobalNamesSniff implements Sniff { |
35 | |
36 | /** |
37 | * @inheritDoc |
38 | */ |
39 | public function register(): array { |
40 | return [ T_FUNCTION ]; |
41 | } |
42 | |
43 | /** |
44 | * @param File $phpcsFile |
45 | * @param int $stackPtr The current token index. |
46 | * @return void |
47 | */ |
48 | public function process( File $phpcsFile, $stackPtr ) { |
49 | $tokens = $phpcsFile->getTokens(); |
50 | if ( !isset( $tokens[$stackPtr]['scope_opener'] ) ) { |
51 | // An interface or abstract function which doesn't have a body |
52 | return; |
53 | } |
54 | |
55 | $scopeOpener = $tokens[$stackPtr]['scope_opener'] + 1; |
56 | $scopeCloser = $tokens[$stackPtr]['scope_closer']; |
57 | |
58 | $endOfGlobal = 0; |
59 | $globalVariables = []; |
60 | $misleadingVariables = []; |
61 | |
62 | for ( $i = $scopeOpener; $i < $scopeCloser; $i++ ) { |
63 | if ( $tokens[$i]['code'] === T_GLOBAL ) { |
64 | $endOfGlobal = $phpcsFile->findEndOfStatement( $i, T_COMMA ); |
65 | } elseif ( $tokens[$i]['code'] === T_VARIABLE ) { |
66 | $variableName = $tokens[$i]['content']; |
67 | if ( strncmp( $variableName, '$wg', 3 ) === 0 ) { |
68 | if ( $i < $endOfGlobal ) { |
69 | $globalVariables[$variableName] = null; |
70 | } elseif ( !array_key_exists( $variableName, $globalVariables ) && |
71 | !isset( $misleadingVariables[$variableName] ) && |
72 | ctype_upper( substr( $variableName, 3, 1 ) ) |
73 | ) { |
74 | $misleadingVariables[$variableName] = $i; |
75 | } |
76 | } |
77 | } |
78 | } |
79 | |
80 | foreach ( $misleadingVariables as $variableName => $stackPtr ) { |
81 | $phpcsFile->addWarning( |
82 | "The 'wg' prefix should only be used with the 'global' keyword", |
83 | $stackPtr, |
84 | 'Misleading' . $variableName |
85 | ); |
86 | } |
87 | } |
88 | } |