Skip to content

Commit

Permalink
chore: Cleanup to fix several issues pointed out by Codacy (#359)
Browse files Browse the repository at this point in the history
Signed-off-by: andreas hilti <[email protected]>
  • Loading branch information
andreas-hilti authored Sep 15, 2024
1 parent 97fe22d commit 0dcccad
Show file tree
Hide file tree
Showing 34 changed files with 224 additions and 110 deletions.
5 changes: 5 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"MD013": {
"code_blocks": false
}
}
2 changes: 1 addition & 1 deletion benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

Just a little project to help with benchmarking and performance tuning.

`dotnet run --configuration Release`
`dotnet run --configuration Release`
2 changes: 1 addition & 1 deletion docs/articles/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ using (var outputFile = File.OpenWrite("bom-1.3.json"))
bom.SpecVersion = SpecificationVersion.v1_3;
Json.Serializer.Serialize(bom, outputFile);
}
```
```
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ Tests are automatically run on Windows, MacOS and Linux for every pull request.
And build warnings will break the build.

If you are having trouble debugging a test that is failing for a platform you
don't have access to please us know.
don't have access to please us know.
2 changes: 1 addition & 1 deletion docs/namespaces/CycloneDX.Exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
uid: CycloneDX.Exceptions
summary: *content
---
Namespace containing all custom exceptions the `CycloneDX.Core` library uses.
Namespace containing all custom exceptions the `CycloneDX.Core` library uses.
2 changes: 1 addition & 1 deletion docs/namespaces/CycloneDX.Json.Converters.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ uid: CycloneDX.Json.Converters.v1_2
summary: *content
---
Namespace containing all custom JSON converters required for serialization and
deserialization.
deserialization.
2 changes: 1 addition & 1 deletion docs/namespaces/CycloneDX.Json.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
uid: CycloneDX.Json
summary: *content
---
Namespace containing all JSON specific functionality. Including serialization, deserialization, and validation.
Namespace containing all JSON specific functionality. Including serialization, deserialization, and validation.
2 changes: 1 addition & 1 deletion docs/namespaces/CycloneDX.Models.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
uid: CycloneDX.Models
summary: *content
---
Namespace containing all CycloneDX data models.
Namespace containing all CycloneDX data models.
2 changes: 1 addition & 1 deletion docs/namespaces/CycloneDX.Protobuf.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
uid: CycloneDX.Json
summary: *content
---
Namespace containing all Protocol Buffer specific functionality. Including serialization and deserialization.
Namespace containing all Protocol Buffer specific functionality. Including serialization and deserialization.
2 changes: 1 addition & 1 deletion docs/namespaces/CycloneDX.Utils.Exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
uid: CycloneDX.Utils.Exceptions
summary: *content
---
Namespace containing all custom exceptions the `CycloneDX.Utils` library uses.
Namespace containing all custom exceptions the `CycloneDX.Utils` library uses.
2 changes: 1 addition & 1 deletion docs/namespaces/CycloneDX.Utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
uid: CycloneDX.Utils
summary: *content
---
Namespace containing utility methods that operate on CycloneDX BOMs.
Namespace containing utility methods that operate on CycloneDX BOMs.
2 changes: 1 addition & 1 deletion docs/namespaces/CycloneDX.Xml.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
uid: CycloneDX.Xml
summary: *content
---
Namespace containing all XML specific functionality. Including serialization, deserialization, and validation.
Namespace containing all XML specific functionality. Including serialization, deserialization, and validation.
2 changes: 1 addition & 1 deletion docs/namespaces/CycloneDX.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
uid: CycloneDX
summary: *content
---
The base CycloneDX namespace.
The base CycloneDX namespace.
34 changes: 28 additions & 6 deletions src/CycloneDX.Core/BomUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ internal static Bom CopyBomAndDowngrade(Bom bom)
bomCopy.Properties = null;
bomCopy.Formulation = null;

if (bomCopy.Metadata != null) bomCopy.Metadata.Lifecycles = null;
if (bomCopy.Metadata != null)
{
bomCopy.Metadata.Lifecycles = null;
}

if (bomCopy.Compositions != null)
{
Expand All @@ -177,7 +180,10 @@ internal static Bom CopyBomAndDowngrade(Bom bom)
{
component.ModelCard = null;
component.Data = null;
if ((int)component.Type > 8) component.Type = Component.Classification.Library;
if ((int)component.Type > 8)
{
component.Type = Component.Classification.Library;
}
});

EnumerateAllServices(bomCopy, (service) =>
Expand Down Expand Up @@ -425,15 +431,21 @@ public static void EnumerateAllEvidence(Bom bom, Action<Evidence> callback)
{
EnumerateAllComponents(bom, (component) =>
{
if (component.Evidence != null) callback(component.Evidence);
if (component.Evidence != null)
{
callback(component.Evidence);
}
});
}

public static void EnumerateAllLicenses(Bom bom, Action<License> callback)
{
EnumerateAllLicenseChoices(bom, (licenseChoice) =>
{
if (licenseChoice.License != null) callback(licenseChoice.License);
if (licenseChoice.License != null)
{
callback(licenseChoice.License);
}
});
}

Expand Down Expand Up @@ -496,7 +508,9 @@ public static void EnumerateAllOrganizationalEntity(Bom bom, Action<Organization
foreach (var annotation in bom.Annotations)
{
if (annotation.Annotator?.Organization != null)
{
callback(annotation.Annotator.Organization);
}
}
}

Expand All @@ -511,7 +525,10 @@ public static void EnumerateAllOrganizationalEntity(Bom bom, Action<Organization
});
EnumerateAllComponents(bom, (component) =>
{
if (component.Supplier != null) callback(component.Supplier);
if (component.Supplier != null)
{
callback(component.Supplier);
}
component.ModelCard?.Considerations?.EnvironmentalConsiderations?.EnergyConsumptions?
Expand All @@ -529,7 +546,10 @@ public static void EnumerateAllOrganizationalEntity(Bom bom, Action<Organization
});
EnumerateAllServices(bom, (service) =>
{
if (service.Provider != null) callback(service.Provider);
if (service.Provider != null)
{
callback(service.Provider);
}
});
}

Expand Down Expand Up @@ -577,7 +597,9 @@ public static void EnumerateAllToolChoices(Bom bom, Action<ToolChoices> callback
EnumerateAllVulnerabilities(bom, (vuln) =>
{
if (vuln.Tools != null)
{
callback(vuln.Tools);
}
});
}

Expand Down
10 changes: 8 additions & 2 deletions src/CycloneDX.Core/Models/Annotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public List<string> Subjects
{
if (XmlSubjects == null) return null;
var result = new List<string>();
foreach (var subject in XmlSubjects) result.Add(subject.Ref);
foreach (var subject in XmlSubjects)
{
result.Add(subject.Ref);
}
return result;
}
set
Expand All @@ -59,7 +62,10 @@ public List<string> Subjects
else
{
XmlSubjects = new List<XmlAnnotationSubject>();
foreach (var subject in value) XmlSubjects.Add(new XmlAnnotationSubject() { Ref = subject});
foreach (var subject in value)
{
XmlSubjects.Add(new XmlAnnotationSubject { Ref = subject });
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/CycloneDX.Core/Models/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ public ServiceDataChoices XmlData
{
get
{
if (Data == null) return null;
if (Data == null) { return null; }
if (SpecVersion < SpecificationVersion.v1_5)
{
var result = new ServiceDataChoices()
var result = new ServiceDataChoices
{
SpecVersion = SpecVersion,
DataClassifications = new List<DataClassification>()
Expand Down
8 changes: 6 additions & 2 deletions src/CycloneDX.Core/Models/ServiceDataChoices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public void ReadXml(XmlReader reader)
{
if (reader.LocalName == "classification")
{
if (this.DataClassifications == null) this.DataClassifications = new List<DataClassification>();
if (this.DataClassifications == null) { this.DataClassifications = new List<DataClassification>(); }
var serializer = Xml.Serializer.GetElementSerializer<DataClassification>(SpecVersion, "classification");
var dataClassification = (DataClassification)serializer.Deserialize(reader);
this.DataClassifications.Add(dataClassification);
}
if (reader.LocalName == "dataflow")
{
if (this.DataFlows == null) this.DataFlows = new List<DataFlow>();
if (this.DataFlows == null) { this.DataFlows = new List<DataFlow>(); }
var serializer = Xml.Serializer.GetElementSerializer<DataFlow>(SpecVersion, "dataflow");
var dataflow = (DataFlow)serializer.Deserialize(reader);
this.DataFlows.Add(dataflow);
Expand All @@ -72,14 +72,18 @@ public void WriteXml(System.Xml.XmlWriter writer)
{
var serializer = Xml.Serializer.GetElementSerializer<DataClassification>(SpecVersion, "classification");
foreach (var dc in this.DataClassifications)
{
serializer.Serialize(writer, dc);
}
}

if (this.DataFlows != null)
{
var serializer = Xml.Serializer.GetElementSerializer<DataFlow>(SpecVersion, "dataflow");
foreach (var df in this.DataFlows)
{
serializer.Serialize(writer, df);
}
}
}

Expand Down
21 changes: 18 additions & 3 deletions src/CycloneDX.Core/Models/ToolChoices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,21 @@ public void ReadXml(XmlReader reader)
if (reader.LocalName == "tool")
{
#pragma warning disable 618
if (this.Tools == null) this.Tools = new List<Tool>();
if (this.Tools == null)
{
this.Tools = new List<Tool>();
}
var serializer = Xml.Serializer.GetElementSerializer<Tool>(SpecVersion, "tool");
var tool = (Tool)serializer.Deserialize(reader);
#pragma warning restore 618
this.Tools.Add(tool);
}
if (reader.LocalName == "components")
{
if (this.Components == null) this.Components = new List<Component>();
if (this.Components == null)
{
this.Components = new List<Component>();
}
var serializer = Xml.Serializer.GetElementSerializer<Component>(SpecVersion, "component");
reader.ReadStartElement();
while (reader.LocalName == "component")
Expand All @@ -81,7 +87,10 @@ public void ReadXml(XmlReader reader)
}
if (reader.LocalName == "services")
{
if (this.Services == null) this.Services = new List<Service>();
if (this.Services == null)
{
this.Services = new List<Service>();
}
var serializer = Xml.Serializer.GetElementSerializer<Service>(SpecVersion, "service");
reader.ReadStartElement();
while (reader.LocalName == "service")
Expand All @@ -102,15 +111,19 @@ public void WriteXml(System.Xml.XmlWriter writer) {
var serializer = Xml.Serializer.GetElementSerializer<Tool>(SpecVersion, "tool");
#pragma warning restore 618
foreach (var tool in this.Tools)
{
serializer.Serialize(writer, tool);
}
}

if (this.Components != null)
{
writer.WriteStartElement("components");
var serializer = Xml.Serializer.GetElementSerializer<Component>(SpecVersion, "component");
foreach (var component in this.Components)
{
serializer.Serialize(writer, component);
}
writer.WriteEndElement();
}

Expand All @@ -119,7 +132,9 @@ public void WriteXml(System.Xml.XmlWriter writer) {
writer.WriteStartElement("services");
var serializer = Xml.Serializer.GetElementSerializer<Service>(SpecVersion, "service");
foreach (var service in this.Services)
{
serializer.Serialize(writer, service);
}
writer.WriteEndElement();
}
}
Expand Down
42 changes: 27 additions & 15 deletions src/CycloneDX.Core/Protobuf/Serializer.Deserialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public static Bom Deserialize(Stream protobufStream)
{
bom.Metadata.Timestamp = DateTime.SpecifyKind(bom.Metadata.Timestamp.Value, DateTimeKind.Utc);
}

CleanupEmptyArrays(bom);

return bom;
}

Expand All @@ -59,7 +59,7 @@ public static Bom Deserialize(byte[] bytes)
var bom = Deserialize(ms);
return bom;
}

private static void CleanupEmptyArrays(Bom bom)
{
if (bom.Metadata?.Authors?.Count == 0)
Expand Down Expand Up @@ -115,7 +115,7 @@ private static void CleanupEmptyArrays(Bom bom)
bom.Dependencies = null;
}
}

private static void CleanupEmptyArrays(Component component)
{
if (component.Hashes?.Count == 0)
Expand Down Expand Up @@ -166,23 +166,35 @@ private static void CleanupEmptyArrays(Service service)

private static void CleanupEmptyArrays(Pedigree pedigree)
{
if (pedigree.Commits?.Count == 0) pedigree.Commits = null;
if (pedigree.Patches?.Count == 0) pedigree.Patches = null;
if (pedigree.Commits?.Count == 0) { pedigree.Commits = null; }
if (pedigree.Patches?.Count == 0) { pedigree.Patches = null; }

if (pedigree.Ancestors?.Count == 0) pedigree.Ancestors = null;
if (pedigree.Ancestors?.Count == 0) { pedigree.Ancestors = null; }
if (pedigree.Ancestors != null)
foreach (var component in pedigree.Ancestors)
CleanupEmptyArrays(component);
{
foreach (var component in pedigree.Ancestors)
{
CleanupEmptyArrays(component);
}
}

if (pedigree.Descendants?.Count == 0) pedigree.Descendants = null;
if (pedigree.Descendants?.Count == 0) { pedigree.Descendants = null; }
if (pedigree.Descendants != null)
foreach (var component in pedigree.Descendants)
CleanupEmptyArrays(component);
{
foreach (var component in pedigree.Descendants)
{
CleanupEmptyArrays(component);
}
}

if (pedigree.Variants?.Count == 0) pedigree.Variants = null;
if (pedigree.Variants?.Count == 0) { pedigree.Variants = null; }
if (pedigree.Variants != null)
foreach (var component in pedigree.Variants)
CleanupEmptyArrays(component);
{
foreach (var component in pedigree.Variants)
{
CleanupEmptyArrays(component);
}
}
}
}
}
Loading

0 comments on commit 0dcccad

Please sign in to comment.