~ubuntu-branches/ubuntu/wily/php-codesniffer/wily

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.3.4/CodeSniffer/Standards/Generic/Sniffs/Strings/UnnecessaryStringConcatSniff.php

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2012-05-31 16:37:24 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120531163724-u6aiaubu8ks5dh5z
Tags: 1.3.4-0.1
* Non-maintainer upload.
* New upstream release (Closes: #599617, #634825).
* Swtiched debian/copyright to format 1.0 (rewrite was needed anyway, as the
upstream license changed).
* Switched package to pkg-php-tools and debhelper 8 sequencer.
* Now running unit tests at build time (so depends on phpunit (>= 3.6)).
* Section is now PHP.
* Added missing Build-Depends-Indep: php-pear.
* Added missing ${misc:Depends}.
* Added Vcs fields.
* Added homepage field.
* Reviewed short and long description.
* Added dependency on php-timer.
* Standards-Version: is now 3.9.3 (lots of changes, see above...).

Show diffs side-by-side

added added

removed removed

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