Completed
Push — feature/new-abstract-function-... ( 9086e9 )
by Juliette
01:51
created

MbstringReplaceEModifierSniff::processParameters()   C

Complexity

Conditions 9
Paths 20

Size

Total Lines 50
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 6
c 0
b 0
f 0
cc 9
eloc 24
nc 20
nop 4
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\MbstringReplaceEModifierSniff.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Juliette Reinders Folmer <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\PHP;
13
14
use PHPCompatibility\AbstractFunctionCallParameterSniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\PHP\MbstringReplaceEModifierSniff.
18
 *
19
 * PHP version 7.1
20
 *
21
 * @category PHP
22
 * @package  PHPCompatibility
23
 * @author   Juliette Reinders Folmer <[email protected]>
24
 */
25
class MbstringReplaceEModifierSniff extends AbstractFunctionCallParameterSniff
26
{
27
28
    /**
29
     * Functions to check for.
30
     *
31
     * Key is the function name, value the parameter position of the options parameter.
32
     *
33
     * @var array
34
     */
35
    protected $targetFunctions = array(
36
        'mb_ereg_replace'      => 4,
37
        'mb_eregi_replace'     => 4,
38
        'mb_regex_set_options' => 1,
39
    );
40
41
42
    /**
43
     * Do a version check to determine if this sniff needs to run at all.
44
     *
45
     * @return bool
46
     */
47
    protected function bowOutEarly()
48
    {
49
        // Version used here should be the highest version from the `$newModifiers` array,
50
        // i.e. the last PHP version in which a new modifier was introduced.
51
        return ($this->supportsAbove('7.1') === false);
52
    }
53
54
55
    /**
56
     * Process the parameters of a matched function.
57
     *
58
     * This method has to be made concrete in child classes.
59
     *
60
     * @param \PHP_CodeSniffer_File $phpcsFile    The file being scanned.
61
     * @param int                   $stackPtr     The position of the current token in the stack.
62
     * @param string                $functionName The token content (function name) which was matched.
63
     * @param array                 $parameters   Array with information about the parameters.
64
     *
65
     * @return int|void Integer stack pointer to skip forward or void to continue
66
     *                  normal file processing.
67
     */
68
    public function processParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $parameters)
69
    {
70
        $tokens         = $phpcsFile->getTokens();
71
        $functionNameLc = strtolower($functionName);
72
73
        // Check whether the options parameter in the function call is passed.
74
        if (isset($parameters[$this->targetFunctions[$functionNameLc]]) === false) {
75
            return;
76
        }
77
78
        $optionsParam = $parameters[$this->targetFunctions[$functionNameLc]];
79
80
        $stringToken = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$stringTokens, $optionsParam['start'], $optionsParam['end'] + 1);
81
        if ($stringToken === false) {
82
            // No string token found in the options parameter, so skip it (e.g. variable passed in).
83
            return;
84
        }
85
86
        $options = '';
87
88
        /*
89
         * Get the content of any string tokens in the options parameter and remove the quotes and variables.
90
         */
91
        for ($i = $stringToken; $i <= $optionsParam['end']; $i++) {
92
            if (in_array($tokens[$i]['code'], \PHP_CodeSniffer_Tokens::$stringTokens, true) === false) {
93
                continue;
94
            }
95
96
            $content = $this->stripQuotes($tokens[$i]['content']);
97
            if ($tokens[$i]['code'] === T_DOUBLE_QUOTED_STRING) {
98
                $content = $this->stripVariables($content);
99
            }
100
            $content = trim($content);
101
102
            if (empty($content) === false) {
103
                $options .= $content;
104
            }
105
        }
106
107
        if (strpos($options, 'e') !== false) {
108
            $error = 'The Mbstring regex "e" modifier is deprecated since PHP 7.1.';
109
110
            // The alternative mb_ereg_replace_callback() function is only available since 5.4.1.
111
            if ($this->supportsBelow('5.4.1') === false) {
112
                $error .= ' Use mb_ereg_replace_callback() instead (PHP 5.4.1+).';
113
            }
114
115
            $phpcsFile->addWarning($error, $stackPtr, 'Deprecated');
116
        }
117
    }
118
}//end class
119