rspec/rules/S6328/php/rule.adoc

39 lines
1.1 KiB
Plaintext
Raw Normal View History

The PHP function ``++preg_replace++`` can be used to perform a search and replace based on regular expression matches. The ``++$replacement++`` parameter can contain references to capturing groups used in the ``++$pattern++`` parameter. This can be achieved with ``++\n++`` or ``++$n++`` to reference the n'th group.
When referencing a nonexistent group, it will be substituted with an empty string. This is in general not the intended behavior, and might stay unnoticed since no warning is raised.
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,php]
----
preg_replace("/(a)(b)(c)/", "\\1, \\9, \\3", "abc"); // Noncompliant - result is "a, , c"
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,php]
----
preg_replace("/(a)(b)(c)/", "\\1, \\2, \\3", "abc");
----
== See
* https://www.php.net/manual/en/function.preg-replace.php[preg_replace] - PHP Documentation
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
== Message
Make sure to reference an existing group.
== Highlighting
The reference to the group that does not exist.
'''
endif::env-github,rspecator-view[]