rspec/rules/S6019/php/rule.adoc

21 lines
883 B
Plaintext
Raw Normal View History

When a reluctant (or lazy) quantifier is followed by a pattern that can match the empty string or directly by the end of the regex, it will always match zero times for `+*?+` or one time for `++?+`. If a reluctant quantifier is followed directly by the end anchor (`+$+`), it behaves indistinguishably from a greedy quantifier while being less efficient.
This is likely a sign that the regex does not work as intended.
== Noncompliant Code Example
----
preg_replace("/start\w*?(end)?/", "x", "start123endstart456"); // Noncompliant. In contrast to what one would expect, the result is not "xx".
preg_match("/^\d*?$/", "123456789"); // Noncompliant. Matches the same as "/^\d*$/", but will backtrack in every position.
----
== Compliant Solution
----
preg_replace("/start\w*?(end|$)/", "x", "start123endstart456"); // Result is "xx".
preg_match("/^\d*$/", "123456789");
----