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_LanguageConstructSpacingSniff.
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: LanguageConstructSpacingSniff.php 245680 2007-11-06 01:09:34Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Squiz_Sniffs_WhiteSpace_LanguageConstructSpacingSniff.
19
 *
20
 * Ensures all language constructs (without brackets) contain a
21
 * single space between themselves and their content.
22
 *
23
 * @category  PHP
24
 * @package   PHP_CodeSniffer
25
 * @author    Greg Sherwood <gsherwood@squiz.net>
26
 * @author    Marc McIntyre <mmcintyre@squiz.net>
27
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
28
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
29
 * @version   Release: 1.2.1
30
 * @link      http://pear.php.net/package/PHP_CodeSniffer
31
 */
32
class Squiz_Sniffs_WhiteSpace_LanguageConstructSpacingSniff implements PHP_CodeSniffer_Sniff
33
{
34
 
35
 
36
    /**
37
     * Returns an array of tokens this test wants to listen for.
38
     *
39
     * @return array
40
     */
41
    public function register()
42
    {
43
        return array(
44
                T_ECHO,
45
                T_PRINT,
46
                T_RETURN,
47
                T_INCLUDE,
48
                T_INCLUDE_ONCE,
49
                T_REQUIRE,
50
                T_REQUIRE_ONCE,
51
                T_NEW,
52
               );
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 in
62
     *                                        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 ($tokens[($stackPtr + 1)]['code'] === T_SEMICOLON) {
71
            // No content for this language construct.
72
            return;
73
        }
74
 
75
        if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
76
            $content       = $tokens[($stackPtr + 1)]['content'];
77
            $contentLength = strlen($content);
78
            if ($contentLength !== 1) {
79
                $error = "Language constructs must be followed by a single space; expected 1 space but found $contentLength";
80
                $phpcsFile->addError($error, $stackPtr);
81
            }
82
        } else {
83
            $expected = $tokens[$stackPtr]['content'].' '.$tokens[($stackPtr + 1)]['content'];
84
            $found    = $tokens[$stackPtr]['content'].$tokens[($stackPtr + 1)]['content'];
85
            $error    = "Language constructs must be followed by a single space; expected \"$expected\" but found \"$found\"";
86
            $phpcsFile->addError($error, $stackPtr);
87
        }
88
 
89
    }//end process()
90
 
91
 
92
}//end class
93
 
94
?>