Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeprecatedGlobalVariablesSniff
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 register
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 process
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * Detect use of deprecated global variables.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23namespace MediaWiki\Sniffs\Usage;
24
25use MediaWiki\Sniffs\Utils\ExtensionInfo;
26use PHP_CodeSniffer\Files\File;
27use PHP_CodeSniffer\Sniffs\Sniff;
28
29class DeprecatedGlobalVariablesSniff implements Sniff {
30
31    /**
32     * Deprecated global and last
33     * MW version that old global can still be used
34     */
35    private const DEPRECATED_GLOBALS = [
36        // Deprecation done (T89459)
37        '$wgAuth' => '1.27',
38        // Deprecation done (T160815)
39        '$wgContLang' => '1.32',
40        // Deprecation done (T160811)
41        '$wgParser' => '1.32',
42        // Deprecation done (T159284)
43        '$wgTitle' => '1.19',
44        // Deprecation done (no task)
45        '$parserMemc' => '1.30',
46        // Deprecation done (T160813)
47        '$wgMemc' => '1.35',
48        // Deprecation done (T159299)
49        '$wgUser' => '1.35',
50        // Deprecation done (T212738)
51        '$wgVersion' => '1.35',
52        // Deprecation done (T331602)
53        '$wgHooks' => '1.40',
54        // Deprecation done (T313841)
55        '$wgCommandLineMode' => '1.42',
56
57        // Deprecation planned (T212739)
58        // '$wgConf' => '',
59        // Deprecation planned (T160814)
60        // '$wgLang' => '',
61        // Deprecation planned (T160812)
62        // '$wgOut' => '',
63        // Deprecation planned (T160810)
64        // '$wgRequest' => '',
65    ];
66
67    /**
68     * @inheritDoc
69     */
70    public function register(): array {
71        return [ T_GLOBAL ];
72    }
73
74    /**
75     * @param File $phpcsFile
76     * @param int $stackPtr The current token index.
77     * @return void
78     */
79    public function process( File $phpcsFile, $stackPtr ) {
80        $tokens = $phpcsFile->getTokens();
81
82        $next = $stackPtr++;
83        $endOfGlobal = $phpcsFile->findEndOfStatement( $next, T_COMMA );
84        $extensionInfo = ExtensionInfo::newFromFile( $phpcsFile );
85
86        for ( ; $next < $endOfGlobal; $next++ ) {
87            if ( $tokens[$next]['code'] !== T_VARIABLE ) {
88                continue;
89            }
90
91            $globalVar = $tokens[$next]['content'];
92            if ( !isset( self::DEPRECATED_GLOBALS[$globalVar] ) ||
93                $extensionInfo->supportsMediaWiki( self::DEPRECATED_GLOBALS[$globalVar] )
94            ) {
95                continue;
96            }
97
98            $phpcsFile->addWarning(
99                "Deprecated global $globalVar used",
100                $next,
101                'Deprecated' . $globalVar
102            );
103        }
104    }
105}