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_Whitespace_ScopeClosingBraceSniff.
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: ScopeClosingBraceSniff.php 240383 2007-07-27 05:38:59Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Squiz_Sniffs_Whitespace_ScopeClosingBraceSniff.
19
 *
20
 * Checks that the closing braces of scopes are aligned correctly.
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_ScopeClosingBraceSniff 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 PHP_CodeSniffer_Tokens::$scopeOpeners;
43
 
44
    }//end register()
45
 
46
 
47
    /**
48
     * Processes this test, when one of its tokens is encountered.
49
     *
50
     * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
51
     * @param int                  $stackPtr  The position of the current token in the
52
     *                                        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
        // If this is an inline condition (ie. there is no scope opener), then
61
        // return, as this is not a new scope.
62
        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
63
            return;
64
        }
65
 
66
        // We need to actually find the first piece of content on this line,
67
        // as if this is a method with tokens before it (public, static etc)
68
        // or an if with an else before it, then we need to start the scope
69
        // checking from there, rather than the current token.
70
        $lineStart = ($stackPtr - 1);
71
        for ($lineStart; $lineStart > 0; $lineStart--) {
72
            if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) {
73
                break;
74
            }
75
        }
76
 
77
        // We found a new line, now go forward and find the
78
        // first non-whitespace token.
79
        $lineStart = $phpcsFile->findNext(array(T_WHITESPACE), ($lineStart + 1), null, true);
80
 
81
        $startColumn = $tokens[$lineStart]['column'];
82
        $scopeStart  = $tokens[$stackPtr]['scope_opener'];
83
        $scopeEnd    = $tokens[$stackPtr]['scope_closer'];
84
 
85
        // Check that the closing brace is on it's own line.
86
        $lastContent = $phpcsFile->findPrevious(array(T_WHITESPACE), ($scopeEnd - 1), $scopeStart, true);
87
        if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) {
88
            $error = 'Closing brace must be on a line by itself';
89
            $phpcsFile->addError($error, $scopeEnd);
90
            return;
91
        }
92
 
93
        // Check now that the closing brace is lined up correctly.
94
        $braceIndent = $tokens[$scopeEnd]['column'];
95
        if (in_array($tokens[$stackPtr]['code'], array(T_CASE, T_DEFAULT)) === false) {
96
            if ($braceIndent !== $startColumn) {
97
                $error = 'Closing brace indented incorrectly; expected '.($startColumn - 1).' spaces, found '.($braceIndent - 1);
98
                $phpcsFile->addError($error, $scopeEnd);
99
            }
100
        }
101
 
102
    }//end process()
103
 
104
 
105
}//end class
106
 
107
?>