Skip to content

Reduce AddContractToMerchant cyclomatic complexity#1613

Merged
StuartFerguson merged 4 commits intomasterfrom
copilot/fix-cyclomatic-complexity-issue-again
Mar 13, 2026
Merged

Reduce AddContractToMerchant cyclomatic complexity#1613
StuartFerguson merged 4 commits intomasterfrom
copilot/fix-cyclomatic-complexity-issue-again

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 13, 2026

MerchantDomainService.AddContractToMerchant exceeded the configured cyclomatic complexity limit. This change trims the method back under the threshold without changing its success/failure semantics.

  • Refactor

    • Extracted contract retrieval and creation-state validation into a dedicated private helper: GetCreatedContract(...).
    • Kept the public method focused on orchestration: load estate + merchant, validate relationship, add contract, save aggregate.
  • Behavior preserved

    • Contract lookup failures still flow through ResultHelpers.CreateFailure(...).
    • Uncreated contracts still return the same Result.Invalid(...) response.
    • The aggregate state transition and save path remain unchanged.
  • Net effect

    • Fewer branches in AddContractToMerchant.
    • Validation logic is isolated and reusable without broadening scope beyond the targeted method.
Result<ContractAggregate> contractResult =
    await this.GetCreatedContract(command.RequestDto.ContractId, cancellationToken);

if (contractResult.IsFailed)
    return ResultHelpers.CreateFailure(contractResult);

Result stateResult = merchantAggregate.AddContract(contractResult.Data);

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 /home/REDACTED/work/TransactionProcessor/TransactionProcessor/TransactionProcessor.Tests/TransactionProcessor.Tests.csproj -nologo (dns block)
    • Triggering command: /usr/bin/dotnet dotnet test /home/REDACTED/work/TransactionProcessor/TransactionProcessor/TransactionProcessor.BusinessLogic.Tests/TransactionProcessor.BusinessLogic.Tests.csproj --filter AddContractToMerchant -p:RestoreIgnoreFailedSources=true -nologo (dns block)
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.24.3/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp /opt/hostedtoolcache/CodeQL/2.24.3/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp (dns block)

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

Original prompt

This section details on the original issue you should resolve

<issue_title>[Complexity] Method MerchantDomainService::AddContractToMerchant has a cyclomatic complexity of 9 (limit is 8)</issue_title>
<issue_description>

Codacy ID: 5493826f46d9674a6e82b63cb2a6cbb1

File: TransactionProcessor.BusinessLogic/Services/MerchantDomainService.cs
Line: 407
Rule: Complexity (Warning)
Commit: b53b61ccb0be078d0067c6404180c069a16f839b

Message: Method MerchantDomainService::AddContractToMerchant has a cyclomatic complexity of 9 (limit is 8)

**Code Snippet:**
```csharp
        public async Task<Result> AddContractToMerchant(MerchantCommands.AddMerchantContractCommand command, CancellationToken cancellationToken) 
```

</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot stopped work on behalf of StuartFerguson due to an error March 13, 2026 18:37
Copilot AI and others added 2 commits March 13, 2026 18:44
Co-authored-by: StuartFerguson <16325469+StuartFerguson@users.noreply.github.com>
Co-authored-by: StuartFerguson <16325469+StuartFerguson@users.noreply.github.com>
Copilot AI changed the title [WIP] [Complexity] Reduce cyclomatic complexity of AddContractToMerchant method Reduce AddContractToMerchant cyclomatic complexity Mar 13, 2026
Copilot AI requested a review from StuartFerguson March 13, 2026 18:49
@StuartFerguson StuartFerguson marked this pull request as ready for review March 13, 2026 20:26
@StuartFerguson StuartFerguson merged commit 1fe04da into master Mar 13, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Complexity] Method MerchantDomainService::AddContractToMerchant has a cyclomatic complexity of 9 (limit is 8)

2 participants