Skip to content

Commit

Permalink
Ensure there is always at least 1 valid candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolajlauridsen committed May 22, 2024
1 parent 04ed514 commit b215425
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Umbraco.Web.Website/Routing/EagerMatcherPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public async Task ApplyAsync(HttpContext httpContext, CandidateSet candidates)
}

// If there's only one candidate, we don't need to do anything.
if (candidates.Count < 2)
var candidateCount = candidates.Count;
if (candidateCount < 2)
{
return;
}
Expand All @@ -85,6 +86,14 @@ public async Task ApplyAsync(HttpContext httpContext, CandidateSet candidates)
RouteEndpoint? dynamicEndpoint = null;
for (var i = 0; i < candidates.Count; i++)
{
if (candidates.IsValidCandidate(i) is false)
{
// If the candidate is not valid we reduce the candidate count so we can later ensure that there is always
// at least 1 candidate.
candidateCount -= 1;
continue;
}

CandidateState candidate = candidates[i];

// If it's not a RouteEndpoint there's not much we can do to count it in the order.
Expand Down Expand Up @@ -123,7 +132,7 @@ public async Task ApplyAsync(HttpContext httpContext, CandidateSet candidates)
// Invalidate the dynamic route if another route has a lower order.
// This means that if you register your static route after the dynamic route, the dynamic route will take precedence
// This more closely resembles the existing behaviour.
if (dynamicEndpoint is not null && dynamicId is not null && dynamicEndpoint.Order > lowestOrder)
if (dynamicEndpoint is not null && dynamicId is not null && dynamicEndpoint.Order > lowestOrder && candidateCount > 1)

Check notice on line 135 in src/Umbraco.Web.Website/Routing/EagerMatcherPolicy.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v13/dev)

ℹ Getting worse: Complex Method

ApplyAsync increases in cyclomatic complexity from 15 to 18, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

Check warning on line 135 in src/Umbraco.Web.Website/Routing/EagerMatcherPolicy.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v13/dev)

❌ Getting worse: Complex Conditional

ApplyAsync increases from 1 complex conditionals with 2 branches to 1 complex conditionals with 3 branches, threshold = 2. A complex conditional is an expression inside a branch (e.g. if, for, while) which consists of multiple, logical operators such as AND/OR. The more logical operators in an expression, the more severe the code smell.
{
candidates.SetValidity(dynamicId.Value, false);
}
Expand Down

0 comments on commit b215425

Please sign in to comment.