rspec/rules/S6393/php/rule.adoc

53 lines
1.4 KiB
Plaintext
Raw Normal View History

The pattern provided to PHP regular expression functions is required to be enclosed in valid delimiters. Failing to do so will result in a PHP warning and the pattern never matching. Since the warning only appears during runtime when the pattern is evaluated, such a mistake risks to get unnoticed into production.
A delimiter can be any character that is not alphanumeric, a backslash, or a whitespace. Bracket style delimiters are also allowed.
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,php]
----
// Condition will always evaluate to false
if (preg_match("/.*", $input)) {
echo "true";
} else {
echo "false";
}
// Unclosed bracket delimiters
$result = preg_match("[abc", $input);
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,php]
----
if (preg_match("/.*/", $input)) {
echo "true";
} else {
echo "false";
}
$result = preg_match("[abc]", $input);
----
== See
* https://www.php.net/manual/en/regexp.reference.delimiters.php[Delimiters] - PHP Documentation
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* In case there is no opening delimiter: Add delimiters to this regular expression.
* In case there is no closing delimiter (x being the appropriate end delimiter): Add the missing "x" delimiter to this regular expression.
=== Highlighting
The string representing the regular expression.
endif::env-github,rspecator-view[]