diff --git a/rules/S6397/metadata.json b/rules/S6397/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S6397/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S6397/php/metadata.json b/rules/S6397/php/metadata.json new file mode 100644 index 0000000000..f4e299ad2d --- /dev/null +++ b/rules/S6397/php/metadata.json @@ -0,0 +1,18 @@ +{ + "title": "Character classes in regular expressions should not contain only one character", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "regex" + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-6397", + "sqKey": "S6397", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown" +} diff --git a/rules/S6397/php/rule.adoc b/rules/S6397/php/rule.adoc new file mode 100644 index 0000000000..5f5dc4f71d --- /dev/null +++ b/rules/S6397/php/rule.adoc @@ -0,0 +1,35 @@ +Character classes in regular expressions are a convenient way to match one of several possible characters by listing the allowed characters or ranges of characters. If a character class contains only one character, the effect is the same as just writing the character without a character class. + +Thus, having only one character in a character class is usually a simple oversight that remained after removing other characters of the class. + +== Noncompliant Code Example + +---- +/a[b]c/ +---- + +== Compliant Solution + +---- +/abc/ +---- + +== Exceptions + +This rule does not raise when the character inside the class is a metacharacter. This notation is sometimes used to avoid escaping (e.g., ``++[.]{3}++`` to match three dots). + +ifdef::env-github,rspecator-view[] + +''' +== Implementation Specification +(visible only on this page) + +=== Message + +Replace this character class by the character itself. + +=== Highlighting + +The character class. + +endif::env-github,rspecator-view[] \ No newline at end of file