Skip to content

Commit ffee631

Browse files
committed
Fix violations of S3776: Cognitive Complexity of methods should not be too high
1 parent 1e614b7 commit ffee631

22 files changed

Lines changed: 454 additions & 358 deletions

File tree

‎Steeltoe.Debug.ruleset‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<Rule Id="S3597" Action="None" />
7272
<Rule Id="S3598" Action="None" />
7373
<Rule Id="S3717" Action="Info" />
74-
<Rule Id="S3776" Action="Info" />
74+
<Rule Id="S3776" Action="Warning" />
7575
<Rule Id="S3872" Action="Warning" />
7676
<Rule Id="S3874" Action="Warning" />
7777
<Rule Id="S3878" Action="None" />

‎Steeltoe.Release.ruleset‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<Rule Id="S3597" Action="None" />
6060
<Rule Id="S3598" Action="None" />
6161
<Rule Id="S3717" Action="Warning" />
62+
<Rule Id="S3776" Action="Warning" />
6263
<Rule Id="S3872" Action="Warning" />
6364
<Rule Id="S3874" Action="Warning" />
6465
<Rule Id="S3878" Action="None" />

‎src/Common/src/Common/Net/InetUtils.cs‎

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,31 @@ public InetUtils(IDomainNameResolver domainNameResolver, IOptionsMonitor<InetOpt
3535

3636
public virtual HostInfo FindFirstNonLoopbackHostInfo()
3737
{
38-
InetOptions inetOptions = _optionsMonitor.CurrentValue;
39-
40-
IPAddress? address = FindFirstNonLoopbackAddress(inetOptions);
38+
InetOptions options = _optionsMonitor.CurrentValue;
39+
IPAddress? address = FindFirstNonLoopbackAddress(options);
4140

4241
if (address != null)
4342
{
44-
return ConvertAddress(address, inetOptions);
43+
return ConvertAddress(address, options);
4544
}
4645

47-
return new HostInfo(inetOptions.DefaultHostname!, inetOptions.DefaultIPAddress!);
46+
return new HostInfo(options.DefaultHostname!, options.DefaultIPAddress!);
4847
}
4948

5049
public IPAddress? FindFirstNonLoopbackAddress()
5150
{
5251
return FindFirstNonLoopbackAddress(_optionsMonitor.CurrentValue);
5352
}
5453

55-
private IPAddress? FindFirstNonLoopbackAddress(InetOptions inetOptions)
54+
private IPAddress? FindFirstNonLoopbackAddress(InetOptions options)
5655
{
5756
IPAddress? result = null;
5857

5958
try
6059
{
6160
int lowest = int.MaxValue;
62-
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
6361

64-
foreach (NetworkInterface networkInterface in networkInterfaces)
62+
foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
6563
{
6664
if (networkInterface is { OperationalStatus: OperationalStatus.Up, IsReceiveOnly: false })
6765
{
@@ -73,24 +71,7 @@ public virtual HostInfo FindFirstNonLoopbackHostInfo()
7371
if (iPv4Properties.Index < lowest || result == null)
7472
{
7573
lowest = iPv4Properties.Index;
76-
}
77-
else
78-
{
79-
continue;
80-
}
81-
82-
if (!IgnoreInterface(networkInterface.Name, inetOptions))
83-
{
84-
foreach (UnicastIPAddressInformation addressInfo in properties.UnicastAddresses)
85-
{
86-
IPAddress address = addressInfo.Address;
87-
88-
if (IsInet4Address(address) && !IsLoopbackAddress(address) && IsPreferredAddress(address, inetOptions))
89-
{
90-
LogNonLoopbackInterfaceFound(networkInterface.Name);
91-
result = address;
92-
}
93-
}
74+
result = GetLastNonLoopbackInterfaceAddress(networkInterface, properties, options) ?? result;
9475
}
9576
}
9677
}
@@ -100,12 +81,28 @@ public virtual HostInfo FindFirstNonLoopbackHostInfo()
10081
LogCannotGetNonLoopbackAddress(exception);
10182
}
10283

103-
if (result != null)
84+
return result ?? GetHostAddress();
85+
}
86+
87+
private IPAddress? GetLastNonLoopbackInterfaceAddress(NetworkInterface networkInterface, IPInterfaceProperties properties, InetOptions options)
88+
{
89+
IPAddress? result = null;
90+
91+
if (!IgnoreInterface(networkInterface.Name, options))
10492
{
105-
return result;
93+
foreach (UnicastIPAddressInformation addressInfo in properties.UnicastAddresses)
94+
{
95+
IPAddress address = addressInfo.Address;
96+
97+
if (IsInet4Address(address) && !IsLoopbackAddress(address) && IsPreferredAddress(address, options))
98+
{
99+
LogNonLoopbackInterfaceFound(networkInterface.Name);
100+
result = address;
101+
}
102+
}
106103
}
107104

108-
return GetHostAddress();
105+
return result;
109106
}
110107

111108
private static bool IsInet4Address(IPAddress address)
@@ -118,9 +115,9 @@ private static bool IsLoopbackAddress(IPAddress address)
118115
return IPAddress.IsLoopback(address);
119116
}
120117

121-
internal bool IsPreferredAddress(IPAddress address, InetOptions inetOptions)
118+
internal bool IsPreferredAddress(IPAddress address, InetOptions options)
122119
{
123-
if (inetOptions.UseOnlySiteLocalInterfaces)
120+
if (options.UseOnlySiteLocalInterfaces)
124121
{
125122
bool siteLocalAddress = IsSiteLocalAddress(address);
126123

@@ -132,7 +129,7 @@ internal bool IsPreferredAddress(IPAddress address, InetOptions inetOptions)
132129
return siteLocalAddress;
133130
}
134131

135-
string[] preferredNetworks = inetOptions.GetPreferredNetworks().ToArray();
132+
string[] preferredNetworks = options.GetPreferredNetworks().ToArray();
136133

137134
if (preferredNetworks.Length == 0)
138135
{
@@ -154,32 +151,30 @@ internal bool IsPreferredAddress(IPAddress address, InetOptions inetOptions)
154151
return false;
155152
}
156153

157-
internal bool IgnoreInterface(string interfaceName, InetOptions inetOptions)
154+
internal bool IgnoreInterface(string interfaceName, InetOptions options)
158155
{
159-
if (string.IsNullOrEmpty(interfaceName))
160-
{
161-
return false;
162-
}
163-
164-
foreach (string regex in inetOptions.GetIgnoredInterfaces())
156+
if (!string.IsNullOrEmpty(interfaceName))
165157
{
166-
var matcher = new Regex(regex, InetRegexOptions, RegexMatchTimeout);
167-
168-
if (matcher.IsMatch(interfaceName))
158+
foreach (string regex in options.GetIgnoredInterfaces())
169159
{
170-
LogIgnoringInterface(interfaceName);
171-
return true;
160+
var matcher = new Regex(regex, InetRegexOptions, RegexMatchTimeout);
161+
162+
if (matcher.IsMatch(interfaceName))
163+
{
164+
LogIgnoringInterface(interfaceName);
165+
return true;
166+
}
172167
}
173168
}
174169

175170
return false;
176171
}
177172

178-
internal HostInfo ConvertAddress(IPAddress address, InetOptions inetOptions)
173+
internal HostInfo ConvertAddress(IPAddress address, InetOptions options)
179174
{
180175
string hostname;
181176

182-
if (!inetOptions.SkipReverseDnsLookup)
177+
if (!options.SkipReverseDnsLookup)
183178
{
184179
try
185180
{
@@ -195,29 +190,21 @@ internal HostInfo ConvertAddress(IPAddress address, InetOptions inetOptions)
195190
}
196191
else
197192
{
198-
hostname = inetOptions.DefaultHostname!;
193+
hostname = options.DefaultHostname!;
199194
}
200195

201196
return new HostInfo(hostname, address.ToString());
202197
}
203198

204199
private IPAddress? ResolveHostAddress(string hostName)
205200
{
206-
IPAddress? result = null;
207-
208201
try
209202
{
210-
IPAddress[] results = Dns.GetHostAddresses(hostName);
211-
212-
if (results.Length > 0)
203+
foreach (IPAddress address in Dns.GetHostAddresses(hostName))
213204
{
214-
foreach (IPAddress address in results)
205+
if (address.AddressFamily == AddressFamily.InterNetwork)
215206
{
216-
if (address.AddressFamily == AddressFamily.InterNetwork)
217-
{
218-
result = address;
219-
break;
220-
}
207+
return address;
221208
}
222209
}
223210
}
@@ -226,7 +213,7 @@ internal HostInfo ConvertAddress(IPAddress address, InetOptions inetOptions)
226213
LogUnableToResolveHostAddress(exception);
227214
}
228215

229-
return result;
216+
return null;
230217
}
231218

232219
private string? ResolveHostName()

‎src/Configuration/src/CloudFoundry/ServiceBindings/PostProcessors/CredHubCloudFoundryPostProcessor.cs‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,19 @@ public override void PostProcessConfiguration(PostProcessorConfigurationProvider
3131

3232
foreach ((string fullKey, string? value) in configurationData.ToArray())
3333
{
34-
if (!fullKey.StartsWith(credentialsPrefix, StringComparison.OrdinalIgnoreCase))
34+
if (fullKey.StartsWith(credentialsPrefix, StringComparison.OrdinalIgnoreCase))
3535
{
36-
continue;
37-
}
36+
// Dots in a credential key are converted to colons so secrets can be shared between Spring and .NET apps.
37+
string keyWithoutPrefix = fullKey[credentialsPrefix.Length..];
38+
string normalizedKey = keyWithoutPrefix.Replace(".", ConfigurationPath.KeyDelimiter, StringComparison.Ordinal);
3839

39-
// Dots in a credential key are converted to colons so secrets can be shared between Spring and .NET apps.
40-
string keyWithoutPrefix = fullKey[credentialsPrefix.Length..];
41-
string normalizedKey = keyWithoutPrefix.Replace(".", ConfigurationPath.KeyDelimiter, StringComparison.Ordinal);
40+
if (configurationData.TryGetValue(normalizedKey, out string? existingValue) && existingValue != value)
41+
{
42+
LogOverwritingConfigurationKey(normalizedKey, bindingName);
43+
}
4244

43-
if (configurationData.TryGetValue(normalizedKey, out string? existingValue) && existingValue != value)
44-
{
45-
LogOverwritingConfigurationKey(normalizedKey, bindingName);
45+
configurationData[normalizedKey] = value;
4646
}
47-
48-
configurationData[normalizedKey] = value;
4947
}
5048
}
5149
}

0 commit comments

Comments
 (0)