-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix S107: "Methods with too many parameters" should not raise on constructors calling base #1015
Comments
"Constructor has 8 parameters, which is greater than the 7 authorized" issue is exist sonarqube version 7.9 LTS for .net core applications |
can you please provide a reproducer, @Shyam268 ? |
I still get S107 on SonarAnalyzer.CSharp Version="8.13.1.21947" using .Net Core 3.1 and Microsoft.NET.Sdk.Web. Here is a synthetic example (using stock framework MS services to make repro easier). It trips when analyzed inside our current project. using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
namespace Example
{
public interface IBaseService
{
IHttpContextAccessor HttpContextAccessor { get; }
LinkGenerator LinkGenerator { get; }
IServiceProvider ServiceProvider { get; }
IMemoryCache MemoryCache { get; }
IHttpContextAccessor Hca { get; }
IOptions<Settings> Settings { get; }
}
public class Settings { }
public class BaseService : IBaseService
{
public IHttpContextAccessor HttpContextAccessor { get; }
public LinkGenerator LinkGenerator { get; }
public IServiceProvider ServiceProvider { get; }
public IMemoryCache MemoryCache { get; }
public IHttpContextAccessor Hca { get; }
public IOptions<Settings> Settings { get; }
public BaseService(
IHttpContextAccessor httpContextAccessor,
LinkGenerator linkGenerator,
IServiceProvider serviceProvider,
IMemoryCache memoryCache,
IHttpContextAccessor hca,
IOptions<Settings> settings)
{
HttpContextAccessor = httpContextAccessor;
LinkGenerator = linkGenerator;
ServiceProvider = serviceProvider;
MemoryCache = memoryCache;
Hca = hca;
Settings = settings;
}
}
public class DescendantService : BaseService
{
private readonly IStringLocalizer<BaseService> _stringLocalizer;
private readonly ILogger<DescendantService> _logger;
public DescendantService(
IHttpContextAccessor httpContextAccessor,
LinkGenerator linkGenerator,
IServiceProvider serviceProvider,
IMemoryCache memoryCache,
IHttpContextAccessor hca,
IOptions<Settings> settings,
IStringLocalizer<BaseService> stringLocalizer,
ILogger<DescendantService> logger) // <-- S107 triggers on this constructor.
: base(httpContextAccessor, linkGenerator, serviceProvider, memoryCache, hca, settings)
{
_stringLocalizer = stringLocalizer;
_logger = logger;
}
}
} |
Hi @azhe403, this rule should raise on constructors like in your case. This issue was related to a scenario where the constructor calls the base class constructor. I'm not sure how you're using the analyzer. The easiest way to configure rule parameters is to configure them in SonarCloud Quality Profile and use SonarLint in connected mode to scaffold all configuration needed in your project. If you're not using SonarCloud, you don't need to analyze your project there. You can just benefit from using the UI to do the configuration. As this issue is related to S107 rule only, you can ask for more help on https://community.sonarsource.com |
Description
This rule comes up frequently for constructors due to dependency injection scenarios, especially with ASP.NET Core / Identity framework related classes.
Example
The
Microsoft.AspNetCore.Identity.UserManager<TUser>
class has a constructor with several parameters:There are other similar library classes which are often subclassed to override specific functionality. Issues are generated by this rule in cases which are unavoidable, because while we might prefer to not have so many arguments, it's necessary to pass these along to base members.
Expected behavior
I'm not sure what's ideal here. Either it should be possible to ignore constructors entirely for this rule, or perhaps examine constructors for base constructor usage. So for instance, if the constructor calls a base constructor, the identical parameters of the base constructor are excluded from the total parameter count. This way you could check that too many additional parameters are not added.
I could see a similar issue with overridden methods, but I think this is far more common with constructors due to DI.
Known workarounds
Not sure. Is there a way to exclude code blocks from SonarC# coverage?
Related information
SonarC# Version is 6.5 (build 3766)
The text was updated successfully, but these errors were encountered: