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_ScopeIndentSniff.
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: ScopeIndentSniff.php 254096 2008-03-03 03:28:15Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
if (class_exists('Generic_Sniffs_WhiteSpace_ScopeIndentSniff', true) === false) {
18
    throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_WhiteSpace_ScopeIndentSniff not found');
19
}
20
 
21
/**
22
 * Squiz_Sniffs_Whitespace_ScopeIndentSniff.
23
 *
24
 * Checks that control structures are structured correctly, and their content
25
 * is indented correctly.
26
 *
27
 * @category  PHP
28
 * @package   PHP_CodeSniffer
29
 * @author    Greg Sherwood <gsherwood@squiz.net>
30
 * @author    Marc McIntyre <mmcintyre@squiz.net>
31
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
32
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
33
 * @version   Release: 1.2.1
34
 * @link      http://pear.php.net/package/PHP_CodeSniffer
35
 */
36
class Squiz_Sniffs_WhiteSpace_ScopeIndentSniff extends Generic_Sniffs_WhiteSpace_ScopeIndentSniff
37
{
38
 
39
 
40
    /**
41
     * Calculates the expected indent of a token.
42
     *
43
     * Looks for ob_start() calls because those act as scope openers in the
44
     * Squiz coding standard, and so require additional indentation.
45
     *
46
     * @param array $tokens   The stack of tokens for this file.
47
     * @param int   $stackPtr The position of the token to get indent for.
48
     *
49
     * @return int
50
     */
51
    protected function calculateExpectedIndent(array $tokens, $stackPtr)
52
    {
53
        $expectedIndent = parent::calculateExpectedIndent($tokens, $stackPtr);
54
 
55
        // If we are in a function, check all tokens to the start of the
56
        // function. If we are not in a function, check all tokens to the
57
        // start of the file.
58
        $checkTo = 0;
59
 
60
        $tokenConditions = $tokens[$stackPtr]['conditions'];
61
        foreach ($tokenConditions as $id => $condition) {
62
            if ($condition === T_FUNCTION) {
63
                $checkTo = ($tokens[$id]['scope_opener'] + 1);
64
            }
65
        }
66
 
67
        for ($i = ($stackPtr - 1); $i >= $checkTo; $i--) {
68
            if ($tokens[$i]['code'] !== T_STRING) {
69
                continue;
70
            }
71
 
72
            if ($tokens[$i]['content'] === 'ob_start') {
73
                $expectedIndent += $this->indent;
74
            }
75
 
76
            $bufferClosers = array(
77
                              'ob_end_clean',
78
                              'ob_end_flush',
79
                              'ob_get_clean',
80
                              'ob_get_flush',
81
                             );
82
 
83
            if (in_array($tokens[$i]['content'], $bufferClosers) === true) {
84
                $expectedIndent -= $this->indent;
85
            }
86
        }//end for
87
 
88
        return $expectedIndent;
89
 
90
    }//end calculateExpectedIndent()
91
 
92
 
93
}//end class
94
 
95
?>