rspec/rules/S6395/php/rule.adoc

51 lines
1.1 KiB
Plaintext
Raw Normal View History

Sub-patterns can be wrapped by parentheses to build a group. This enables to restrict alternations, back reference the group or apply quantifier to the sub-pattern.
If this group should not be part of the match result or if no reference to this group is required, a non-capturing group can be created by adding `:?` behind the opening parenthesis.
However, if this non-capturing group does not have a quantifier, or does not wrap an alternation, then imaging this group is redundant.
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,php]
----
"(?:number)\d{2}"
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,php]
----
"number\d{2}"
"(?:number)?\d{2}"
----
== Exceptions
Do not report an issue if the non-capturing group is an alternation.
----
"(?:number|string)"
----
== See
https://www.php.net/manual/en/regexp.reference.subpatterns.php[Subpatterns] - PHP Documentation
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
== Message
Unwrap this unnecessarily grouped subpattern.
== Highlighting
The entire non-capturing group.
'''
endif::env-github,rspecator-view[]