Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
OpeningKeywordParenthesisSniff
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 register
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 process
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
42
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\WhiteSpace;
22
23use PHP_CodeSniffer\Files\File;
24use PHP_CodeSniffer\Sniffs\Sniff;
25
26class OpeningKeywordParenthesisSniff implements Sniff {
27    /**
28     * @inheritDoc
29     */
30    public function register(): array {
31        return [
32            T_EMPTY,
33            T_EVAL,
34            T_EXIT,
35            T_ISSET,
36            T_LIST,
37            T_UNSET,
38            // also check for array(), when not replaced with short syntax
39            T_ARRAY,
40        ];
41    }
42
43    /**
44     * @param File $phpcsFile
45     * @param int $stackPtr Index of registered keywords
46     * @return void
47     */
48    public function process( File $phpcsFile, $stackPtr ) {
49        $tokens = $phpcsFile->getTokens();
50        $next = $stackPtr + 1;
51
52        $openParenthesis = $phpcsFile->findNext( T_WHITESPACE, $next, null, true );
53        if ( $openParenthesis === false ||
54            $tokens[$openParenthesis]['code'] !== T_OPEN_PARENTHESIS
55        ) {
56            // no parenthesis found
57            return;
58        }
59
60        if ( $next === $openParenthesis ) {
61            // no whitespaces found
62            return;
63        }
64
65        $whitespaces = $phpcsFile->getTokensAsString( $next, $openParenthesis - $next );
66        $fix = $phpcsFile->addFixableError(
67            'Expected no space before opening parenthesis; found %s',
68            $openParenthesis,
69            'WrongWhitespaceBeforeParenthesis',
70            [ strlen( $whitespaces ) ]
71        );
72        if ( $fix ) {
73            $phpcsFile->fixer->beginChangeset();
74            for ( $i = $next; $i < $openParenthesis; $i++ ) {
75                $phpcsFile->fixer->replaceToken( $i, '' );
76            }
77            $phpcsFile->fixer->endChangeset();
78        }
79    }
80}