• 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

99.15
/src/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php
1
<?php
2
/**
3
 * Parses and verifies the file doc comment.
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

15
class FileCommentSniff implements Sniff
16
{
17

18

19
    /**
20
     * Returns an array of tokens this test wants to listen for.
21
     *
22
     * @return array<int|string>
23
     */
24
    public function register()
3✔
25
    {
26
        return [T_OPEN_TAG];
3✔
27

28
    }//end register()
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 int
39
     */
40
    public function process(File $phpcsFile, $stackPtr)
3✔
41
    {
42
        $tokens       = $phpcsFile->getTokens();
3✔
43
        $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
3✔
44

45
        if ($tokens[$commentStart]['code'] === T_COMMENT) {
3✔
46
            $phpcsFile->addError('You must use "/**" style comments for a file comment', $commentStart, 'WrongStyle');
3✔
47
            $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'yes');
3✔
48
            return $phpcsFile->numTokens;
3✔
49
        } else if ($commentStart === false || $tokens[$commentStart]['code'] !== T_DOC_COMMENT_OPEN_TAG) {
3✔
50
            $phpcsFile->addError('Missing file doc comment', $stackPtr, 'Missing');
3✔
51
            $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'no');
3✔
52
            return $phpcsFile->numTokens;
3✔
53
        }
54

55
        if (isset($tokens[$commentStart]['comment_closer']) === false
3✔
56
            || ($tokens[$tokens[$commentStart]['comment_closer']]['content'] === ''
3✔
57
            && $tokens[$commentStart]['comment_closer'] === ($phpcsFile->numTokens - 1))
3✔
58
        ) {
59
            // Don't process an unfinished file comment during live coding.
60
            return $phpcsFile->numTokens;
3✔
61
        }
62

63
        $commentEnd = $tokens[$commentStart]['comment_closer'];
3✔
64

65
        for ($nextToken = ($commentEnd + 1); $nextToken < $phpcsFile->numTokens; $nextToken++) {
3✔
66
            if ($tokens[$nextToken]['code'] === T_WHITESPACE) {
3✔
67
                continue;
3✔
68
            }
69

70
            if ($tokens[$nextToken]['code'] === T_ATTRIBUTE
3✔
71
                && isset($tokens[$nextToken]['attribute_closer']) === true
3✔
72
            ) {
73
                $nextToken = $tokens[$nextToken]['attribute_closer'];
3✔
74
                continue;
3✔
75
            }
76

77
            break;
3✔
78
        }
79

80
        if ($nextToken === $phpcsFile->numTokens) {
3✔
81
            $nextToken--;
3✔
82
        }
83

84
        $ignore = [
2✔
85
            T_CLASS,
3✔
86
            T_INTERFACE,
3✔
87
            T_TRAIT,
3✔
88
            T_ENUM,
3✔
89
            T_FUNCTION,
3✔
90
            T_CLOSURE,
3✔
91
            T_PUBLIC,
3✔
92
            T_PRIVATE,
3✔
93
            T_PROTECTED,
3✔
94
            T_FINAL,
3✔
95
            T_STATIC,
3✔
96
            T_ABSTRACT,
3✔
97
            T_READONLY,
3✔
98
            T_CONST,
3✔
99
            T_INCLUDE,
3✔
100
            T_INCLUDE_ONCE,
3✔
101
            T_REQUIRE,
3✔
102
            T_REQUIRE_ONCE,
3✔
103
        ];
2✔
104

105
        if (in_array($tokens[$nextToken]['code'], $ignore, true) === true) {
3✔
106
            $phpcsFile->addError('Missing file doc comment', $stackPtr, 'Missing');
3✔
107
            $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'no');
3✔
108
            return $phpcsFile->numTokens;
3✔
109
        }
110

111
        $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'yes');
3✔
112

113
        // No blank line between the open tag and the file comment.
114
        if ($tokens[$commentStart]['line'] > ($tokens[$stackPtr]['line'] + 1)) {
3✔
115
            $error = 'There must be no blank lines before the file comment';
3✔
116
            $phpcsFile->addError($error, $stackPtr, 'SpacingAfterOpen');
3✔
117
        }
118

119
        // Exactly one blank line after the file comment.
120
        $next = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), null, true);
3✔
121
        if ($next !== false && $tokens[$next]['line'] !== ($tokens[$commentEnd]['line'] + 2)) {
3✔
122
            $error = 'There must be exactly one blank line after the file comment';
3✔
123
            $phpcsFile->addError($error, $commentEnd, 'SpacingAfterComment');
3✔
124
        }
125

126
        // Required tags in correct order.
127
        $required = [
2✔
128
            '@package'    => true,
3✔
129
            '@subpackage' => true,
2✔
130
            '@author'     => true,
2✔
131
            '@copyright'  => true,
2✔
132
        ];
2✔
133

134
        $foundTags = [];
3✔
135
        foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
3✔
136
            $name       = $tokens[$tag]['content'];
3✔
137
            $isRequired = isset($required[$name]);
3✔
138

139
            if ($isRequired === true && in_array($name, $foundTags, true) === true) {
3✔
140
                $error = 'Only one %s tag is allowed in a file comment';
3✔
141
                $data  = [$name];
3✔
142
                $phpcsFile->addError($error, $tag, 'Duplicate'.ucfirst(substr($name, 1)).'Tag', $data);
3✔
143
            }
144

145
            $foundTags[] = $name;
3✔
146

147
            if ($isRequired === false) {
3✔
148
                continue;
3✔
149
            }
150

151
            $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
3✔
152
            if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
3✔
153
                $error = 'Content missing for %s tag in file comment';
3✔
154
                $data  = [$name];
3✔
155
                $phpcsFile->addError($error, $tag, 'Empty'.ucfirst(substr($name, 1)).'Tag', $data);
3✔
156
                continue;
3✔
157
            }
158

159
            if ($name === '@author') {
3✔
160
                if ($tokens[$string]['content'] !== 'Squiz Pty Ltd <products@squiz.net>') {
3✔
161
                    $error = 'Expected "Squiz Pty Ltd <products@squiz.net>" for author tag';
3✔
162
                    $fix   = $phpcsFile->addFixableError($error, $tag, 'IncorrectAuthor');
3✔
163
                    if ($fix === true) {
3✔
164
                        $expected = 'Squiz Pty Ltd <products@squiz.net>';
3✔
165
                        $phpcsFile->fixer->replaceToken($string, $expected);
3✔
166
                    }
167
                }
168
            } else if ($name === '@copyright') {
3✔
169
                if (preg_match('/^([0-9]{4})(-[0-9]{4})? (Squiz Pty Ltd \(ABN 77 084 670 600\))$/', $tokens[$string]['content']) === 0) {
3✔
170
                    $error = 'Expected "xxxx-xxxx Squiz Pty Ltd (ABN 77 084 670 600)" for copyright declaration';
3✔
171
                    $fix   = $phpcsFile->addFixableError($error, $tag, 'IncorrectCopyright');
3✔
172
                    if ($fix === true) {
3✔
173
                        $matches = [];
3✔
174
                        preg_match('/^(([0-9]{4})(-[0-9]{4})?)?.*$/', $tokens[$string]['content'], $matches);
3✔
175
                        if (isset($matches[1]) === false) {
3✔
176
                            $matches[1] = date('Y');
3✔
177
                        }
178

179
                        $expected = $matches[1].' Squiz Pty Ltd (ABN 77 084 670 600)';
3✔
180
                        $phpcsFile->fixer->replaceToken($string, $expected);
3✔
181
                    }
182
                }
183
            }//end if
184
        }//end foreach
185

186
        // Check if the tags are in the correct position.
187
        $pos = 0;
3✔
188
        foreach ($required as $tag => $true) {
3✔
189
            if (in_array($tag, $foundTags, true) === false) {
3✔
190
                $error = 'Missing %s tag in file comment';
3✔
191
                $data  = [$tag];
3✔
192
                $phpcsFile->addError($error, $commentEnd, 'Missing'.ucfirst(substr($tag, 1)).'Tag', $data);
3✔
193
            }
194

195
            if (isset($foundTags[$pos]) === false) {
3✔
196
                break;
×
197
            }
198

199
            if ($foundTags[$pos] !== $tag) {
3✔
200
                $error = 'The tag in position %s should be the %s tag';
3✔
201
                $data  = [
2✔
202
                    ($pos + 1),
3✔
203
                    $tag,
3✔
204
                ];
2✔
205
                $phpcsFile->addError($error, $tokens[$commentStart]['comment_tags'][$pos], ucfirst(substr($tag, 1)).'TagOrder', $data);
3✔
206
            }
207

208
            $pos++;
3✔
209
        }//end foreach
210

211
        // Ignore the rest of the file.
212
        return $phpcsFile->numTokens;
3✔
213

214
    }//end process()
215

216

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