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

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 hits per line

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

98.55
/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
    }//end register()
30

31

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

45
        // We are only interested in function/class/interface doc block comments.
46
        $nextToken = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($stackPtr + 1), null, true);
3✔
47
        $ignore    = [
2✔
48
            T_CLASS     => true,
3✔
49
            T_INTERFACE => true,
3✔
50
            T_ENUM      => true,
3✔
51
            T_ENUM_CASE => true,
3✔
52
            T_FUNCTION  => true,
3✔
53
            T_PUBLIC    => true,
3✔
54
            T_PRIVATE   => true,
3✔
55
            T_PROTECTED => true,
3✔
56
            T_STATIC    => true,
3✔
57
            T_ABSTRACT  => true,
3✔
58
            T_FINAL     => true,
3✔
59
            T_VAR       => true,
3✔
60
            T_READONLY  => true,
3✔
61
        ];
2✔
62

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

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

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

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

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

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

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

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

127
            if ($tokens[($i + 1)]['code'] !== T_DOC_COMMENT_WHITESPACE) {
3✔
128
                $error = 'Expected 1 space after asterisk; 0 found';
3✔
129
                $fix   = $phpcsFile->addFixableError($error, $i, 'NoSpaceAfterStar');
3✔
130
                if ($fix === true) {
3✔
131
                    $phpcsFile->fixer->addContent($i, ' ');
3✔
132
                }
133
            } else if ($tokens[($i + 2)]['code'] === T_DOC_COMMENT_TAG
3✔
134
                && $tokens[($i + 1)]['content'] !== ' '
3✔
135
            ) {
136
                $error = 'Expected 1 space after asterisk; %s found';
3✔
137
                $data  = [$tokens[($i + 1)]['length']];
3✔
138
                $fix   = $phpcsFile->addFixableError($error, $i, 'SpaceAfterStar', $data);
3✔
139
                if ($fix === true) {
3✔
140
                    $phpcsFile->fixer->replaceToken(($i + 1), ' ');
3✔
141
                }
142
            }
143
        }//end for
144

145
    }//end process()
1✔
146

147

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