Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
VariadicArgumentSniff
0.00% covered (danger)
0.00%
0 / 18
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 / 17
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * Warn if any comment containing hints of a variadic argument is found within the arguments list.
4 * This includes comment only containing "...", or containing variable names preceded by "...",
5 * or ",...".
6 * Actual variadic arguments should be used instead.
7 */
8
9namespace MediaWiki\Sniffs\Commenting;
10
11use PHP_CodeSniffer\Files\File;
12use PHP_CodeSniffer\Sniffs\Sniff;
13
14class VariadicArgumentSniff implements Sniff {
15
16    /**
17     * @inheritDoc
18     */
19    public function register(): array {
20        return [ T_FUNCTION ];
21    }
22
23    /**
24     * Processes this test, when one of its tokens is encountered.
25     *
26     * @param File $phpcsFile The file being scanned.
27     * @param int $stackPtr The position of the current token in the stack passed in $tokens.
28     *
29     * @return void
30     */
31    public function process( File $phpcsFile, $stackPtr ) {
32        $tokens = $phpcsFile->getTokens();
33        if ( !isset( $tokens[$stackPtr]['parenthesis_opener'] ) ) {
34            // Live coding
35            return;
36        }
37
38        $end = $tokens[$stackPtr]['parenthesis_closer'];
39        $commentPos = $phpcsFile->findNext( T_COMMENT, $tokens[$stackPtr]['parenthesis_opener'] + 1, $end );
40        while ( $commentPos !== false ) {
41            $comment = $tokens[$commentPos]['content'];
42            if ( str_starts_with( $comment, '/*' ) ) {
43                $content = substr( $comment, 2, -2 );
44                if ( preg_match( '/^[,\s]*\.\.\.\s*$|\.\.\.\$|\$[a-z_][a-z0-9_]*,\.\.\./i', $content ) ) {
45                    // An autofix would be trivial to write, but we shouldn't offer that. Removing the
46                    // comment is not enough, because people should also add the actual variadic parameter.
47                    // For some methods, variadic parameters are only documented via this inline comment,
48                    // hence an autofixer would effectively remove any documentation about them.
49                    $phpcsFile->addError(
50                        'Comments indicating variadic arguments are superfluous and should be replaced ' .
51                            'with actual variadic arguments',
52                        $commentPos,
53                        'SuperfluousVariadicArgComment'
54                    );
55                }
56            }
57            $commentPos = $phpcsFile->findNext( T_COMMENT, $commentPos + 1, $end );
58        }
59    }
60
61}