Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Rewrite parameter null checks using helper methods #9024

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ public abstract class BaseModelAttributeHandler<T> : IModelAttributeHandler wher
private Type _type;
protected BaseModelAttributeHandler(Type type, IModelAttributeHandler handler)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
ArgumentNullException.ThrowIfNull(type);
ArgumentNullException.ThrowIfNull(handler);

if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
_type = type;
_typeInfo = GetTypeInfo(type);
Handler = handler;
Expand All @@ -37,10 +31,7 @@ public object Handle(object obj, HandleModelAttributesContext context)
throw new InvalidOperationException($"Input type {type} is not the supported type {_type}");
}

if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
ArgumentNullException.ThrowIfNull(context);

if (obj == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,48 @@ public class HandleGenericItemsHelper
{
public static bool EnumerateIEnumerable(object currentObj, Func<object, object> handler)
{
if (currentObj == null)
{
throw new ArgumentNullException(nameof(currentObj));
}

if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
ArgumentNullException.ThrowIfNull(currentObj);
ArgumentNullException.ThrowIfNull(handler);

return HandleItems(typeof(IEnumerable<>), typeof(EnumerateIEnumerableItems<>), currentObj, handler);
}

public static bool EnumerateIDictionary(object currentObj, Func<object, object> handler)
{
if (currentObj == null)
{
throw new ArgumentNullException(nameof(currentObj));
}

if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
ArgumentNullException.ThrowIfNull(currentObj);
ArgumentNullException.ThrowIfNull(handler);

return HandleItems(typeof(IDictionary<,>), typeof(EnumerateIDictionaryItems<,>), currentObj, handler);
}

public static bool EnumerateIReadonlyDictionary(object currentObj, Func<object, object> handler)
{
if (currentObj == null)
{
throw new ArgumentNullException(nameof(currentObj));
}

if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
ArgumentNullException.ThrowIfNull(currentObj);
ArgumentNullException.ThrowIfNull(handler);

return HandleItems(typeof(IReadOnlyDictionary<,>), typeof(EnumerateIReadonlyDictionaryItems<,>), currentObj, handler);
}

public static bool HandleIList(object currentObj, Func<object, object> handler)
{
if (currentObj == null)
{
throw new ArgumentNullException(nameof(currentObj));
}

if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
ArgumentNullException.ThrowIfNull(currentObj);
ArgumentNullException.ThrowIfNull(handler);

return HandleItems(typeof(IList<>), typeof(HandleIListItems<>), currentObj, handler);
}

public static bool HandleIDictionary(object currentObj, Func<object, object> handler)
{
if (currentObj == null)
{
throw new ArgumentNullException(nameof(currentObj));
}

if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
ArgumentNullException.ThrowIfNull(currentObj);
ArgumentNullException.ThrowIfNull(handler);

return HandleItems(typeof(IDictionary<,>), typeof(HandleIDictionaryItems<,>), currentObj, handler);
}

private static bool HandleItems(Type genericInterface, Type implHandlerType, object currentObj, Func<object, object> handler)
{
if (currentObj == null)
{
throw new ArgumentNullException(nameof(currentObj));
}
ArgumentNullException.ThrowIfNull(currentObj);

var type = currentObj.GetType();
var genericType = ReflectionHelper.GetGenericType(type, genericInterface);
if (genericType != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,8 @@ public object Handle(object obj, HandleModelAttributesContext context)
return null;
}

if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (context.Host == null)
{
throw new ArgumentNullException(nameof(context.Host));
}
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(context.Host);
filzrev marked this conversation as resolved.
Show resolved Hide resolved

if (context.SkipMarkup)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,8 @@ public object Handle(object obj, HandleModelAttributesContext context)
return null;
}

if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (context.Host == null)
{
throw new ArgumentNullException(nameof(context.Host));
}
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(context.Host);

var type = obj.GetType();
return _cache.GetOrAdd(type, t => new UrlContentHandlerImpl(t, this)).Handle(obj, context);
Expand Down
48 changes: 12 additions & 36 deletions src/Docfx.Build.Common/ModelAttributeHandlers/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,10 @@ where prop.GetIndexParameters().Length == 0

public static object CreateInstance(Type type, Type[] typeArguments, Type[] argumentTypes, object[] arguments)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
if (argumentTypes == null)
{
throw new ArgumentNullException(nameof(argumentTypes));
}
if (arguments == null)
{
throw new ArgumentNullException(nameof(arguments));
}
ArgumentNullException.ThrowIfNull(type);
ArgumentNullException.ThrowIfNull(argumentTypes);
ArgumentNullException.ThrowIfNull(arguments);

var func = _createInstanceCache.GetOrAdd(
Tuple.Create(type, typeArguments ?? Type.EmptyTypes, argumentTypes),
GetCreateInstanceFunc);
Expand Down Expand Up @@ -142,20 +134,14 @@ private static Func<object[], object> GetCreateInstanceFuncCore(ConstructorInfo

public static List<PropertyInfo> GetSettableProperties(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
ArgumentNullException.ThrowIfNull(type);

return _settablePropertiesCache.GetOrAdd(type, _getSettableProperties);
}

public static List<PropertyInfo> GetGettableProperties(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
ArgumentNullException.ThrowIfNull(type);

return _gettablePropertiesCache.GetOrAdd(type, _getGettableProperties);
}
Expand Down Expand Up @@ -254,14 +240,9 @@ private static IEnumerable<PropertyInfo> GetProperties(Type type, BindingFlags b

public static object GetPropertyValue(object instance, PropertyInfo prop)
{
if (instance == null)
{
throw new ArgumentNullException(nameof(instance));
}
if (prop == null)
{
throw new ArgumentNullException(nameof(prop));
}
ArgumentNullException.ThrowIfNull(instance);
ArgumentNullException.ThrowIfNull(prop);

var func = _propertyGetterCache.GetOrAdd(prop, CreateGetPropertyFunc);
return func(instance);
}
Expand Down Expand Up @@ -291,14 +272,9 @@ private static Func<object, object> CreateGetPropertyFunc(PropertyInfo prop)

public static void SetPropertyValue(object instance, PropertyInfo prop, object value)
{
if (instance == null)
{
throw new ArgumentNullException(nameof(instance));
}
if (prop == null)
{
throw new ArgumentNullException(nameof(prop));
}
ArgumentNullException.ThrowIfNull(instance);
ArgumentNullException.ThrowIfNull(prop);

var action = _propertySetterCache.GetOrAdd(prop, CreateSetPropertyFunc);
action(instance, value);
}
Expand Down
5 changes: 1 addition & 4 deletions src/Docfx.Build.Common/Reference/OverwriteDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ public class OverwriteDocumentReader
{
public static FileModel Read(FileAndType file)
{
if (file == null)
{
throw new ArgumentNullException(nameof(file));
}
ArgumentNullException.ThrowIfNull(file);

if (file.Type != DocumentType.Overwrite)
{
Expand Down
5 changes: 1 addition & 4 deletions src/Docfx.Build.ConceptualDocuments/CountWord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ internal static class WordCounter

public static long CountWord(string html)
{
if (html == null)
{
throw new ArgumentNullException(nameof(html));
}
ArgumentNullException.ThrowIfNull(html);

HtmlDocument document = new();

Expand Down
6 changes: 2 additions & 4 deletions src/Docfx.Build.ConceptualDocuments/HtmlDocumentUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ public static class HtmlDocumentUtility
{
public static SeparatedHtmlInfo SeparateHtml(string contentHtml)
{
if (contentHtml == null)
{
throw new ArgumentNullException();
}
ArgumentNullException.ThrowIfNull(contentHtml);

var content = new SeparatedHtmlInfo();

var document = new HtmlDocument();
Expand Down
20 changes: 4 additions & 16 deletions src/Docfx.Build.Engine/CompositionContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,9 @@ public static T GetExport<T>(CompositionHost container, string name) =>

public static object GetExport(CompositionHost container, Type type, string name)
{
if (container == null)
{
throw new ArgumentNullException(nameof(container));
}
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
ArgumentNullException.ThrowIfNull(container);
ArgumentNullException.ThrowIfNull(type);
ArgumentNullException.ThrowIfNull(name);

object exportedObject = null;
try
Expand All @@ -58,10 +49,7 @@ public static object GetExport(CompositionHost container, Type type, string name

public static CompositionHost GetContainer(IEnumerable<Assembly> assemblies)
{
if (assemblies == null)
{
throw new ArgumentNullException(nameof(assemblies));
}
ArgumentNullException.ThrowIfNull(assemblies);

var configuration = new ContainerConfiguration();
foreach (var assembly in assemblies)
Expand Down
Loading