Skip to content

Commit e8c2315

Browse files
committed
Fixed R# warning: use extension blocks
1 parent 0f07195 commit e8c2315

39 files changed

Lines changed: 903 additions & 717 deletions

src/Examples/DapperExample/Repositories/CommandDefinitionExtensions.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ internal static class CommandDefinitionExtensions
88
{
99
// SQL Server and MySQL require any active DbTransaction to be explicitly associated to the DbConnection.
1010

11-
public static CommandDefinition Associate(this CommandDefinition command, DbTransaction transaction)
11+
extension(CommandDefinition command)
1212
{
13-
return new CommandDefinition(command.CommandText, command.Parameters, transaction, cancellationToken: command.CancellationToken);
14-
}
13+
public CommandDefinition Associate(DbTransaction transaction)
14+
{
15+
return new CommandDefinition(command.CommandText, command.Parameters, transaction, cancellationToken: command.CancellationToken);
16+
}
1517

16-
public static CommandDefinition Associate(this CommandDefinition command, AmbientTransaction? transaction)
17-
{
18-
return transaction != null
19-
? new CommandDefinition(command.CommandText, command.Parameters, transaction.Current, cancellationToken: command.CancellationToken)
20-
: command;
18+
public CommandDefinition Associate(AmbientTransaction? transaction)
19+
{
20+
return transaction != null
21+
? new CommandDefinition(command.CommandText, command.Parameters, transaction.Current, cancellationToken: command.CancellationToken)
22+
: command;
23+
}
2124
}
2225
}

src/Examples/NoEntityFrameworkExample/Data/ResourceGraphExtensions.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ namespace NoEntityFrameworkExample.Data;
88

99
internal static class ResourceGraphExtensions
1010
{
11-
public static IReadOnlyModel ToEntityModel(this IResourceGraph resourceGraph)
11+
extension(IResourceGraph resourceGraph)
1212
{
13-
var modelBuilder = new ModelBuilder();
14-
15-
foreach (ResourceType resourceType in resourceGraph.GetResourceTypes())
13+
public IReadOnlyModel ToEntityModel()
1614
{
17-
IncludeResourceType(resourceType, modelBuilder);
18-
}
15+
var modelBuilder = new ModelBuilder();
1916

20-
return modelBuilder.Model;
17+
foreach (ResourceType resourceType in resourceGraph.GetResourceTypes())
18+
{
19+
IncludeResourceType(resourceType, modelBuilder);
20+
}
21+
22+
return modelBuilder.Model;
23+
}
2124
}
2225

2326
private static void IncludeResourceType(ResourceType resourceType, ModelBuilder builder)

src/JsonApiDotNetCore.Annotations/PolyfillCollectionExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ namespace JsonApiDotNetCore;
55
// These methods provide polyfills for lower .NET versions.
66
internal static class PolyfillCollectionExtensions
77
{
8-
public static IReadOnlySet<T> AsReadOnly<T>(this HashSet<T> source)
8+
extension<T>(HashSet<T> source)
99
{
10-
return new ReadOnlySet<T>(source);
10+
public IReadOnlySet<T> AsReadOnly()
11+
{
12+
return new ReadOnlySet<T>(source);
13+
}
1114
}
1215
}

src/JsonApiDotNetCore.Annotations/TypeExtensions.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,31 @@ namespace JsonApiDotNetCore;
22

33
internal static class TypeExtensions
44
{
5-
/// <summary>
6-
/// Whether the specified source type implements or equals the specified interface.
7-
/// </summary>
8-
public static bool IsOrImplementsInterface<TInterface>(this Type? source)
5+
extension(Type? source)
96
{
10-
return IsOrImplementsInterface(source, typeof(TInterface));
11-
}
12-
13-
/// <summary>
14-
/// Whether the specified source type implements or equals the specified interface. This overload enables testing for an open generic interface.
15-
/// </summary>
16-
private static bool IsOrImplementsInterface(this Type? source, Type interfaceType)
17-
{
18-
ArgumentNullException.ThrowIfNull(interfaceType);
19-
20-
if (source == null)
7+
/// <summary>
8+
/// Whether the specified source type implements or equals the specified interface.
9+
/// </summary>
10+
public bool IsOrImplementsInterface<TInterface>()
2111
{
22-
return false;
12+
return IsOrImplementsInterface(source, typeof(TInterface));
2313
}
2414

25-
return AreTypesEqual(interfaceType, source, interfaceType.IsGenericType) ||
26-
source.GetInterfaces().Any(type => AreTypesEqual(interfaceType, type, interfaceType.IsGenericType));
15+
/// <summary>
16+
/// Whether the specified source type implements or equals the specified interface. This overload enables testing for an open generic interface.
17+
/// </summary>
18+
private bool IsOrImplementsInterface(Type interfaceType)
19+
{
20+
ArgumentNullException.ThrowIfNull(interfaceType);
21+
22+
if (source == null)
23+
{
24+
return false;
25+
}
26+
27+
return AreTypesEqual(interfaceType, source, interfaceType.IsGenericType) ||
28+
source.GetInterfaces().Any(type => AreTypesEqual(interfaceType, type, interfaceType.IsGenericType));
29+
}
2730
}
2831

2932
private static bool AreTypesEqual(Type left, Type right, bool isLeftGeneric)

src/JsonApiDotNetCore.OpenApi.Swashbuckle/ActionDescriptorExtensions.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,36 @@ namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
77

88
internal static class ActionDescriptorExtensions
99
{
10-
public static MethodInfo? TryGetActionMethod(this ActionDescriptor descriptor)
10+
extension(ActionDescriptor descriptor)
1111
{
12-
ArgumentNullException.ThrowIfNull(descriptor);
13-
14-
if (descriptor is ControllerActionDescriptor controllerActionDescriptor)
12+
public MethodInfo? TryGetActionMethod()
1513
{
16-
return controllerActionDescriptor.MethodInfo;
14+
ArgumentNullException.ThrowIfNull(descriptor);
15+
16+
if (descriptor is ControllerActionDescriptor controllerActionDescriptor)
17+
{
18+
return controllerActionDescriptor.MethodInfo;
19+
}
20+
21+
return descriptor.EndpointMetadata.OfType<MethodInfo>().FirstOrDefault();
1722
}
1823

19-
return descriptor.EndpointMetadata.OfType<MethodInfo>().FirstOrDefault();
20-
}
24+
public ControllerParameterDescriptor? GetBodyParameterDescriptor()
25+
{
26+
ArgumentNullException.ThrowIfNull(descriptor);
2127

22-
public static ControllerParameterDescriptor? GetBodyParameterDescriptor(this ActionDescriptor descriptor)
23-
{
24-
ArgumentNullException.ThrowIfNull(descriptor);
28+
ParameterDescriptor? parameterDescriptor = descriptor.Parameters.FirstOrDefault(parameterDescriptor =>
29+
parameterDescriptor.BindingInfo?.BindingSource == BindingSource.Body);
2530

26-
ParameterDescriptor? parameterDescriptor = descriptor.Parameters.FirstOrDefault(parameterDescriptor =>
27-
parameterDescriptor.BindingInfo?.BindingSource == BindingSource.Body);
31+
if (parameterDescriptor != null)
32+
{
33+
var controllerParameterDescriptor = parameterDescriptor as ControllerParameterDescriptor;
34+
ConsistencyGuard.ThrowIf(controllerParameterDescriptor == null);
2835

29-
if (parameterDescriptor != null)
30-
{
31-
var controllerParameterDescriptor = parameterDescriptor as ControllerParameterDescriptor;
32-
ConsistencyGuard.ThrowIf(controllerParameterDescriptor == null);
36+
return controllerParameterDescriptor;
37+
}
3338

34-
return controllerParameterDescriptor;
39+
return null;
3540
}
36-
37-
return null;
3841
}
3942
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/ObjectExtensions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ internal static class ObjectExtensions
88
new(() => typeof(object).GetMethod(nameof(MemberwiseClone), BindingFlags.Instance | BindingFlags.NonPublic)!,
99
LazyThreadSafetyMode.ExecutionAndPublication);
1010

11-
public static T MemberwiseClone<T>(this T source)
11+
extension<T>(T source)
1212
where T : class
1313
{
14-
ArgumentNullException.ThrowIfNull(source);
14+
public T MemberwiseClone()
15+
{
16+
ArgumentNullException.ThrowIfNull(source);
1517

16-
return (T)MemberwiseCloneMethod.Value.Invoke(source, null)!;
18+
return (T)MemberwiseCloneMethod.Value.Invoke(source, null)!;
19+
}
1720
}
1821
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/OpenApiSchemaExtensions.cs

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,85 +4,97 @@ namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
44

55
internal static class OpenApiSchemaExtensions
66
{
7-
public static OpenApiSchema AsInlineSchema(this IOpenApiSchema schema)
7+
extension(IOpenApiSchema schema)
88
{
9-
ConsistencyGuard.ThrowIf(schema is not OpenApiSchema);
10-
return (OpenApiSchema)schema;
11-
}
9+
public OpenApiSchema AsInlineSchema()
10+
{
11+
ConsistencyGuard.ThrowIf(schema is not OpenApiSchema);
12+
return (OpenApiSchema)schema;
13+
}
1214

13-
public static OpenApiSchemaReference AsReferenceSchema(this IOpenApiSchema schema)
14-
{
15-
ConsistencyGuard.ThrowIf(schema is not OpenApiSchemaReference);
16-
return (OpenApiSchemaReference)schema;
15+
public OpenApiSchemaReference AsReferenceSchema()
16+
{
17+
ConsistencyGuard.ThrowIf(schema is not OpenApiSchemaReference);
18+
return (OpenApiSchemaReference)schema;
19+
}
1720
}
1821

19-
public static string GetReferenceId(this OpenApiSchemaReference referenceSchema)
22+
extension(OpenApiSchemaReference referenceSchema)
2023
{
21-
string? schemaId = referenceSchema.Reference.Id;
22-
ConsistencyGuard.ThrowIf(schemaId is null);
23-
return schemaId;
24+
public string GetReferenceId()
25+
{
26+
string? schemaId = referenceSchema.Reference.Id;
27+
ConsistencyGuard.ThrowIf(schemaId is null);
28+
return schemaId;
29+
}
2430
}
2531

26-
public static void SetNullable(this OpenApiSchema inlineSchema, bool nullable)
32+
extension(OpenApiSchema inlineSchema)
2733
{
28-
ArgumentNullException.ThrowIfNull(inlineSchema);
29-
30-
if (nullable)
31-
{
32-
inlineSchema.Type ??= JsonSchemaType.Null;
33-
inlineSchema.Type |= JsonSchemaType.Null;
34-
}
35-
else
34+
public void SetNullable(bool nullable)
3635
{
37-
if (inlineSchema.Type != null)
36+
ArgumentNullException.ThrowIfNull(inlineSchema);
37+
38+
if (nullable)
3839
{
39-
inlineSchema.Type &= ~JsonSchemaType.Null;
40+
inlineSchema.Type ??= JsonSchemaType.Null;
41+
inlineSchema.Type |= JsonSchemaType.Null;
42+
}
43+
else
44+
{
45+
if (inlineSchema.Type != null)
46+
{
47+
inlineSchema.Type &= ~JsonSchemaType.Null;
48+
}
4049
}
4150
}
42-
}
43-
44-
public static void ReorderProperties(this OpenApiSchema inlineSchema, IEnumerable<string> propertyNamesInOrder)
45-
{
46-
ArgumentNullException.ThrowIfNull(inlineSchema);
47-
ArgumentNullException.ThrowIfNull(propertyNamesInOrder);
4851

49-
if (inlineSchema.Properties is { Count: > 1 })
52+
public void ReorderProperties(IEnumerable<string> propertyNamesInOrder)
5053
{
51-
var propertiesInOrder = new Dictionary<string, IOpenApiSchema>();
54+
ArgumentNullException.ThrowIfNull(inlineSchema);
55+
ArgumentNullException.ThrowIfNull(propertyNamesInOrder);
5256

53-
foreach (string propertyName in propertyNamesInOrder)
57+
if (inlineSchema.Properties is { Count: > 1 })
5458
{
55-
if (inlineSchema.Properties.TryGetValue(propertyName, out IOpenApiSchema? schema))
59+
var propertiesInOrder = new Dictionary<string, IOpenApiSchema>();
60+
61+
foreach (string propertyName in propertyNamesInOrder)
5662
{
57-
propertiesInOrder.Add(propertyName, schema);
63+
if (inlineSchema.Properties.TryGetValue(propertyName, out IOpenApiSchema? schema))
64+
{
65+
propertiesInOrder.Add(propertyName, schema);
66+
}
5867
}
59-
}
6068

61-
ConsistencyGuard.ThrowIf(inlineSchema.Properties.Count != propertiesInOrder.Count);
69+
ConsistencyGuard.ThrowIf(inlineSchema.Properties.Count != propertiesInOrder.Count);
6270

63-
inlineSchema.Properties = propertiesInOrder;
71+
inlineSchema.Properties = propertiesInOrder;
72+
}
6473
}
6574
}
6675

67-
public static OpenApiSchema WrapInExtendedSchema(this IOpenApiSchema source)
76+
extension(IOpenApiSchema source)
6877
{
69-
ArgumentNullException.ThrowIfNull(source);
70-
71-
return new OpenApiSchema
78+
public OpenApiSchema WrapInExtendedSchema()
7279
{
73-
AllOf = [source]
74-
};
75-
}
80+
ArgumentNullException.ThrowIfNull(source);
7681

77-
public static IOpenApiSchema UnwrapLastExtendedSchema(this IOpenApiSchema source)
78-
{
79-
ArgumentNullException.ThrowIfNull(source);
82+
return new OpenApiSchema
83+
{
84+
AllOf = [source]
85+
};
86+
}
8087

81-
if (source is OpenApiSchema && source.AllOf is { Count: > 0 })
88+
public IOpenApiSchema UnwrapLastExtendedSchema()
8289
{
83-
return source.AllOf.Last();
84-
}
90+
ArgumentNullException.ThrowIfNull(source);
8591

86-
return source;
92+
if (source is OpenApiSchema && source.AllOf is { Count: > 0 })
93+
{
94+
return source.AllOf.Last();
95+
}
96+
97+
return source;
98+
}
8799
}
88100
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SchemaRepositoryExtensions.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@ namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
66

77
internal static class SchemaRepositoryExtensions
88
{
9-
public static OpenApiSchemaReference LookupByType(this SchemaRepository schemaRepository, Type schemaType)
9+
extension(SchemaRepository schemaRepository)
1010
{
11-
ArgumentNullException.ThrowIfNull(schemaRepository);
12-
ArgumentNullException.ThrowIfNull(schemaType);
13-
14-
if (!schemaRepository.TryLookupByTypeSafe(schemaType, out OpenApiSchemaReference? referenceSchema))
11+
public OpenApiSchemaReference LookupByType(Type schemaType)
1512
{
16-
throw new InvalidOperationException($"Reference schema for '{schemaType.Name}' does not exist.");
17-
}
13+
ArgumentNullException.ThrowIfNull(schemaRepository);
14+
ArgumentNullException.ThrowIfNull(schemaType);
1815

19-
return referenceSchema;
20-
}
16+
if (!schemaRepository.TryLookupByTypeSafe(schemaType, out OpenApiSchemaReference? referenceSchema))
17+
{
18+
throw new InvalidOperationException($"Reference schema for '{schemaType.Name}' does not exist.");
19+
}
2120

22-
public static bool TryLookupByTypeSafe(this SchemaRepository schemaRepository, Type type, [NotNullWhen(true)] out OpenApiSchemaReference? referenceSchema)
23-
{
24-
bool result = schemaRepository.TryLookupByType(type, out OpenApiSchemaReference? obliviousReferenceSchema);
25-
referenceSchema = result ? obliviousReferenceSchema : null;
26-
return result;
21+
return referenceSchema;
22+
}
23+
24+
public bool TryLookupByTypeSafe(Type type, [NotNullWhen(true)] out OpenApiSchemaReference? referenceSchema)
25+
{
26+
bool result = schemaRepository.TryLookupByType(type, out OpenApiSchemaReference? obliviousReferenceSchema);
27+
referenceSchema = result ? obliviousReferenceSchema : null;
28+
return result;
29+
}
2730
}
2831
}

0 commit comments

Comments
 (0)