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_Strings_DoubleQuoteUsageSniff.
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: DoubleQuoteUsageSniff.php 289078 2009-10-02 02:01:28Z sebastian $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Squiz_Sniffs_Strings_DoubleQuoteUsageSniff.
19
 *
20
 * Makes sure that any use of Double Quotes ("") are warranted.
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_Strings_DoubleQuoteUsageSniff 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(
43
                T_CONSTANT_ENCAPSED_STRING,
44
                T_DOUBLE_QUOTED_STRING,
45
               );
46
 
47
    }//end register()
48
 
49
 
50
    /**
51
     * Processes this test, when one of its tokens is encountered.
52
     *
53
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
54
     * @param int                  $stackPtr  The position of the current token
55
     *                                        in the stack passed in $tokens.
56
     *
57
     * @return void
58
     */
59
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
60
    {
61
        $tokens = $phpcsFile->getTokens();
62
 
63
        // The use of variables in double quoted strings is not allowed.
64
        if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING) {
65
            $stringTokens = token_get_all('<?php '.$tokens[$stackPtr]['content']);
66
            foreach ($stringTokens as $token) {
67
                if (is_array($token) === true && $token[0] === T_VARIABLE) {
68
                    $error = 'Variable "'.$token[1].'" not allowed in double quoted string; use concatenation instead';
69
                    $phpcsFile->addError($error, $stackPtr);
70
                }
71
            }
72
 
73
            return;
74
        }//end if
75
 
76
        $workingString = $tokens[$stackPtr]['content'];
77
 
78
        // Check if it's a double quoted string.
79
        if (strpos($workingString, '"') === false) {
80
            return;
81
        }
82
 
83
        // Make sure it's not a part of a string started above.
84
        // If it is, then we have already checked it.
85
        if ($workingString[0] !== '"') {
86
            return;
87
        }
88
 
89
        // Work through the following tokens, in case this string is stretched
90
        // over multiple Lines.
91
        for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
92
            if ($tokens[$i]['type'] !== 'T_CONSTANT_ENCAPSED_STRING') {
93
                break;
94
            }
95
 
96
            $workingString .= $tokens[$i]['content'];
97
        }
98
 
99
        $allowedChars = array(
100
                         '\0',
101
                         '\n',
102
                         '\r',
103
                         '\f',
104
                         '\t',
105
                         '\v',
106
                         '\x',
107
                         '\'',
108
                        );
109
 
110
        foreach ($allowedChars as $testChar) {
111
            if (strpos($workingString, $testChar) !== false) {
112
                return;
113
            }
114
        }
115
 
116
        $error = "String $workingString does not require double quotes; use single quotes instead";
117
        $phpcsFile->addError($error, $stackPtr);
118
 
119
    }//end process()
120
 
121
 
122
}//end class
123
 
124
?>