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_Formatting_FunctionSpacingSniff.
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: FunctionSpacingSniff.php 253905 2008-02-28 06:06:00Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Squiz_Sniffs_WhiteSpace_FunctionSpacingSniff.
19
 *
20
 * Checks the separation between methods in a class or interface.
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_WhiteSpace_FunctionSpacingSniff implements PHP_CodeSniffer_Sniff
32
{
33
 
34
 
35
    /**
36
     * Returns an array of tokens this test wants to listen for.
37
     *
38
     * @return array
39
     */
40
    public function register()
41
    {
42
        return array(T_FUNCTION);
43
 
44
    }//end register()
45
 
46
 
47
    /**
48
     * Processes this sniff, when one of its tokens is encountered.
49
     *
50
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
51
     * @param int                  $stackPtr  The position of the current token
52
     *                                        in the stack passed in $tokens.
53
     *
54
     * @return void
55
     */
56
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
57
    {
58
        $tokens = $phpcsFile->getTokens();
59
 
60
        /*
61
            Check the number of blank lines
62
            after the function.
63
        */
64
 
65
        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
66
            // Must be an interface method, so the closer is the semi-colon.
67
            $closer = $phpcsFile->findNext(T_SEMICOLON, $stackPtr);
68
        } else {
69
            $closer = $tokens[$stackPtr]['scope_closer'];
70
        }
71
 
72
        // There needs to be 2 blank lines after the closer.
73
        $nextLineToken = null;
74
        for ($i = $closer; $i < $phpcsFile->numTokens; $i++) {
75
            if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) === false) {
76
                continue;
77
            } else {
78
                $nextLineToken = ($i + 1);
79
                break;
80
            }
81
        }
82
 
83
        if (is_null($nextLineToken) === true) {
84
            // Never found the next line, which means
85
            // there are 0 blank lines after the function.
86
            $foundLines = 0;
87
        } else {
88
            $nextContent = $phpcsFile->findNext(array(T_WHITESPACE), ($nextLineToken + 1), null, true);
89
            if ($nextContent === false) {
90
                // We are at the end of the file.
91
                $foundLines = 0;
92
            } else {
93
                $foundLines = ($tokens[$nextContent]['line'] - $tokens[$nextLineToken]['line']);
94
            }
95
        }
96
 
97
        if ($foundLines !== 2) {
98
            $phpcsFile->addError("Expected 2 blank lines after function; $foundLines found", $closer);
99
        }
100
 
101
        /*
102
            Check the number of blank lines
103
            before the function.
104
        */
105
 
106
        $prevLineToken = null;
107
        for ($i = $stackPtr; $i > 0; $i--) {
108
            if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) === false) {
109
                continue;
110
            } else {
111
                $prevLineToken = $i;
112
                break;
113
            }
114
        }
115
 
116
        if (is_null($prevLineToken) === true) {
117
            // Never found the previous line, which means
118
            // there are 0 blank lines before the function.
119
            $foundLines = 0;
120
        } else {
121
            $prevContent = $phpcsFile->findPrevious(array(T_WHITESPACE, T_DOC_COMMENT), $prevLineToken, null, true);
122
 
123
            // Before we throw an error, check that we are not throwing an error
124
            // for another function. We don't want to error for no blank lines after
125
            // the previous function and no blank lines before this one as well.
126
            $currentLine = $tokens[$stackPtr]['line'];
127
            $prevLine    = ($tokens[$prevContent]['line'] - 1);
128
            $i           = ($stackPtr - 1);
129
            $foundLines  = 0;
130
            while ($currentLine != $prevLine && $currentLine > 1 && $i > 0) {
131
                if (isset($tokens[$i]['scope_condition']) === true) {
132
                    $scopeCondition = $tokens[$i]['scope_condition'];
133
                    if ($tokens[$scopeCondition]['code'] === T_FUNCTION) {
134
                        // Found a previous function.
135
                        return;
136
                    }
137
                } else if ($tokens[$i]['code'] === T_FUNCTION) {
138
                    // Found another interface function.
139
                    return;
140
                }
141
 
142
                $currentLine = $tokens[$i]['line'];
143
                if ($currentLine === $prevLine) {
144
                    break;
145
                }
146
 
147
                if ($tokens[($i - 1)]['line'] < $currentLine && $tokens[($i + 1)]['line'] > $currentLine) {
148
                    // This token is on a line by itself. If it is whitespace, the line is empty.
149
                    if ($tokens[$i]['code'] === T_WHITESPACE) {
150
                        $foundLines++;
151
                    }
152
                }
153
 
154
                $i--;
155
            }//end while
156
        }//end if
157
 
158
        if ($foundLines !== 2) {
159
            $phpcsFile->addError("Expected 2 blank lines before function; $foundLines found", $stackPtr);
160
        }
161
 
162
    }//end process()
163
 
164
 
165
}//end class
166
 
167
?>