Skip to content

Commit

Permalink
Modify S1192: Migrate to LayC (#3234)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardo-pilastri-sonarsource authored Oct 11, 2023
1 parent 44f4c80 commit a117458
Show file tree
Hide file tree
Showing 22 changed files with 277 additions and 264 deletions.
18 changes: 6 additions & 12 deletions rules/S1192/abap/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
== Why is this an issue?
include::../rule.adoc[]

include::../description.adoc[]
=== Code examples

=== Noncompliant code example
==== Noncompliant code example

With the default threshold of 3:

[source,abap]
[source,abap,diff-id=1,diff-type=noncompliant]
----
WRITE: / 'Firstname'.
*...
Expand All @@ -15,9 +15,9 @@ WRITE: / 'Firstname'.
WRITE: / 'Firstname'.
----

=== Compliant solution
==== Compliant solution

[source,abap]
[source,abap,diff-id=1,diff-type=compliant]
----
CONSTANTS: fname TYPE c LENGTH 9 VALUE 'Firstname',
Expand All @@ -28,8 +28,6 @@ WRITE: / fname.
WRITE: / fname.
----

include::../exceptions.adoc[]

ifdef::env-github,rspecator-view[]

'''
Expand All @@ -43,9 +41,5 @@ include::../parameters.adoc[]
include::../highlighting.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
25 changes: 14 additions & 11 deletions rules/S1192/apex/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@

include::../description.adoc[]

=== Noncompliant code example
=== Exceptions

To prevent generating some false-positives, literals having 5 or less characters are excluded as well as literals containing only letters, digits and '_'.

== How to fix it

include::../howtofix.adoc[]

=== Code examples

[source,apex]
==== Noncompliant code example

[source,apex,diff-id=1,diff-type=noncompliant]
----
class A {
void doSomething(String msg) {
Expand All @@ -21,9 +31,9 @@ class A {
}
----

=== Compliant solution
==== Compliant solution

[source,apex]
[source,apex,diff-id=1,diff-type=compliant]
----
class A {
static final String STRING_CONST = 'string literal';
Expand All @@ -36,9 +46,6 @@ class A {
}
----

=== Exceptions

To prevent generating some false-positives, literals having 5 or less characters are excluded as well as literals containing only letters, digits and '_'.

ifdef::env-github,rspecator-view[]

Expand All @@ -53,9 +60,5 @@ include::../parameters.adoc[]
include::../highlighting.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
31 changes: 16 additions & 15 deletions rules/S1192/cobol/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@

include::../description.adoc[]

=== Noncompliant code example
=== Exceptions

Literals with fewer than 7 characters are ignored.

Only duplications located in a `PROCEDURE DIVISION`, not those contained in copybooks are reported.

== How to fix it

include::../howtofix.adoc[]

=== Code examples

==== Noncompliant code example

With the default threshold of 3:

[source,cobol]
[source,cobol,diff-id=1,diff-type=noncompliant]
----
PROCEDURE DIVISION.
Expand All @@ -17,9 +29,9 @@ With the default threshold of 3:
DISPLAY "Firstname: ".
----

=== Compliant solution
==== Compliant solution

[source,cobol]
[source,cobol,diff-id=1,diff-type=compliant]
----
WORKING-STORAGE SECTION.
01 FIRST-NAME-HEADER PIC X(42) VALUE "Firstname: ".
Expand All @@ -32,13 +44,6 @@ With the default threshold of 3:
DISPLAY FIRST-NAME-HEADER
----

=== Exceptions

Literals with fewer than 7 characters are ignored.


Only duplications located in a ``++PROCEDURE DIVISION++``, not those contained in copybooks are reported.

ifdef::env-github,rspecator-view[]

'''
Expand All @@ -52,9 +57,5 @@ include::../parameters.adoc[]
include::../highlighting.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
11 changes: 0 additions & 11 deletions rules/S1192/comments-and-links.adoc

This file was deleted.

40 changes: 18 additions & 22 deletions rules/S1192/csharp/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@

include::../description.adoc[]

=== Noncompliant code example
=== Exceptions

The following are ignored:

* literals with fewer than 5 characters
* literals matching one of the parameter names
* literals used in attributes

== How to fix it

include::../howtofix.adoc[]

[source,csharp]
=== Code examples

==== Noncompliant code example

[source,csharp,diff-id=1,diff-type=noncompliant]
----
public class Foo
{
Expand All @@ -19,9 +33,9 @@ public class Foo
}
----

=== Compliant solution
==== Compliant solution

[source,csharp]
[source,csharp,diff-id=1,diff-type=compliant]
----
public class Foo
{
Expand All @@ -38,14 +52,6 @@ public class Foo
}
----

=== Exceptions

The following are ignored:

* literals with fewer than 5 characters
* literals matching one of the parameter names
* literals used in attributes

ifdef::env-github,rspecator-view[]

'''
Expand All @@ -67,15 +73,5 @@ secondaries: all instances of the string literal


'''
== Comments And Links
(visible only on this page)

=== on 21 Mar 2018, 15:11:26 Amaury Levé wrote:
Could you review this sub-task please?

=== on 21 Mar 2018, 15:33:08 Ann Campbell wrote:
I think a brief code example would be helpful for the second item, [~amaury.leve].

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
4 changes: 1 addition & 3 deletions rules/S1192/description.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Duplicated string literals make the process of refactoring complex and error-prone, as any change would need to be propagated on all occurrences.
2 changes: 0 additions & 2 deletions rules/S1192/exceptions.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
=== Exceptions

To prevent generating some false-positives, literals having less than 5 characters are excluded.
26 changes: 14 additions & 12 deletions rules/S1192/go/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

include::../description.adoc[]

=== Noncompliant code example
=== Exceptions

To prevent generating some false-positives, literals having 5 or less characters are excluded as well as literals containing only letters, digits and '_'.

== How to fix it

include::../howtofix.adoc[]

=== Code examples

==== Noncompliant code example

With the default threshold of 3:

[source,go]
[source,go,diff-id=1,diff-type=noncompliant]
----
func run() {
prepare("This should be a constant") // Noncompliant; 'This should ...' is duplicated 3 times
Expand All @@ -15,9 +25,9 @@ func run() {
}
----

=== Compliant solution
==== Compliant solution

[source,go]
[source,go,diff-id=1,diff-type=compliant]
----
const ACTION = "This should be a constant"
Expand All @@ -28,10 +38,6 @@ func run() {
}
----

=== Exceptions

To prevent generating some false-positives, literals having 5 or less characters are excluded as well as literals containing only letters, digits and '_'.

ifdef::env-github,rspecator-view[]

'''
Expand All @@ -45,9 +51,5 @@ include::../parameters.adoc[]
include::../highlighting.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
2 changes: 2 additions & 0 deletions rules/S1192/howtofix.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Instead, use constants to replace the duplicated string literals.
Constants can be referenced from many places, but only need to be updated in a single place.
23 changes: 8 additions & 15 deletions rules/S1192/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
== Why is this an issue?
include::../rule.adoc[]

include::../description.adoc[]
=== Code examples

=== Noncompliant code example
==== Noncompliant code example

With the default threshold of 3:

[source,java]
[source,java,diff-id=1,diff-type=noncompliant]
----
public void run() {
prepare("action1"); // Noncompliant - "action1" is duplicated 3 times
Expand All @@ -19,15 +19,14 @@ private void method1() { /* ... */ }
@SuppressWarning("all")
private void method2() { /* ... */ }
public String method3(String a) {
System.out.println("'" + a + "'"); // Compliant - literal "'" has less than 5 characters and is excluded
return ""; // Compliant - literal "" has less than 5 characters and is excluded
public String printInQuotes(String a, String b) {
return "'" + a + "'" + b + "'"; // Compliant - literal "'" has less than 5 characters and is excluded
}
----

=== Compliant solution
==== Compliant solution

[source,java]
[source,java,diff-id=1,diff-type=compliant]
----
private static final String ACTION_1 = "action1"; // Compliant
Expand All @@ -38,8 +37,6 @@ public void run() {
}
----

include::../exceptions.adoc[]

ifdef::env-github,rspecator-view[]

'''
Expand All @@ -53,9 +50,5 @@ include::../parameters.adoc[]
include::../highlighting.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
Loading

0 comments on commit a117458

Please sign in to comment.