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

PHPCSStandards / PHP_CodeSniffer / 15036337869

15 May 2025 04:03AM UTC coverage: 78.375% (-0.2%) from 78.556%
15036337869

Pull #856

github

web-flow
Merge 93f570b46 into f5e7943d0
Pull Request #856: [Doc] Cover all errors of PEAR ClassDeclaration

25112 of 32041 relevant lines covered (78.37%)

69.4 hits per line

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

98.88
/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 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
        $ignore    = [
1✔
65
            T_CLASS     => true,
3✔
66
            T_INTERFACE => true,
3✔
67
            T_ENUM      => true,
3✔
68
            T_ENUM_CASE => true,
3✔
69
            T_FUNCTION  => true,
3✔
70
            T_PUBLIC    => true,
3✔
71
            T_PRIVATE   => true,
3✔
72
            T_PROTECTED => true,
3✔
73
            T_STATIC    => true,
3✔
74
            T_ABSTRACT  => true,
3✔
75
            T_FINAL     => true,
3✔
76
            T_PROPERTY  => true,
3✔
77
            T_OBJECT    => true,
3✔
78
            T_PROTOTYPE => true,
3✔
79
            T_VAR       => true,
3✔
80
            T_READONLY  => true,
3✔
81
        ];
2✔
82

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

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

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

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

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

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

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

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

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

165
    }//end process()
2✔
166

167

168
}//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

© 2026 Coveralls, Inc