From 96d5129663608e5faec3ff03725422a817807fe9 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 16 Jun 2024 14:43:05 -0500 Subject: [PATCH 1/3] Create ThrowHelper class --- src/Common/ThrowHelper.cs | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/Common/ThrowHelper.cs diff --git a/src/Common/ThrowHelper.cs b/src/Common/ThrowHelper.cs new file mode 100644 index 0000000..9cc7962 --- /dev/null +++ b/src/Common/ThrowHelper.cs @@ -0,0 +1,57 @@ +using static DotEnv.Core.ExceptionMessages; + +namespace DotEnv.Core; + +/// +/// Helper methods to efficiently throw exceptions. +/// +internal class ThrowHelper +{ + /// + /// Throws an if argument is null. + /// + /// + /// The reference type argument to validate as non-null. + /// + /// + /// The name of the parameter with which argument corresponds. + /// + /// + public static void ThrowIfNull(object argument, string paramName) + { + if(argument is null) + throw new ArgumentNullException(paramName); + } + + /// + /// Throws an exception if argument is null, empty, or consists only of white-space characters. + /// + /// + /// The string argument to validate. + /// + /// + /// The name of the parameter with which argument corresponds. + /// + /// + public static void ThrowIfNullOrWhiteSpace(string argument, string paramName) + { + if (string.IsNullOrWhiteSpace(argument)) + throw new ArgumentException(ArgumentIsNullOrWhiteSpaceMessage, paramName); + } + + /// + /// Throws an exception if the argument is an empty collection. + /// + /// + /// The collection argument to validate. + /// + /// + /// The name of the parameter with which argument corresponds. + /// + /// + public static void ThrowIfEmptyCollection(IEnumerable argument, string paramName) + { + if (argument.IsEmpty()) + throw new ArgumentException(LengthOfParamsListIsZeroMessage, paramName); + } +} From 01e1f6e63b6434e72cb16cc0b9b6fb7f3f747ea5 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 16 Jun 2024 14:47:05 -0500 Subject: [PATCH 2/3] refactor: Use guard clauses to reduce code size --- .../src/ServiceCollectionExtensions.cs | 12 +++++--- src/Common/Env.cs | 2 +- src/Loader/EnvLoader.ConfigurationMethods.cs | 24 +++++++-------- src/Loader/EnvLoader.HelperMethods.cs | 10 +++---- src/Parser/EnvParser.HelperMethods.cs | 30 +++++++++---------- src/Parser/EnvParser.cs | 2 +- src/Providers/DefaultEnvironmentProvider.cs | 2 +- src/Providers/DictionaryProvider.cs | 2 +- src/Reader/EnvReader.Try.cs | 28 ++++++++--------- src/Reader/EnvReader.cs | 2 +- src/Validator/EnvValidator.cs | 8 ++--- 11 files changed, 60 insertions(+), 62 deletions(-) diff --git a/plugins/Microsoft.Extensions.DI/src/ServiceCollectionExtensions.cs b/plugins/Microsoft.Extensions.DI/src/ServiceCollectionExtensions.cs index d038567..b3f728e 100644 --- a/plugins/Microsoft.Extensions.DI/src/ServiceCollectionExtensions.cs +++ b/plugins/Microsoft.Extensions.DI/src/ServiceCollectionExtensions.cs @@ -28,7 +28,9 @@ public static IEnvReader AddDotEnv(this IServiceCollection services) /// An instance that allows access to the environment variables. public static IEnvReader AddDotEnv(this IServiceCollection services, params string[] paths) { - _ = services ?? throw new ArgumentNullException(nameof(services)); + ThrowHelper.ThrowIfNull(services, nameof(services)); + ThrowHelper.ThrowIfNull(paths, nameof(paths)); + ThrowHelper.ThrowIfEmptyCollection(paths, nameof(paths)); var envVars = Load(paths); return services.AddEnvReader(envVars); } @@ -57,7 +59,9 @@ public static IEnvReader AddDotEnv(this IServiceCollection services, params stri public static TSettings AddDotEnv(this IServiceCollection services, params string[] paths) where TSettings : class, new() { - _ = services ?? throw new ArgumentNullException(nameof(services)); + ThrowHelper.ThrowIfNull(services, nameof(services)); + ThrowHelper.ThrowIfNull(paths, nameof(paths)); + ThrowHelper.ThrowIfEmptyCollection(paths, nameof(paths)); var envVars = Load(paths); return services.AddTSettings(envVars); } @@ -73,7 +77,7 @@ public static TSettings AddDotEnv(this IServiceCollection services, p /// An instance that allows access to the environment variables. public static IEnvReader AddCustomEnv(this IServiceCollection services, string basePath = null, string environmentName = null) { - _ = services ?? throw new ArgumentNullException(nameof(services)); + ThrowHelper.ThrowIfNull(services, nameof(services)); var envVars = LoadEnv(basePath, environmentName); return services.AddEnvReader(envVars); } @@ -91,7 +95,7 @@ public static IEnvReader AddCustomEnv(this IServiceCollection services, string b public static TSettings AddCustomEnv(this IServiceCollection services, string basePath = null, string environmentName = null) where TSettings : class, new() { - _ = services ?? throw new ArgumentNullException(nameof(services)); + ThrowHelper.ThrowIfNull(services, nameof(services)); var envVars = LoadEnv(basePath, environmentName); return services.AddTSettings(envVars); } diff --git a/src/Common/Env.cs b/src/Common/Env.cs index 4dc71da..2dc491a 100644 --- a/src/Common/Env.cs +++ b/src/Common/Env.cs @@ -56,7 +56,7 @@ public static bool IsProduction() /// environmentName is null. public static bool IsEnvironment(string environmentName) { - _ = environmentName ?? throw new ArgumentNullException(nameof(environmentName)); + ThrowHelper.ThrowIfNull(environmentName, nameof(environmentName)); return string.Equals(CurrentEnvironment, environmentName, StringComparison.OrdinalIgnoreCase); } } diff --git a/src/Loader/EnvLoader.ConfigurationMethods.cs b/src/Loader/EnvLoader.ConfigurationMethods.cs index 5173abf..604c9bb 100644 --- a/src/Loader/EnvLoader.ConfigurationMethods.cs +++ b/src/Loader/EnvLoader.ConfigurationMethods.cs @@ -12,7 +12,7 @@ public partial class EnvLoader /// public IEnvLoader SetDefaultEnvFileName(string envFileName) { - _ = envFileName ?? throw new ArgumentNullException(nameof(envFileName)); + ThrowHelper.ThrowIfNull(envFileName, nameof(envFileName)); _configuration.DefaultEnvFileName = envFileName; return this; } @@ -20,7 +20,7 @@ public IEnvLoader SetDefaultEnvFileName(string envFileName) /// public IEnvLoader SetBasePath(string basePath) { - _ = basePath ?? throw new ArgumentNullException(nameof(basePath)); + ThrowHelper.ThrowIfNull(basePath, nameof(basePath)); _configuration.BasePath = basePath; return this; } @@ -28,10 +28,8 @@ public IEnvLoader SetBasePath(string basePath) /// public IEnvLoader AddEnvFiles(params string[] paths) { - _ = paths ?? throw new ArgumentNullException(nameof(paths)); - if (paths.IsEmpty()) - throw new ArgumentException(LengthOfParamsListIsZeroMessage, nameof(paths)); - + ThrowHelper.ThrowIfNull(paths, nameof(paths)); + ThrowHelper.ThrowIfEmptyCollection(paths, nameof(paths)); foreach (string path in paths) AddEnvFile(path); return this; @@ -48,7 +46,7 @@ public IEnvLoader AddEnvFile(string path, Encoding encoding) /// public IEnvLoader AddEnvFile(string path, Encoding encoding, bool optional) { - _ = path ?? throw new ArgumentNullException(nameof(path)); + ThrowHelper.ThrowIfNull(path, nameof(path)); _configuration.EnvFiles.Add(new EnvFile { Path = path, Encoding = encoding, Optional = optional}); return this; } @@ -60,8 +58,8 @@ public IEnvLoader AddEnvFile(string path, string encodingName) /// public IEnvLoader AddEnvFile(string path, string encodingName, bool optional) { - _ = path ?? throw new ArgumentNullException(nameof(path)); - _ = encodingName ?? throw new ArgumentNullException(nameof(encodingName)); + ThrowHelper.ThrowIfNull(path, nameof(path)); + ThrowHelper.ThrowIfNull(encodingName, nameof(encodingName)); try { AddEnvFile(path, Encoding.GetEncoding(encodingName), optional); @@ -80,7 +78,7 @@ public IEnvLoader AddEnvFile(string path, bool optional) /// public IEnvLoader SetEncoding(Encoding encoding) { - _ = encoding ?? throw new ArgumentNullException(nameof(encoding)); + ThrowHelper.ThrowIfNull(encoding, nameof(encoding)); _configuration.Encoding = encoding; return this; } @@ -109,10 +107,8 @@ public IEnvLoader EnableFileNotFoundException() /// public IEnvLoader SetEnvironmentName(string envName) { - _ = envName ?? throw new ArgumentNullException(nameof(envName)); - if (string.IsNullOrWhiteSpace(envName)) - throw new ArgumentException(ArgumentIsNullOrWhiteSpaceMessage, nameof(envName)); - + ThrowHelper.ThrowIfNull(envName, nameof(envName)); + ThrowHelper.ThrowIfNullOrWhiteSpace(envName, nameof(envName)); _configuration.EnvironmentName = envName; return this; } diff --git a/src/Loader/EnvLoader.HelperMethods.cs b/src/Loader/EnvLoader.HelperMethods.cs index da159df..d7342fe 100644 --- a/src/Loader/EnvLoader.HelperMethods.cs +++ b/src/Loader/EnvLoader.HelperMethods.cs @@ -16,7 +16,7 @@ public partial class EnvLoader /// envFile is null. private void CheckIfEnvFileNotExistsAndIsNotOptional(EnvFile envFile) { - _ = envFile ?? throw new ArgumentNullException(nameof(envFile)); + ThrowHelper.ThrowIfNull(envFile, nameof(envFile)); if (!envFile.Exists && !envFile.Optional) _validationResult.Add(errorMsg: string.Format(FileNotFoundMessage, envFile.Path)); } @@ -61,7 +61,7 @@ private EnvValidationResult GetInstanceOfValidationResult() /// true if the .env file exists, otherwise false. private bool ReadAndParse(EnvFile envFile) { - _ = envFile ?? throw new ArgumentNullException(nameof(envFile)); + ThrowHelper.ThrowIfNull(envFile, nameof(envFile)); Result result; if (_configuration.SearchParentDirectories) result = GetEnvFilePath(envFile.Path); @@ -94,7 +94,7 @@ private bool ReadAndParse(EnvFile envFile) /// private Result GetEnvFilePath(string envFileName) { - _ = envFileName ?? throw new ArgumentNullException(nameof(envFileName)); + ThrowHelper.ThrowIfNull(envFileName, nameof(envFileName)); string path; if (Path.IsPathRooted(envFileName)) { @@ -122,7 +122,7 @@ private Result GetEnvFilePath(string envFileName) /// envFile is null. private void SetConfigurationEnvFile(EnvFile envFile) { - _ = envFile ?? throw new ArgumentNullException(nameof(envFile)); + ThrowHelper.ThrowIfNull(envFile, nameof(envFile)); if (!Path.HasExtension(envFile.Path)) envFile.Path = Path.Combine(envFile.Path, _configuration.DefaultEnvFileName); @@ -138,7 +138,7 @@ private void SetConfigurationEnvFile(EnvFile envFile) /// envFilesNames is null. private void AddOptionalEnvFiles(params string[] envFilesNames) { - _ = envFilesNames ?? throw new ArgumentNullException(nameof(envFilesNames)); + ThrowHelper.ThrowIfNull(envFilesNames, nameof(envFilesNames)); foreach (string envFileName in envFilesNames) _configuration.EnvFiles.Add(new EnvFile { Path = envFileName, Optional = true }); } diff --git a/src/Parser/EnvParser.HelperMethods.cs b/src/Parser/EnvParser.HelperMethods.cs index 8a4b609..e0857bd 100644 --- a/src/Parser/EnvParser.HelperMethods.cs +++ b/src/Parser/EnvParser.HelperMethods.cs @@ -28,7 +28,7 @@ internal void ThrowParserExceptionIfErrorsExist() /// true if the line is a comment, otherwise false. private bool IsComment(string line) { - _ = line ?? throw new ArgumentNullException(nameof(line)); + ThrowHelper.ThrowIfNull(line, nameof(line)); line = _configuration.TrimStartComments ? line.TrimStart() : line; return line[0] == _configuration.CommentChar; } @@ -42,7 +42,7 @@ private bool IsComment(string line) /// A string without the inline comment. private string RemoveInlineComment(string line, out string comment) { - _ = line ?? throw new ArgumentNullException(nameof(line)); + ThrowHelper.ThrowIfNull(line, nameof(line)); var substrings = line.Split(_configuration.InlineCommentChars, MaxCount, StringSplitOptions.None); comment = substrings.Length == 1 ? null : substrings[1]; return substrings[0]; @@ -68,7 +68,7 @@ private string ConcatCommentWithValue(string value, string comment) /// private string TrimKey(string key) { - _ = key ?? throw new ArgumentNullException(nameof(key)); + ThrowHelper.ThrowIfNull(key, nameof(key)); key = _configuration.TrimStartKeys ? key.TrimStart() : key; key = _configuration.TrimEndKeys ? key.TrimEnd() : key; return key; @@ -85,7 +85,7 @@ private string TrimKey(string key) /// private string TrimValue(string value) { - _ = value ?? throw new ArgumentNullException(nameof(value)); + ThrowHelper.ThrowIfNull(value, nameof(value)); value = _configuration.TrimStartValues ? value.TrimStart() : value; value = _configuration.TrimEndValues ? value.TrimEnd() : value; return value; @@ -99,7 +99,7 @@ private string TrimValue(string value) /// The key extracted. private string ExtractKey(string line) { - _ = line ?? throw new ArgumentNullException(nameof(line)); + ThrowHelper.ThrowIfNull(line, nameof(line)); string key = line.Split(_configuration.DelimiterKeyValuePair, MaxCount)[0]; return key; } @@ -112,7 +112,7 @@ private string ExtractKey(string line) /// The value extracted. private string ExtractValue(string line) { - _ = line ?? throw new ArgumentNullException(nameof(line)); + ThrowHelper.ThrowIfNull(line, nameof(line)); string value = line.Split(_configuration.DelimiterKeyValuePair, MaxCount)[1]; return value; } @@ -125,7 +125,7 @@ private string ExtractValue(string line) /// true if the line has no the key-value pair, otherwise false. private bool HasNoKeyValuePair(string line) { - _ = line ?? throw new ArgumentNullException(nameof(line)); + ThrowHelper.ThrowIfNull(line, nameof(line)); var keyValuePair = line.Split(_configuration.DelimiterKeyValuePair, MaxCount); return keyValuePair.Length != 2 || string.IsNullOrWhiteSpace(keyValuePair[0]); } @@ -148,7 +148,7 @@ private string ConcatValues(string currentValue, string value) /// A string with each environment variable replaced by its value. private string ExpandEnvironmentVariables(string name, int currentLine) { - _ = name ?? throw new ArgumentNullException(nameof(name)); + ThrowHelper.ThrowIfNull(name, nameof(name)); var pattern = @"\$\{([^}]*)\}"; name = Regex.Replace(name, pattern, match => { @@ -197,7 +197,7 @@ private string ExpandEnvironmentVariables(string name, int currentLine) /// true if the text is quoted, or false. private bool IsQuoted(string text) { - _ = text ?? throw new ArgumentNullException(nameof(text)); + ThrowHelper.ThrowIfNull(text, nameof(text)); text = text.Trim(); if(text.Length <= 1) return false; @@ -213,7 +213,7 @@ private bool IsQuoted(string text) /// A string without single or double quotes. private string RemoveQuotes(string text) { - _ = text ?? throw new ArgumentNullException(nameof(text)); + ThrowHelper.ThrowIfNull(text, nameof(text)); return text.Trim().Trim([SingleQuote, DoubleQuote]); } @@ -226,8 +226,8 @@ private string RemoveQuotes(string text) /// A key without the prefix. private string RemovePrefixBeforeKey(string key, string prefix) { - _ = key ?? throw new ArgumentNullException(nameof(key)); - _ = prefix ?? throw new ArgumentNullException(nameof(prefix)); + ThrowHelper.ThrowIfNull(key, nameof(key)); + ThrowHelper.ThrowIfNull(prefix, nameof(prefix)); var aux = key; key = key.TrimStart(); return key.IndexOf(prefix) == 0 ? key.Remove(0, prefix.Length) : aux; @@ -243,7 +243,7 @@ private string RemovePrefixBeforeKey(string key, string prefix) /// private bool IsMultiline(string value) { - _ = value ?? throw new ArgumentNullException(nameof(value)); + ThrowHelper.ThrowIfNull(value, nameof(value)); value = value.Trim(); if(value.Length == 0) return false; @@ -268,8 +268,8 @@ private bool IsMultiline(string value) /// private Result GetValuesMultilines(string[] lines, ref int index, string value) { - _ = lines ?? throw new ArgumentNullException(nameof(lines)); - _ = value ?? throw new ArgumentNullException(nameof(value)); + ThrowHelper.ThrowIfNull(lines, nameof(lines)); + ThrowHelper.ThrowIfNull(value, nameof(value)); value = value.TrimStart(); // Double or single-quoted. char quoteChar = value[0]; diff --git a/src/Parser/EnvParser.cs b/src/Parser/EnvParser.cs index 989115f..c1691c4 100644 --- a/src/Parser/EnvParser.cs +++ b/src/Parser/EnvParser.cs @@ -49,7 +49,7 @@ public IEnvironmentVariablesProvider Parse(string dataSource) /// public IEnvironmentVariablesProvider Parse(string dataSource, out EnvValidationResult result) { - _ = dataSource ?? throw new ArgumentNullException(nameof(dataSource)); + ThrowHelper.ThrowIfNull(dataSource, nameof(dataSource)); result = ValidationResult; ParseStart(dataSource); ThrowParserExceptionIfErrorsExist(); diff --git a/src/Providers/DefaultEnvironmentProvider.cs b/src/Providers/DefaultEnvironmentProvider.cs index ca96040..28f389a 100644 --- a/src/Providers/DefaultEnvironmentProvider.cs +++ b/src/Providers/DefaultEnvironmentProvider.cs @@ -16,7 +16,7 @@ public string this[string variable] get => Environment.GetEnvironmentVariable(variable); set { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); Environment.SetEnvironmentVariable(variable, value); } } diff --git a/src/Providers/DictionaryProvider.cs b/src/Providers/DictionaryProvider.cs index 2e80979..4f7778a 100644 --- a/src/Providers/DictionaryProvider.cs +++ b/src/Providers/DictionaryProvider.cs @@ -22,7 +22,7 @@ public string this[string variable] } set { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); _keyValuePairs[variable] = value; } } diff --git a/src/Reader/EnvReader.Try.cs b/src/Reader/EnvReader.Try.cs index 030c03f..76f666a 100644 --- a/src/Reader/EnvReader.Try.cs +++ b/src/Reader/EnvReader.Try.cs @@ -12,7 +12,7 @@ public partial class EnvReader /// public virtual bool TryGetStringValue(string variable, out string value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if(retrievedValue is null) { @@ -26,7 +26,7 @@ public virtual bool TryGetStringValue(string variable, out string value) /// public virtual bool TryGetBoolValue(string variable, out bool value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if (retrievedValue is null) { @@ -40,7 +40,7 @@ public virtual bool TryGetBoolValue(string variable, out bool value) /// public virtual bool TryGetByteValue(string variable, out byte value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if (retrievedValue is null) { @@ -54,7 +54,7 @@ public virtual bool TryGetByteValue(string variable, out byte value) /// public virtual bool TryGetSByteValue(string variable, out sbyte value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if (retrievedValue is null) { @@ -68,7 +68,7 @@ public virtual bool TryGetSByteValue(string variable, out sbyte value) /// public virtual bool TryGetCharValue(string variable, out char value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if (retrievedValue is null) { @@ -82,7 +82,7 @@ public virtual bool TryGetCharValue(string variable, out char value) /// public virtual bool TryGetIntValue(string variable, out int value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if (retrievedValue is null) { @@ -96,7 +96,7 @@ public virtual bool TryGetIntValue(string variable, out int value) /// public virtual bool TryGetUIntValue(string variable, out uint value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if (retrievedValue is null) { @@ -110,7 +110,7 @@ public virtual bool TryGetUIntValue(string variable, out uint value) /// public virtual bool TryGetLongValue(string variable, out long value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if (retrievedValue is null) { @@ -124,7 +124,7 @@ public virtual bool TryGetLongValue(string variable, out long value) /// public virtual bool TryGetULongValue(string variable, out ulong value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if (retrievedValue is null) { @@ -138,7 +138,7 @@ public virtual bool TryGetULongValue(string variable, out ulong value) /// public virtual bool TryGetShortValue(string variable, out short value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if (retrievedValue is null) { @@ -152,7 +152,7 @@ public virtual bool TryGetShortValue(string variable, out short value) /// public virtual bool TryGetUShortValue(string variable, out ushort value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if (retrievedValue is null) { @@ -166,7 +166,7 @@ public virtual bool TryGetUShortValue(string variable, out ushort value) /// public virtual bool TryGetDecimalValue(string variable, out decimal value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if (retrievedValue is null) { @@ -180,7 +180,7 @@ public virtual bool TryGetDecimalValue(string variable, out decimal value) /// public virtual bool TryGetDoubleValue(string variable, out double value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if (retrievedValue is null) { @@ -194,7 +194,7 @@ public virtual bool TryGetDoubleValue(string variable, out double value) /// public virtual bool TryGetFloatValue(string variable, out float value) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; if (retrievedValue is null) { diff --git a/src/Reader/EnvReader.cs b/src/Reader/EnvReader.cs index df14df4..66a1672 100644 --- a/src/Reader/EnvReader.cs +++ b/src/Reader/EnvReader.cs @@ -37,7 +37,7 @@ public EnvReader(IEnvironmentVariablesProvider provider) /// public virtual bool HasValue(string variable) { - _ = variable ?? throw new ArgumentNullException(nameof(variable)); + ThrowHelper.ThrowIfNull(variable, nameof(variable)); var retrievedValue = _envVars[variable]; return retrievedValue is not null; } diff --git a/src/Validator/EnvValidator.cs b/src/Validator/EnvValidator.cs index 579c0b3..3d78127 100644 --- a/src/Validator/EnvValidator.cs +++ b/src/Validator/EnvValidator.cs @@ -62,10 +62,8 @@ public void Validate(out EnvValidationResult result) /// public IEnvValidator SetRequiredKeys(params string[] keys) { - _ = keys ?? throw new ArgumentNullException(nameof(keys)); - if (keys.IsEmpty()) - throw new ArgumentException(LengthOfParamsListIsZeroMessage, nameof(keys)); - + ThrowHelper.ThrowIfNull(keys, nameof(keys)); + ThrowHelper.ThrowIfEmptyCollection(keys, nameof(keys)); _configuration.RequiredKeys = keys; return this; } @@ -87,7 +85,7 @@ public IEnvValidator SetRequiredKeys() /// public IEnvValidator SetRequiredKeys(Type keysType) { - _ = keysType ?? throw new ArgumentNullException(nameof(keysType)); + ThrowHelper.ThrowIfNull(keysType, nameof(keysType)); var readablePropertyNames = from propertyInfo in keysType.GetProperties() where propertyInfo.CanRead && propertyInfo.PropertyType == typeof(string) From b39c5e69291223a71080155bb452e7da67941eba Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 16 Jun 2024 14:47:27 -0500 Subject: [PATCH 3/3] chore: Update project file --- src/DotEnv.Core.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DotEnv.Core.csproj b/src/DotEnv.Core.csproj index 1849904..15bd170 100644 --- a/src/DotEnv.Core.csproj +++ b/src/DotEnv.Core.csproj @@ -31,6 +31,7 @@ +