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_PHP_CommentedOutCodeSniff.
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: CommentedOutCodeSniff.php 268065 2008-10-31 04:53:56Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Squiz_Sniffs_PHP_CommentedOutCodeSniff.
19
 *
20
 * Warn about commented out code.
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_PHP_CommentedOutCodeSniff 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
                                   'CSS',
42
                                  );
43
 
44
    /**
45
     * If a comment is more than $maxPercentage% code, a warning will be shown.
46
     *
47
     * @var int
48
     */
49
    protected $maxPercentage = 35;
50
 
51
 
52
    /**
53
     * Returns an array of tokens this test wants to listen for.
54
     *
55
     * @return array
56
     */
57
    public function register()
58
    {
59
        return PHP_CodeSniffer_Tokens::$commentTokens;
60
 
61
    }//end register()
62
 
63
 
64
    /**
65
     * Processes this test, when one of its tokens is encountered.
66
     *
67
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
68
     * @param int                  $stackPtr  The position of the current token
69
     *                                        in the stack passed in $tokens.
70
     *
71
     * @return void
72
     */
73
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
74
    {
75
        $tokens = $phpcsFile->getTokens();
76
 
77
        // Process whole comment blocks at once, so skip all but the first token.
78
        if ($stackPtr > 0 && $tokens[$stackPtr]['code'] === $tokens[($stackPtr - 1)]['code']) {
79
            return;
80
        }
81
 
82
        // Ignore comments at the end of code blocks.
83
        if (substr($tokens[$stackPtr]['content'], 0, 6) === '//end ') {
84
            return;
85
        }
86
 
87
        $content = '';
88
        if ($phpcsFile->tokenizerType === 'PHP') {
89
            $content = '<?php ';
90
        }
91
 
92
        for ($i = $stackPtr; $i < $phpcsFile->numTokens; $i++) {
93
            if ($tokens[$stackPtr]['code'] !== $tokens[$i]['code']) {
94
                break;
95
            }
96
 
97
            /*
98
                Trim as much off the comment as possible so we don't
99
                have additional whitespace tokens or comment tokens
100
            */
101
 
102
            $tokenContent = trim($tokens[$i]['content']);
103
 
104
            if (substr($tokenContent, 0, 2) === '//') {
105
                $tokenContent = substr($tokenContent, 2);
106
            }
107
 
108
            if (substr($tokenContent, 0, 1) === '#') {
109
                $tokenContent = substr($tokenContent, 1);
110
            }
111
 
112
            if (substr($tokenContent, 0, 3) === '/**') {
113
                $tokenContent = substr($tokenContent, 3);
114
            }
115
 
116
            if (substr($tokenContent, 0, 2) === '/*') {
117
                $tokenContent = substr($tokenContent, 2);
118
            }
119
 
120
            if (substr($tokenContent, -2) === '*/') {
121
                $tokenContent = substr($tokenContent, 0, -2);
122
            }
123
 
124
            if (substr($tokenContent, 0, 1) === '*') {
125
                $tokenContent = substr($tokenContent, 1);
126
            }
127
 
128
            $content .= $tokenContent.$phpcsFile->eolChar;
129
        }//end for
130
 
131
        $content = trim($content);
132
 
133
        if ($phpcsFile->tokenizerType === 'PHP') {
134
            $content .= ' ?>';
135
        }
136
 
137
        $stringTokens = PHP_CodeSniffer_File::tokenizeString($content, $phpcsFile->tokenizer, $phpcsFile->eolChar);
138
 
139
        $emptyTokens = array(
140
                        T_WHITESPACE,
141
                        T_STRING,
142
                        T_STRING_CONCAT,
143
                        T_ENCAPSED_AND_WHITESPACE,
144
                        T_NONE,
145
                       );
146
 
147
        $numTokens = count($stringTokens);
148
 
149
        /*
150
            We know what the first two and last two tokens should be
151
            (because we put them there) so ignore this comment if those
152
            tokens were not parsed correctly. It obvously means this is not
153
            valid code.
154
        */
155
 
156
        // First token is always the opening PHP tag.
157
        if ($stringTokens[0]['code'] !== T_OPEN_TAG) {
158
            return;
159
        }
160
 
161
        // Last token is always the closing PHP tag.
162
        if ($stringTokens[($numTokens - 1)]['code'] !== T_CLOSE_TAG) {
163
            return;
164
        }
165
 
166
        // Second last token is always whitespace or a comment, depending
167
        // on the code inside the comment.
168
        if (in_array($stringTokens[($numTokens - 2)]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
169
            return;
170
        }
171
 
172
        $numComment = 0;
173
        $numCode    = 0;
174
 
175
        for ($i = 0; $i < $numTokens; $i++) {
176
            if (in_array($stringTokens[$i]['code'], $emptyTokens) === true) {
177
                // Looks like comment.
178
                $numComment++;
179
            } else {
180
                // Looks like code.
181
                $numCode++;
182
            }
183
        }
184
 
185
        // We subtract 3 from the token number so we ignore the start/end tokens
186
        // and their surrounding whitespace. We take 2 off the number of code
187
        // tokens so we ignore the start/end tokens.
188
        if ($numTokens > 3) {
189
            $numTokens -= 3;
190
        }
191
 
192
        if ($numCode >= 2) {
193
            $numCode -= 2;
194
        }
195
 
196
        $percentCode = ceil((($numCode / $numTokens) * 100));
197
        if ($percentCode > $this->maxPercentage) {
198
            // Just in case.
199
            $percentCode = min(100, $percentCode);
200
 
201
            $error = "This comment is ${percentCode}% valid code; is this commented out code?";
202
            $phpcsFile->addWarning($error, $stackPtr);
203
        }
204
 
205
    }//end process()
206
 
207
 
208
}//end class
209
 
210
?>