Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 6 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
SuperGlobalsUsageSniff | |
0.00% |
0 / 6 |
|
0.00% |
0 / 2 |
12 | |
0.00% |
0 / 1 |
register | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
process | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * Do not access php superglobals like $__GET,$__POST,$__SERVER. |
5 | * Fail: $_GET['id'] |
6 | * Fail: $_POST['user'] |
7 | * Fail: $_SERVER['ip'] |
8 | */ |
9 | |
10 | namespace MediaWiki\Sniffs\Usage; |
11 | |
12 | use PHP_CodeSniffer\Files\File; |
13 | use PHP_CodeSniffer\Sniffs\Sniff; |
14 | |
15 | class SuperGlobalsUsageSniff implements Sniff { |
16 | |
17 | // The list of forbidden superglobals |
18 | // As per https://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP#Global_objects |
19 | private const FORBIDDEN_SUPER_GLOBALS = [ |
20 | '$_POST' => true, |
21 | '$_GET' => true, |
22 | '$_FILES' => true, |
23 | ]; |
24 | |
25 | /** |
26 | * @inheritDoc |
27 | */ |
28 | public function register(): array { |
29 | return [ T_VARIABLE ]; |
30 | } |
31 | |
32 | /** |
33 | * @param File $phpcsFile |
34 | * @param int $stackPtr The current token index. |
35 | * @return void |
36 | */ |
37 | public function process( File $phpcsFile, $stackPtr ) { |
38 | $tokens = $phpcsFile->getTokens(); |
39 | $currentToken = $tokens[$stackPtr]; |
40 | if ( isset( self::FORBIDDEN_SUPER_GLOBALS[$currentToken['content']] ) ) { |
41 | $error = '"%s" superglobals should not be accessed.'; |
42 | $phpcsFile->addError( $error, $stackPtr, 'SuperGlobals', [ $currentToken['content'] ] ); |
43 | } |
44 | } |
45 | } |