Skip to content

Commit 6dad0a9

Browse files
committed
feat: add SonarLint configuration and improve dependency analysis logic
1 parent 5e57fbb commit 6dad0a9

5 files changed

Lines changed: 31 additions & 22 deletions

File tree

.sonarlint/CodeLineCounter.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"sonarCloudOrganization": "magic5644",
3+
"projectKey": "magic5644_codeLineCounter"
4+
}

CodeLineCounter/Services/DependencyAnalyzer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ public static void AnalyzeFile(string filePath, string sourceCode)
125125
.Usings.FirstOrDefault()?
126126
.Name?.ToString() ?? "",
127127
TargetClass = dependency.Split('.')[(dependency.Split('.').Length - 1)],
128-
TargetNamespace = dependency.Contains(".") ? dependency.Substring(0, dependency.LastIndexOf('.')) : "",
129-
TargetAssembly = dependency.Contains(".") ? dependency.Substring(0, dependency.LastIndexOf('.')) : "",
128+
TargetNamespace = dependency.Contains('.') ? dependency.Substring(0, dependency.LastIndexOf('.')) : "",
129+
TargetAssembly = dependency.Contains('.') ? dependency.Substring(0, dependency.LastIndexOf('.')) : "",
130130
FilePath = filePath,
131131
StartLine = classDeclaration.GetLocation().GetLineSpan().StartLinePosition.Line
132132
};
@@ -150,7 +150,7 @@ public static void AnalyzeFile(string filePath, string sourceCode)
150150
});
151151
}
152152

153-
private static IEnumerable<string> ExtractDependencies(ClassDeclarationSyntax classDeclaration)
153+
private static HashSet<string> ExtractDependencies(ClassDeclarationSyntax classDeclaration)
154154
{
155155
var dependencies = new HashSet<string>();
156156
var usings = GetUsingsWithCurrentNamespace(classDeclaration);
@@ -278,15 +278,15 @@ private static string GetFullTypeNameFromSymbol(string typeName, IEnumerable<str
278278
if (string.IsNullOrEmpty(typeName))
279279
return typeName;
280280

281-
if (!typeName.Contains("<"))
281+
if (!typeName.Contains('<'))
282282
return ResolveSimpleTypeName(typeName, usings);
283283

284284
return HandleGenericType(typeName, usings);
285285
}
286286

287287
private static string ResolveSimpleTypeName(string typeName, IEnumerable<string?> usings)
288288
{
289-
if (typeName.Contains("."))
289+
if (typeName.Contains('.'))
290290
return typeName;
291291

292292
return FindTypeInUsings(typeName, usings);
@@ -308,7 +308,7 @@ private static string FindTypeInUsings(string typeName, IEnumerable<string?> usi
308308
private static string HandleGenericType(string typeName, IEnumerable<string?> usings)
309309
{
310310
var parts = SplitGenericType(typeName);
311-
return string.Join("", parts.Select(p => p.Contains("<")
311+
return string.Join("", parts.Select(p => p.Contains('<')
312312
? p
313313
: GetFullTypeNameFromSymbol(p, usings)));
314314
}

CodeLineCounter/Services/DependencyGraphGenerator.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,30 +118,36 @@ private static void GroupByNamespace(Dictionary<string, (int incoming, int outgo
118118
}
119119

120120
// Group by namespace
121-
if (!namespaceGroups.ContainsKey(dep.SourceNamespace))
121+
if (!namespaceGroups.TryGetValue(dep.SourceNamespace, out var sourceNamespaceList))
122122
{
123-
namespaceGroups[dep.SourceNamespace] = new List<string>();
123+
sourceNamespaceList = [];
124+
namespaceGroups[dep.SourceNamespace] = sourceNamespaceList;
125+
124126
}
125-
if (!namespaceGroups.ContainsKey(dep.TargetNamespace))
127+
if (!sourceNamespaceList.Contains(dep.SourceClass))
126128
{
127-
namespaceGroups[dep.TargetNamespace] = new List<string>();
129+
sourceNamespaceList.Add(dep.SourceClass);
128130
}
129131

130-
if (!namespaceGroups[dep.SourceNamespace].Contains(dep.SourceClass))
132+
if (!namespaceGroups.TryGetValue(dep.TargetNamespace, out var targetNamespaceList))
131133
{
132-
namespaceGroups[dep.SourceNamespace].Add(dep.SourceClass);
134+
targetNamespaceList = [];
135+
namespaceGroups[dep.TargetNamespace] = targetNamespaceList;
133136
}
134-
if (!namespaceGroups[dep.TargetNamespace].Contains(dep.TargetClass))
137+
138+
if (!targetNamespaceList.Contains(dep.TargetClass))
135139
{
136-
namespaceGroups[dep.TargetNamespace].Add(dep.TargetClass);
140+
targetNamespaceList.Add(dep.TargetClass);
137141
}
138142
}
139143

140144
private static async Task CompileGraphAndWriteToFile(string outputPath, DotGraph graph)
141145
{
142146
await using var writer = new StringWriter();
143-
var options = new CompilationOptions();
144-
options.Indented = true;
147+
var options = new CompilationOptions
148+
{
149+
Indented = true
150+
};
145151
var context = new CompilationContext(writer, options);
146152
graph.Directed = true;
147153
context.DirectedGraph = true;

CodeLineCounter/Utils/CoreUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static (bool Verbose, string? DirectoryPath, bool Help, ExportFormat form
4949
}
5050
else
5151
{
52-
Console.WriteLine($"Invalid format: {formatString}. Valid formats are: {string.Join(", ", Enum.GetNames(typeof(ExportFormat)))}. Using default format {format}");
52+
Console.WriteLine($"Invalid format: {formatString}. Valid formats are: {string.Join(", ", Enum.GetNames<ExportFormat>())}. Using default format {format}");
5353
}
5454
}
5555
break;

CodeLineCounter/Utils/DataExporter.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@ public static class DataExporter
1515

1616
public static void Export<T>(string filePath, T data, CoreUtils.ExportFormat format) where T : class
1717
{
18-
if (data == null)
19-
throw new ArgumentNullException(nameof(data));
18+
ArgumentNullException.ThrowIfNull(data);
2019

21-
ExportCollection(filePath, new List<T> { data }, format);
20+
ExportCollection(filePath, [data], format);
2221
}
2322

2423
public static void ExportCollection<T>(string filePath, IEnumerable<T> data, CoreUtils.ExportFormat format) where T : class
2524
{
2625
if (string.IsNullOrEmpty(filePath))
2726
throw new ArgumentException("File path cannot be null or empty", nameof(filePath));
28-
if (data == null)
29-
throw new ArgumentNullException(nameof(data));
27+
28+
ArgumentNullException.ThrowIfNull(data);
3029

3130
try
3231
{

0 commit comments

Comments
 (0)