refactor: replace throw helper with explicit null check

This commit is contained in:
Oliver Booth 2024-02-15 22:38:48 +00:00
parent 682edbe8c6
commit 5ffc7a5a3b
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
18 changed files with 206 additions and 48 deletions

View File

@ -28,8 +28,15 @@ public sealed class AliasesAttribute : Attribute
public AliasesAttribute(string alias, params string[] aliases)
#pragma warning restore CA1019
{
ArgumentNullException.ThrowIfNull(alias);
ArgumentNullException.ThrowIfNull(aliases);
if (alias is null)
{
throw new ArgumentNullException(nameof(alias));
}
if (aliases is null)
{
throw new ArgumentNullException(nameof(aliases));
}
if (string.IsNullOrWhiteSpace(alias))
{

View File

@ -14,7 +14,11 @@ public sealed class CommandAttribute : Attribute
/// <exception cref="ArgumentException"><paramref name="name" /> is empty, or consists of only whitespace.</exception>
public CommandAttribute(string name)
{
ArgumentNullException.ThrowIfNull(name);
if (name is null)
{
throw new ArgumentNullException(nameof(name));
}
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("Name cannot be empty");

View File

@ -14,7 +14,11 @@ public sealed class RequireAvatarNameAttribute : PreExecutionCheckAttribute
/// <exception cref="ArgumentNullException"><paramref name="names" /> is <see langword="null" />.</exception>
public RequireAvatarNameAttribute(IEnumerable<string> names)
{
ArgumentNullException.ThrowIfNull(names);
if (names is null)
{
throw new ArgumentNullException(nameof(names));
}
Names = names.ToArray();
}
@ -26,7 +30,11 @@ public sealed class RequireAvatarNameAttribute : PreExecutionCheckAttribute
[CLSCompliant(false)]
public RequireAvatarNameAttribute(params string[] names)
{
ArgumentNullException.ThrowIfNull(names);
if (names is null)
{
throw new ArgumentNullException(nameof(names));
}
Names = names[..];
}
@ -39,7 +47,11 @@ public sealed class RequireAvatarNameAttribute : PreExecutionCheckAttribute
/// <inheritdoc />
protected internal override Task<bool> PerformAsync(CommandContext context)
{
ArgumentNullException.ThrowIfNull(context);
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
return Task.FromResult(Names.Contains(context.Avatar.Name));
}
}

View File

@ -8,7 +8,11 @@ public sealed class RequireBotAttribute : PreExecutionCheckAttribute
/// <inheritdoc />
protected internal override Task<bool> PerformAsync(CommandContext context)
{
ArgumentNullException.ThrowIfNull(context);
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
return Task.FromResult(context.Avatar.IsBot);
}
}

View File

@ -8,7 +8,11 @@ public sealed class RequireBotOwnerAttribute : PreExecutionCheckAttribute
/// <inheritdoc />
protected internal override Task<bool> PerformAsync(CommandContext context)
{
ArgumentNullException.ThrowIfNull(context);
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
return Task.FromResult(context.Avatar.UserId == context.Client.CurrentUser?.Id);
}
}

View File

@ -8,7 +8,11 @@ public sealed class RequireHumanAttribute : PreExecutionCheckAttribute
/// <inheritdoc />
protected internal override Task<bool> PerformAsync(CommandContext context)
{
ArgumentNullException.ThrowIfNull(context);
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
return Task.FromResult(!context.Avatar.IsBot);
}
}

View File

@ -14,7 +14,11 @@ public sealed class RequireUserIdAttribute : PreExecutionCheckAttribute
/// <exception cref="ArgumentNullException"><paramref name="userIds" /> is <see langword="null" />.</exception>
public RequireUserIdAttribute(IEnumerable<int> userIds)
{
ArgumentNullException.ThrowIfNull(userIds);
if (userIds is null)
{
throw new ArgumentNullException(nameof(userIds));
}
UserIds = userIds.ToArray();
}
@ -26,7 +30,11 @@ public sealed class RequireUserIdAttribute : PreExecutionCheckAttribute
[CLSCompliant(false)]
public RequireUserIdAttribute(params int[] userIds)
{
ArgumentNullException.ThrowIfNull(userIds);
if (userIds is null)
{
throw new ArgumentNullException(nameof(userIds));
}
UserIds = userIds[..];
}
@ -39,7 +47,11 @@ public sealed class RequireUserIdAttribute : PreExecutionCheckAttribute
/// <inheritdoc />
protected internal override Task<bool> PerformAsync(CommandContext context)
{
ArgumentNullException.ThrowIfNull(context);
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
return Task.FromResult(UserIds.Contains(context.Avatar.UserId));
}
}

View File

@ -16,7 +16,11 @@ public sealed class RequireUserNameAttribute : PreExecutionCheckAttribute
/// <exception cref="ArgumentNullException"><paramref name="names" /> is <see langword="null" />.</exception>
public RequireUserNameAttribute(IEnumerable<string> names)
{
ArgumentNullException.ThrowIfNull(names);
if (names is null)
{
throw new ArgumentNullException(nameof(names));
}
Names = names.ToArray();
}
@ -28,7 +32,11 @@ public sealed class RequireUserNameAttribute : PreExecutionCheckAttribute
[CLSCompliant(false)]
public RequireUserNameAttribute(params string[] names)
{
ArgumentNullException.ThrowIfNull(names);
if (names is null)
{
throw new ArgumentNullException(nameof(names));
}
Names = names[..];
}
@ -41,7 +49,11 @@ public sealed class RequireUserNameAttribute : PreExecutionCheckAttribute
/// <inheritdoc />
protected internal override async Task<bool> PerformAsync(CommandContext context)
{
ArgumentNullException.ThrowIfNull(context);
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
VirtualParadiseUser user = await context.Avatar.GetUserAsync().ConfigureAwait(false);
return Names.Contains(user.Name);
}

View File

@ -33,8 +33,16 @@ public sealed class CommandsExtension : VirtualParadiseClientExtension
public CommandsExtension(VirtualParadiseClient client, CommandsExtensionConfiguration configuration)
: base(client)
{
ArgumentNullException.ThrowIfNull(client);
ArgumentNullException.ThrowIfNull(configuration);
if (client is null)
{
throw new ArgumentNullException(nameof(client));
}
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
_configuration = configuration;
_configuration.Services ??= client.Services;
@ -64,7 +72,10 @@ public sealed class CommandsExtension : VirtualParadiseClientExtension
/// </exception>
public void RegisterCommands(Assembly assembly)
{
ArgumentNullException.ThrowIfNull(assembly);
if (assembly is null)
{
throw new ArgumentNullException(nameof(assembly));
}
foreach (Type type in assembly.GetTypes())
{
@ -136,7 +147,10 @@ public sealed class CommandsExtension : VirtualParadiseClientExtension
/// </exception>
public void RegisterCommands(Type moduleType)
{
ArgumentNullException.ThrowIfNull(moduleType);
if (moduleType is null)
{
throw new ArgumentNullException(nameof(moduleType));
}
if (moduleType.IsAbstract)
{
@ -164,7 +178,10 @@ public sealed class CommandsExtension : VirtualParadiseClientExtension
/// <inheritdoc />
protected internal override Task OnMessageReceived(VirtualParadiseMessage message)
{
ArgumentNullException.ThrowIfNull(message);
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}
if (message.Type != MessageType.ChatMessage)
{

View File

@ -14,7 +14,11 @@ public static class VirtualParadiseClientExtensions
/// <exception cref="ArgumentNullException"><paramref name="client" /> is <see langword="null" />.</exception>
public static CommandsExtension UseCommands(this VirtualParadiseClient client, CommandsExtensionConfiguration configuration)
{
ArgumentNullException.ThrowIfNull(client);
if (client is null)
{
throw new ArgumentNullException(nameof(client));
}
return client.AddExtension<CommandsExtension>(configuration);
}
}

View File

@ -224,7 +224,11 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
/// <remarks>Passing <see langword="null" /> to <paramref name="name" /> will hide the name from the recipient.</remarks>
public Task<VirtualParadiseMessage> SendMessageAsync(string? name, string message, FontStyle fontStyle, Color color)
{
ArgumentNullException.ThrowIfNull(message);
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}
if (string.IsNullOrWhiteSpace(message))
{
throw new ArgumentException(ExceptionMessages.ValueCannotBeEmpty, nameof(message));
@ -282,7 +286,10 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
/// <exception cref="ArgumentNullException"><paramref name="uri" /> is <see langword="null" />.</exception>
public Task SendUriAsync(Uri uri, UriTarget target = UriTarget.Browser)
{
ArgumentNullException.ThrowIfNull(uri);
if (uri is null)
{
throw new ArgumentNullException(nameof(uri));
}
// ReSharper disable once InconsistentlySynchronizedField
if (this == _client.CurrentAvatar)
@ -306,7 +313,10 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
/// <exception cref="UnauthorizedAccessException">The client does not have permission to modify world settings.</exception>
public async Task SendWorldSettings(Action<WorldSettingsBuilder> action)
{
ArgumentNullException.ThrowIfNull(action);
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
// ReSharper disable once InconsistentlySynchronizedField
var builder = new WorldSettingsBuilder(_client, Session);
@ -323,7 +333,11 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
/// <exception cref="ArgumentNullException"><paramref name="world" /> is <see langword="null" />.</exception>
public Task TeleportAsync(VirtualParadiseWorld world, Vector3d position)
{
ArgumentNullException.ThrowIfNull(world);
if (world is null)
{
throw new ArgumentNullException(nameof(world));
}
return TeleportAsync(world.Name, position, Rotation.None);
}
@ -336,7 +350,11 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
/// <exception cref="ArgumentNullException"><paramref name="world" /> is <see langword="null" />.</exception>
public Task TeleportAsync(VirtualParadiseWorld world, Vector3d position, Rotation rotation)
{
ArgumentNullException.ThrowIfNull(world);
if (world is null)
{
throw new ArgumentNullException(nameof(world));
}
return TeleportAsync(world.Name, position, rotation);
}
@ -358,15 +376,15 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
/// <param name="rotation">The rotation to which this avatar should be teleported.</param>
public async Task TeleportAsync(string world, Vector3d position, Rotation rotation)
{
ArgumentNullException.ThrowIfNull(world);
#if NET7_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(world);
#else
if (world is null)
{
throw new ArgumentNullException(nameof(world));
}
if (string.IsNullOrEmpty(world))
{
throw new ArgumentException(ExceptionMessages.ValueCannotBeEmpty, nameof(world));
}
#endif
// ReSharper disable InconsistentlySynchronizedField
bool isSelf = this == _client.CurrentAvatar;

View File

@ -52,7 +52,10 @@ public class VirtualParadiseModelObject : VirtualParadiseObject
/// </exception>
public async Task ModifyAsync(Action<VirtualParadiseModelObjectBuilder> action)
{
ArgumentNullException.ThrowIfNull(action);
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
var taskCompletionSource = new TaskCompletionSource<ReasonCode>();
var builder = new VirtualParadiseModelObjectBuilder(Client, this, ObjectBuilderMode.Modify);

View File

@ -20,7 +20,11 @@ public abstract class VirtualParadiseObject : IEquatable<VirtualParadiseObject>
/// <exception cref="ArgumentNullException"><paramref name="client" /> is <see langword="null" />.</exception>
protected internal VirtualParadiseObject(VirtualParadiseClient client, int id)
{
ArgumentNullException.ThrowIfNull(client);
if (client is null)
{
throw new ArgumentNullException(nameof(client));
}
Client = client;
Id = id;
}
@ -237,7 +241,10 @@ public abstract class VirtualParadiseObject : IEquatable<VirtualParadiseObject>
/// <param name="builder">The builder whose values to extract.</param>
protected internal virtual void ExtractFromBuilder(VirtualParadiseObjectBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
Location location = Location;
Vector3d position = builder.Position.ValueOr(location.Position);
@ -256,7 +263,11 @@ public abstract class VirtualParadiseObject : IEquatable<VirtualParadiseObject>
/// <exception cref="ArgumentNullException"><paramref name="virtualParadiseObject" /> is <see langword="null" />.</exception>
protected internal virtual void ExtractFromOther(VirtualParadiseObject virtualParadiseObject)
{
ArgumentNullException.ThrowIfNull(virtualParadiseObject);
if (virtualParadiseObject is null)
{
throw new ArgumentNullException(nameof(virtualParadiseObject));
}
Location = virtualParadiseObject.Location;
ModificationTimestamp = virtualParadiseObject.ModificationTimestamp;
Owner = virtualParadiseObject.Owner;

View File

@ -123,7 +123,10 @@ public sealed class VirtualParadiseWorld : IEquatable<VirtualParadiseWorld>
/// <exception cref="UnauthorizedAccessException">The client does not have permission to modify world settings.</exception>
public async Task ModifyAsync(Action<WorldSettingsBuilder> action)
{
ArgumentNullException.ThrowIfNull(action);
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
var builder = new WorldSettingsBuilder(_client);
await Task.Run(() => action(builder)).ConfigureAwait(false);

View File

@ -17,14 +17,26 @@ internal static class DependencyInjectionUtility
public static object CreateInstance(Type type, VirtualParadiseClient client)
{
ArgumentNullException.ThrowIfNull(type);
ArgumentNullException.ThrowIfNull(client);
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
if (client is null)
{
throw new ArgumentNullException(nameof(client));
}
return CreateInstance(type, client.Services);
}
public static object CreateInstance(Type type, IServiceProvider? serviceProvider = null)
{
ArgumentNullException.ThrowIfNull(type);
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
object? instance;
TypeInfo typeInfo = type.GetTypeInfo();

View File

@ -46,7 +46,10 @@ public sealed partial class VirtualParadiseClient
/// </exception>
public VirtualParadiseClientExtension AddExtension(Type type, params object?[]? arguments)
{
ArgumentNullException.ThrowIfNull(type);
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
if (!type.IsSubclassOf(typeof(VirtualParadiseClientExtension)))
{
@ -85,7 +88,10 @@ public sealed partial class VirtualParadiseClient
/// <exception cref="InvalidOperationException">No extension with the specified type is added to this client.</exception>
public VirtualParadiseClientExtension GetExtension(Type type)
{
ArgumentNullException.ThrowIfNull(type);
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
VirtualParadiseClientExtension? result = _extensions.Find(e => e.GetType() == type);
if (result is null)
@ -126,7 +132,10 @@ public sealed partial class VirtualParadiseClient
/// <exception cref="InvalidOperationException">No extension with the specified type is added to this client.</exception>
public bool TryGetExtension(Type type, [NotNullWhen(true)] out VirtualParadiseClientExtension? extension)
{
ArgumentNullException.ThrowIfNull(type);
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
extension = _extensions.Find(e => e.GetType() == type);
return extension is not null;

View File

@ -160,7 +160,10 @@ public sealed partial class VirtualParadiseClient
private VirtualParadiseObject AddOrUpdateObject(VirtualParadiseObject obj)
{
ArgumentNullException.ThrowIfNull(obj);
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
return _objects.AddOrUpdate(obj.Id, obj, (_, existing) =>
{

View File

@ -46,7 +46,11 @@ public sealed partial class VirtualParadiseClient : IDisposable
/// <exception cref="ArgumentNullException"><paramref name="configuration" /> is <see langword="null" />.</exception>
public VirtualParadiseClient(VirtualParadiseConfiguration configuration)
{
ArgumentNullException.ThrowIfNull(configuration);
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
Services = configuration.Services;
_configuration = new VirtualParadiseConfiguration(configuration);
Initialize();
@ -298,7 +302,11 @@ public sealed partial class VirtualParadiseClient : IDisposable
/// <exception cref="TimeoutException">Connection to the world server timed out.</exception>
public async Task EnterAsync(VirtualParadiseWorld world)
{
ArgumentNullException.ThrowIfNull(world);
if (world is null)
{
throw new ArgumentNullException(nameof(world));
}
await EnterAsync(world.Name).ConfigureAwait(false);
}
@ -317,7 +325,10 @@ public sealed partial class VirtualParadiseClient : IDisposable
/// <exception cref="TimeoutException">Connection to the world server timed out.</exception>
public async Task<VirtualParadiseWorld> EnterAsync(string worldName)
{
ArgumentNullException.ThrowIfNull(worldName);
if (worldName is null)
{
throw new ArgumentNullException(nameof(worldName));
}
if (CurrentWorld is not null)
{
@ -555,7 +566,11 @@ public sealed partial class VirtualParadiseClient : IDisposable
/// </exception>
public Task<VirtualParadiseMessage> SendMessageAsync(string message)
{
ArgumentNullException.ThrowIfNull(message);
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}
if (string.IsNullOrWhiteSpace(message))
{
throw new ArgumentException(ExceptionMessages.ValueCannotBeEmpty, nameof(message));
@ -628,7 +643,11 @@ public sealed partial class VirtualParadiseClient : IDisposable
/// </exception>
public Task<VirtualParadiseMessage> SendMessageAsync(string? name, string message, FontStyle fontStyle, Color color)
{
ArgumentNullException.ThrowIfNull(message);
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}
if (string.IsNullOrWhiteSpace(message))
{
throw new ArgumentException(ExceptionMessages.ValueCannotBeEmpty, nameof(message));