• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 15637524486

13 Jun 2025 02:54PM UTC coverage: 78.436% (+0.06%) from 78.375%
15637524486

Pull #1108

github

web-flow
Merge ce5067991 into ef0b6a62c
Pull Request #1108: Squiz/SelfMemberReference: update XML doc

25193 of 32119 relevant lines covered (78.44%)

69.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.85
/src/Standards/Squiz/Sniffs/Commenting/DocCommentAlignmentSniff.php
1
<?php
2
/**
3
 * Tests that the stars in a doc comment align correctly.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8
 */
9

10
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Sniffs\Sniff;
14
use PHP_CodeSniffer\Util\Tokens;
15

16
class DocCommentAlignmentSniff implements Sniff
17
{
18

19
    /**
20
     * A list of tokenizers this sniff supports.
21
     *
22
     * @var array
23
     */
24
    public $supportedTokenizers = [
25
        'PHP',
26
        'JS',
27
    ];
28

29

30
    /**
31
     * Returns an array of tokens this test wants to listen for.
32
     *
33
     * @return array<int|string>
34
     */
35
    public function register()
3✔
36
    {
37
        return [T_DOC_COMMENT_OPEN_TAG];
3✔
38

39
    }//end register()
40

41

42
    /**
43
     * Processes this test, when one of its tokens is encountered.
44
     *
45
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
46
     * @param int                         $stackPtr  The position of the current token
47
     *                                               in the stack passed in $tokens.
48
     *
49
     * @return void
50
     */
51
    public function process(File $phpcsFile, $stackPtr)
3✔
52
    {
53
        $tokens = $phpcsFile->getTokens();
3✔
54

55
        // We are only interested in function/class/interface/enum/property/const doc block comments.
56
        $ignore = Tokens::$emptyTokens;
3✔
57
        if ($phpcsFile->tokenizerType === 'JS') {
3✔
58
            $ignore[] = T_EQUAL;
3✔
59
            $ignore[] = T_STRING;
3✔
60
            $ignore[] = T_OBJECT_OPERATOR;
3✔
61
        }
1✔
62

63
        $nextToken = $phpcsFile->findNext($ignore, ($stackPtr + 1), null, true);
3✔
64

65
        $ignore  = Tokens::$scopeModifiers;
3✔
66
        $ignore += [
1✔
67
            T_CLASS     => true,
3✔
68
            T_INTERFACE => true,
3✔
69
            T_ENUM      => true,
3✔
70
            T_ENUM_CASE => true,
3✔
71
            T_FUNCTION  => true,
3✔
72
            T_STATIC    => true,
3✔
73
            T_ABSTRACT  => true,
3✔
74
            T_FINAL     => true,
3✔
75
            T_PROPERTY  => true,
3✔
76
            T_OBJECT    => true,
3✔
77
            T_PROTOTYPE => true,
3✔
78
            T_VAR       => true,
3✔
79
            T_READONLY  => true,
3✔
80
        ];
1✔
81

82
        if ($nextToken === false || isset($ignore[$tokens[$nextToken]['code']]) === false) {
3✔
83
            // Could be a file comment.
84
            $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
3✔
85
            if ($tokens[$prevToken]['code'] !== T_OPEN_TAG) {
3✔
86
                return;
3✔
87
            }
88
        }
89

90
        // There must be one space after each star (unless it is an empty comment line)
91
        // and all the stars must be aligned correctly.
92
        $requiredColumn = ($tokens[$stackPtr]['column'] + 1);
3✔
93
        $endComment     = $tokens[$stackPtr]['comment_closer'];
3✔
94
        for ($i = ($stackPtr + 1); $i <= $endComment; $i++) {
3✔
95
            if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR
3✔
96
                && $tokens[$i]['code'] !== T_DOC_COMMENT_CLOSE_TAG
3✔
97
            ) {
1✔
98
                continue;
3✔
99
            }
100

101
            if ($tokens[$i]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
3✔
102
                if (trim($tokens[$i]['content']) === '') {
3✔
103
                    // Don't process an unfinished docblock close tag during live coding.
104
                    continue;
×
105
                }
106

107
                // Can't process the close tag if it is not the first thing on the line.
108
                $prev = $phpcsFile->findPrevious(T_DOC_COMMENT_WHITESPACE, ($i - 1), $stackPtr, true);
3✔
109
                if ($tokens[$prev]['line'] === $tokens[$i]['line']) {
3✔
110
                    continue;
3✔
111
                }
112
            }
1✔
113

114
            if ($tokens[$i]['column'] !== $requiredColumn) {
3✔
115
                $pluralizeSpace = 's';
3✔
116
                if (($requiredColumn - 1) === 1) {
3✔
117
                    $pluralizeSpace = '';
3✔
118
                }
1✔
119

120
                $error = 'Expected %s space%s before asterisk; %s found';
3✔
121
                $data  = [
1✔
122
                    ($requiredColumn - 1),
3✔
123
                    $pluralizeSpace,
3✔
124
                    ($tokens[$i]['column'] - 1),
3✔
125
                ];
2✔
126
                $fix   = $phpcsFile->addFixableError($error, $i, 'SpaceBeforeStar', $data);
3✔
127
                if ($fix === true) {
3✔
128
                    $padding = str_repeat(' ', ($requiredColumn - 1));
3✔
129
                    if ($tokens[$i]['column'] === 1) {
3✔
130
                        $phpcsFile->fixer->addContentBefore($i, $padding);
3✔
131
                    } else {
1✔
132
                        $phpcsFile->fixer->replaceToken(($i - 1), $padding);
3✔
133
                    }
134
                }
1✔
135
            }//end if
1✔
136

137
            if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR) {
3✔
138
                continue;
3✔
139
            }
140

141
            if ($tokens[($i + 2)]['line'] !== $tokens[$i]['line']) {
3✔
142
                // Line is empty.
143
                continue;
3✔
144
            }
145

146
            if ($tokens[($i + 1)]['code'] !== T_DOC_COMMENT_WHITESPACE) {
3✔
147
                $error = 'Expected 1 space after asterisk; 0 found';
3✔
148
                $fix   = $phpcsFile->addFixableError($error, $i, 'NoSpaceAfterStar');
3✔
149
                if ($fix === true) {
3✔
150
                    $phpcsFile->fixer->addContent($i, ' ');
3✔
151
                }
1✔
152
            } else if ($tokens[($i + 2)]['code'] === T_DOC_COMMENT_TAG
3✔
153
                && $tokens[($i + 1)]['content'] !== ' '
3✔
154
            ) {
1✔
155
                $error = 'Expected 1 space after asterisk; %s found';
3✔
156
                $data  = [$tokens[($i + 1)]['length']];
3✔
157
                $fix   = $phpcsFile->addFixableError($error, $i, 'SpaceAfterStar', $data);
3✔
158
                if ($fix === true) {
3✔
159
                    $phpcsFile->fixer->replaceToken(($i + 1), ' ');
3✔
160
                }
1✔
161
            }
1✔
162
        }//end for
1✔
163

164
    }//end process()
2✔
165

166

167
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc