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_FunctionClosingBraceSpaceSniff.
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: FunctionClosingBraceSpaceSniff.php 267849 2008-10-27 02:59:21Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Squiz_Sniffs_WhiteSpace_FunctionClosingBraceSpaceSniff.
19
 *
20
 * Checks that there is one empty line before the closing brace of a function.
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_FunctionClosingBraceSpaceSniff 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
                                   'JS',
42
                                  );
43
 
44
 
45
    /**
46
     * Returns an array of tokens this test wants to listen for.
47
     *
48
     * @return array
49
     */
50
    public function register()
51
    {
52
        return array(T_FUNCTION);
53
 
54
    }//end register()
55
 
56
 
57
    /**
58
     * Processes this test, when one of its tokens is encountered.
59
     *
60
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
61
     * @param int                  $stackPtr  The position of the current token
62
     *                                        in the stack passed in $tokens.
63
     *
64
     * @return void
65
     */
66
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
67
    {
68
        $tokens = $phpcsFile->getTokens();
69
 
70
        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
71
            // Probably an interface method.
72
            return;
73
        }
74
 
75
        $closeBrace  = $tokens[$stackPtr]['scope_closer'];
76
        $prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBrace - 1), null, true);
77
 
78
        // Special case for empty JS functions
79
        if ($phpcsFile->tokenizerType === 'JS' && $prevContent === $tokens[$stackPtr]['scope_opener']) {
80
            // In this case, the opening and closing brace must be
81
            // right next to each other.
82
            if ($tokens[$stackPtr]['scope_closer'] !== ($tokens[$stackPtr]['scope_opener'] + 1)) {
83
                $error = 'The opening and closing braces of empty functions must be directly next to each other; e.g., function () {}';
84
                $phpcsFile->addError($error, $closeBrace);
85
            }
86
 
87
            return;
88
        }
89
 
90
        $braceLine = $tokens[$closeBrace]['line'];
91
        $prevLine  = $tokens[$prevContent]['line'];
92
 
93
        $found = ($braceLine - $prevLine - 1);
94
        if ($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true || isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
95
            // Nested function.
96
            if ($found < 0) {
97
                $error = "Closing brace of nested function must be on a new line";
98
                $phpcsFile->addError($error, $closeBrace);
99
            } else if ($found > 0) {
100
                $error = "Expected 0 blank lines before closing brace of nested function; $found found";
101
                $phpcsFile->addError($error, $closeBrace);
102
            }
103
        } else {
104
            if ($found !== 1) {
105
                $error = "Expected 1 blank line before closing function brace; $found found";
106
                $phpcsFile->addError($error, $closeBrace);
107
            }
108
        }
109
 
110
    }//end process()
111
 
112
 
113
}//end class
114
 
115
?>