[v2][cnpj-val] Upgrade to latest API and add support to alphanumeric CNPJ format#43
Conversation
BREAKING CHANGE: update package resources namespaces.
BREAKING CHANGE: add support to alphanumeric CNPJ and validation options.
BREAKING CHANGE: add support to alphanumeric CNPJ and validation options.
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe PR refactors the ChangesCNPJ Validator 2.0.0 Refactoring
Sequence Diagram(s)sequenceDiagram
participant Client
participant cnpj_val
participant CnpjValidator
participant CnpjValidatorOptions
participant CnpjCheckDigits
Client->>cnpj_val: cnpj_val(input, options, type, caseSensitive)
cnpj_val->>CnpjValidator: isValid(input, options, type, caseSensitive)
CnpjValidator->>CnpjValidator: toStringInput(input)
CnpjValidator->>CnpjValidatorOptions: merge per-call overrides
CnpjValidator->>CnpjValidator: sanitize based on type/case
CnpjValidator->>CnpjCheckDigits: computeCheckDigits(sanitized)
CnpjCheckDigits-->>CnpjValidator: digits
CnpjValidator-->>cnpj_val: bool result
cnpj_val-->>Client: bool
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 8
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/cnpj-val/composer.json`:
- Line 57: The composer.json for package `packages/cnpj-val` still lists the
dependency "lacus/cnpj-gen": "^1.0" but the codebase shows no usage of symbols
like `CnpjGeneratorVerifierDigit` or `Lacus\\CnpjGen`; remove the unused
dependency from the `require` section of `composer.json` in `packages/cnpj-val`,
run `composer update` (or `composer remove lacus/cnpj-gen`) to update
composer.lock and vendor, and run the package tests to confirm nothing breaks;
if any references are found, replace them with the new v2 engine equivalents
before removing.
In `@packages/cnpj-val/README.md`:
- Line 10: Replace the fragile github.com/user-attachments link for the "new
alphanumeric CNPJ format" in README.md with the canonical Receita Federal URL
used elsewhere in the repo (use the exact URL already present in other docs),
and make the identical substitution in README.pt.md so both READMEs point to the
official Receita Federal reference for the alphanumeric CNPJ format.
In `@packages/cnpj-val/src/cnpj-val.php`:
- Line 5: Add a compatibility shim for the old entrypoint by reintroducing the
function in the original namespace Lacus\CnpjVal named cnpj_val that simply
delegates to the moved implementation in Lacus\BrUtils\Cnpj (e.g., call
\Lacus\BrUtils\Cnpj\cnpj_val($cnpjString) or the equivalent new function/class
method). Ensure the shim is declared in namespace Lacus\CnpjVal and accepts the
same signature/return value as the original so existing callers in
packages/cnpj-utils/src/cnpj_utils.php continue to work.
In `@packages/cnpj-val/src/CnpjValidator.php`:
- Around line 91-104: The per-call $options parameter is not validated and
non-array/non-CnpjValidatorOptions values are silently ignored; update the
method that builds $actualOptions (the code calling new CnpjValidatorOptions and
using $options ?? []) to explicitly validate $options: if $options is an
instance of CnpjValidatorOptions use it directly, else if it is an array use it
in the overrides merge, otherwise throw an InvalidArgumentException (or the
repository's standard exception) to match the documented throws contract; keep
usage of toStringInput($cnpjInput) and the existing overrides for 'type' and
'caseSensitive' when constructing or merging into CnpjValidatorOptions.
- Around line 109-117: The sanitization regex currently uses the
case-insensitive flag ('/i') unconditionally, which violates the intended
case-sensitive behavior controlled by $actualOptions->caseSensitive in
CnpjValidator; change the sanitization so that when
$actualOptions->caseSensitive is true you use a case-sensitive pattern (e.g.
'/[^0-9A-Z]/' without the 'i' flag) and only use the '/i' flag when
caseSensitive is false (or rely on the earlier strtoupper branch), applying this
conditional pattern to the non-numeric branch that assigns $sanitizedCnpj (and
keep the numeric branch using '/[^0-9]/').
In `@packages/cnpj-val/src/CnpjValidatorOptions.php`:
- Around line 178-183: The setCaseSensitive method currently coerces any value
to bool with (bool) allowing invalid types like the string "false" to become
true; change validation in setCaseSensitive to accept only actual booleans (or
null) and throw CnpjValidatorOptionsTypeError for any other runtime types, using
self::DEFAULT_CASE_SENSITIVE when $value is null, and then assign the validated
boolean to $this->options['caseSensitive']; reference the setCaseSensitive
method, self::DEFAULT_CASE_SENSITIVE, CnpjValidatorOptionsTypeError, and
$this->options['caseSensitive'] when implementing the check.
In `@packages/cnpj-val/tests/Specs/CnpjValidator.spec.php`:
- Around line 190-208: Update the test description string in the failing spec:
change the 'it' description from "returns `false` for a CNPJ with all digits the
same" to "returns `false` for a CNPJ with all base digits the same" so it
accurately reflects that the test uses repeated 12-digit bases (the $prefix
values) and varies the final check digits; adjust the string passed to the
it(...) call near the anonymous function that uses $prefix and $isValid.
In `@packages/cnpj-val/tests/Specs/Exceptions.spec.php`:
- Line 115: The test constructs CnpjValidatorOptionsTypeError with an invalid
optionName 'hidden', violating the constructor PHPDoc which expects
'caseSensitive'|'type' (and parseCnpjValidationType treats optionName as
'type'); update the failing test cases in the Specs/Exceptions spec to pass a
valid option name (use 'type' or 'caseSensitive' as appropriate) or, if you
intend to allow unknown option names, widen the PHPDoc on
CnpjValidatorOptionsTypeError::__construct (and adjust
CnpjValidatorOptions::parseCnpjValidationType annotations) to accept a broader
string union so PHPStan no longer complains.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 44d23b1c-fcc2-42d3-af0b-f94e805b08df
📒 Files selected for processing (23)
.php-stan.config.neonpackages/cnpj-val/.pest.config.xmlpackages/cnpj-val/CHANGELOG.mdpackages/cnpj-val/README.mdpackages/cnpj-val/README.pt.mdpackages/cnpj-val/composer.jsonpackages/cnpj-val/src/CnpjValidator.phppackages/cnpj-val/src/CnpjValidatorOptions.phppackages/cnpj-val/src/Enums/CnpjValidationType.phppackages/cnpj-val/src/Exceptions/CnpjValidatorException.phppackages/cnpj-val/src/Exceptions/CnpjValidatorInputTypeError.phppackages/cnpj-val/src/Exceptions/CnpjValidatorOptionTypeInvalidException.phppackages/cnpj-val/src/Exceptions/CnpjValidatorOptionsTypeError.phppackages/cnpj-val/src/Exceptions/CnpjValidatorTypeError.phppackages/cnpj-val/src/cnpj-val.phppackages/cnpj-val/tests/CnpjValidatorClassTest.phppackages/cnpj-val/tests/CnpjValidatorFunctionTest.phppackages/cnpj-val/tests/CnpjValidatorTestCases.phppackages/cnpj-val/tests/Pest.phppackages/cnpj-val/tests/Specs/CnpjValidator.spec.phppackages/cnpj-val/tests/Specs/CnpjValidatorOptions.spec.phppackages/cnpj-val/tests/Specs/Exceptions.spec.phppackages/cnpj-val/tests/Specs/cnpj-val.spec.php
💤 Files with no reviewable changes (3)
- packages/cnpj-val/tests/CnpjValidatorFunctionTest.php
- packages/cnpj-val/tests/CnpjValidatorClassTest.php
- packages/cnpj-val/tests/CnpjValidatorTestCases.php
Fox as per CodeRabbit AI review comment at #43 (comment). Co-authored-by: CodeRabbit AI <support@coderabbit.ai>
Fox as per CodeRabbit AI review comment at #43 (comment). Co-authored-by: CodeRabbit AI <support@coderabbit.ai>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
There was a problem hiding this comment.
Actionable comments posted: 1
♻️ Duplicate comments (2)
packages/cnpj-val/README.md (1)
10-10:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winissue (if-minor): Use the canonical Receita Federal URL for alphanumeric CNPJ reference.
The
github.com/user-attachments/...link is fragile and less durable for long-term documentation. Use the official Receita Federal URL for consistency and reliability.Proposed diff
-> 🚀 **Full support for the [new alphanumeric CNPJ format](https://github.com/user-attachments/files/23937961/calculodvcnpjalfanaumerico.pdf).** +> 🚀 **Full support for the [new alphanumeric CNPJ format](https://www.gov.br/receitafederal/pt-br/assuntos/noticias/2023/julho/cnpj-alfa-numerico).**🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/cnpj-val/README.md` at line 10, Replace the fragile GitHub attachment link in the README line containing "🚀 **Full support for the [new alphanumeric CNPJ format]..." with the official Receita Federal reference URL for the alphanumeric CNPJ format; update the markdown link target so the visible text stays the same but the href points to the canonical Receita Federal page (use the Receita Federal's published URL for the alphanumeric CNPJ specification).packages/cnpj-val/README.pt.md (1)
3-3:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winissue (if-minor): Use the canonical Receita Federal URL for alphanumeric CNPJ reference.
For consistency with the English README and long-term documentation durability, replace the
github.com/user-attachments/...link with the official Receita Federal URL.Proposed diff
-> 🚀 **Suporte total ao [novo formato alfanumérico de CNPJ](https://github.com/user-attachments/files/23937961/calculodvcnpjalfanaumerico.pdf).** +> 🚀 **Suporte total ao [novo formato alfanumérico de CNPJ](https://www.gov.br/receitafederal/pt-br/assuntos/noticias/2023/julho/cnpj-alfa-numerico).**🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/cnpj-val/README.pt.md` at line 3, Replace the non-canonical GitHub link used in the README line that mentions "novo formato alfanumérico de CNPJ" with the official Receita Federal reference URL; edit the text containing "https://github.com/user-attachments/files/23937961/calculodvcnpjalfanaumerico.pdf" and swap it for the canonical Receita Federal link so the README.pt.md uses the authoritative source.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/cnpj-val/README.md`:
- Line 143: The README sentence "cnpj_val() run the validation on a
`CnpjValidator` instance..." has a subject-verb agreement error; update the
sentence for the `cnpj_val()` symbol to read "cnpj_val() runs the validation on
a `CnpjValidator` instance with the same arguments passed to the function. Use
named arguments for options:" so the verb agrees with the singular subject
`cnpj_val()`.
---
Duplicate comments:
In `@packages/cnpj-val/README.md`:
- Line 10: Replace the fragile GitHub attachment link in the README line
containing "🚀 **Full support for the [new alphanumeric CNPJ format]..." with
the official Receita Federal reference URL for the alphanumeric CNPJ format;
update the markdown link target so the visible text stays the same but the href
points to the canonical Receita Federal page (use the Receita Federal's
published URL for the alphanumeric CNPJ specification).
In `@packages/cnpj-val/README.pt.md`:
- Line 3: Replace the non-canonical GitHub link used in the README line that
mentions "novo formato alfanumérico de CNPJ" with the official Receita Federal
reference URL; edit the text containing
"https://github.com/user-attachments/files/23937961/calculodvcnpjalfanaumerico.pdf"
and swap it for the canonical Receita Federal link so the README.pt.md uses the
authoritative source.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 53b480f5-e954-4db3-b2b6-7c214be1b742
📒 Files selected for processing (7)
packages/cnpj-val/CHANGELOG.mdpackages/cnpj-val/README.mdpackages/cnpj-val/README.pt.mdpackages/cnpj-val/composer.jsonpackages/cnpj-val/src/CnpjValidator.phppackages/cnpj-val/src/cnpj-val.phppackages/cnpj-val/tests/Specs/Exceptions.spec.php
💤 Files with no reviewable changes (1)
- packages/cnpj-val/composer.json
Fix as per CodeRabbit AI review at #43 (comment). Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
Fox as per CodeRabbit AI review comment at LacusSolutions/br-utils-php#43 (comment). Co-authored-by: CodeRabbit AI <support@coderabbit.ai>
Fox as per CodeRabbit AI review comment at LacusSolutions/br-utils-php#43 (comment). Co-authored-by: CodeRabbit AI <support@coderabbit.ai>
Fix as per CodeRabbit AI review at LacusSolutions/br-utils-php#43 (comment). Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Summary by CodeRabbit
New Features
Breaking Changes
Documentation
Tests
Chores