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_OutputBufferingIndentSniff.
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: OutputBufferingIndentSniff.php 258893 2008-05-02 06:37:15Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Squiz_Sniffs_Formatting_OutputBufferingIndentSniff.
19
 *
20
 * Checks the indenting used when an ob_start() call occurs.
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_Formatting_OutputBufferingIndentSniff 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_STRING);
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 The file being scanned.
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 ($tokens[$stackPtr]['content'] !== 'ob_start') {
61
            return;
62
        }
63
 
64
        $bufferEnd = $stackPtr;
65
        while (($bufferEnd = $phpcsFile->findNext(array(T_STRING, T_FUNCTION), ($bufferEnd + 1), null, false)) !== false) {
66
            if ($tokens[$bufferEnd]['code'] === T_FUNCTION) {
67
                // We should not cross funtions or move into functions.
68
                $bufferEnd = false;
69
                break;
70
            }
71
 
72
            $stringContent = $tokens[$bufferEnd]['content'];
73
            if (($stringContent === 'ob_end_clean') || ($stringContent === 'ob_end_flush')) {
74
                break;
75
            }
76
 
77
            if (($stringContent === 'ob_get_clean') || ($stringContent === 'ob_get_flush')) {
78
                // Generate the error because the functions are not allowed, but
79
                // continue to check the indentation.
80
                $phpcsFile->addError('Output buffering must be closed using ob_end_clean or ob_end_flush', $bufferEnd);
81
                break;
82
            }
83
        }
84
 
85
        if ($bufferEnd === false) {
86
            $phpcsFile->addError('Output buffering, started here, was never stopped', $stackPtr);
87
            return;
88
        }
89
 
90
        $requiredIndent = ($tokens[$stackPtr]['column'] + 3);
91
 
92
        for ($stackPtr; $stackPtr < $bufferEnd; $stackPtr++) {
93
            if (strpos($tokens[$stackPtr]['content'], $phpcsFile->eolChar) === false) {
94
                continue;
95
            }
96
 
97
            $nextContent = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), $bufferEnd, true);
98
            if ($tokens[$nextContent]['line'] !== ($tokens[$stackPtr]['line'] + 1)) {
99
                // Empty line.
100
                continue;
101
            }
102
 
103
            // The spaces at the start of inline HTML are not considered indent by
104
            // PHP_CodeSniffer, so we need to ignore them because their indentation
105
            // if not a coding standard issue, it is a HTML output issue.
106
            if ($tokens[$nextContent]['code'] === T_INLINE_HTML) {
107
                continue;
108
            }
109
 
110
            $foundIndent = ($tokens[$nextContent]['column'] - 1);
111
 
112
            // The line has content, now if it is less than the required indent, throw error.
113
            if ($foundIndent < $requiredIndent) {
114
                $error = "Buffered line not indented correctly. Expected at least $requiredIndent spaces; found $foundIndent.";
115
                $phpcsFile->addError($error, $nextContent);
116
            }
117
        }
118
 
119
    }//end process()
120
 
121
 
122
}//end class
123
 
124
?>