[v2][br-utils] Upgrade to latest API and add support to alphanumeric CNPJ format - #52
Conversation
BREAKING CHANGE: namespaces for test files have been simplified for consistency with latest versions of packages
BREAKING CHANGE: renname namespace of CPF-related resources, to match latest standards.
Introduced new classes for CPF formatting, generation, and utility functions, extending existing base classes for enhanced functionality.
…ling BREAKING CHANGE: update resource namespace, constructor arguments order and constructor arguments types. Co-authored-by: Cursor Agent <cursoragent@cursor.ai>
Co-authored-by: Cursor Agent <cursoragent@cursor.ai>
Co-authored-by: Cursor Agent <cursoragent@cursor.ai>
Co-authored-by: Cursor Agent <cursoragent@cursor.ai>
…e structure Co-authored-by: Cursor Agent <cursoragent@cursor.ai>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
📝 WalkthroughWalkthroughThis PR modernizes br-utils to v2: it requires PHP ^8.2 and strict types, refactors the BrUtils facade (namespace and constructor), reorganizes CPF utilities and helpers, removes local CNPJ wrappers, migrates tests to Pest (with legacy PHPUnit namespaces), and updates README/CHANGELOG (EN/PT). Changesbr-utils Version 2.0.0 Upgrade
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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: 5
🤖 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/br-utils/composer.json`:
- Around line 54-58: The test:cov script in composer.json currently writes both
Pest and PHPUnit HTML coverage to the same output directory, causing the second
report to overwrite the first; update the test:cov array so the Pest HTML
coverage uses a different directory than PHPUnit (for example use distinct
folders like coverage/pest-html and coverage/phpunit-html or include timestamped
subfolders) by modifying the two commands that run pest with --coverage-html and
phpunit with --coverage-html in the test:cov script so each produces its own
unique output directory.
In `@packages/br-utils/src/BrUtils.php`:
- Around line 42-57: Update the PHPDoc for the $cpf parameter so the nested
formatter and generator option element types are nullable to match
CpfUtils/CpfFormatter/CpfGenerator behavior: in the docblock for the
CpfUtils|array $cpf parameter (referencing CpfFormatterOptions and
CpfGeneratorOptions and the 'formatter' and 'generator' keys) add |null to any
option element types currently documented as non-nullable (e.g., make escape?:
bool|null, hidden?: bool|null, hiddenKey?: string|null, hiddenStart?: int|null,
hiddenEnd?: int|null, dotKey?: string|null, dashKey?: string|null, onFail?:
Closure|null and format?: bool|null, prefix?: string|null) so the PHPDoc aligns
with the constructors that accept nullable values.
In `@packages/br-utils/src/BrUtils/Cpf/cpf-fmt.php`:
- Around line 19-26: The PHPDoc `@param` list in
packages/br-utils/src/BrUtils/Cpf/cpf-fmt.php is out of order compared to the
actual function signature (parameters like $escape and
$hidden/$hiddenKey/$hiddenStart/$hiddenEnd are misaligned); update the PHPDoc
for the cpf formatting function (cpf_fmt or the function declared in this file)
so the `@param` entries appear in the exact same order and types as the function
signature (e.g., place $escape where it appears in the signature and then list
$hidden, $hiddenKey, $hiddenStart, $hiddenEnd, $dotKey, $dashKey, $onFail
accordingly), and apply the same ordering fix to the duplicate block covering
lines 31-39.
In `@packages/br-utils/src/BrUtils/Cpf/CpfFormatterOptions.php`:
- Around line 9-11: Override the merge() method in CpfFormatterOptions to
preserve the wrapper class instead of instantiating the base type: implement
CpfFormatterOptions::merge(...) to perform the same merge logic but instantiate
the result with new static(...) (or by delegating to parent::merge and
re-wrapping into static) so callers that expect
Lacus\BrUtils\Cpf\CpfFormatterOptions keep that concrete type; reference the
existing BaseCpfFormatterOptions::merge and Lacus\CpfFmt\CpfFormatterOptions
constructors to replicate the merge behavior while returning the wrapper class.
In `@packages/br-utils/tests/specs/BrUtils.spec.php`:
- Around line 185-197: The failing static analysis is caused by passing
intentionally invalid typed literals into the BrUtils constructor in the
negative tests; modify the three offending expect(...) calls that instantiate
new BrUtils(...) (the ones asserting CnpjGeneratorOptionTypeInvalidException,
CnpjGeneratorOptionsTypeError, and CnpjValidatorOptionTypeInvalidException) to
avoid PHPStan type checks by either prefixing each offending line with a
localized PHPStan ignore comment (e.g. /** `@phpstan-ignore-next-line` */) or by
casting the invalid argument to mixed (e.g. new BrUtils(cnpj: (mixed)
['generator' => ['type' => 'invalid']]) ), so the runtime assertions still run
but static analysis no longer flags those calls.
🪄 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: 68cd5c45-fecd-46c1-a8b6-a6b1c996dfb4
📒 Files selected for processing (41)
AGENTS.mdpackages/br-utils/.pest.config.xmlpackages/br-utils/.phpunit.config.xmlpackages/br-utils/CHANGELOG.mdpackages/br-utils/README.mdpackages/br-utils/README.pt.mdpackages/br-utils/composer.jsonpackages/br-utils/src/BrUtils.phppackages/br-utils/src/BrUtils/Cpf/CpfFormatter.phppackages/br-utils/src/BrUtils/Cpf/CpfFormatterOptions.phppackages/br-utils/src/BrUtils/Cpf/CpfGenerator.phppackages/br-utils/src/BrUtils/Cpf/CpfGeneratorOptions.phppackages/br-utils/src/BrUtils/Cpf/CpfUtils.phppackages/br-utils/src/BrUtils/Cpf/CpfValidator.phppackages/br-utils/src/BrUtils/Cpf/cpf-fmt.phppackages/br-utils/src/BrUtils/Cpf/cpf-gen.phppackages/br-utils/src/BrUtils/Cpf/cpf-val.phppackages/br-utils/src/Cnpj/CnpjFormatter.phppackages/br-utils/src/Cnpj/CnpjGenerator.phppackages/br-utils/src/Cnpj/CnpjValidator.phppackages/br-utils/src/Cnpj/cnpj_utils.phppackages/br-utils/src/CnpjUtils.phppackages/br-utils/src/Cpf/cpf_utils.phppackages/br-utils/tests/BrUtilsTest.phppackages/br-utils/tests/Cnpj/CnpjFormatterClassTest.phppackages/br-utils/tests/Cnpj/CnpjFormatterFunctionTest.phppackages/br-utils/tests/Cnpj/CnpjGeneratorClassTest.phppackages/br-utils/tests/Cnpj/CnpjGeneratorFunctionTest.phppackages/br-utils/tests/Cnpj/CnpjValidatorClassTest.phppackages/br-utils/tests/Cnpj/CnpjValidatorFunctionTest.phppackages/br-utils/tests/CnpjUtilsTest.phppackages/br-utils/tests/Pest.phppackages/br-utils/tests/phpunit/Cpf/CpfFormatterClassTest.phppackages/br-utils/tests/phpunit/Cpf/CpfFormatterFunctionTest.phppackages/br-utils/tests/phpunit/Cpf/CpfGeneratorClassTest.phppackages/br-utils/tests/phpunit/Cpf/CpfGeneratorFunctionTest.phppackages/br-utils/tests/phpunit/Cpf/CpfUtilsTest.phppackages/br-utils/tests/phpunit/Cpf/CpfValidatorClassTest.phppackages/br-utils/tests/phpunit/Cpf/CpfValidatorFunctionTest.phppackages/br-utils/tests/specs/BrUtils.spec.phppackages/cnpj-utils/CHANGELOG.md
💤 Files with no reviewable changes (14)
- packages/br-utils/tests/Cnpj/CnpjFormatterFunctionTest.php
- packages/br-utils/src/Cnpj/CnpjFormatter.php
- packages/br-utils/tests/Cnpj/CnpjGeneratorFunctionTest.php
- packages/br-utils/tests/Cnpj/CnpjValidatorFunctionTest.php
- packages/br-utils/tests/CnpjUtilsTest.php
- packages/br-utils/tests/BrUtilsTest.php
- packages/br-utils/src/Cnpj/cnpj_utils.php
- packages/br-utils/src/Cnpj/CnpjValidator.php
- packages/br-utils/src/CnpjUtils.php
- packages/br-utils/tests/Cnpj/CnpjValidatorClassTest.php
- packages/br-utils/tests/Cnpj/CnpjGeneratorClassTest.php
- packages/br-utils/src/Cnpj/CnpjGenerator.php
- packages/br-utils/src/Cpf/cpf_utils.php
- packages/br-utils/tests/Cnpj/CnpjFormatterClassTest.php
Fix as per @coderabbitai review comment at #52 (comment). Co-authored-by: CodeRabbit AI <noreply@coderabbit.ai>
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
🤖 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/br-utils/src/BrUtils/Cpf/cpf-fmt.php`:
- Line 28: Update the PHPDoc for cpf_fmt() to accurately describe when
InvalidArgumentException is thrown: state that \InvalidArgumentException is
thrown by CpfFormatterOptions::setHiddenRange() when hiddenStart/hiddenEnd are
out of allowed range (not for generic "invalid type"), and clarify that invalid
CPF length is handled by the onFail callback rather than thrown; locate the doc
block above the cpf_fmt() function and replace the current "`@throws`
InvalidArgumentException If any option has an invalid type." with the corrected
description.
🪄 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: 6f3924a5-51af-480a-8df9-b7fac73e3189
📒 Files selected for processing (4)
packages/br-utils/CHANGELOG.mdpackages/br-utils/src/BrUtils/Cpf/CpfUtils.phppackages/br-utils/src/BrUtils/Cpf/cpf-fmt.phppackages/br-utils/tests/specs/BrUtils.spec.php
Fix as per @coderabbitai review comment at LacusSolutions/br-utils-php#52 (comment). Co-authored-by: CodeRabbit AI <noreply@coderabbit.ai>
Summary by CodeRabbit
Documentation
New Features
Improvements
Tests