Skip to content

Commit 59229ff

Browse files
Bart KoelmanBart Koelman
authored andcommitted
Added AV1561:MaxConstructorParameterCount configuration setting
Fixes #113
1 parent c31b5bf commit 59229ff

File tree

5 files changed

+205
-52
lines changed

5 files changed

+205
-52
lines changed

docs/Configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The behavior of rules can be customized by adding a file named `CSharpGuidelines
77
<cSharpGuidelinesAnalyzerSettings>
88
<setting rule="AV1500" name="MaxStatementCount" value="12" />
99
<setting rule="AV1561" name="MaxParameterCount" value="5" />
10+
<setting rule="AV1561" name="MaxConstructorParameterCount" value="8" />
1011
</cSharpGuidelinesAnalyzerSettings>
1112
```
1213

docs/Overview.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ This analyzer reports when a method, constructor, local function, indexer or del
105105
- declares a tuple parameter
106106
- returns a tuple with more than 2 elements.
107107

108-
**Note:** This rule can be customized using a [configuration file](/docs/Configuration.md) by setting **MaxParameterCount** to a value in range 0-255.
108+
**Note:** This rule can be customized using a [configuration file](/docs/Configuration.md) by setting **MaxParameterCount** and/or **MaxConstructorParameterCount** to a value in range 0-255.
109+
When **MaxConstructorParameterCount** is omitted, the value from **MaxParameterCount** (or its default) is used for constructors.
109110

110111
### [AV1562](https://github.com/dennisdoomen/CSharpGuidelines/blob/7a66f7468da6ce1477753a02e416e04bc9a44e45/_pages/1500_MaintainabilityGuidelines.md#av1562): Don't use ref or out parameters ![](/images/warn.png "severity: warning")
111112
This analyzer reports when:

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/Specs/Maintainability/AvoidSignatureWithManyParametersSpecs.cs

Lines changed: 124 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -740,11 +740,35 @@ internal void When_method_contains_nine_parameters_it_must_be_reported()
740740
}
741741

742742
[Fact]
743-
internal void When_settings_are_corrupt_it_must_use_default_value()
743+
internal void When_constructor_contains_nine_parameters_it_must_be_reported()
744+
{
745+
// Arrange
746+
ParsedSourceCode source = new TypeSourceCodeBuilder()
747+
.WithSettings(new AnalyzerSettingsBuilder()
748+
.Including(DiagnosticId, "MaxParameterCount", "8"))
749+
.InGlobalScope(@"
750+
class C
751+
{
752+
public [|C|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
753+
{
754+
}
755+
}
756+
")
757+
.Build();
758+
759+
// Act and assert
760+
VerifyGuidelineDiagnostic(source,
761+
"Constructor for 'C' contains 9 parameters, which exceeds the maximum of 8 parameters.");
762+
}
763+
764+
[Fact]
765+
internal void When_method_contains_nine_parameters_with_constructor_override_it_must_be_reported()
744766
{
745767
// Arrange
746768
ParsedSourceCode source = new MemberSourceCodeBuilder()
747-
.WithSettings("*** BAD XML ***")
769+
.WithSettings(new AnalyzerSettingsBuilder()
770+
.Including(DiagnosticId, "MaxParameterCount", "8")
771+
.Including(DiagnosticId, "MaxConstructorParameterCount", "4"))
748772
.InDefaultClass(@"
749773
void [|M|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
750774
{
@@ -754,49 +778,115 @@ internal void When_settings_are_corrupt_it_must_use_default_value()
754778

755779
// Act and assert
756780
VerifyGuidelineDiagnostic(source,
781+
"Method 'M' contains 9 parameters, which exceeds the maximum of 8 parameters.");
782+
}
783+
784+
[Fact]
785+
internal void When_constructor_contains_nine_parameters_with_constructor_override_it_must_be_reported()
786+
{
787+
// Arrange
788+
ParsedSourceCode source = new TypeSourceCodeBuilder()
789+
.WithSettings(new AnalyzerSettingsBuilder()
790+
.Including(DiagnosticId, "MaxParameterCount", "4")
791+
.Including(DiagnosticId, "MaxConstructorParameterCount", "8"))
792+
.InGlobalScope(@"
793+
class C
794+
{
795+
public [|C|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
796+
{
797+
}
798+
}
799+
")
800+
.Build();
801+
802+
// Act and assert
803+
VerifyGuidelineDiagnostic(source,
804+
"Constructor for 'C' contains 9 parameters, which exceeds the maximum of 8 parameters.");
805+
}
806+
807+
[Fact]
808+
internal void When_settings_are_corrupt_it_must_use_default_values()
809+
{
810+
// Arrange
811+
ParsedSourceCode source = new TypeSourceCodeBuilder()
812+
.WithSettings("*** BAD XML ***")
813+
.InGlobalScope(@"
814+
class C
815+
{
816+
public [|C|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
817+
{
818+
}
819+
820+
void [|M|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
821+
{
822+
}
823+
}
824+
")
825+
.Build();
826+
827+
// Act and assert
828+
VerifyGuidelineDiagnostic(source,
829+
"Constructor for 'C' contains 9 parameters, which exceeds the maximum of 3 parameters.",
757830
"Method 'M' contains 9 parameters, which exceeds the maximum of 3 parameters.");
758831
}
759832

760833
[Fact]
761-
internal void When_setting_is_missing_it_must_use_default_value()
834+
internal void When_settings_are_missing_it_must_use_default_values()
762835
{
763836
// Arrange
764-
ParsedSourceCode source = new MemberSourceCodeBuilder()
837+
ParsedSourceCode source = new TypeSourceCodeBuilder()
765838
.WithSettings(new AnalyzerSettingsBuilder()
766839
.Including(DiagnosticId, "OtherUnusedSetting", "SomeValue"))
767-
.InDefaultClass(@"
768-
void [|M|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
840+
.InGlobalScope(@"
841+
class C
769842
{
843+
public [|C|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
844+
{
845+
}
846+
847+
void [|M|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
848+
{
849+
}
770850
}
771851
")
772852
.Build();
773853

774854
// Act and assert
775855
VerifyGuidelineDiagnostic(source,
856+
"Constructor for 'C' contains 9 parameters, which exceeds the maximum of 3 parameters.",
776857
"Method 'M' contains 9 parameters, which exceeds the maximum of 3 parameters.");
777858
}
778859

779860
[Fact]
780-
internal void When_setting_value_is_missing_it_must_use_default_value()
861+
internal void When_setting_values_are_missing_it_must_use_default_values()
781862
{
782863
// Arrange
783-
ParsedSourceCode source = new MemberSourceCodeBuilder()
864+
ParsedSourceCode source = new TypeSourceCodeBuilder()
784865
.WithSettings(new AnalyzerSettingsBuilder()
785-
.Including(DiagnosticId, "MaxParameterCount", null))
786-
.InDefaultClass(@"
787-
void [|M|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
866+
.Including(DiagnosticId, "MaxParameterCount", null)
867+
.Including(DiagnosticId, "MaxConstructorParameterCount", null))
868+
.InGlobalScope(@"
869+
class C
788870
{
871+
public [|C|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
872+
{
873+
}
874+
875+
void [|M|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
876+
{
877+
}
789878
}
790879
")
791880
.Build();
792881

793882
// Act and assert
794883
VerifyGuidelineDiagnostic(source,
884+
"Constructor for 'C' contains 9 parameters, which exceeds the maximum of 3 parameters.",
795885
"Method 'M' contains 9 parameters, which exceeds the maximum of 3 parameters.");
796886
}
797887

798888
[Fact]
799-
internal void When_setting_value_is_out_of_range_it_must_fail()
889+
internal void When_parameter_setting_value_is_out_of_range_it_must_fail()
800890
{
801891
// Arrange
802892
ParsedSourceCode source = new MemberSourceCodeBuilder()
@@ -817,6 +907,28 @@ internal void When_setting_value_is_out_of_range_it_must_fail()
817907
.WithMessage("*Value for AV1561:MaxParameterCount configuration setting must be in range 0-255.*");
818908
}
819909

910+
[Fact]
911+
internal void When_constructor_parameter_setting_value_is_out_of_range_it_must_fail()
912+
{
913+
// Arrange
914+
ParsedSourceCode source = new MemberSourceCodeBuilder()
915+
.WithSettings(new AnalyzerSettingsBuilder()
916+
.Including(DiagnosticId, "MaxConstructorParameterCount", "-1"))
917+
.InDefaultClass(@"
918+
void [|M|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
919+
{
920+
}
921+
")
922+
.Build();
923+
924+
// Act
925+
Action action = () => VerifyGuidelineDiagnostic(source);
926+
927+
// Assert
928+
action.Should().Throw<Exception>()
929+
.WithMessage("*Value for AV1561:MaxConstructorParameterCount configuration setting must be in range 0-255.*");
930+
}
931+
820932
#endregion
821933

822934
protected override DiagnosticAnalyzer CreateAnalyzer()

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Rules/Maintainability/AvoidMemberWithManyStatementsAnalyzer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public sealed class AvoidMemberWithManyStatementsAnalyzer : DiagnosticAnalyzer
3333
[NotNull]
3434
private static readonly Action<CompilationStartAnalysisContext> RegisterCompilationStartAction = RegisterCompilationStart;
3535

36+
[NotNull]
37+
private static readonly AnalyzerSettingKey MaxStatementCountKey =
38+
new AnalyzerSettingKey(DiagnosticId, "MaxStatementCount");
39+
3640
[ItemNotNull]
3741
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
3842

@@ -58,8 +62,7 @@ private static int GetMaxStatementCountFromSettings([NotNull] AnalyzerOptions op
5862
{
5963
AnalyzerSettingsRegistry registry = AnalyzerSettingsProvider.LoadSettings(options, cancellationToken);
6064

61-
var settingKey = new AnalyzerSettingKey(DiagnosticId, "MaxStatementCount");
62-
return registry.TryGetInt32(settingKey, 0, 255) ?? DefaultMaxStatementCount;
65+
return registry.TryGetInt32(MaxStatementCountKey, 0, 255) ?? DefaultMaxStatementCount;
6366
}
6467

6568
private static void AnalyzeCodeBlock(CodeBlockAnalysisContext context, int maxStatementCount)

0 commit comments

Comments
 (0)