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

PHPCSStandards / PHP_CodeSniffer / 17663621563

12 Sep 2025 03:25AM UTC coverage: 78.786%. Remained the same
17663621563

push

github

web-flow
Merge pull request #1243 from PHPCSStandards/phpcs-4.x/feature/155-normalize-some-code-style-rules-5

CS: normalize code style rules [5]

294 of 308 new or added lines in 191 files covered. (95.45%)

2354 existing lines in 130 files now uncovered.

19732 of 25045 relevant lines covered (78.79%)

96.47 hits per line

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

98.51
/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
    /**
21
     * Returns an array of tokens this test wants to listen for.
22
     *
23
     * @return array<int|string>
24
     */
25
    public function register()
3✔
26
    {
27
        return [T_DOC_COMMENT_OPEN_TAG];
3✔
28
    }
29

30

31
    /**
32
     * Processes this test, when one of its tokens is encountered.
33
     *
34
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
35
     * @param int                         $stackPtr  The position of the current token
36
     *                                               in the stack passed in $tokens.
37
     *
38
     * @return void
39
     */
40
    public function process(File $phpcsFile, int $stackPtr)
3✔
41
    {
42
        $tokens = $phpcsFile->getTokens();
3✔
43

44
        // We are only interested in function/class/interface/enum/property/const doc block comments.
45
        $nextToken = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($stackPtr + 1), null, true);
3✔
46

47
        $ignore  = Tokens::SCOPE_MODIFIERS;
3✔
48
        $ignore += [
2✔
49
            T_CLASS     => true,
3✔
50
            T_INTERFACE => true,
3✔
51
            T_ENUM      => true,
3✔
52
            T_ENUM_CASE => true,
3✔
53
            T_FUNCTION  => true,
3✔
54
            T_STATIC    => true,
3✔
55
            T_ABSTRACT  => true,
3✔
56
            T_FINAL     => true,
3✔
57
            T_VAR       => true,
3✔
58
            T_READONLY  => true,
3✔
59
        ];
2✔
60

61
        if ($nextToken === false || isset($ignore[$tokens[$nextToken]['code']]) === false) {
3✔
62
            // Could be a file comment.
63
            $prevToken = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($stackPtr - 1), null, true);
3✔
64
            if ($tokens[$prevToken]['code'] !== T_OPEN_TAG) {
3✔
65
                return;
3✔
66
            }
67
        }
68

69
        // There must be one space after each star (unless it is an empty comment line)
70
        // and all the stars must be aligned correctly.
71
        $requiredColumn = ($tokens[$stackPtr]['column'] + 1);
3✔
72
        $endComment     = $tokens[$stackPtr]['comment_closer'];
3✔
73
        for ($i = ($stackPtr + 1); $i <= $endComment; $i++) {
3✔
74
            if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR
3✔
75
                && $tokens[$i]['code'] !== T_DOC_COMMENT_CLOSE_TAG
3✔
76
            ) {
77
                continue;
3✔
78
            }
79

80
            if ($tokens[$i]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
3✔
81
                if (trim($tokens[$i]['content']) === '') {
3✔
82
                    // Don't process an unfinished docblock close tag during live coding.
UNCOV
83
                    continue;
×
84
                }
85

86
                // Can't process the close tag if it is not the first thing on the line.
87
                $prev = $phpcsFile->findPrevious(T_DOC_COMMENT_WHITESPACE, ($i - 1), $stackPtr, true);
3✔
88
                if ($tokens[$prev]['line'] === $tokens[$i]['line']) {
3✔
89
                    continue;
3✔
90
                }
91
            }
92

93
            if ($tokens[$i]['column'] !== $requiredColumn) {
3✔
94
                $pluralizeSpace = 's';
3✔
95
                if (($requiredColumn - 1) === 1) {
3✔
96
                    $pluralizeSpace = '';
3✔
97
                }
98

99
                $error = 'Expected %s space%s before asterisk; %s found';
3✔
100
                $data  = [
2✔
101
                    ($requiredColumn - 1),
3✔
102
                    $pluralizeSpace,
3✔
103
                    ($tokens[$i]['column'] - 1),
3✔
104
                ];
2✔
105
                $fix   = $phpcsFile->addFixableError($error, $i, 'SpaceBeforeStar', $data);
3✔
106
                if ($fix === true) {
3✔
107
                    $padding = str_repeat(' ', ($requiredColumn - 1));
3✔
108
                    if ($tokens[$i]['column'] === 1) {
3✔
109
                        $phpcsFile->fixer->addContentBefore($i, $padding);
3✔
110
                    } else {
111
                        $phpcsFile->fixer->replaceToken(($i - 1), $padding);
3✔
112
                    }
113
                }
114
            }//end if
115

116
            if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR) {
3✔
117
                continue;
3✔
118
            }
119

120
            if ($tokens[($i + 2)]['line'] !== $tokens[$i]['line']) {
3✔
121
                // Line is empty.
122
                continue;
3✔
123
            }
124

125
            if ($tokens[($i + 1)]['code'] !== T_DOC_COMMENT_WHITESPACE) {
3✔
126
                $error = 'Expected 1 space after asterisk; 0 found';
3✔
127
                $fix   = $phpcsFile->addFixableError($error, $i, 'NoSpaceAfterStar');
3✔
128
                if ($fix === true) {
3✔
129
                    $phpcsFile->fixer->addContent($i, ' ');
3✔
130
                }
131
            } elseif ($tokens[($i + 2)]['code'] === T_DOC_COMMENT_TAG
3✔
132
                && $tokens[($i + 1)]['content'] !== ' '
3✔
133
            ) {
134
                $error = 'Expected 1 space after asterisk; %s found';
3✔
135
                $data  = [$tokens[($i + 1)]['length']];
3✔
136
                $fix   = $phpcsFile->addFixableError($error, $i, 'SpaceAfterStar', $data);
3✔
137
                if ($fix === true) {
3✔
138
                    $phpcsFile->fixer->replaceToken(($i + 1), ' ');
3✔
139
                }
140
            }
141
        }//end for
142
    }
1✔
143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc