Search code examples
c#.net-coresonarqubeasp.net-core-webapicode-analysis

Sonarqube reporting duplicate code block on controller actions


I am analysing my .NET Core application using SonarQube and it is now reporting duplicate blocks on both of the controller actions below: the three parameters obtained from the query string and the call of ParametersHelper.ProcessAndSanitizeSee2InputParams, as you can see on the image.

SonarQube duplicated code block.

The static method call sanitizes each parameter - null-checking, trimming and custom logic - and groups them in a dictionary.

Any tips on how to refactor these actions in order to avoid triggering SonarQube's duplicated code detection?

// comments omitted
[HttpGet("{seasonKey}")]
[ProducesResponseType(typeof(See2Season), 200)]
public async Task<IActionResult> GetSeasonByKeyAsync(
    string seasonKey,
    [FromQuery(Name = "provider")] string providers,
    [FromQuery(Name = "lang")] string languages,
    [FromQuery(Name = "priority")] string priority)
{
    var sanitizedSee2Params = ParametersHelper.ProcessAndSanitizeSee2InputParams(
        this._logger,
        true,
        new KeyValuePair<string, string>(InputParameterNames.PROVIDERS, providers),
        new KeyValuePair<string, string>(InputParameterNames.LANGUAGE, languages),
        new KeyValuePair<string, string>(InputParameterNames.PRIORITY, priority));

    var result = await this._seasonManager.GetSeasonByKeyAsync(
        seasonKey,
        sanitizedSee2Params).ConfigureAwait(false); 

    return this.Ok(result);
}

// comments omitted
[HttpGet("{seasonKey}/children")]
[ProducesResponseType(typeof(IEnumerable<See2Content>), 200)]
public async Task<IActionResult> GetSeasonChildrenByKeyAsync(
    string seasonKey,
    [FromQuery(Name = "overrideDefaultDirection")] bool overrideDefaultDirection,
    [FromQuery(Name = "provider")] string providers,
    [FromQuery(Name = "lang")] string languages,
    [FromQuery(Name = "priority")] string priority)
{
    var sanitizedSee2Params = ParametersHelper.ProcessAndSanitizeSee2InputParams(
        this._logger,
        true,
        new KeyValuePair<string, string>(InputParameterNames.PROVIDERS, providers),
        new KeyValuePair<string, string>(InputParameterNames.LANGUAGE, languages),
        new KeyValuePair<string, string>(InputParameterNames.PRIORITY, priority));

    var result = await this._seasonManager.GetSeasonChildrenByKeyAsync(
        seasonKey,
        overrideDefaultDirection,
        sanitizedSee2Params).ConfigureAwait(false);

    return this.Ok(result);
}

Solution

  • You do not need to get rid of the whole duplication to resolve this duplication issue (clone). It should be sufficient to remove / reduce the duplication caused by lines 96-101 of the first clone. You could consider extracting that logic to a separate method. Like this:

    private SanitizedInputParams sanitizeSeasonParams(string providers, string languages, string priority) {
      return ParametersHelper.ProcessAndSanitizeSee2InputParams(
        this._logger,
        true,
        new KeyValuePair<string, string>(InputParameterNames.PROVIDERS, providers),
        new KeyValuePair<string, string>(InputParameterNames.LANGUAGE, languages),
        new KeyValuePair<string, string>(InputParameterNames.PRIORITY, priority));
    }
    

    Thereby, the clone length is reduced such that SonarQube likely will no longer report this issue (depending on the configured minimum clone length). Alternatively, you could consider increasing the minimal clone length to focus on longer, more relevant clones. See also: https://docs.sonarqube.org/display/SONARQUBE45/Duplications