Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * PEAR_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 287526 2009-08-21 05:42:05Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * PEAR_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 PEAR_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
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
        // 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
        $scopeStart  = $tokens[$stackPtr]['scope_opener'];
67
        $scopeEnd    = $tokens[$stackPtr]['scope_closer'];
68
 
69
        // If the scope closer doesn't think it belongs to this scope opener
70
        // then the opener is sharing its closer ith other tokens. We only
71
        // want to process the closer once, so skip this one.
72
        if ($tokens[$scopeEnd]['scope_condition'] !== $stackPtr) {
73
            return;
74
        }
75
 
76
        // We need to actually find the first piece of content on this line,
77
        // because if this is a method with tokens before it (public, static etc)
78
        // or an if with an else before it, then we need to start the scope
79
        // checking from there, rather than the current token.
80
        $lineStart = ($stackPtr - 1);
81
        for ($lineStart; $lineStart > 0; $lineStart--) {
82
            if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) {
83
                break;
84
            }
85
        }
86
 
87
        // We found a new line, now go forward and find the first non-whitespace
88
        // token.
89
        $lineStart= $phpcsFile->findNext(
90
            array(T_WHITESPACE),
91
            ($lineStart + 1),
92
            null,
93
            true
94
        );
95
 
96
        $startColumn = $tokens[$lineStart]['column'];
97
 
98
        // Check that the closing brace is on it's own line.
99
        $lastContent = $phpcsFile->findPrevious(
100
            array(T_WHITESPACE),
101
            ($scopeEnd - 1),
102
            $scopeStart,
103
            true
104
        );
105
 
106
        if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) {
107
            $error = 'Closing brace must be on a line by itself';
108
            $phpcsFile->addError($error, $scopeEnd);
109
            return;
110
        }
111
 
112
        // Check now that the closing brace is lined up correctly.
113
        $braceIndent   = $tokens[$scopeEnd]['column'];
114
        $isBreakCloser = ($tokens[$scopeEnd]['code'] === T_BREAK);
115
        if (in_array($tokens[$stackPtr]['code'], array(T_CASE, T_DEFAULT)) === true
116
            && $isBreakCloser === true
117
        ) {
118
            // BREAK statements should be indented 4 spaces from the
119
            // CASE or DEFAULT statement.
120
            if ($braceIndent !== ($startColumn + 4)) {
121
                $error = 'Break statement indented incorrectly; expected '.($startColumn + 3).' spaces, found '.($braceIndent - 1);
122
                $phpcsFile->addError($error, $scopeEnd);
123
            }
124
        } else {
125
            if ($braceIndent !== $startColumn) {
126
                $error = 'Closing brace indented incorrectly; expected '.($startColumn - 1).' spaces, found '.($braceIndent - 1);
127
                $phpcsFile->addError($error, $scopeEnd);
128
            }
129
        }
130
 
131
    }//end process()
132
 
133
 
134
}//end class
135
 
136
?>