Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Class Declaration Test.
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: ClassDeclarationSniff.php 259960 2008-05-19 05:59:25Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
if (class_exists('PEAR_Sniffs_Classes_ClassDeclarationSniff', true) === false) {
18
    $error = 'Class PEAR_Sniffs_Classes_ClassDeclarationSniff not found';
19
    throw new PHP_CodeSniffer_Exception($error);
20
}
21
 
22
/**
23
 * Class Declaration Test.
24
 *
25
 * Checks the declaration of the class and its inheritance is correct.
26
 *
27
 * @category  PHP
28
 * @package   PHP_CodeSniffer
29
 * @author    Greg Sherwood <gsherwood@squiz.net>
30
 * @author    Marc McIntyre <mmcintyre@squiz.net>
31
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
32
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
33
 * @version   Release: 1.2.1
34
 * @link      http://pear.php.net/package/PHP_CodeSniffer
35
 */
36
class Squiz_Sniffs_Classes_ClassDeclarationSniff extends PEAR_Sniffs_Classes_ClassDeclarationSniff
37
{
38
 
39
 
40
    /**
41
     * Returns an array of tokens this test wants to listen for.
42
     *
43
     * @return array
44
     */
45
    public function register()
46
    {
47
        return array(
48
                T_CLASS,
49
                T_INTERFACE,
50
               );
51
 
52
    }//end register()
53
 
54
 
55
    /**
56
     * Processes this test, when one of its tokens is encountered.
57
     *
58
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
59
     * @param int                  $stackPtr  The position of the current token
60
     *                                         in the stack passed in $tokens.
61
     *
62
     * @return void
63
     */
64
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
65
    {
66
        // We want all the errors from the PEAR standard, plus some of our own.
67
        parent::process($phpcsFile, $stackPtr);
68
 
69
        $tokens = $phpcsFile->getTokens();
70
 
71
        /*
72
            Check that this is the only class or interface in the file.
73
        */
74
 
75
        $nextClass = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), ($stackPtr + 1));
76
 
77
        if ($nextClass !== false) {
78
            // We have another, so an error is thrown.
79
            $error = 'Only one interface or class is allowed in a file';
80
            $phpcsFile->addError($error, $nextClass);
81
        }
82
 
83
        /*
84
            Check alignment of the keyword and braces.
85
        */
86
 
87
        if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) {
88
            $prevContent = $tokens[($stackPtr - 1)]['content'];
89
            if ($prevContent !== $phpcsFile->eolChar) {
90
                $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
91
                $spaces     = strlen($blankSpace);
92
 
93
                if (in_array($tokens[($stackPtr - 2)]['code'], array(T_ABSTRACT, T_FINAL)) === false) {
94
                    if ($spaces !== 0) {
95
                        $type  = strtolower($tokens[$stackPtr]['content']);
96
                        $error = "Expected 0 spaces before $type keyword; $spaces found";
97
                        $phpcsFile->addError($error, $stackPtr);
98
                    }
99
                } else {
100
                    if ($spaces !== 1) {
101
                        $type        = strtolower($tokens[$stackPtr]['content']);
102
                        $prevContent = strtolower($tokens[($stackPtr - 2)]['content']);
103
                        $error       = "Expected 1 space between $prevContent and $type keywords; $spaces found";
104
                        $phpcsFile->addError($error, $stackPtr);
105
                    }
106
                }
107
            }
108
        }//end if
109
 
110
        if (isset($tokens[$stackPtr]['scope_opener']) === false) {
111
            $error  = 'Possible parse error: ';
112
            $error .= $tokens[$stackPtr]['content'];
113
            $error .= ' missing opening or closing brace';
114
            $phpcsFile->addWarning($error, $stackPtr);
115
            return;
116
        }
117
 
118
        $closeBrace = $tokens[$stackPtr]['scope_closer'];
119
        if ($tokens[($closeBrace - 1)]['code'] === T_WHITESPACE) {
120
            $prevContent = $tokens[($closeBrace - 1)]['content'];
121
            if ($prevContent !== $phpcsFile->eolChar) {
122
                $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
123
                $spaces     = strlen($blankSpace);
124
                if ($spaces !== 0) {
125
                    $error = "Expected 0 spaces before closing brace; $spaces found";
126
                    $phpcsFile->addError($error, $closeBrace);
127
                }
128
            }
129
        }
130
 
131
        // Check that the closing brace has one blank line after it.
132
        $nextContent = $phpcsFile->findNext(array(T_WHITESPACE, T_COMMENT), ($closeBrace + 1), null, true);
133
        if ($nextContent === false) {
134
            // No content found, so we reached the end of the file.
135
            // That means there was no closing tag either.
136
            $error  = 'Closing brace of a ';
137
            $error .= $tokens[$stackPtr]['content'];
138
            $error .= ' must be followed by a blank line and then a closing PHP tag';
139
            $phpcsFile->addError($error, $closeBrace);
140
        } else {
141
            $nextLine  = $tokens[$nextContent]['line'];
142
            $braceLine = $tokens[$closeBrace]['line'];
143
            if ($braceLine === $nextLine) {
144
                $error  = 'Closing brace of a ';
145
                $error .= $tokens[$stackPtr]['content'];
146
                $error .= ' must be followed by a single blank line';
147
                $phpcsFile->addError($error, $closeBrace);
148
            } else if ($nextLine !== ($braceLine + 2)) {
149
                $difference = ($nextLine - $braceLine - 1).' lines';
150
                $error      = 'Closing brace of a ';
151
                $error     .= $tokens[$stackPtr]['content'];
152
                $error     .= ' must be followed by a single blank line; found '.$difference;
153
                $phpcsFile->addError($error, $closeBrace);
154
            }
155
        }//end if
156
 
157
        // Check the closing brace is on it's own line, but allow
158
        // for comments like "//end class".
159
        $nextContent = $phpcsFile->findNext(T_COMMENT, ($closeBrace + 1), null, true);
160
        if ($tokens[$nextContent]['content'] !== $phpcsFile->eolChar && $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']) {
161
            $type  = strtolower($tokens[$stackPtr]['content']);
162
            $error = "Closing $type brace must be on a line by itself";
163
            $phpcsFile->addError($error, $closeBrace);
164
        }
165
 
166
        /*
167
            Check that each of the parent classes or interfaces specified
168
            are spaced correctly.
169
        */
170
 
171
        // We need to map out each of the possible tokens in the declaration.
172
        $keyword      = $stackPtr;
173
        $openingBrace = $tokens[$stackPtr]['scope_opener'];
174
        $className    = $phpcsFile->findNext(T_STRING, $stackPtr);
175
 
176
        /*
177
            Now check the spacing of each token.
178
        */
179
 
180
        $name = strtolower($tokens[$keyword]['content']);
181
 
182
        // Spacing of the keyword.
183
        $gap = $tokens[($stackPtr + 1)]['content'];
184
        if (strlen($gap) !== 1) {
185
            $found = strlen($gap);
186
            $error = "Expected 1 space between $name keyword and $name name; $found found";
187
            $phpcsFile->addError($error, $stackPtr);
188
        }
189
 
190
        // Check after the name.
191
        $gap = $tokens[($className + 1)]['content'];
192
        if (strlen($gap) !== 1) {
193
            $found = strlen($gap);
194
            $error = "Expected 1 space after $name name; $found found";
195
            $phpcsFile->addError($error, $stackPtr);
196
        }
197
 
198
        // Now check each of the parents.
199
        $parents    = array();
200
        $nextParent = ($className + 1);
201
        while (($nextParent = $phpcsFile->findNext(array(T_STRING, T_IMPLEMENTS), ($nextParent + 1), ($openingBrace - 1))) !== false) {
202
            $parents[] = $nextParent;
203
        }
204
 
205
        $parentCount = count($parents);
206
 
207
        for ($i = 0; $i < $parentCount; $i++) {
208
            if ($tokens[$parents[$i]]['code'] === T_IMPLEMENTS) {
209
                continue;
210
            }
211
 
212
            if ($tokens[($parents[$i] - 1)]['code'] !== T_WHITESPACE) {
213
                $name  = $tokens[$parents[$i]]['content'];
214
                $error = "Expected 1 space before \"$name\"; 0 found";
215
                $phpcsFile->addError($error, ($nextComma + 1));
216
            } else {
217
                $spaceBefore = strlen($tokens[($parents[$i] - 1)]['content']);
218
                if ($spaceBefore !== 1) {
219
                    $name  = $tokens[$parents[$i]]['content'];
220
                    $error = "Expected 1 space before \"$name\"; $spaceBefore found";
221
                    $phpcsFile->addError($error, $stackPtr);
222
                }
223
            }
224
 
225
            if ($tokens[($parents[$i] + 1)]['code'] !== T_COMMA) {
226
                if ($i !== ($parentCount - 1)) {
227
                    // This is not the last parent, and the comma
228
                    // is not where we expect it to be.
229
                    if ($tokens[($parents[$i] + 2)]['code'] !== T_IMPLEMENTS) {
230
                        $found = strlen($tokens[($parents[$i] + 1)]['content']);
231
                        $name  = $tokens[$parents[$i]]['content'];
232
                        $error = "Expected 0 spaces between \"$name\" and comma; $found found";
233
                        $phpcsFile->addError($error, $stackPtr);
234
                    }
235
                }
236
 
237
                $nextComma = $phpcsFile->findNext(T_COMMA, $parents[$i]);
238
            } else {
239
                $nextComma = ($parents[$i] + 1);
240
            }
241
        }//end for
242
 
243
    }//end process()
244
 
245
 
246
}//end class
247
 
248
?>