Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Squiz_Sniffs_Commenting_InlineCommentSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @author    Marc McIntyre <mmcintyre@squiz.net>
11
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
12
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13
 * @version   CVS: $Id: InlineCommentSniff.php 288252 2009-09-11 01:50:05Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Squiz_Sniffs_Commenting_InlineCommentSniff.
19
 *
20
 * Checks that there is adequate spacing between comments.
21
 *
22
 * @category  PHP
23
 * @package   PHP_CodeSniffer
24
 * @author    Greg Sherwood <gsherwood@squiz.net>
25
 * @author    Marc McIntyre <mmcintyre@squiz.net>
26
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
27
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
28
 * @version   Release: 1.2.1
29
 * @link      http://pear.php.net/package/PHP_CodeSniffer
30
 */
31
class Squiz_Sniffs_Commenting_InlineCommentSniff implements PHP_CodeSniffer_Sniff
32
{
33
 
34
    /**
35
     * A list of tokenizers this sniff supports.
36
     *
37
     * @var array
38
     */
39
    public $supportedTokenizers = array(
40
                                   'PHP',
41
                                   'JS',
42
                                  );
43
 
44
    /**
45
     * Returns an array of tokens this test wants to listen for.
46
     *
47
     * @return array
48
     */
49
    public function register()
50
    {
51
        return array(
52
                T_COMMENT,
53
                T_DOC_COMMENT,
54
               );
55
 
56
    }//end register()
57
 
58
 
59
    /**
60
     * Processes this test, when one of its tokens is encountered.
61
     *
62
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
63
     * @param int                  $stackPtr  The position of the current token in the
64
     *                                        stack passed in $tokens.
65
     *
66
     * @return void
67
     */
68
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
69
    {
70
        $tokens = $phpcsFile->getTokens();
71
 
72
        // If this is a function/class/interface doc block comment, skip it.
73
        // We are only interested in inline doc block comments, which are
74
        // not allowed.
75
        if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT) {
76
            $nextToken = $phpcsFile->findNext(
77
                PHP_CodeSniffer_Tokens::$emptyTokens,
78
                ($stackPtr + 1),
79
                null,
80
                true
81
            );
82
 
83
            $ignore = array(
84
                       T_CLASS,
85
                       T_INTERFACE,
86
                       T_FUNCTION,
87
                       T_PUBLIC,
88
                       T_PRIVATE,
89
                       T_PROTECTED,
90
                       T_STATIC,
91
                       T_ABSTRACT,
92
                       T_CONST,
93
                       T_OBJECT,
94
                       T_PROPERTY,
95
                      );
96
 
97
            if (in_array($tokens[$nextToken]['code'], $ignore) === true) {
98
                return;
99
            } else {
100
                if ($phpcsFile->tokenizerType === 'JS') {
101
                    // We allow block comments if a function is being assigned
102
                    // to a variable.
103
                    $ignore    = PHP_CodeSniffer_Tokens::$emptyTokens;
104
                    $ignore[]  = T_EQUAL;
105
                    $ignore[]  = T_STRING;
106
                    $ignore[]  = T_OBJECT_OPERATOR;
107
                    $nextToken = $phpcsFile->findNext($ignore, ($nextToken + 1), null, true);
108
                    if ($tokens[$nextToken]['code'] === T_FUNCTION) {
109
                        return;
110
                    }
111
                }
112
 
113
                $prevToken = $phpcsFile->findPrevious(
114
                    PHP_CodeSniffer_Tokens::$emptyTokens,
115
                    ($stackPtr - 1),
116
                    null,
117
                    true
118
                );
119
 
120
                if ($tokens[$prevToken]['code'] === T_OPEN_TAG) {
121
                    return;
122
                }
123
 
124
                // Only error once per comment.
125
                if (substr($tokens[$stackPtr]['content'], 0, 3) === '/**') {
126
                    $error  = 'Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead';
127
                    $phpcsFile->addError($error, $stackPtr);
128
                }
129
            }//end if
130
        }//end if
131
 
132
        if ($tokens[$stackPtr]['content']{0} === '#') {
133
            $error  = 'Perl-style comments are not allowed; use "// Comment" instead';
134
            $phpcsFile->addError($error, $stackPtr);
135
        }
136
 
137
        // We don't want end of block comments. If the last comment is a closing
138
        // curly brace.
139
        $previousContent = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true);
140
        if (($tokens[$previousContent]['line'] === $tokens[$stackPtr]['line']) && ($tokens[$previousContent]['code'] === T_CLOSE_CURLY_BRACKET)) {
141
            return;
142
        }
143
 
144
        $comment = rtrim($tokens[$stackPtr]['content']);
145
        // Only want inline comments.
146
        if (substr($comment, 0, 2) !== '//') {
147
            return;
148
        }
149
 
150
        $spaceCount = 0;
151
        for ($i = 2; $i < strlen($comment); $i++) {
152
            if ($comment[$i] !== ' ') {
153
                break;
154
            }
155
 
156
            $spaceCount++;
157
        }
158
 
159
        if ($spaceCount === 0) {
160
            $error = 'No space before comment text; expected "// '.substr($comment, 2).'" but found "'.$comment.'"';
161
            $phpcsFile->addError($error, $stackPtr);
162
        }
163
 
164
        if ($spaceCount > 1) {
165
            $error = $spaceCount.' spaces found before inline comment; expected "// '.substr($comment, (2 + $spaceCount)).'" but found "'.$comment.'"';
166
            $phpcsFile->addError($error, $stackPtr);
167
        }
168
 
169
 
170
        // The below section determines if a comment block is correctly capitalised,
171
        // and ends in a full-stop. It will find the last comment in a block, and
172
        // work its way up.
173
        $nextComment = $phpcsFile->findNext(array(T_COMMENT), ($stackPtr + 1), null, false);
174
 
175
        if (($nextComment !== false) && (($tokens[$nextComment]['line']) === ($tokens[$stackPtr]['line'] + 1))) {
176
            return;
177
        }
178
 
179
        $topComment  = $stackPtr;
180
        $lastComment = $stackPtr;
181
        while (($topComment = $phpcsFile->findPrevious(array(T_COMMENT), ($lastComment - 1), null, false)) !== false) {
182
            if ($tokens[$topComment]['line'] !== ($tokens[$lastComment]['line'] - 1)) {
183
                break;
184
            }
185
 
186
            $lastComment = $topComment;
187
        }
188
 
189
        $topComment  = $lastComment;
190
        $commentText = '';
191
 
192
        for ($i = $topComment; $i <= $stackPtr; $i++) {
193
            if ($tokens[$i]['code'] === T_COMMENT) {
194
                $commentText .= trim(substr($tokens[$i]['content'], 2));
195
            }
196
        }
197
 
198
        if ($commentText === '') {
199
            $error = 'Blank comments are not allowed';
200
            $phpcsFile->addError($error, $stackPtr);
201
            return;
202
        }
203
 
204
        if (preg_match('|[A-Z]|', $commentText[0]) === 0) {
205
            $error = 'Inline comments must start with a capital letter';
206
            $phpcsFile->addError($error, $topComment);
207
        }
208
 
209
        $commentCloser   = $commentText[(strlen($commentText) - 1)];
210
        $acceptedClosers = array(
211
                            'full-stops'        => '.',
212
                            'exclamation marks' => '!',
213
                            'or question marks' => '?',
214
                           );
215
 
216
        if (in_array($commentCloser, $acceptedClosers) === false) {
217
            $error = 'Inline comments must end in';
218
            foreach ($acceptedClosers as $closerName => $symbol) {
219
                $error .= ' '.$closerName.',';
220
            }
221
 
222
            $error = rtrim($error, ',');
223
            $phpcsFile->addError($error, $stackPtr);
224
        }
225
 
226
        // Finally, the line below the last comment cannot be empty.
227
        $start = false;
228
        for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
229
            if ($tokens[$i]['line'] === ($tokens[$stackPtr]['line'] + 1)) {
230
                if ($tokens[$i]['code'] !== T_WHITESPACE) {
231
                    return;
232
                }
233
            } else if ($tokens[$i]['line'] > ($tokens[$stackPtr]['line'] + 1)) {
234
                break;
235
            }
236
        }
237
 
238
        $error = 'There must be no blank line following an inline comment';
239
        $phpcsFile->addError($error, $stackPtr);
240
 
241
    }//end process()
242
 
243
 
244
}//end class
245
 
246
 
247
?>