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_ControlStructures_InlineControlStructureSniff.
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: InlineIfDeclarationSniff.php 244212 2007-10-15 04:46:21Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Squiz_Sniffs_ControlStructures_InlineIfDeclarationSniff.
19
 *
20
 * Tests the spacing of shorthand IF statements.
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_ControlStructures_InlineIfDeclarationSniff 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_INLINE_THEN);
43
 
44
    }//end register()
45
 
46
 
47
    /**
48
     * Processes this sniff, 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
        // Find the opening bracket of the inline IF.
61
        for ($i = ($stackPtr - 1); $i > 0; $i--) {
62
            if (isset($tokens[$i]['parenthesis_opener']) === true && $tokens[$i]['parenthesis_opener'] < $i) {
63
                $i = $tokens[$i]['parenthesis_opener'];
64
                continue;
65
            }
66
 
67
            if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
68
                break;
69
            }
70
        }
71
 
72
        if ($i <= 0) {
73
            // Could not find the begining of the statement, so we can't
74
            // find the end either.
75
            return;
76
        }
77
 
78
        $statementEnd = $tokens[$i]['parenthesis_closer'];
79
 
80
        // Make sure it's all on the same line.
81
        if ($tokens[$statementEnd]['line'] !== $tokens[$stackPtr]['line']) {
82
            $error = 'Inline shorthand IF statement must be declared on a single line';
83
            $phpcsFile->addError($error, $stackPtr);
84
            return;
85
        }
86
 
87
        // Make sure there are spaces around the question mark.
88
        $contentBefore = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true);
89
        $contentAfter  = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
90
        if ($tokens[$contentBefore]['code'] !== T_CLOSE_PARENTHESIS) {
91
            $error = 'Inline shorthand IF statement requires brackets around comparison';
92
            $phpcsFile->addError($error, $stackPtr);
93
            return;
94
        }
95
 
96
        $spaceBefore = ($tokens[$stackPtr]['column'] - ($tokens[$contentBefore]['column'] + strlen($tokens[$contentBefore]['content'])));
97
        if ($spaceBefore !== 1) {
98
            $error = "Inline shorthand IF statement requires 1 space before THEN; $spaceBefore found.";
99
            $phpcsFile->addError($error, $stackPtr);
100
        }
101
 
102
        $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1));
103
        if ($spaceAfter !== 1) {
104
            $error = "Inline shorthand IF statement requires 1 space after THEN; $spaceAfter found.";
105
            $phpcsFile->addError($error, $stackPtr);
106
        }
107
 
108
        // If there is an else in this condition, make sure it has correct spacing.
109
        $inlineElse = $phpcsFile->findNext(array(T_COLON), ($stackPtr + 1), $statementEnd, false);
110
        if ($inlineElse === false) {
111
            // No else condition.
112
            return;
113
        }
114
 
115
        $contentBefore = $phpcsFile->findPrevious(array(T_WHITESPACE), ($inlineElse - 1), null, true);
116
        $contentAfter  = $phpcsFile->findNext(array(T_WHITESPACE), ($inlineElse + 1), null, true);
117
 
118
        $spaceBefore = ($tokens[$inlineElse]['column'] - ($tokens[$contentBefore]['column'] + strlen($tokens[$contentBefore]['content'])));
119
        if ($spaceBefore !== 1) {
120
            $error = "Inline shorthand IF statement requires 1 space before ELSE; $spaceBefore found.";
121
            $phpcsFile->addError($error, $inlineElse);
122
        }
123
 
124
        $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$inlineElse]['column'] + 1));
125
        if ($spaceAfter !== 1) {
126
            $error = "Inline shorthand IF statement requires 1 space after ELSE; $spaceAfter found.";
127
            $phpcsFile->addError($error, $inlineElse);
128
        }
129
 
130
    }//end process()
131
 
132
 
133
}//end class
134
 
135
 
136
?>