Alternation is used to match a single regular expression out of several possible regular expressions. If one of the alternatives is empty it would
match any input, which is most probably a mistake.
Noncompliant code example
preg_match("/Jack|Peter|/", "John"); // Noncompliant - returns 1
preg_match("/Jack||Peter/", "John"); // Noncompliant - returns 1
Compliant solution
preg_match("/Jack|Peter/", "John"); // returns 0
Exceptions
One could use an empty alternation to make a regular expression group optional. Note that the empty alternation should be the first or the last
within the group, or else the rule will still report.
preg_match("/mandatory(|-optional)/", "mandatory"); // returns 1
preg_match("/mandatory(-optional|)/", "mandatory-optional"); // returns 1
However, if there is a quantifier after the group the issue will be reported as using both |
and quantifier is redundant.
preg_match("/mandatory(-optional|)?/", "mandatory-optional"); // Noncompliant - using both `|` inside the group and `?` for the group.