Skip to content

Commit

Permalink
refactor null checks using ArgumentException and ArgumentNullException
Browse files Browse the repository at this point in the history
Simplify null and whitespace checks by using ArgumentException.ThrowIfNullOrWhiteSpace and ArgumentNullException.ThrowIfNull. This reduces repetitive code and enhances readability across various methods.
  • Loading branch information
arturcic committed Nov 19, 2024
1 parent e8a2fbe commit f4ca541
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 50 deletions.
3 changes: 2 additions & 1 deletion src/GitVersion.Core.Tests/Helpers/TestConsole.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GitVersion.Extensions;
using GitVersion.Helpers;
using GitVersion.Logging;

Expand All @@ -12,7 +13,7 @@ public class TestConsole(params string[] responses) : IConsole

public void WriteLine() => this.log.Info(PathHelper.NewLine);

public void Write(string? msg) => this.log.Info(msg ?? throw new ArgumentNullException(nameof(msg)));
public void Write(string? msg) => this.log.Info(msg.NotNull());

public string ReadLine() => this.responses.Dequeue();

Expand Down
6 changes: 2 additions & 4 deletions src/GitVersion.Core/Core/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ public void WriteAllText(string? file, string fileContents)

public void WriteAllText(string? file, string fileContents, Encoding encoding)
{
if (string.IsNullOrEmpty(file))
throw new ArgumentNullException(nameof(file));
ArgumentException.ThrowIfNullOrWhiteSpace(file);

File.WriteAllText(file, fileContents, encoding);
}

public IEnumerable<string> DirectoryEnumerateFiles(string? directory, string searchPattern, SearchOption searchOption)
{
if (string.IsNullOrEmpty(directory))
throw new ArgumentNullException(nameof(directory));
ArgumentException.ThrowIfNullOrWhiteSpace(directory);

return Directory.EnumerateFiles(directory, searchPattern, searchOption);
}
Expand Down
14 changes: 5 additions & 9 deletions src/GitVersion.Core/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ public static class EnumerableExtensions
{
public static T? OnlyOrDefault<T>(this IEnumerable<T> source)
{
switch (source)
ArgumentNullException.ThrowIfNull(source);

if (source is IList<T> { Count: 1 } list)
{
case null:
throw new ArgumentNullException(nameof(source));
case IList<T> { Count: 1 } list:
return list[0];
return list[0];
}

using var e = source.GetEnumerator();
Expand All @@ -21,10 +20,7 @@ public static class EnumerableExtensions

public static T SingleOfType<T>(this IEnumerable source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);

return source.OfType<T>().Single();
}
Expand Down
15 changes: 9 additions & 6 deletions src/GitVersion.Core/Helpers/PathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public static string GetFullPath(string? path)

public static string Combine(string? path1, string? path2)
{
if (path1 == null || path2 == null)
throw new ArgumentNullException((path1 == null) ? nameof(path1) : nameof(path2));
ArgumentException.ThrowIfNullOrWhiteSpace(path1);
ArgumentException.ThrowIfNullOrWhiteSpace(path2);

return Path.Combine(path1, path2);
}
Expand All @@ -57,16 +57,19 @@ public static string Combine(string? path1)

public static string Combine(string? path1, string? path2, string? path3)
{
if (path1 == null || path2 == null || path3 == null)
throw new ArgumentNullException((path1 == null) ? nameof(path1) : (path2 == null) ? nameof(path2) : nameof(path3));
ArgumentException.ThrowIfNullOrWhiteSpace(path1);
ArgumentException.ThrowIfNullOrWhiteSpace(path2);
ArgumentException.ThrowIfNullOrWhiteSpace(path3);

return Path.Combine(path1, path2, path3);
}

public static string Combine(string? path1, string? path2, string? path3, string? path4)
{
if (path1 == null || path2 == null || path3 == null || path4 == null)
throw new ArgumentNullException((path1 == null) ? nameof(path1) : (path2 == null) ? nameof(path2) : (path3 == null) ? nameof(path3) : nameof(path4));
ArgumentException.ThrowIfNullOrWhiteSpace(path1);
ArgumentException.ThrowIfNullOrWhiteSpace(path2);
ArgumentException.ThrowIfNullOrWhiteSpace(path3);
ArgumentException.ThrowIfNullOrWhiteSpace(path4);

return Path.Combine(path1, path2, path3, path4);
}
Expand Down
11 changes: 2 additions & 9 deletions src/GitVersion.Core/Helpers/StringFormatWith.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,8 @@ internal static class StringFormatWithExtension
/// </example>
public static string FormatWith<T>(this string template, T? source, IEnvironment environment)
{
if (template is null)
{
throw new ArgumentNullException(nameof(template));
}

if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(template);
ArgumentNullException.ThrowIfNull(source);

foreach (Match match in RegexPatterns.Common.ExpandTokensRegex.Matches(template).Cast<Match>())
{
Expand Down
5 changes: 1 addition & 4 deletions src/GitVersion.Core/Logging/LogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ private static void Write(this ILog log, LogLevel level, LogAction? logAction)

private static IDisposable WithVerbosity(this ILog log, Verbosity verbosity)
{
if (log == null)
{
throw new ArgumentNullException(nameof(log));
}
ArgumentNullException.ThrowIfNull(log);
var lastVerbosity = log.Verbosity;
log.Verbosity = verbosity;
return Disposable.Create(() => log.Verbosity = lastVerbosity);
Expand Down
15 changes: 3 additions & 12 deletions src/GitVersion.Output/TemplateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ internal class TemplateManager(TemplateType templateType)

public string? GetTemplateFor(string fileExtension)
{
if (fileExtension == null)
{
throw new ArgumentNullException(nameof(fileExtension));
}
ArgumentNullException.ThrowIfNull(fileExtension);

string? result = null;

Expand All @@ -32,10 +29,7 @@ internal class TemplateManager(TemplateType templateType)

public string? GetAddFormatFor(string fileExtension)
{
if (fileExtension == null)
{
throw new ArgumentNullException(nameof(fileExtension));
}
ArgumentNullException.ThrowIfNull(fileExtension);

string? result = null;

Expand All @@ -49,10 +43,7 @@ internal class TemplateManager(TemplateType templateType)

public bool IsSupported(string fileExtension)
{
if (fileExtension == null)
{
throw new ArgumentNullException(nameof(fileExtension));
}
ArgumentNullException.ThrowIfNull(fileExtension);

return this.templates.ContainsKey(fileExtension);
}
Expand Down
3 changes: 2 additions & 1 deletion src/GitVersion.Testing/Fixtures/RepositoryFixtureBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GitVersion.Helpers;
using GitVersion.Testing.Internal;
using LibGit2Sharp;
using Shouldly;

namespace GitVersion.Testing;

Expand All @@ -17,7 +18,7 @@ protected RepositoryFixtureBase(Func<string, Repository> repositoryBuilder)
protected RepositoryFixtureBase(Repository repository)
{
SequenceDiagram = new();
Repository = repository ?? throw new ArgumentNullException(nameof(repository));
Repository = repository.ShouldNotBeNull();
Repository.Config.Set("user.name", "Test");
Repository.Config.Set("user.email", "[email protected]");
}
Expand Down
6 changes: 2 additions & 4 deletions src/GitVersion.Testing/Helpers/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ public static class ProcessHelper
// http://csharptest.net/532/using-processstart-to-capture-console-output/
public static int Run(Action<string> output, Action<string> errorOutput, TextReader? input, string exe, string args, string workingDirectory, params KeyValuePair<string, string?>[] environmentalVariables)
{
if (string.IsNullOrEmpty(exe))
throw new ArgumentNullException(nameof(exe));
if (output == null)
throw new ArgumentNullException(nameof(output));
ArgumentException.ThrowIfNullOrWhiteSpace(exe);
ArgumentNullException.ThrowIfNull(output);

var psi = new ProcessStartInfo
{
Expand Down

0 comments on commit f4ca541

Please sign in to comment.