Completed
Push — feature/heredocinit-extend-sca... ( d59408...dabd0c )
by Juliette
03:26 queued 01:12
created

PregReplaceEModifierSniff::processRegexPattern()   C

Complexity

Conditions 9
Paths 48

Size

Total Lines 49
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 5.7446
c 0
b 0
f 0
cc 9
eloc 23
nc 48
nop 4
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\PregReplaceEModifierSniff.
4
 *
5
 * PHP version 5.5
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 * @copyright 2014 Cu.be Solutions bvba
11
 */
12
13
namespace PHPCompatibility\Sniffs\PHP;
14
15
use PHPCompatibility\AbstractFunctionCallParameterSniff;
16
17
/**
18
 * \PHPCompatibility\Sniffs\PHP\PregReplaceEModifierSniff.
19
 *
20
 * Check for usage of the `e` modifier with PCRE functions which is deprecated since PHP 5.5
21
 * and removed as of PHP 7.0.
22
 *
23
 * PHP version 5.5
24
 *
25
 * @category  PHP
26
 * @package   PHPCompatibility
27
 * @author    Wim Godden <[email protected]>
28
 * @copyright 2014 Cu.be Solutions bvba
29
 */
30
class PregReplaceEModifierSniff extends AbstractFunctionCallParameterSniff
31
{
32
33
    /**
34
     * Functions to check for.
35
     *
36
     * @var array
37
     */
38
    protected $targetFunctions = array(
39
        'preg_replace' => true,
40
        'preg_filter'  => true,
41
    );
42
43
    /**
44
     * Regex bracket delimiters.
45
     *
46
     * @var array
47
     */
48
    protected $doublesSeparators = array(
49
        '{' => '}',
50
        '[' => ']',
51
        '(' => ')',
52
        '<' => '>',
53
    );
54
55
56
    /**
57
     * Process the parameters of a matched function.
58
     *
59
     * @param \PHP_CodeSniffer_File $phpcsFile    The file being scanned.
60
     * @param int                   $stackPtr     The position of the current token in the stack.
61
     * @param string                $functionName The token content (function name) which was matched.
62
     * @param array                 $parameters   Array with information about the parameters.
63
     *
64
     * @return int|void Integer stack pointer to skip forward or void to continue
65
     *                  normal file processing.
66
     */
67
    public function processParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $parameters)
68
    {
69
        // Check the first parameter in the function call as that should contain the regex(es).
70
        if (isset($parameters[1]) === false) {
71
            return;
72
        }
73
74
        $tokens         = $phpcsFile->getTokens();
75
        $functionNameLc = strtolower($functionName);
76
        $firstParam     = $parameters[1];
77
78
        // Differentiate between an array of patterns passed and a single pattern.
79
        $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, $firstParam['start'], ($firstParam['end'] + 1), true);
80
        if ($nextNonEmpty !== false && ($tokens[$nextNonEmpty]['code'] === T_ARRAY || $tokens[$nextNonEmpty]['code'] === T_OPEN_SHORT_ARRAY)) {
81
            $arrayValues = $this->getFunctionCallParameters($phpcsFile, $nextNonEmpty);
82
            if ($functionNameLc === 'preg_replace_callback_array') {
83
                // For preg_replace_callback_array(), the patterns will be in the array keys.
84
                foreach ($arrayValues as $value) {
85
                    $hasKey = $phpcsFile->findNext(T_DOUBLE_ARROW, $value['start'], ($value['end'] + 1));
86
                    if ($hasKey === false) {
87
                        continue;
88
                    }
89
90
                    $value['end'] = ($hasKey - 1);
91
                    $value['raw'] = trim($phpcsFile->getTokensAsString($value['start'], ($hasKey - $value['start'])));
92
                    $this->processRegexPattern($value, $phpcsFile, $value['end'], $functionName);
93
                }
94
95
            } else {
96
                // Otherwise, the patterns will be in the array values.
97
                foreach ($arrayValues as $value) {
98
                    $hasKey = $phpcsFile->findNext(T_DOUBLE_ARROW, $value['start'], ($value['end'] + 1));
99
                    if ($hasKey !== false) {
100
                        $value['start'] = ($hasKey + 1);
101
                        $value['raw']   = trim($phpcsFile->getTokensAsString($value['start'], (($value['end'] + 1) - $value['start'])));
102
                    }
103
104
                    $this->processRegexPattern($value, $phpcsFile, $value['end'], $functionName);
105
                }
106
            }
107
108
        } else {
109
            $this->processRegexPattern($firstParam, $phpcsFile, $stackPtr, $functionName);
110
        }
111
    }
112
113
114
    /**
115
     * Do a version check to determine if this sniff needs to run at all.
116
     *
117
     * @return bool
118
     */
119
    protected function bowOutEarly()
120
    {
121
        return ($this->supportsAbove('5.5') === false);
122
    }
123
124
125
    /**
126
     * Analyse a potential regex pattern for usage of the /e modifier.
127
     *
128
     * @param array                 $pattern      Array containing the start and end token
129
     *                                            pointer of the potential regex pattern and
130
     *                                            the raw string value of the pattern.
131
     * @param \PHP_CodeSniffer_File $phpcsFile    The file being scanned.
132
     * @param int                   $stackPtr     The position of the current token in the
133
     *                                            stack passed in $tokens.
134
     * @param string                $functionName The function which contained the pattern.
135
     *
136
     * @return void
137
     */
138
    protected function processRegexPattern($pattern, \PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName)
139
    {
140
        $tokens = $phpcsFile->getTokens();
141
142
        /*
143
         * The pattern might be build up of a combination of strings, variables
144
         * and function calls. We are only concerned with the strings.
145
         */
146
        $regex = '';
147
        for ($i = $pattern['start']; $i <= $pattern['end']; $i++) {
148
            if (in_array($tokens[$i]['code'], \PHP_CodeSniffer_Tokens::$stringTokens, true) === true) {
149
                $content = $this->stripQuotes($tokens[$i]['content']);
150
                if ($tokens[$i]['code'] === T_DOUBLE_QUOTED_STRING) {
151
                    $content = $this->stripVariables($content);
152
                }
153
154
                $regex .= trim($content);
155
            }
156
        }
157
158
        // Deal with multi-line regexes which were broken up in several string tokens.
159
        if ($tokens[$pattern['start']]['line'] !== $tokens[$pattern['end']]['line']) {
160
            $regex = $this->stripQuotes($regex);
161
        }
162
163
        if ($regex === '') {
164
            // No string token found in the first parameter, so skip it (e.g. if variable passed in).
165
            return;
166
        }
167
168
        $regexFirstChar = substr($regex, 0, 1);
169
170
        // Make sure that the character identified as the delimiter is valid.
171
        // Otherwise, it is a false positive caused by the string concatenation.
172
        if (preg_match('`[a-z0-9\\\\ ]`i', $regexFirstChar) === 1) {
173
            return;
174
        }
175
176
        if (isset($this->doublesSeparators[$regexFirstChar])) {
177
            $regexEndPos = strrpos($regex, $this->doublesSeparators[$regexFirstChar]);
178
        } else {
179
            $regexEndPos = strrpos($regex, $regexFirstChar);
180
        }
181
182
        if ($regexEndPos !== false) {
183
            $modifiers = substr($regex, $regexEndPos + 1);
184
            $this->examineModifiers($phpcsFile, $stackPtr, $functionName, $modifiers);
185
        }
186
    }//end processRegexPattern()
187
188
189
    /**
190
     * Examine the regex modifier string.
191
     *
192
     * @param \PHP_CodeSniffer_File $phpcsFile    The file being scanned.
193
     * @param int                   $stackPtr     The position of the current token in the
194
     *                                            stack passed in $tokens.
195
     * @param string                $functionName The function which contained the pattern.
196
     * @param string                $modifiers    The regex modifiers found.
197
     *
198
     * @return void
199
     */
200
    protected function examineModifiers(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $modifiers)
201
    {
202
        if (strpos($modifiers, 'e') !== false) {
203
            $error     = '%s() - /e modifier is deprecated since PHP 5.5';
204
            $isError   = false;
205
            $errorCode = 'Deprecated';
206
            $data      = array($functionName);
207
208
            if ($this->supportsAbove('7.0')) {
209
                $error    .= ' and removed since PHP 7.0';
210
                $isError   = true;
211
                $errorCode = 'Removed';
212
            }
213
214
            $this->addMessage($phpcsFile, $error, $stackPtr, $isError, $errorCode, $data);
215
        }
216
    }
217
218
}//end class
219