Add PHP to S6323 (#650)

Co-authored-by: Nils Werner <64034005+nils-werner-sonarsource@users.noreply.github.com>
This commit is contained in:
Karim El Ouerghemmi 2022-02-02 14:10:58 +01:00 committed by GitHub
parent 71cb635542
commit ddb9c3332b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 62 additions and 20 deletions

View File

@ -0,0 +1 @@
Alternation is used to match a single regular expression out of several possible regular expressions. If one of the alternatives is empty it would match any input, which is most probably a mistake.

View File

@ -1,20 +1,2 @@
{
"title": "Alternation in regular expressions should not contain empty alternatives",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"regex"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6323",
"sqKey": "S6323",
"scope": "Main",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown"
}

View File

@ -1,4 +1,4 @@
Alternation is used to match a single regular expression out of several possible regular expressions. If one of the alternatives is empty it would match any input, which is most probably a mistake.
include::../description.adoc[]
== Noncompliant Code Example
----

View File

@ -1,2 +1,21 @@
{
}
"title": "Alternation in regular expressions should not contain empty alternatives",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"regex"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6323",
"sqKey": "S6323",
"scope": "Main",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown"
}

View File

@ -0,0 +1,2 @@
{
}

38
rules/S6323/php/rule.adoc Normal file
View File

@ -0,0 +1,38 @@
include::../description.adoc[]
== Noncompliant Code Example
----
preg_match("/Jack|Peter|/", "John"); // Noncompliant - returns 1
preg_match("/Jack||Peter/", "John"); // Noncompliant - returns 1
----
== Compliant Solution
----
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.
----
preg_match("/mandatory(-optional|)/", "mandatory"); // returns 1
preg_match("/mandatory(-optional|)/", "mandatory-optional"); // returns 1
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
== Message
Remove this empty alternative.
== Highlighting
The | should be highlighted.
'''
endif::env-github,rspecator-view[]