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

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-07-21 14:42:41 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20140721144241-g4orlcuk4jzn9mhs
Tags: 1.5.3-1
* Team upload
* Focus on stable release
* Update copyright
* Bump standards version to 3.9.5
* Update Homepage
* Use ${phppear:…} instead of hardcoding them
* Run tests in dh_auto_test
* Update patch with gbp pq
* Simplify configuration installation
* Edit package.xml to move script
* Add DEP-8 tests

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-2014 Squiz Pty Ltd (ABN 77 084 670 600)
 
11
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt 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-2014 Squiz Pty Ltd (ABN 77 084 670 600)
 
25
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 
26
 * @version   Release: 1.5.3
 
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
?>