Skip to content

Commit

Permalink
Merge pull request #137 from christianhelle/resolve-code-smells
Browse files Browse the repository at this point in the history
Resolve SonarCloud Code Smells
  • Loading branch information
christianhelle committed Sep 4, 2023
2 parents 80c1bb1 + 9eee90f commit 7025d61
Showing 1 changed file with 11 additions and 28 deletions.
39 changes: 11 additions & 28 deletions src/Refitter.Core/RefitGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,22 @@ public static async Task<RefitGenerator> CreateAsync(RefitGeneratorSettings sett
return new RefitGenerator(settings, openApiDocument);
}

private static void ProcessTagFilters(OpenApiDocument document,
string[] includeTags)
private static void ProcessTagFilters(OpenApiDocument document, IReadOnlyCollection<string> includeTags)
{
if (includeTags.Length == 0)
if (includeTags.Count == 0)
{
return;
}
var clonedPaths = document.Paths
.Where(x => x.Value != null)
.ToArray();
var clonedPaths = document.Paths.Where(pair => pair.Value != null);
foreach (var path in clonedPaths)
{
var methods = path.Value
.Where(x => x.Value != null)
.ToArray();
var methods = path.Value.Where(pair => pair.Value != null);
foreach (var method in methods)
{
var exclude = true;
foreach (var tag in includeTags)
{
if (method.Value.Tags?.Any(x => x == tag) == true)
{
exclude = false;
}
exclude = method.Value.Tags?.Exists(x => x == tag) != true;
}
if (exclude)
{
Expand All @@ -71,23 +63,14 @@ public static async Task<RefitGenerator> CreateAsync(RefitGeneratorSettings sett
}

// compile all expressions here once, as we will use them more than once
var regexes = pathMatchExpressions.Select(x => new Regex(x, RegexOptions.Compiled)).ToArray();
var regexes = pathMatchExpressions.Select(x => new Regex(x, RegexOptions.Compiled)).ToList();
var paths = document.Paths.Keys
.Where(pathKey => regexes.TrueForAll(regex => !regex.IsMatch(pathKey)))
.ToArray();

var clonedPaths = document.Paths.ToArray();
foreach (var path in clonedPaths)
foreach (string pathKey in paths)
{
var exclude = true;
foreach (var regex in regexes)
{
if (regex.IsMatch(path.Key))
{
exclude = false;
}
}
if (exclude)
{
document.Paths.Remove(path.Key);
}
document.Paths.Remove(pathKey);
}
}

Expand Down

0 comments on commit 7025d61

Please sign in to comment.