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
DeprecatedConstantUsageSniff
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 register
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 process
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Sniffs\Usage;
22
23use MediaWiki\Sniffs\Utils\ExtensionInfo;
24use PHP_CodeSniffer\Files\File;
25use PHP_CodeSniffer\Sniffs\Sniff;
26
27class DeprecatedConstantUsageSniff implements Sniff {
28
29    /**
30     * Deprecated constant => Replacement, and last
31     * MW version that old constant should still be used
32     */
33    private const DEPRECATED_CONSTANTS = [
34        'DB_SLAVE' => [
35            'replace' => 'DB_REPLICA',
36            'version' => '1.27.3',
37        ],
38        'DB_MASTER' => [
39            'replace' => 'DB_PRIMARY',
40            'version' => '1.35.2',
41        ],
42        'NS_IMAGE' => [
43            'replace' => 'NS_FILE',
44            'version' => '1.13',
45        ],
46        'NS_IMAGE_TALK' => [
47            'replace' => 'NS_FILE_TALK',
48            'version' => '1.13',
49        ],
50        'DO_MAINTENANCE' => [
51            'replace' => 'RUN_MAINTENANCE_IF_MAIN',
52            'version' => '1.16.3',
53        ]
54    ];
55
56    /**
57     * @inheritDoc
58     */
59    public function register(): array {
60        return [
61            T_STRING,
62        ];
63    }
64
65    /**
66     * Check for any deprecated constants
67     *
68     * @param File $phpcsFile Current file
69     * @param int $stackPtr Position
70     * @return void
71     */
72    public function process( File $phpcsFile, $stackPtr ) {
73        $token = $phpcsFile->getTokens()[$stackPtr];
74        $current = $token['content'];
75        if ( isset( self::DEPRECATED_CONSTANTS[$current] ) ) {
76            $extensionInfo = ExtensionInfo::newFromFile( $phpcsFile );
77            if ( $extensionInfo->supportsMediaWiki( self::DEPRECATED_CONSTANTS[$current]['version'] ) ) {
78                return;
79            }
80            $fix = $phpcsFile->addFixableWarning(
81                'Deprecated constant %s used',
82                $stackPtr,
83                $current,
84                [ $current ]
85            );
86            if ( $fix ) {
87                $phpcsFile->fixer->replaceToken( $stackPtr, self::DEPRECATED_CONSTANTS[$current]['replace'] );
88            }
89        }
90    }
91}