SonarQube is telling you that this portion of the code contains duplicated logic. This doesn't necessarily mean that the code itself is copy-pasted, but that, conceptually, the exact same thing is happening at multiple places. In this case, the logic of returning a String value with regard to the int value is clearly repeated.
A simple solution here:
String[] array = { "One", "Two", "Three", "Four", "Five", "Six" };
if (i >= 1 && i <= array.length) {
return array[i - 1];
}
SonarRules for Java projects:
A piece of code is considered as duplicated as soon as there is the same sequence of 10 successive statements whatever the number of tokens and lines. This threshold cannot be overridden.
You need to modify that multiple if return sections
public class Test
{
private static final String[] WORDS;
static {
WORDS = new String[] {
"One", "Two", "Three", "Four", "Five", "Six"
};
}
public String intToEnglishValue(final int number) {
return number > 0 && number <= WORDS.length ? WORDS[number - 1] : "";
}
}