Skip to content

NotOptimalRegularExpressions ​

warning on by default quick-fix

Group: Performance · PhpStorm name: NotOptimalRegularExpressionsInspection

A family of checks on PCRE patterns passed to the preg_* functions: broken or missing delimiters, invalid / pointless / missing modifiers, class spellings that have a shorter escape, redundant or backtracking-prone constructs, and calls where a plain string function (strpos, str_replace, trim, explode, a === comparison) does the same job faster. Only a few findings carry a fix (the plain-function replacements).

Example ​

Default level (e.g. 7.0):

php
<?php
function modifiersDemo($line, $m) {
    preg_match('%k\d+%', $line, $m);
    preg_match('[k\d+]x', $line, $m);
    preg_match('', $line, $m);
    preg_match('k\d+', $line, $m);
    preg_match(')k\d(', $line, $m);
    preg_match('/k\d/9', $line, $m);
    preg_quote('k.d', '/');
    preg_quote('/k/Q', '/');
    preg_replace('#k(\d)#e', '$1', $line);
    preg_replace(['#k(\d)#e'], '$1', $line);
    preg_match('/k\d/Q', $line, $m);
    preg_match('/(k)\d/n', $line, $m);

    preg_match('/^k\d$/D', $line, $m);
    preg_match('/^k\d/D', $line, $m);
    preg_match('/k\$/D', $line, $m);
    preg_match('/k\d$/mD', $line, $m);

    preg_match('/k.\d/s', $line, $m);
    preg_match('/k\[.\]/s', $line, $m);
    preg_match('/k\d+/s', $line, $m);
    preg_match('/k[.,]\d/s', $line, $m);
    preg_match('/k\.\d/s', $line, $m);

    preg_match('/k\d/i', $line, $m);
    preg_match('/\\d/i', $line, $m);
    preg_match('/ид/iu', $line, $m);
    preg_match('/\d{3}-\d{2}/i', $line, $m);
    preg_match('/
        \s+ \W
    /ix', $line, $m);

    $rx = 'k\w+';
    preg_match($rx, $line, $m);
}

Reported:

  • line 6: Pattern has no valid delimiters.
  • line 7: Pattern has no valid delimiters.
  • line 8: Pattern has no valid delimiters.
  • line 11: The /e flag was removed from PCRE; use a callback replacement.
  • line 12: The /e flag was removed from PCRE; use a callback replacement.
  • line 13: 'Q' is not a valid PCRE modifier.
  • line 14: 'n' is not a valid PCRE modifier.
  • line 17: The /D flag is pointless: the pattern has no '$'.
  • line 18: The /D flag is pointless: the pattern has no '$'.
  • line 19: The /D flag has no effect together with /m.
  • line 23: The /s flag is pointless: the pattern has no '.'.
  • line 24: The /s flag is pointless: the pattern has no '.'.
  • line 25: The /s flag is pointless: the pattern has no '.'.
  • line 30: The /i flag is pointless: the pattern has no letters.
  • line 31: The /i flag is pointless: the pattern has no letters.
  • line 35: Pattern has no valid delimiters.

Configure ​

In custos.json:

json
{
  "rules": {
    "NotOptimalRegularExpressions": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore NotOptimalRegularExpressions

/**
 * @noinspection NotOptimalRegularExpressionsInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.