Completed
Push — master ( 01d73e...c6357c )
by Juliette
03:01
created

MbstringReplaceEModifierSniff   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 89
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
C process() 0 50 10
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
/**
13
 * PHPCompatibility_Sniffs_PHP_MbstringReplaceEModifierSniff.
14
 *
15
 * @category  PHP
16
 * @package   PHPCompatibility
17
 * @author    Juliette Reinders Folmer <[email protected]>
18
 */
19
class PHPCompatibility_Sniffs_PHP_MbstringReplaceEModifierSniff extends PHPCompatibility_Sniff
20
{
21
22
    /**
23
     * Functions to check for.
24
     *
25
     * Key is the function name, value the parameter position of the options parameter.
26
     *
27
     * @var array
28
     */
29
    protected $functions = array(
30
        'mb_ereg_replace'      => 4,
31
        'mb_eregi_replace'     => 4,
32
        'mb_regex_set_options' => 1,
33
    );
34
35
36
    /**
37
     * Returns an array of tokens this test wants to listen for.
38
     *
39
     * @return array
40
     */
41
    public function register()
42
    {
43
        return array(T_STRING);
44
    }//end register()
45
46
47
    /**
48
     * Processes this test, when one of its tokens is encountered.
49
     *
50
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
51
     * @param int                  $stackPtr  The position of the current token in the
52
     *                                        stack passed in $tokens.
53
     *
54
     * @return void
55
     */
56
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
57
    {
58
        if ($this->supportsAbove('7.1') === false) {
59
            return;
60
        }
61
62
        $tokens         = $phpcsFile->getTokens();
63
        $functionNameLc = strtolower($tokens[$stackPtr]['content']);
64
65
        // Bow out if not one of the functions we're targetting.
66
        if ( isset($this->functions[$functionNameLc]) === false ) {
67
            return;
68
        }
69
70
        // Get the options parameter in the function call.
71
        $optionsParam = $this->getFunctionCallParameter($phpcsFile, $stackPtr, $this->functions[$functionNameLc]);
72
        if ($optionsParam === false) {
73
            return;
74
        }
75
76
        $stringToken = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $optionsParam['start'], $optionsParam['end'] + 1);
77
        if ($stringToken === false) {
78
            // No string token found in the options parameter, so skip it (e.g. variable passed in).
79
            return;
80
        }
81
82
        /**
83
         * Get the content of any string tokens in the options parameter and remove the quotes.
84
         */
85
        $options = trim($tokens[$stringToken]['content'], '\'"');
86
        if ($stringToken !== $optionsParam['end']) {
87
            while ($stringToken = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stringToken + 1, $optionsParam['end'] + 1)) {
88
                if ($tokens[$stringToken]['code'] === T_CONSTANT_ENCAPSED_STRING) {
89
                    $options .= trim($tokens[$stringToken]['content'], '\'"');
90
                }
91
            }
92
        }
93
94
        if (strpos($options, 'e') !== false) {
95
            $error = 'The Mbstring regex "e" modifier is deprecated since PHP 7.1.';
96
97
            // The alternative mb_ereg_replace_callback() function is only available since 5.4.1
98
            if ($this->supportsBelow('5.4.1') === false) {
99
                $error .= ' Use mb_ereg_replace_callback() instead (PHP 5.4.1+).';
100
            }
101
102
            $phpcsFile->addError($error, $stackPtr, 'Found');
103
        }
104
105
    }//end process()
106
107
}//end class
108