Skip to content

Commit

Permalink
Roslynator plus .net 5 analysers
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Dec 26, 2020
1 parent 2dfad8b commit 3402d6d
Show file tree
Hide file tree
Showing 46 changed files with 453 additions and 279 deletions.
1 change: 1 addition & 0 deletions .linting/roslynator.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Just add ruleset file to a solution and open it.

<!-- Specify default action that should be applied to a specified analyzer. -->
<Rule Id="RCS0044" Action="None" />
<Rule Id="CA1014" Action="None" />

</Rules>

Expand Down
2 changes: 1 addition & 1 deletion src/App/NetDaemon.App/Common/AppRuntimeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public sealed class AppRuntimeInfo
/// in app switch
/// </summary>
[JsonPropertyName("app_attributes")]
public Dictionary<string, object> AppAttributes { get; set; } = new();
public Dictionary<string, object> AppAttributes { get; } = new();
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/App/NetDaemon.App/Common/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@ public sealed class DisableLogAttribute : System.Attribute
{
// See the attribute guidelines at
// http://go.microsoft.com/fwlink/?LinkId=85236
private readonly SupressLogType[] _logTypesToSupress;
private readonly SupressLogType[]? _logTypesToSupress;

/// <summary>
/// Default constructor
/// </summary>
/// <param name="logTypes">List of logtypes to supress</param>
public DisableLogAttribute(params SupressLogType[] logTypes)
{
_logTypesToSupress = logTypes;
}
public DisableLogAttribute(params SupressLogType[] logTypes) => _logTypesToSupress = logTypes;

/// <summary>
/// Log types to supress
/// </summary>
public IEnumerable<SupressLogType> LogTypesToSupress => _logTypesToSupress;
public IEnumerable<SupressLogType>? LogTypesToSupress => _logTypesToSupress;

/// <summary>
/// Log tupes used
/// </summary>
public SupressLogType[]? LogTypes { get; }
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class HomeAssistantSettings
/// <summary>
/// Connect using ssl
/// </summary>
public bool Ssl { get; set; } = false;
public bool Ssl { get; set; }
/// <summary>
/// Token to authorize
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Globalization;
using System.IO;
using NetDaemon.Common.Exceptions;

namespace NetDaemon.Common.Configuration
{
Expand Down Expand Up @@ -27,7 +29,7 @@ public class NetDaemonSettings
/// the apps to be in the file paths and tries to find
/// all apps recursivly
/// </remarks>
public string? AppSource { get; set; } = null;
public string? AppSource { get; set; }

/// <summary>
/// Returns the directory path of AppSource
Expand All @@ -36,12 +38,13 @@ public string GetAppSourceDirectory()
{
var source = AppSource?.Trim() ?? Directory.GetCurrentDirectory();

if (source.EndsWith(".csproj") || source.EndsWith(".dll"))
if (source.EndsWith(".csproj", true, CultureInfo.InvariantCulture)
|| source.EndsWith(".dll", true, CultureInfo.InvariantCulture))
{
source = Path.GetDirectoryName(source);
}

return source ?? throw new NullReferenceException("Source cannot be null!");
return source ?? throw new NetDaemonNullReferenceException("Source cannot be null!");
}
}
}
4 changes: 2 additions & 2 deletions src/App/NetDaemon.App/Common/DelayResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DelayResult : IDelayResult
{
private readonly INetDaemonApp _daemonApp;
private readonly TaskCompletionSource<bool> _delayTaskCompletionSource;
private bool _isCanceled = false;
private bool _isCanceled;

/// <summary>
/// Constructor
Expand Down Expand Up @@ -52,7 +52,7 @@ public void Cancel()

#region IDisposable Support

private bool disposedValue = false; // To detect redundant calls
private bool disposedValue; // To detect redundant calls

/// <summary>
/// Disposes the object and cancel delay
Expand Down
66 changes: 66 additions & 0 deletions src/App/NetDaemon.App/Common/Exceptions/Exceptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;

namespace NetDaemon.Common.Exceptions
{
/// <inheritdoc/>
public class NetDaemonNullReferenceException : NullReferenceException
{
/// <inheritdoc/>
public NetDaemonNullReferenceException()
{
}

/// <inheritdoc/>
public NetDaemonNullReferenceException(string? message) : base(message)
{
}

/// <inheritdoc/>
public NetDaemonNullReferenceException(string? message, Exception? innerException) : base(message, innerException)
{
}
}

/// <inheritdoc/>
public class NetDaemonException : Exception
{
/// <inheritdoc/>
public NetDaemonException()
{
}

/// <inheritdoc/>
public NetDaemonException(string? message) : base(message)
{
}

/// <inheritdoc/>
public NetDaemonException(string? message, Exception? innerException) : base(message, innerException)
{
}
}

/// <inheritdoc/>
public class NetDaemonArgumentNullException : ArgumentNullException
{
/// <inheritdoc/>
public NetDaemonArgumentNullException()
{
}

/// <inheritdoc/>
public NetDaemonArgumentNullException(string? paramName) : base(paramName)
{
}

/// <inheritdoc/>
public NetDaemonArgumentNullException(string? message, Exception? innerException) : base(message, innerException)
{
}

/// <inheritdoc/>
public NetDaemonArgumentNullException(string? paramName, string? message) : base(paramName, message)
{
}
}
}
33 changes: 23 additions & 10 deletions src/App/NetDaemon.App/Common/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
using System.Globalization;
using System.Text;
using NetDaemon.Common.Fluent;

using System.Diagnostics.CodeAnalysis;
namespace NetDaemon.Common
{
/// <summary>
/// Useful extension methods used
/// </summary>
public static class Extensions
public static class NetDaemonExtensions
{
/// <summary>
/// Converts a valuepair to dynamic object
Expand All @@ -20,30 +20,39 @@ public static dynamic ToDynamic(this (string name, object val)[] attributeNameVa
{
// Convert the tuple name/value pair to tuple that can be serialized dynamically
var attributes = new FluentExpandoObject(true, true);
foreach (var (attribute, value) in attributeNameValuePair)
if (attributeNameValuePair is not null)
{
((IDictionary<string, object>)attributes).Add(attribute, value);
foreach (var (attribute, value) in attributeNameValuePair)
{
if (value is not null)
{
// We only add non-null values since the FluentExpandoObject will
// return null on missing anyway
attributes.Add(attribute, value);
}
}
}

dynamic result = attributes;
return result;
return (dynamic)attributes;
}

/// <summary>
/// Converts a anoumous type to expando object
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static ExpandoObject? ToExpandoObject(this object obj)
{
// Null-check

IDictionary<string, object?> expando = new ExpandoObject();

foreach (PropertyDescriptor? property in TypeDescriptor.GetProperties(obj.GetType()))
if (obj is not null)
{
if (property is not null)
expando.Add(property.Name, property.GetValue(obj));
foreach (PropertyDescriptor? property in TypeDescriptor.GetProperties(obj.GetType()))
{
if (property is not null)
expando.Add(property.Name, property.GetValue(obj));
}
}

return (ExpandoObject)expando;
Expand All @@ -53,8 +62,12 @@ public static dynamic ToDynamic(this (string name, object val)[] attributeNameVa
/// Converts any unicode string to a safe Home Assistant name
/// </summary>
/// <param name="str">The unicode string to convert</param>
[SuppressMessage(category: "Microsoft.Globalization", checkId: "CA1308")]
public static string ToSafeHomeAssistantEntityId(this string str)
{
if (str is null)
return string.Empty;

string normalizedString = str.Normalize(NormalizationForm.FormD);
StringBuilder stringBuilder = new(str.Length);

Expand Down
15 changes: 9 additions & 6 deletions src/App/NetDaemon.App/Common/Fluent/EntityBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using NetDaemon.Common.Exceptions;

namespace NetDaemon.Common.Fluent
{
Expand All @@ -20,17 +20,17 @@ public class EntityBase //: EntityState
/// <summary>
/// The daemon used in the API
/// </summary>
protected readonly INetDaemonApp App;
protected INetDaemonApp App { get; }

/// <summary>
/// The daemon used in the API
/// </summary>
protected readonly INetDaemon Daemon;
protected INetDaemon Daemon { get; }

/// <summary>
/// The EntityIds used
/// </summary>
protected readonly IEnumerable<string> EntityIds;
protected IEnumerable<string> EntityIds { get; }

/// <summary>
/// Constructor
Expand All @@ -48,9 +48,12 @@ public EntityBase(IEnumerable<string> entityIds, INetDaemon daemon, INetDaemonAp
/// <inheritdoc/>
protected static string GetDomainFromEntity(string entity)
{
if (string.IsNullOrEmpty(entity))
throw new NetDaemonNullReferenceException(nameof(entity));

var entityParts = entity.Split('.');
if (entityParts.Length != 2)
throw new ApplicationException($"entity_id is mal formatted {entity}");
throw new NetDaemonException($"entity_id is mal formatted {entity}");

return entityParts[0];
}
Expand Down
16 changes: 7 additions & 9 deletions src/App/NetDaemon.App/Common/Fluent/EntityManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand All @@ -9,7 +10,7 @@ namespace NetDaemon.Common.Fluent
/// <summary>
/// Implements interface for managing entities in the fluent API
/// </summary>
public class EntityManager : EntityBase, IEntity, IAction,
public sealed class EntityManager : EntityBase, IEntity, IAction,
IStateEntity, IState, IStateAction, IScript
{
/// <summary>
Expand Down Expand Up @@ -39,16 +40,14 @@ public IExecute Call(Func<string, EntityState?, EntityState?, Task> func)
/// <inheritdoc/>
IDelayResult IDelayStateChange.DelayUntilStateChange(object? to, object? from, bool allChanges)
{
return App.DelayUntilStateChange(this.EntityIds, to, from, allChanges);
return App.DelayUntilStateChange(EntityIds, to, from, allChanges);
}

/// <inheritdoc/>
IDelayResult IDelayStateChange.DelayUntilStateChange(Func<EntityState?, EntityState?, bool> stateFunc)
{
return App.DelayUntilStateChange(this.EntityIds, stateFunc);
}
IDelayResult IDelayStateChange.DelayUntilStateChange(Func<EntityState?, EntityState?, bool> stateFunc) => App.DelayUntilStateChange(EntityIds, stateFunc);

/// <inheritdoc/>
[SuppressMessage("Microsoft.Design", "CA1031")]
public void Execute()
{
foreach (var entityId in EntityIds)
Expand Down Expand Up @@ -200,8 +199,8 @@ async Task IScript.ExecuteAsync()
foreach (var scriptName in EntityIds)
{
var name = scriptName;
if (scriptName.Contains('.'))
name = scriptName[(scriptName.IndexOf('.') + 1)..];
if (scriptName.Contains('.', StringComparison.InvariantCulture))
name = scriptName[(scriptName.IndexOf('.', StringComparison.InvariantCulture) + 1)..];
var task = Daemon.CallServiceAsync("script", name);
taskList.Add(task);
}
Expand All @@ -220,7 +219,6 @@ async Task IScript.ExecuteAsync()
/// You want to keep the items when using this as part of an automation
/// that are kept over time. Not keeping when just doing a command
/// </remarks>
/// <returns></returns>
public async Task ExecuteAsync(bool keepItems)
{
if (keepItems)
Expand Down
6 changes: 6 additions & 0 deletions src/App/NetDaemon.App/Common/Fluent/Fluent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;

namespace NetDaemon.Common.Fluent
Expand Down Expand Up @@ -42,6 +43,7 @@ public interface IDelayStateChange
/// <param name="to">The state change to, or null if any state</param>
/// <param name="from">The state changed from or null if any state</param>
/// <param name="allChanges">Get all changed, even only attribute changes</param>
[SuppressMessage("Microsoft.Naming", "CA1716")]
IDelayResult DelayUntilStateChange(object? to = null, object? from = null, bool allChanges = false);

/// <summary>
Expand Down Expand Up @@ -95,6 +97,8 @@ public interface IScript
/// <summary>
/// Represent state change actions
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1716")]

public interface IState
{
/// <summary>
Expand Down Expand Up @@ -150,6 +154,7 @@ public interface IStateAction : IExecute
/// <summary>
/// When state change
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1716")]
public interface IStateChanged
{
/// <summary>
Expand Down Expand Up @@ -229,6 +234,7 @@ public interface ISpeak<T>
/// Generic interface for stop
/// </summary>
/// <typeparam name="T">Return type of stop operation</typeparam>
[SuppressMessage("Microsoft.Naming", "CA1716")]
public interface IStop<T>
{
/// <summary>
Expand Down
Loading

0 comments on commit 3402d6d

Please sign in to comment.