Skip to content

Commit

Permalink
Code quality improvements (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Oct 26, 2019
1 parent b5dd1b9 commit 495d4fa
Show file tree
Hide file tree
Showing 19 changed files with 183 additions and 234 deletions.
1 change: 0 additions & 1 deletion src/PSRule.Benchmark/PSRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using PSRule.Configuration;
using PSRule.Pipeline;
using PSRule.Rules;
Expand Down
20 changes: 10 additions & 10 deletions src/PSRule/Common/PSObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ public static T PropertyValue<T>(this PSObject o, string propertyName)
return (T)o.Properties[propertyName].Value;
}

public static string ValueAsString(this PSObject o, string propertyName, bool caseSensitive)
public static bool PropertyValue(this PSObject o, string propertyName, bool caseSensitive, out object value)
{
value = null;
var p = o.Properties[propertyName];
if (p == null)
return null;
return false;

if (caseSensitive && !StringComparer.Ordinal.Equals(p.Name, propertyName))
return null;
return false;

return p.Value.ToString();
value = p.Value;
return true;
}

public static bool PropertyValue(this PSObject o, string propertyName, bool caseSensitive, out object value)
public static string ValueAsString(this PSObject o, string propertyName, bool caseSensitive)
{
value = null;
var p = o.Properties[propertyName];
if (p == null)
return false;
return null;

if (caseSensitive && !StringComparer.Ordinal.Equals(p.Name, propertyName))
return false;
return null;

value = p.Value;
return true;
return p.Value.ToString();
}

public static bool HasProperty(this PSObject o, string propertyName)
Expand Down
14 changes: 5 additions & 9 deletions src/PSRule/Common/YamlConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,17 +402,13 @@ private bool TryMetadata(IParser reader, Func<IParser, Type, object> nestedObjec
private bool TryResource(string name, IParser reader, Func<IParser, Type, object> nestedObjectDeserializer, ResourceMetadata metadata, CommentMetadata comment, out IResource spec)
{
spec = null;
if (_Factory.TryDescriptor(name, out ISpecDescriptor descriptor))
if (_Factory.TryDescriptor(name, out ISpecDescriptor descriptor) && reader.Accept<MappingStart>())
{
// Using object style
if (reader.Accept<MappingStart>())
{
if (!_Next.Deserialize(reader, descriptor.SpecType, nestedObjectDeserializer, out object value))
return false;
if (!_Next.Deserialize(reader, descriptor.SpecType, nestedObjectDeserializer, out object value))
return false;

spec = descriptor.CreateInstance(PipelineContext.CurrentThread.Source.File, metadata, comment, value);
return true;
}
spec = descriptor.CreateInstance(PipelineContext.CurrentThread.Source.File, metadata, comment, value);
return true;
}
return false;
}
Expand Down
20 changes: 9 additions & 11 deletions src/PSRule/Configuration/BindingOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ public BindingOption(BindingOption option)

public override bool Equals(object obj)
{
return obj != null &&
obj is BindingOption &&
Equals(obj as BindingOption);
return obj is BindingOption option && Equals(option);
}

public bool Equals(BindingOption other)
{
return other != null &&
IgnoreCase == other.IgnoreCase &&
TargetName == other.TargetName &&
TargetType == other.TargetType;
}

public override int GetHashCode()
Expand All @@ -51,14 +57,6 @@ public override int GetHashCode()
}
}

public bool Equals(BindingOption other)
{
return other != null &&
IgnoreCase == other.IgnoreCase &&
TargetName == other.TargetName &&
TargetType == other.TargetType;
}

/// <summary>
/// Determines if custom binding uses ignores case when matching properties.
/// </summary>
Expand Down
26 changes: 12 additions & 14 deletions src/PSRule/Configuration/OutputOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,18 @@ public OutputOption(OutputOption option)

public override bool Equals(object obj)
{
return obj != null &&
obj is OutputOption &&
Equals(obj as OutputOption);
return obj is OutputOption option && Equals(option);
}

public bool Equals(OutputOption other)
{
return other != null &&
As == other.As &&
Culture == other.Culture &&
Encoding == other.Encoding &&
Format == other.Format &&
Path == other.Path &&
Style == other.Style;
}

public override int GetHashCode()
Expand All @@ -66,17 +75,6 @@ public override int GetHashCode()
}
}

public bool Equals(OutputOption other)
{
return other != null &&
As == other.As &&
Culture == other.Culture &&
Encoding == other.Encoding &&
Format == other.Format &&
Path == other.Path &&
Style == other.Style;
}

/// <summary>
/// The type of result to produce.
/// </summary>
Expand Down
34 changes: 16 additions & 18 deletions src/PSRule/Configuration/PSRuleOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,28 @@ public static implicit operator PSRuleOption(Hashtable hashtable)
public static implicit operator PSRuleOption(string path)
{
var option = FromFile(path: path, silentlyContinue: false);

return option;
}

public override bool Equals(object obj)
{
return obj != null &&
obj is PSRuleOption &&
Equals(obj as PSRuleOption);
return obj is PSRuleOption option && Equals(option);
}

public bool Equals(PSRuleOption other)
{
return other != null &&
Binding == other.Binding &&
Configuration == other.Configuration &&
Execution == other.Execution &&
Input == other.Input &&
Logging == other.Logging &&
Output == other.Output &&
Suppression == other.Suppression &&
Pipeline == other.Pipeline &&
Rule == other.Rule;
}

public override int GetHashCode()
{
unchecked // Overflow is fine
Expand All @@ -352,20 +364,6 @@ public override int GetHashCode()
}
}

public bool Equals(PSRuleOption other)
{
return other != null &&
Binding == other.Binding &&
Configuration == other.Configuration &&
Execution == other.Execution &&
Input == other.Input &&
Logging == other.Logging &&
Output == other.Output &&
Suppression == other.Suppression &&
Pipeline == other.Pipeline &&
Rule == other.Rule;
}

/// <summary>
/// Get a fully qualified file path.
/// </summary>
Expand Down
23 changes: 13 additions & 10 deletions src/PSRule/Parser/MarkdownReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private void Text()
_Stream.MarkExtentStart();

// Set the default style
var textStyle = MarkdownTokenFlags.None;
var textStyle = MarkdownTokens.None;

var startOfLine = _Stream.IsStartOfLine;

Expand All @@ -233,12 +233,12 @@ private void Text()
_Context = MarkdownReaderMode.List;

if (_Output.Current != null && _Output.Current.Flag.IsEnding() && !_Output.Current.Flag.ShouldPreserve())
_Output.Current.Flag |= MarkdownTokenFlags.Preserve;
_Output.Current.Flag |= MarkdownTokens.Preserve;
}

// Override line ending if the line was a list item so that the line ending is preserved
if (_Context == MarkdownReaderMode.List && ending.IsEnding())
ending |= MarkdownTokenFlags.Preserve;
ending |= MarkdownTokens.Preserve;

// Add the text to the output stream
_Output.Text(text, flag: textStyle | ending);
Expand All @@ -247,9 +247,9 @@ private void Text()
_Context = MarkdownReaderMode.None;
}

private string UnwrapStyleMarkers(MarkdownStream stream, out MarkdownTokenFlags flag)
private string UnwrapStyleMarkers(MarkdownStream stream, out MarkdownTokens flag)
{
flag = MarkdownTokenFlags.None;
flag = MarkdownTokens.None;

// Check for style
var styleChar = stream.Current;
Expand All @@ -275,10 +275,10 @@ private string UnwrapStyleMarkers(MarkdownStream stream, out MarkdownTokenFlags
text = Pad(text, styleChar, left: styleCount - styleEnding);

if (styleEnding == 1 || styleEnding == 3)
flag |= MarkdownTokenFlags.Italic;
flag |= MarkdownTokens.Italic;

if (styleEnding >= 2)
flag |= MarkdownTokenFlags.Bold;
flag |= MarkdownTokens.Bold;
}
else
{
Expand All @@ -298,7 +298,7 @@ private string UnwrapStyleMarkers(MarkdownStream stream, out MarkdownTokenFlags
text = Pad(text, styleChar, left: codeCount - codeEnding);

if (codeEnding == 1)
flag |= MarkdownTokenFlags.Code;
flag |= MarkdownTokens.Code;
}
else
{
Expand Down Expand Up @@ -329,9 +329,12 @@ private bool IsList(string text)
return false;
}

private MarkdownTokenFlags GetEnding(int lineEndings)
private MarkdownTokens GetEnding(int lineEndings)
{
return lineEndings == 0 ? MarkdownTokenFlags.None : (lineEndings == 1) ? MarkdownTokenFlags.LineEnding : MarkdownTokenFlags.LineBreak;
if (lineEndings == 0)
return MarkdownTokens.None;

return (lineEndings == 1) ? MarkdownTokens.LineEnding : MarkdownTokens.LineBreak;
}

/// <summary>
Expand Down
Loading

0 comments on commit 495d4fa

Please sign in to comment.