Skip to content

refactor(packages): reduce cognitive complexity of check licenses - #1757

Merged
marcossevilla merged 4 commits into
mainfrom
refactor/cc-packages
Oct 8, 2026
Merged

marcossevilla merged 4 commits into
mainfrom
refactor/cc-packages

Conversation

@marcossevilla

@marcossevilla marcossevilla commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Status

READY

Description

  • PackagesCheckLicensesCommand.run reads as early-return steps: validate options, load the lock file, select dependencies, collect licenses, report. Fetching one dependency's licenses lives in _retrieveLicenses, which throws a private _LicenseRetrievalFailure carrying the message and exit code, caught in one place by run and in one place by _collectLicenses when --ignore-retrieval-failures is set.
  • The four-clause dependency type check becomes an exhaustive switch from PubspecDependencyType to its option name.
  • _composeReport hands the per-package listing to _composeLicenseListing. Counts and their first-seen order are unchanged.
  • resolveWorkspaceDependencies moves its nested visit closure into a private _WorkspaceDependencyCollector with one method each for visiting a package, expanding a workspace entry, parsing a member and ranking dependency types. The walk is still depth-first and the warnings fire under the same conditions.

Found by the full cognitive complexity scan in #1755, which tests VeryGoodOpenSource/very_good_workflows#520. Every function in the touched files now scores 15 or under. Behavior, log output, exit codes and public APIs are unchanged, and no tests were needed because the existing ones already reach every new branch.

Function Before After
PackagesCheckLicensesCommand.run 53 9
_composeReport 21 6
resolveWorkspaceDependencies 24 4

Part of a three-PR cleanup. Once all three merge, the full scan on #1755 passes:

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

🤖 Generated with Claude Code

Comment thread lib/src/commands/packages/commands/check/commands/licenses.dart Outdated
Comment thread lib/src/pubspec_workspace/pubspec_workspace.dart Outdated
Comment thread lib/src/commands/packages/commands/check/commands/licenses.dart Outdated
Comment thread lib/src/pubspec_workspace/pubspec_workspace.dart Outdated
Comment thread lib/src/pubspec_workspace/pubspec_workspace.dart Outdated
@marcossevilla
marcossevilla changed the base branch from main to refactor/cc-test-runner October 1, 2026 13:41
@marcossevilla
marcossevilla added this pull request to stack #1759 October 1, 2026 13:41
@marcossevilla

Copy link
Copy Markdown
Member Author

@vgvbot /review-pr

@unicoderbot

unicoderbot Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

I'll review the code and share my feedback shortly.

  • Preparing environment
    • Cloning repository
    • Setting up dev environment
  • Reviewing code
  • Posting review

Code review submitted — PR approved.

unicoderbot[bot]
unicoderbot Bot previously approved these changes Oct 1, 2026

@unicoderbot unicoderbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a well-executed refactor that reduces cognitive complexity in the check licenses command and workspace dependency resolver while preserving behavior. The decomposition into focused helpers (_validateLicenseOptions, _dependenciesToCheck, _collectLicenses, _retrieveLicenses, _bannedDependenciesFor, _composeLicenseListing) and the _WorkspaceDependencyCollector class is clean, and the private _LicenseRetrievalFailure exception elegantly consolidates the error-handling paths.

I verified behavior preservation on the subtle points:

  • _argResults.rest.firstOrNull ?? '.' matches the earlier length-guarded ternary because the > 1 check throws before this line.
  • The exhaustive switch on PubspecDependencyType to optionName is equivalent to the old four-clause || chain (and will fail to compile if the enum ever gains a case, which is a reasonable tradeoff for exhaustiveness).
  • First-seen ordering in _composeReport is preserved because licenseCount.entries iterates insertion order, matching the previous licenseTypes.toSet() LinkedHashSet behavior.
  • totalLicenseCount = licenseTypes.length equals the previous sum-of-counts since each occurrence contributes one to both.
  • Default bannedLicenseTypes to const <String>{} safely replaces the old != null && contains guard.
  • _WorkspaceDependencyCollector.classify() preserves the directMain > directDev > directOverridden precedence (first iteration with putIfAbsent on an empty map is equivalent to the old unconditional assignment).
  • Progress message format and the \n prefix on ignored-failure logs are both maintained.

No correctness issues found. The three complexity reductions in the PR description are real and the touched test surface should be unchanged.

@unicoderbot

unicoderbot Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Heads up — I'm auto-updating this PR by merging the latest refactor/cc-test-runner into this branch. No action needed; I'll comment again only if this update hits a conflict or an unexpected error.

1 similar comment
@unicoderbot

unicoderbot Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Heads up — I'm auto-updating this PR by merging the latest refactor/cc-test-runner into this branch. No action needed; I'll comment again only if this update hits a conflict or an unexpected error.

@unicoderbot

unicoderbot Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Heads up — I'm auto-updating this PR by merging the latest refactor/cc-test-runner into this branch. No action needed; I'll comment again only if this update hits a conflict or an unexpected error.

1 similar comment
@unicoderbot

unicoderbot Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Heads up — I'm auto-updating this PR by merging the latest refactor/cc-test-runner into this branch. No action needed; I'll comment again only if this update hits a conflict or an unexpected error.

Base automatically changed from refactor/cc-test-runner to main October 8, 2026 09:34
marcossevilla and others added 4 commits October 8, 2026 11:34
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
- Declare _LicenseRetrievalFailure with a primary constructor.
- Move the --dependency-type option name onto PubspecDependencyType.
- Extract the workspace collector into its own part file, declare its
  logger through a primary constructor, and inject it into
  resolveWorkspaceDependencies so it can be tested in isolation.
Every dependency is assigned a license set, with retrieval failures
recorded as unknown, so the nullable value type only forced dead null
checks in the banned-license and report helpers.
PubspecDependencyType.optionName and dependencyTypeAllowedValues listed
the same option names, so the CLI parser and very_good.yaml validator
could drift from the type filter. The allowed values are now built from
the enum, leaving optionName as the single source of truth.
@unicoderbot

unicoderbot Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Heads up — I'm auto-updating this PR by merging the latest main into this branch. No action needed; I'll comment again only if this update hits a conflict or an unexpected error.

@marcossevilla
marcossevilla merged commit 4c6232f into main Oct 8, 2026
26 checks passed
@marcossevilla
marcossevilla deleted the refactor/cc-packages branch October 8, 2026 12:50
@vgvbot vgvbot mentioned this pull request Oct 8, 2026
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.

2 participants