Subversion Repositories Applications.framework

Rev

Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 1
<?php
2
/**
3
 * Generic_Sniffs_Strings_UnnecessaryStringConcatSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
11
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
34 aurelien 12
 * @version   CVS: $Id: UnnecessaryStringConcatSniff.php 34 2009-04-09 07:34:39Z aurelien $
5 aurelien 13
 * @link      http://pear.php.net/package/PHP_CodeSniffer
14
 */
15
 
16
/**
17
 * Generic_Sniffs_Strings_UnnecessaryStringConcatSniff.
18
 *
19
 * Checks that two strings are not concatenated together; suggests
20
 * using one string instead.
21
 *
22
 * @category  PHP
23
 * @package   PHP_CodeSniffer
24
 * @author    Greg Sherwood <gsherwood@squiz.net>
25
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
26
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
27
 * @version   Release: 1.2.0RC1
28
 * @link      http://pear.php.net/package/PHP_CodeSniffer
29
 */
30
class Generic_Sniffs_Strings_UnnecessaryStringConcatSniff implements PHP_CodeSniffer_Sniff
31
{
32
 
33
    /**
34
     * A list of tokenizers this sniff supports.
35
     *
36
     * @var array
37
     */
38
    public $supportedTokenizers = array(
39
                                   'PHP',
40
                                   'JS',
41
                                  );
42
 
43
    /**
44
     * If true, an error will be thrown; otherwise a warning.
45
     *
46
     * @var bool
47
     */
48
    protected $error = true;
49
 
50
 
51
    /**
52
     * Returns an array of tokens this test wants to listen for.
53
     *
54
     * @return array
55
     */
56
    public function register()
57
    {
58
        return array(
59
                T_STRING_CONCAT,
60
                T_PLUS,
61
               );
62
 
63
    }//end register()
64
 
65
 
66
    /**
67
     * Processes this sniff, when one of its tokens is encountered.
68
     *
69
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
70
     * @param int                  $stackPtr  The position of the current token
71
     *                                        in the stack passed in $tokens.
72
     *
73
     * @return void
74
     */
75
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
76
    {
77
        // Work out which type of file this is for.
78
        $tokens = $phpcsFile->getTokens();
79
        if ($tokens[$stackPtr]['code'] === T_STRING_CONCAT) {
80
            if ($phpcsFile->tokenizerType === 'JS') {
81
                return;
82
            }
83
        } else {
84
            if ($phpcsFile->tokenizerType === 'PHP') {
85
                return;
86
            }
87
        }
88
 
89
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
90
        $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
91
        if ($prev === false || $next === false) {
92
            return;
93
        }
94
 
95
        $stringTokens = PHP_CodeSniffer_Tokens::$stringTokens;
96
        if (in_array($tokens[$prev]['code'], $stringTokens) === true
97
            && in_array($tokens[$next]['code'], $stringTokens) === true
98
        ) {
99
            $error = 'String concat is not required here; use a single string instead';
100
            if ($this->error === true) {
101
                $phpcsFile->addError($error, $stackPtr);
102
            } else {
103
                $phpcsFile->addWarning($error, $stackPtr);
104
            }
105
        }
106
 
107
    }//end process()
108
 
109
 
110
}//end class
111
 
112
?>