Skip to content

Commit b703130

Browse files
authored
Fix CA1062 warning (#2229)
Fix CA1062 and S4456 warnings for `IPolicyWrapExtension`.
1 parent 72331dc commit b703130

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/Polly/Wrap/IPolicyWrapExtension.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/// <summary>
44
/// Extension methods for IPolicyWrap.
55
/// </summary>
6-
#pragma warning disable CA1062 // Validate arguments of public methods
76
public static class IPolicyWrapExtension
87
{
98
/// <summary>
@@ -13,21 +12,12 @@ public static class IPolicyWrapExtension
1312
/// <returns>An <see cref="IEnumerable{IsPolicy}"/> of all the policies in the wrap.</returns>
1413
public static IEnumerable<IsPolicy> GetPolicies(this IPolicyWrap policyWrap)
1514
{
16-
var childPolicies = new[] { policyWrap.Outer, policyWrap.Inner };
17-
foreach (var childPolicy in childPolicies)
15+
if (policyWrap is null)
1816
{
19-
if (childPolicy is IPolicyWrap anotherWrap)
20-
{
21-
foreach (var policy in anotherWrap.GetPolicies())
22-
{
23-
yield return policy;
24-
}
25-
}
26-
else if (childPolicy != null)
27-
{
28-
yield return childPolicy;
29-
}
17+
throw new ArgumentNullException(nameof(policyWrap));
3018
}
19+
20+
return GetPoliciesIterator(policyWrap);
3121
}
3222

3323
/// <summary>
@@ -83,4 +73,23 @@ public static TPolicy GetPolicy<TPolicy>(this IPolicyWrap policyWrap, Func<TPoli
8373

8474
return policyWrap.GetPolicies().OfType<TPolicy>().SingleOrDefault(filter);
8575
}
76+
77+
private static IEnumerable<IsPolicy> GetPoliciesIterator(IPolicyWrap policyWrap)
78+
{
79+
var childPolicies = new[] { policyWrap.Outer, policyWrap.Inner };
80+
foreach (var childPolicy in childPolicies)
81+
{
82+
if (childPolicy is IPolicyWrap anotherWrap)
83+
{
84+
foreach (var policy in anotherWrap.GetPolicies())
85+
{
86+
yield return policy;
87+
}
88+
}
89+
else if (childPolicy != null)
90+
{
91+
yield return childPolicy;
92+
}
93+
}
94+
}
8695
}

test/Polly.Specs/Wrap/IPolicyWrapExtensionSpecs.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
public class IPolicyWrapExtensionSpecs
44
{
5+
[Fact]
6+
public void Should_throw_when_policy_wrap_is_null()
7+
{
8+
IPolicyWrap policyWrap = null!;
9+
10+
var action = () => policyWrap.GetPolicies();
11+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("policyWrap");
12+
}
13+
514
[Fact]
615
public void Should_pass_all_nested_policies_from_PolicyWrap_in_same_order_they_were_added()
716
{

0 commit comments

Comments
 (0)