• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCompatibility / PHPCompatibility / 19804455605

30 Nov 2025 08:31PM UTC coverage: 98.306% (-0.04%) from 98.346%
19804455605

push

github

web-flow
Merge pull request #2014 from PHPCompatibility/php-8.5/new-removedterminatingcasewithsemicolon-sniff

PHP 8.5 | ✨ New `PHPCompatibility.ControlStructures.RemovedTerminatingCaseWithSemicolon` sniff (RFC)

30 of 34 new or added lines in 1 file covered. (88.24%)

21 existing lines in 15 files now uncovered.

8355 of 8499 relevant lines covered (98.31%)

20.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.06
/PHPCompatibility/Sniffs/ParameterValues/RemovedMbstringModifiersSniff.php
1
<?php
2
/**
3
 * PHPCompatibility, an external standard for PHP_CodeSniffer.
4
 *
5
 * @package   PHPCompatibility
6
 * @copyright 2012-2020 PHPCompatibility Contributors
7
 * @license   https://opensource.org/licenses/LGPL-3.0 LGPL3
8
 * @link      https://github.com/PHPCompatibility/PHPCompatibility
9
 */
10

11
namespace PHPCompatibility\Sniffs\ParameterValues;
12

13
use PHPCompatibility\AbstractFunctionCallParameterSniff;
14
use PHPCompatibility\Helpers\ScannedCode;
15
use PHP_CodeSniffer\Files\File;
16
use PHP_CodeSniffer\Util\Tokens;
17
use PHPCSUtils\Utils\MessageHelper;
18
use PHPCSUtils\Utils\TextStrings;
19

20
/**
21
 * Check for use of deprecated and removed regex modifiers for MbString regex functions.
22
 *
23
 * Initially just checks for the PHP 7.1 deprecated `e` modifier.
24
 *
25
 * PHP version 7.1+
26
 *
27
 * @link https://wiki.php.net/rfc/deprecate_mb_ereg_replace_eval_option
28
 * @link https://www.php.net/manual/en/function.mb-regex-set-options.php
29
 *
30
 * @since 7.0.5
31
 * @since 7.0.8  This sniff now throws a warning instead of an error as the functionality is
32
 *               only deprecated (for now).
33
 * @since 8.2.0  Now extends the `AbstractFunctionCallParameterSniff` instead of the base `Sniff` class.
34
 * @since 9.0.0  Renamed from `MbstringReplaceEModifierSniff` to `RemovedMbstringModifiersSniff`.
35
 * @since 10.0.0 This class is now `final`.
36
 */
37
final class RemovedMbstringModifiersSniff extends AbstractFunctionCallParameterSniff
38
{
39

40
    /**
41
     * Functions to check for.
42
     *
43
     * Key is the function name, value the parameter position of the options parameter.
44
     *
45
     * @since 7.0.5
46
     * @since 8.2.0 Renamed from `$functions` to `$targetFunctions`.
47
     *
48
     * @var array<string, int>
49
     */
50
    protected $targetFunctions = [
51
        'mb_ereg_replace'      => 4,
52
        'mb_eregi_replace'     => 4,
53
        'mb_regex_set_options' => 1,
54
        'mbereg_replace'       => 4, // Undocumented, but valid function alias.
55
        'mberegi_replace'      => 4, // Undocumented, but valid function alias.
56
    ];
57

58

59
    /**
60
     * Do a version check to determine if this sniff needs to run at all.
61
     *
62
     * @since 8.2.0
63
     *
64
     * @return bool
65
     */
66
    protected function bowOutEarly()
8✔
67
    {
68
        // Version used here should be the lowest version, i.e the version in which the
69
        // first modifier being detected by this sniff was removed.
70
        return (ScannedCode::shouldRunOnOrAbove('7.1') === false);
8✔
71
    }
72

73

74
    /**
75
     * Process the parameters of a matched function.
76
     *
77
     * @since 7.0.5
78
     * @since 8.2.0 Renamed from `process()` to `processParameters()` and removed
79
     *              logic superfluous now the sniff extends the abstract.
80
     *
81
     * @param \PHP_CodeSniffer\Files\File $phpcsFile    The file being scanned.
82
     * @param int                         $stackPtr     The position of the current token in the stack.
83
     * @param string                      $functionName The token content (function name) which was matched.
84
     * @param array                       $parameters   Array with information about the parameters.
85
     *
86
     * @return void
87
     */
88
    public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
4✔
89
    {
90
        $tokens         = $phpcsFile->getTokens();
4✔
91
        $functionNameLc = \strtolower($functionName);
4✔
92

93
        // Check whether the options parameter in the function call is passed.
94
        if (isset($parameters[$this->targetFunctions[$functionNameLc]]) === false) {
4✔
UNCOV
95
            return;
×
96
        }
97

98
        $optionsParam = $parameters[$this->targetFunctions[$functionNameLc]];
4✔
99
        $options      = '';
4✔
100

101
        /*
102
         * Get the content of any string tokens in the options parameter and remove the quotes and variables.
103
         */
104
        for ($i = $optionsParam['start']; $i <= $optionsParam['end']; $i++) {
4✔
105
            if (isset(Tokens::NAME_TOKENS[$tokens[$i]['code']]) === true
4✔
106
                || $tokens[$i]['code'] === \T_VARIABLE
4✔
107
            ) {
108
                // Variable, constant, function call. Ignore as undetermined.
109
                return;
4✔
110
            }
111

112
            if (isset(Tokens::STRING_TOKENS[$tokens[$i]['code']]) === false) {
4✔
113
                continue;
4✔
114
            }
115

116
            $content = TextStrings::stripQuotes($tokens[$i]['content']);
4✔
117
            if ($tokens[$i]['code'] === \T_DOUBLE_QUOTED_STRING) {
4✔
118
                $content = TextStrings::stripEmbeds($content);
4✔
119
            }
120
            $content = \trim($content);
4✔
121

122
            if (empty($content) === false) {
4✔
123
                $options .= $content;
4✔
124
            }
125
        }
126

127
        if (\strpos($options, 'e') !== false) {
4✔
128
            $error   = 'The Mbstring regex "e" modifier is deprecated since PHP 7.1';
4✔
129
            $code    = 'Deprecated';
4✔
130
            $isError = false;
4✔
131

132
            if (ScannedCode::shouldRunOnOrAbove('8.0') === true) {
4✔
133
                $error  .= ' and removed since PHP 8.0';
4✔
134
                $code    = 'Removed';
4✔
135
                $isError = true;
4✔
136
            }
137

138
            $error .= '.';
4✔
139

140
            // The alternative mb_ereg_replace_callback() function is only available since 5.4.1.
141
            if (ScannedCode::shouldRunOnOrBelow('5.4.1') === false) {
4✔
142
                $error .= ' Use mb_ereg_replace_callback() instead (PHP 5.4.1+).';
4✔
143
            }
144

145
            MessageHelper::addMessage($phpcsFile, $error, $stackPtr, $isError, $code);
4✔
146
        }
147
    }
2✔
148
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc