Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
IllegalSingleLineCommentSniff
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 2
156
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 / 32
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2/**
3 * Detect and fix the inline comments start or end with multiple asterisks.
4 * Fail: /*** Comment here *\/
5 * Fail: /*** Comments here again ***\/
6 * Pass: /* Your comments here *\/
7 */
8
9namespace MediaWiki\Sniffs\Commenting;
10
11use PHP_CodeSniffer\Files\File;
12use PHP_CodeSniffer\Sniffs\Sniff;
13
14class IllegalSingleLineCommentSniff implements Sniff {
15
16    /**
17     * @inheritDoc
18     */
19    public function register(): array {
20        return [
21            T_COMMENT
22        ];
23    }
24
25    /**
26     * @param File $phpcsFile
27     * @param int $stackPtr The index of current token.
28     * @return void
29     */
30    public function process( File $phpcsFile, $stackPtr ) {
31        $tokens = $phpcsFile->getTokens();
32        $currentToken = $tokens[$stackPtr];
33
34        if ( str_starts_with( $currentToken['content'], '/*' ) ) {
35            // Possible inline comment
36            if ( !str_ends_with( $currentToken['content'], '*/' ) ) {
37                // Whether it's a comment across multiple lines
38                $numOfTokens = $phpcsFile->numTokens;
39                for ( $i = $stackPtr + 1; $i < $numOfTokens; $i++ ) {
40                    $token = $tokens[$i];
41                    if ( ( $token['code'] !== T_COMMENT && $token['code'] !== T_WHITESPACE ) || (
42                        !str_starts_with( $token['content'], '/*' ) &&
43                        str_ends_with( $token['content'], '*/' )
44                    ) ) {
45                        return;
46                    }
47                }
48                $fix = $phpcsFile->addFixableError(
49                    'Missing proper ending of a single line comment',
50                    $stackPtr,
51                    'MissingCommentEnding'
52                );
53                if ( $fix ) {
54                    $phpcsFile->fixer->replaceToken(
55                        $stackPtr,
56                        rtrim( $currentToken['content'] ) . ' */' . $phpcsFile->eolChar
57                    );
58                }
59            } else {
60                // Determine whether multiple "*" appears right before the "*/"
61                if ( preg_match( '/(\*){2,}\//', $currentToken['content'] ) !== 0 ) {
62                    $fix = $phpcsFile->addFixableWarning(
63                        'Invalid end of a single line comment',
64                        $stackPtr,
65                        'IllegalSingleLineCommentEnd'
66                    );
67                    if ( $fix ) {
68                        $phpcsFile->fixer->replaceToken(
69                            $stackPtr,
70                            preg_replace( '/(\*){2,}\//', '*/', $currentToken['content'] )
71                        );
72                    }
73                }
74            }
75        }
76    }
77}