33 lines
922 B
Plaintext
33 lines
922 B
Plaintext
include::../description.adoc[]
|
|
|
|
== Noncompliant Code Example
|
|
[source,php]
|
|
----
|
|
preg_match("/Jack|Peter|/", "John"); // Noncompliant - returns 1
|
|
preg_match("/Jack||Peter/", "John"); // Noncompliant - returns 1
|
|
----
|
|
== Compliant Solution
|
|
[source,php]
|
|
----
|
|
preg_match("/Jack|Peter/", "John"); // returns 0
|
|
----
|
|
|
|
== Exceptions
|
|
|
|
One could use an empty alternation to make a regular expression group optional. Rule will not report on such cases.
|
|
|
|
[source,php]
|
|
----
|
|
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.
|
|
|
|
[source,php]
|
|
----
|
|
preg_match("/mandatory(-optional|)?/", "mandatory-optional"); // Noncompliant - using both `|` inside the group and `?` for the group.
|
|
----
|
|
|
|
include::../implementation.adoc[]
|