A literal pattern passed to a preg function has no valid delimiters

tx0/php:regex-delimiterdefaultwarningreliability/strings-and-regexdepth fastbug

What it finds

preg_match('^\d+$', $s): the PCRE functions want the pattern between delimiters, as '/^\d+$/'. A pattern that starts with a letter, a digit or a backslash, or has no closing delimiter, makes the call fail with a warning and return false, which reads as "no match".

From the rule's source, regex_delimiter.rs.

Example from the rule's tests
<?phpfunction valid($s){    $a = preg_match('\d+$', $s);tx0 reports: the pattern of preg_match starts with a letter, digit or backslash instead of a delimiter    $b = preg_match('/^\d+$', $s);tx0 reports: the pattern of preg_match has no closing delimiter    $c = preg_replace('#a#i', 'b', $s);    $d = preg_split('{\s+}', $s);    $e = preg_match('/x/u', $s);    return [$a, $b, $c, $d, $e];}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/php:regex-delimiter" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/php:regex-delimiter] reason="..."

tx0 explain tx0/php:regex-delimiter shows the same in the terminal. Found a false positive? Report it.