Skip to content

[v3][cnpj-dv] Create cnpj-dv package for CNPJ check digit calculation supporting alphanumeric format#37

Merged
juliolmuller merged 11 commits intomainfrom
feat/cnpj-dv
Mar 24, 2026
Merged

[v3][cnpj-dv] Create cnpj-dv package for CNPJ check digit calculation supporting alphanumeric format#37
juliolmuller merged 11 commits intomainfrom
feat/cnpj-dv

Conversation

@juliolmuller
Copy link
Collaborator

@juliolmuller juliolmuller commented Mar 23, 2026

Summary by CodeRabbit

  • New Features

    • New package for computing CNPJ check digits, including 2026 alphanumeric CNPJ support; lazy, cached digit computation and input validation.
  • Documentation

    • Added comprehensive README (English and Portuguese) and changelog with usage examples, algorithm details, constants, and installation guidance.
  • Tests

    • Added extensive test coverage for validation rules, computation, caching, and exception behavior.
  • Chores

    • Project manifest and MIT license added.

@juliolmuller juliolmuller requested a review from a team as a code owner March 23, 2026 20:38
@juliolmuller juliolmuller linked an issue Mar 23, 2026 that may be closed by this pull request
6 tasks
@coderabbitai
Copy link

coderabbitai bot commented Mar 23, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: c176e9d0-3b61-4612-9ea6-b83247fbb972

📥 Commits

Reviewing files that changed from the base of the PR and between 5962324 and 1b53ef2.

📒 Files selected for processing (1)
  • packages/cnpj-dv/src/CnpjCheckDigits.php

📝 Walkthrough

Walkthrough

Adds a new PHP package cnpj-dv implementing CNPJ check-digit computation (including 2026 alphanumeric support): core class, constants, exception hierarchy, docs (EN/PT), Composer and test configs, and comprehensive tests. Also small alignment changes in the existing cpf-dv package.

Changes

Cohort / File(s) Summary
CNPJ-DV Documentation
packages/cnpj-dv/CHANGELOG.md, packages/cnpj-dv/README.md, packages/cnpj-dv/README.pt.md, packages/cnpj-dv/LICENSE
Adds changelog, English and Portuguese READMEs describing usage, algorithm, validation rules, and MIT license.
CNPJ-DV Packaging & Test Config
packages/cnpj-dv/composer.json, packages/cnpj-dv/phpunit.xml, packages/cnpj-dv/tests/Pest.php
Composer manifest (php ^8.2, deps, autoload), PHPUnit config, and Pest bootstrap for the new package.
CNPJ-DV Core
packages/cnpj-dv/src/CnpjCheckDigits.php, packages/cnpj-dv/src/cnpj-dv.php
New CnpjCheckDigits class: input normalization (string
CNPJ-DV Exceptions
packages/cnpj-dv/src/Exceptions/...
CnpjCheckDigitsException.php, CnpjCheckDigitsTypeError.php, CnpjCheckDigitsInputTypeError.php, CnpjCheckDigitsInputLengthException.php, CnpjCheckDigitsInputInvalidException.php
Introduces abstract and concrete exception types (TypeError-based and domain exceptions) with typed public properties and formatted messages for input/type/length/invalid cases.
CNPJ-DV Tests & Mocks
packages/cnpj-dv/tests/Mocks/CnpjCheckDigitsWithCalculateSpy.php, packages/cnpj-dv/tests/Specs/*.spec.php
Extensive Pest specs covering constructor validation, exception semantics, computed properties (first, second, both, cnpj), caching behavior, formatted input parsing, and edge cases; includes a spy mock for calculate() calls.
CPF-DV Alignment
packages/cpf-dv/src/CpfCheckDigits.php, packages/cpf-dv/src/cpf-dv.php, packages/cpf-dv/tests/Specs/Package.spec.php
Minor refactor: constants made self-referential and input parsing/helpers adjusted; small test import change.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~75 minutes

Possibly related issues

Possibly related PRs

Suggested labels

enhancement

Poem

🐇 A tiny rabbit hops through code and clover,
New CNPJ digits — two more bytes to discover.
Letters and numbers in a modulo dance,
Cached and tidy — give the checks a chance! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: creating a new cnpj-dv package with CNPJ check digit calculation supporting alphanumeric format, which aligns with the comprehensive implementation shown in the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 84.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/cnpj-dv

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 6

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/cnpj-dv/README.md`:
- Around line 94-96: Update the emphasis style in the README entries for
CnpjCheckDigitsTypeError and CnpjCheckDigitsException by replacing the
underscore-wrapped `_abstract_` with asterisk-wrapped `*abstract*` so it matches
the rest of the document’s use of asterisks; locate the lines referencing
CnpjCheckDigitsTypeError, CnpjCheckDigitsInputTypeError, and
CnpjCheckDigitsException and change `_abstract_` to `*abstract*` for
consistency.

In `@packages/cnpj-dv/README.pt.md`:
- Around line 66-70: Replace the underscore-based emphasis with asterisk-based
emphasis for consistency: update the two occurrences of `_abstract_` to
`*abstract*` in the README entries for CnpjCheckDigitsTypeError and
CnpjCheckDigitsException so they match the document's existing asterisk emphasis
style.

In `@packages/cnpj-dv/src/CnpjCheckDigits.php`:
- Around line 175-178: The error currently throws CnpjCheckDigitsInputTypeError
with the entire $cnpjArray inside the foreach, which hides which element failed;
update the throw so it reports the offending member (use $item and ideally its
index) instead of $cnpjArray — locate the foreach over $cnpjArray and change the
thrown CnpjCheckDigitsInputTypeError payload/arguments to include the invalid
$item (and/or its key) and the expected type 'string or string[]' so the
exception shows the exact bad value.
- Around line 251-256: The method validateNonRepeatedDigits uses the bare
constant CNPJ_MIN_LENGTH which pulls a namespace-level constant instead of the
class constant; update the reference in validateNonRepeatedDigits to use
self::CNPJ_MIN_LENGTH (match other usages in the class) so the method relies on
the class-defined constant and avoids the hidden cross-file dependency.

In `@packages/cnpj-dv/src/Exceptions/CnpjCheckDigitsInputLengthException.php`:
- Line 36: Update the exception message in CnpjCheckDigitsInputLengthException
(the parent::__construct call) to use "characters" instead of "digits" so it
reflects alphanumeric CNPJ support; locate the constructor that currently builds
the message "CNPJ input {$fmtActual} does not contain {$minExpectedLength} to
{$maxExpectedLength} digits. Got {$fmtEvaluated}." and replace "digits" with
"characters" while preserving the formatted placeholders and punctuation.

In `@packages/cnpj-dv/tests/Specs/Exceptions.spec.php`:
- Around line 196-210: The spec and exception message hard-code "digits" which
is now incorrect for alphanumeric CNPJs; update the message wording in the
CnpjCheckDigitsInputLengthException (and its test in Specs/Exceptions.spec.php)
to use "characters" or "alphanumeric characters" instead of "digits" — locate
the CnpjCheckDigitsInputLengthException constructor/ getMessage implementation
and change the phrase that builds the message, then adjust the test's
$actualMessage string to match the new wording so
expect($exception->getMessage())->toBe($actualMessage) passes.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: bd476309-3096-4502-b49a-92755ba8193a

📥 Commits

Reviewing files that changed from the base of the PR and between ae7be7e and 5962324.

📒 Files selected for processing (21)
  • packages/cnpj-dv/CHANGELOG.md
  • packages/cnpj-dv/LICENSE
  • packages/cnpj-dv/README.md
  • packages/cnpj-dv/README.pt.md
  • packages/cnpj-dv/composer.json
  • packages/cnpj-dv/phpunit.xml
  • packages/cnpj-dv/src/CnpjCheckDigits.php
  • packages/cnpj-dv/src/Exceptions/CnpjCheckDigitsException.php
  • packages/cnpj-dv/src/Exceptions/CnpjCheckDigitsInputInvalidException.php
  • packages/cnpj-dv/src/Exceptions/CnpjCheckDigitsInputLengthException.php
  • packages/cnpj-dv/src/Exceptions/CnpjCheckDigitsInputTypeError.php
  • packages/cnpj-dv/src/Exceptions/CnpjCheckDigitsTypeError.php
  • packages/cnpj-dv/src/cnpj-dv.php
  • packages/cnpj-dv/tests/Mocks/CnpjCheckDigitsWithCalculateSpy.php
  • packages/cnpj-dv/tests/Pest.php
  • packages/cnpj-dv/tests/Specs/CnpjCheckDigits.spec.php
  • packages/cnpj-dv/tests/Specs/Exceptions.spec.php
  • packages/cnpj-dv/tests/Specs/Package.spec.php
  • packages/cpf-dv/src/CpfCheckDigits.php
  • packages/cpf-dv/src/cpf-dv.php
  • packages/cpf-dv/tests/Specs/Package.spec.php
💤 Files with no reviewable changes (1)
  • packages/cpf-dv/src/cpf-dv.php

@coderabbitai coderabbitai bot added the enhancement New minor or major features. label Mar 23, 2026
@juliolmuller juliolmuller merged commit 5f245a1 into main Mar 24, 2026
51 checks passed
@juliolmuller juliolmuller deleted the feat/cnpj-dv branch March 24, 2026 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New minor or major features.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create cnpj-dv

1 participant