I’m using SonarQube on my Java projects, and want to eliminate code duplications from our code as far as possible.
My problem is that SonarQube’s code duplication detection doesn’t take into account method boundaries. It lists identical parts of files as duplications, and it often happens that a duplication starts in the middle of a method and ends in the middle of another. These can hardly be refactored.
Here is an example. Click on file MavenArtifactRepository.java in the upper right list box, and take a look at the 4th duplication block in the lower part of the page.
Is there any way to parameterize the code duplication detector plug-in to show duplications, which are syntactically coherent?
To do so, you can either ignore files in the SonarQube UI under General Settings > Analysis Scope > Duplications using e.g. **/*. html , or make this restriction directly in your sonar-project. properties using the sonar.
For most languages, SonarQube supports the use of the generic mechanism: //NOSONAR at the end of the line of the issue. This will suppress all issues - now and in the future - that might be raised on the line.
Currently you cannot achieve that by configuring SonarQube itself. However, you can try our tool, SourceMeter with its SonarQube plug-in, which implements an AST based clone detection and therefore it presents syntactically coherent duplications inside SonarQube. For an example, you may take a look at the online demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With