From 266765d9895a22860c03149589f3c48981818153 Mon Sep 17 00:00:00 2001 From: Dave Roman <43916038+MrDave1999@users.noreply.github.com> Date: Thu, 27 Jun 2024 20:40:51 -0500 Subject: [PATCH] refactor: Create private method called StartFileLoading (#180) * Remove the private method "CheckIfEnvFileNotExistsAndIsNotOptional" * Remove the private method "SetConfigurationEnvFile" --- src/Loader/EnvLoader.HelperMethods.cs | 84 +++++++++++---------------- src/Loader/EnvLoader.cs | 12 +--- 2 files changed, 37 insertions(+), 59 deletions(-) diff --git a/src/Loader/EnvLoader.HelperMethods.cs b/src/Loader/EnvLoader.HelperMethods.cs index d7342fe..0a4e322 100644 --- a/src/Loader/EnvLoader.HelperMethods.cs +++ b/src/Loader/EnvLoader.HelperMethods.cs @@ -9,50 +9,20 @@ namespace DotEnv.Core; // This class defines the helper private methods. public partial class EnvLoader { - /// - /// Checks if the .env file does not exist and is not optional. - /// - /// The instance representing the .env file. - /// envFile is null. - private void CheckIfEnvFileNotExistsAndIsNotOptional(EnvFile envFile) + private void StartFileLoading(EnvFile envFile) { - ThrowHelper.ThrowIfNull(envFile, nameof(envFile)); - if (!envFile.Exists && !envFile.Optional) - _validationResult.Add(errorMsg: string.Format(FileNotFoundMessage, envFile.Path)); - } - - /// - /// Throws an exception if there are errors. - /// - /// - /// - private void ThrowExceptionIfErrorsExist() - { - _parser.ThrowParserExceptionIfErrorsExist(); - if (_validationResult.HasError()) - { - if (_configuration.ThrowFileNotFoundException) - throw new FileNotFoundException(message: _validationResult.ErrorMessages); + if (!Path.HasExtension(envFile.Path)) + envFile.Path = Path.Combine(envFile.Path, _configuration.DefaultEnvFileName); - CombineValidationResults(); - } - } + envFile.Encoding ??= _configuration.Encoding; + envFile.Optional = envFile.Optional ? envFile.Optional : _configuration.Optional; + envFile.Path = Path.Combine(_configuration.BasePath, envFile.Path); + envFile.Exists = ReadAndParse(envFile); - /// - /// Combines the validation result of the loader with the parser. - /// - private void CombineValidationResults() - { - if (_parser.ValidationResult.HasError()) - _parser.ValidationResult.AddRange(errorMessages: _validationResult); + if (!envFile.Exists && !envFile.Optional) + _validationResult.Add(errorMsg: string.Format(FileNotFoundMessage, envFile.Path)); } - /// - /// Gets an instance of validation result. - /// - private EnvValidationResult GetInstanceOfValidationResult() - => _parser.ValidationResult.HasError() ? _parser.ValidationResult : _validationResult; - /// /// Reads the contents of a .env file and invokes the parser. /// @@ -116,21 +86,37 @@ private Result GetEnvFilePath(string envFileName) } /// - /// Sets the configuration of a .env file. + /// Throws an exception if there are errors. /// - /// The instance representing the .env file. - /// envFile is null. - private void SetConfigurationEnvFile(EnvFile envFile) + /// + /// + private void ThrowExceptionIfErrorsExist() { - ThrowHelper.ThrowIfNull(envFile, nameof(envFile)); - if (!Path.HasExtension(envFile.Path)) - envFile.Path = Path.Combine(envFile.Path, _configuration.DefaultEnvFileName); + _parser.ThrowParserExceptionIfErrorsExist(); + if (_validationResult.HasError()) + { + if (_configuration.ThrowFileNotFoundException) + throw new FileNotFoundException(message: _validationResult.ErrorMessages); - envFile.Encoding ??= _configuration.Encoding; - envFile.Optional = envFile.Optional ? envFile.Optional : _configuration.Optional; - envFile.Path = Path.Combine(_configuration.BasePath, envFile.Path); + CombineValidationResults(); + } + } + + /// + /// Combines the validation result of the loader with the parser. + /// + private void CombineValidationResults() + { + if (_parser.ValidationResult.HasError()) + _parser.ValidationResult.AddRange(errorMessages: _validationResult); } + /// + /// Gets an instance of validation result. + /// + private EnvValidationResult GetInstanceOfValidationResult() + => _parser.ValidationResult.HasError() ? _parser.ValidationResult : _validationResult; + /// /// Adds optional .env files to a collection. /// diff --git a/src/Loader/EnvLoader.cs b/src/Loader/EnvLoader.cs index ac346e1..c864ccc 100644 --- a/src/Loader/EnvLoader.cs +++ b/src/Loader/EnvLoader.cs @@ -44,11 +44,7 @@ public IEnvironmentVariablesProvider Load(out EnvValidationResult result) } foreach (EnvFile envFile in _configuration.EnvFiles) - { - SetConfigurationEnvFile(envFile); - envFile.Exists = ReadAndParse(envFile); - CheckIfEnvFileNotExistsAndIsNotOptional(envFile); - } + StartFileLoading(envFile); ThrowExceptionIfErrorsExist(); result = GetInstanceOfValidationResult(); @@ -77,11 +73,7 @@ public IEnvironmentVariablesProvider LoadEnv(out EnvValidationResult result) envFiles.AddRange(copyEnvFiles); foreach (EnvFile envFile in _configuration.EnvFiles) - { - SetConfigurationEnvFile(envFile); - envFile.Exists = ReadAndParse(envFile); - CheckIfEnvFileNotExistsAndIsNotOptional(envFile); - } + StartFileLoading(envFile); if (environment is null) {