Skip to content

Reduce cyclomatic complexity of ContractDomainService.CreateContract from 11 to 8#1719

Merged
StuartFerguson merged 2 commits intomasterfrom
copilot/reduce-complexity-create-contract-method
Apr 7, 2026
Merged

Reduce cyclomatic complexity of ContractDomainService.CreateContract from 11 to 8#1719
StuartFerguson merged 2 commits intomasterfrom
copilot/reduce-complexity-create-contract-method

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 7, 2026

CreateContract exceeded Codacy's cyclomatic complexity limit of 8 (reported as 11) due to the operator validation and duplicate contract-name check being inlined alongside the aggregate fetch/save flow.

Changes

  • Extracted ValidateContractCreationPreConditions — new private async helper encapsulates the operator-on-estate check and the EventStore transient-query duplicate-name check, replacing 4 decision points in CreateContract with a single if (preConditionResult.IsFailed) guard.
  • Fixed null-safety in JSON parsing — replaced the fragile jsonResult.Property("contractId").Values<String>().Single() chain (throws on missing property) with the safe jsonResult["contractId"]?.Value<String>(), combining the Guid.TryParse boolean return with the empty-GUID check into one condition.
// Before: all inlined in CreateContract (complexity 11)
if (estate.Operators.Any(o => o.OperatorId == command.RequestDTO.OperatorId) == false) { ... }
Result<String> result = await this.Context.RunTransientQuery(projection, cancellationToken);
if (result.IsFailed) ...
if (String.IsNullOrEmpty(resultString) == false) {
    ...
    if (contractIdResult != Guid.Empty) { ... }
}

// After: delegated to helper (CreateContract complexity 8)
Result preConditionResult = await this.ValidateContractCreationPreConditions(command, estate, cancellationToken);
if (preConditionResult.IsFailed)
    return preConditionResult;

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • f.feedz.io
    • Triggering command: /usr/bin/dotnet dotnet test TransactionProcessor.BusinessLogic.Tests/TransactionProcessor.BusinessLogic.Tests.csproj --filter ContractDomainService_ --no-restore -nologo (dns block)
    • Triggering command: /usr/bin/dotnet dotnet test TransactionProcessor.BusinessLogic.Tests/TransactionProcessor.BusinessLogic.Tests.csproj --filter ContractDomainService_ --no-restore -nologo -v normal (dns block)
    • Triggering command: /usr/bin/dotnet dotnet test TransactionProcessor.BusinessLogic.Tests/TransactionProcessor.BusinessLogic.Tests.csproj --filter ContractDomainService_ --no-restore (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented Apr 7, 2026

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 4 complexity · 0 duplication

Metric Results
Complexity 4
Duplication 0

View in Codacy

TIP This summary will be updated as you push new changes. Give us feedback

Copilot AI changed the title [WIP] Refactor CreateContract method to reduce cyclomatic complexity Reduce cyclomatic complexity of ContractDomainService.CreateContract from 11 to 8 Apr 7, 2026
Copilot AI requested a review from StuartFerguson April 7, 2026 18:48
@StuartFerguson StuartFerguson marked this pull request as ready for review April 7, 2026 18:59
@StuartFerguson StuartFerguson merged commit 1bdfa1a into master Apr 7, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Complexity] Method ContractDomainService::CreateContract has a cyclomatic complexity of 11 (limit is 8)

2 participants