Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6c6329b
chore(cnpj-fmt): update package description
juliolmuller Mar 25, 2026
5eeee4d
chore(cnpj-fmt): migrate test runner from PhpUnit to Pest
juliolmuller Mar 25, 2026
60c138c
refactor(cnpj-fmt): update project namespaces
juliolmuller Mar 25, 2026
f6fb6cb
feat(cnpj-fmt): create errors and exceptions for package
juliolmuller Mar 28, 2026
2101697
feat(cnpj-fmt): create resource to manage formatting options
juliolmuller Mar 28, 2026
d4b973d
feat(cnpj-fmt): create CNPJ formatter
juliolmuller Mar 28, 2026
2b35ba9
feat(cnpj-fmt): update formatter helper function
juliolmuller Mar 28, 2026
a3abb45
chore(cnpj-fmt): drop stale test files
juliolmuller Mar 28, 2026
b3103c5
feat(cpf-dv): add method to return error/exception name
juliolmuller Mar 28, 2026
11f3795
docs(cpf-dv): update changelog
juliolmuller Mar 28, 2026
9fca0eb
feat(cnpj-dv): add method to return error/exception name
juliolmuller Mar 28, 2026
e6f8634
docs(cnpj-dv): update changelog
juliolmuller Mar 28, 2026
c986f77
docs(cnpj-fmt): update README documentation
juliolmuller Mar 28, 2026
880a0c1
docs(cnpj-fmt): feat create Portuguese version of README documentation
juliolmuller Mar 28, 2026
caa6256
docs(cnpj-fmt): create CHANGELOG documentation
juliolmuller Mar 28, 2026
96bcdfb
docs(cnpj-fmt): improve PhpDocs as per linter comment
juliolmuller Mar 28, 2026
287f088
docs(cnpj-dv): fix grammar
juliolmuller Mar 29, 2026
2fb0958
docs(cpf-dv): fix API reference
juliolmuller Mar 29, 2026
8b4ac45
docs(cnpj-dv): fix grammar
juliolmuller Mar 29, 2026
5e571e0
docs(cpf-dv): fix grammar
juliolmuller Mar 29, 2026
70c18dd
docs(cnpj-fmt): fix grammar
juliolmuller Mar 29, 2026
a2d95a9
docs(cnpj-fmt): fix typo
juliolmuller Mar 29, 2026
4d79f92
docs(cnpj-fmt): fix grammar
juliolmuller Mar 29, 2026
168a863
docs(cnpj-dv): fix grammar
juliolmuller Mar 29, 2026
79b9059
fix(cnpj-fmt): fix erroneous class name logic
juliolmuller Mar 29, 2026
328a3c8
style(cnpj-dv): remove extra blank line
juliolmuller Mar 29, 2026
14127cb
style(cnpj-fmt): remove extra blank line
juliolmuller Mar 29, 2026
37e520d
docs(cpf-dv): fix grammar
juliolmuller Mar 29, 2026
2630c09
test(cnpj-fmt): fix misleading test case
juliolmuller Mar 30, 2026
32c9dbd
docs(cnpj-fmt): fix typo
juliolmuller Mar 30, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/cnpj-dv/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# lacus/cnpj-dv

## 1.1.0

### New Features

- a90dd0a89b18a19bbb8ad72200d65df01c465fdb Created **`getName()`** to all package-specific errors and exceptions. Now `CnpjCheckDigitsException`, `CnpjCheckDigitsTypeError` and all their subclasses can return their class names without namespaces. This change is an API alignment across all **BR Utils** initiatives.

## 1.0.0

### 🚀 Stable Version Released!
Expand Down
2 changes: 1 addition & 1 deletion packages/cnpj-dv/README.pt.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ A classe `CnpjCheckDigits` aceita múltiplos formatos de entrada:

**String:** dígitos e/ou letras crus, ou CNPJ formatado (ex.: `91.415.732/0007-93`, `MG.KGM.J9X/0001-68`). Caracteres não alfanuméricos são removidos; letras minúsculas viram maiúsculas.

**Array de strings:** cada elemento deve ser string; os valores são concatenados e interpretados como uma única string (ex.: `['9','1','4',…]`, `['9141','5732','0007']`, `['MG','KGM','J9X','0001']`). Elementos que não são string não são permitidos.
**Array de strings:** cada elemento deve ser string; os valores são concatenados e interpretados como uma única string (ex.: `['9','1','4',…]`, `['9141','5732','0007']`, `['MG','KGM','J9X','0001']`). Elementos que não são strings não são permitidos.

### Erros e exceções

Expand Down
1 change: 0 additions & 1 deletion packages/cnpj-dv/src/CnpjCheckDigits.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class CnpjCheckDigits
/** Maximum number of characters accepted as input for the CNPJ check digits calculation. */
public const CNPJ_MAX_LENGTH = CNPJ_MAX_LENGTH;


/** @var list<string> */
private array $cnpjChars;
private ?int $cachedFirstDigit = null;
Expand Down
15 changes: 15 additions & 0 deletions packages/cnpj-dv/src/Exceptions/CnpjCheckDigitsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Lacus\BrUtils\Cnpj\Exceptions;

use Exception;
use ReflectionClass;

/**
* Base exception for all `cnpj-dv` rules-related errors.
Expand All @@ -16,4 +17,18 @@
*/
abstract class CnpjCheckDigitsException extends Exception
{
public function __construct(string $message)
{
parent::__construct($message);
}

/**
* Get the short class name of the exception instance.
*/
public function getName(): string
{
$thisReflection = new ReflectionClass($this);

return $thisReflection->getShortName();
}
}
16 changes: 12 additions & 4 deletions packages/cnpj-dv/src/Exceptions/CnpjCheckDigitsTypeError.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Lacus\BrUtils\Cnpj\Exceptions;

use Throwable;
use ReflectionClass;
use TypeError;

/**
Expand All @@ -24,12 +24,20 @@ public function __construct(
string $actualType,
string $expectedType,
string $message,
int $code = 0,
?Throwable $previous = null,
) {
parent::__construct($message, $code, $previous);
parent::__construct($message);
$this->actualInput = $actualInput;
$this->actualType = $actualType;
$this->expectedType = $expectedType;
}

/**
* Get the short class name of the error instance.
*/
Comment thread
coderabbitai[bot] marked this conversation as resolved.
public function getName(): string
{
$thisReflection = new ReflectionClass($this);

return $thisReflection->getShortName();
}
}
124 changes: 63 additions & 61 deletions packages/cnpj-dv/tests/Specs/Exceptions.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,60 @@

namespace Lacus\BrUtils\Cnpj\Tests\Specs;

use Exception;
use Lacus\BrUtils\Cnpj\Exceptions\CnpjCheckDigitsException;
use Lacus\BrUtils\Cnpj\Exceptions\CnpjCheckDigitsInputInvalidException;
use Lacus\BrUtils\Cnpj\Exceptions\CnpjCheckDigitsInputLengthException;
use Lacus\BrUtils\Cnpj\Exceptions\CnpjCheckDigitsInputTypeError;
use Lacus\BrUtils\Cnpj\Exceptions\CnpjCheckDigitsTypeError;
use TypeError;

final class TestCnpjCheckDigitsTypeError extends CnpjCheckDigitsTypeError
{
public function __construct()
describe('CnpjCheckDigitsTypeError', function () {
final class TestTypeError extends CnpjCheckDigitsTypeError
{
parent::__construct(123, 'number', 'string', 'some error');
}
}

final class TestCnpjCheckDigitsException extends CnpjCheckDigitsException
{
}

describe('CnpjCheckDigitsTypeError', function () {
describe('when instantiated through a subclass', function () {
it('is an instance of TypeError', function () {
$error = new TestCnpjCheckDigitsTypeError();
$error = new TestTypeError(123, 'number', 'string', 'some error');

expect($error)->toBeInstanceOf(TypeError::class);
});

it('is an instance of CnpjCheckDigitsTypeError', function () {
$error = new TestCnpjCheckDigitsTypeError();
$error = new TestTypeError(123, 'number', 'string', 'some error');

expect($error)->toBeInstanceOf(CnpjCheckDigitsTypeError::class);
});

it('has the correct class name', function () {
$error = new TestCnpjCheckDigitsTypeError();

expect($error::class)->toBe(TestCnpjCheckDigitsTypeError::class);
});

it('sets the `actualInput` property', function () {
$error = new TestCnpjCheckDigitsTypeError();
$error = new TestTypeError(123, 'number', 'string', 'some error');

expect($error->actualInput)->toBe(123);
});

it('sets the `actualType` property', function () {
$error = new TestCnpjCheckDigitsTypeError();
$error = new TestTypeError(123, 'number', 'string', 'some error');

expect($error->actualType)->toBe('number');
});

it('sets the `expectedType` property', function () {
$error = new TestCnpjCheckDigitsTypeError();
$error = new TestTypeError(123, 'number', 'string', 'some error');

expect($error->expectedType)->toBe('string');
});

it('has a `message` property', function () {
$error = new TestCnpjCheckDigitsTypeError();
it('has the correct message', function () {
$exception = new TestTypeError(123, 'number', 'string', 'some error');

expect($error->getMessage())->toBe('some error');
expect($exception->getMessage())->toBe('some error');
});

it('has the correct name', function () {
$exception = new TestTypeError(123, 'number', 'string', 'some error');

expect($exception->getName())->toBe('TestTypeError');
});
});
});
Expand All @@ -83,17 +76,10 @@ final class TestCnpjCheckDigitsException extends CnpjCheckDigitsException
expect($error)->toBeInstanceOf(CnpjCheckDigitsTypeError::class);
});

it('has the correct class name', function () {
$error = new CnpjCheckDigitsInputTypeError(123, 'string');

expect($error::class)->toBe(CnpjCheckDigitsInputTypeError::class);
});

it('sets the `actualInput` property', function () {
$input = 123;
$error = new CnpjCheckDigitsInputTypeError($input, 'string');
$error = new CnpjCheckDigitsInputTypeError(123, 'string');

expect($error->actualInput)->toBe($input);
expect($error->actualInput)->toBe(123);
});

it('sets the `actualType` property', function () {
Expand All @@ -108,43 +94,56 @@ final class TestCnpjCheckDigitsException extends CnpjCheckDigitsException
expect($error->expectedType)->toBe('string or string[]');
});

it('generates a message describing the error', function () {
it('has the correct message', function () {
$actualInput = 123;
$actualType = 'integer number';
$expectedType = 'string[]';
$actualMessage = "CNPJ input must be of type {$expectedType}. Got {$actualType}.";

$error = new CnpjCheckDigitsInputTypeError($actualInput, $expectedType);
$error = new CnpjCheckDigitsInputTypeError(
$actualInput,
$expectedType,
);

expect($error->getMessage())->toBe($actualMessage);
});

it('has the correct name', function () {
$error = new CnpjCheckDigitsInputTypeError(123, 'string');

expect($error->getName())->toBe('CnpjCheckDigitsInputTypeError');
});
});
});

describe('CnpjCheckDigitsException', function () {
final class TestException extends CnpjCheckDigitsException
{
}

describe('when instantiated through a subclass', function () {
it('is an instance of Exception', function () {
$exception = new TestCnpjCheckDigitsException('some error');
$exception = new TestException('some error');

expect($exception)->toBeInstanceOf(\Exception::class);
expect($exception)->toBeInstanceOf(Exception::class);
});

it('is an instance of CnpjCheckDigitsException', function () {
$exception = new TestCnpjCheckDigitsException('some error');
$exception = new TestException('some error');

expect($exception)->toBeInstanceOf(CnpjCheckDigitsException::class);
});

it('has the correct class name', function () {
$exception = new TestCnpjCheckDigitsException('some error');
it('has the correct message', function () {
$exception = new TestException('some exception');

expect($exception::class)->toBe(TestCnpjCheckDigitsException::class);
expect($exception->getMessage())->toBe('some exception');
});

it('has a `message` property', function () {
$exception = new TestCnpjCheckDigitsException('some error');
it('has the correct name', function () {
$exception = new TestException('some error');

expect($exception->getMessage())->toBe('some error');
expect($exception->getName())->toBe('TestException');
});
});
});
Expand All @@ -154,7 +153,7 @@ final class TestCnpjCheckDigitsException extends CnpjCheckDigitsException
it('is an instance of Exception', function () {
$exception = new CnpjCheckDigitsInputLengthException('1.2.3.4.5', '12345', 12, 14);

expect($exception)->toBeInstanceOf(\Exception::class);
expect($exception)->toBeInstanceOf(Exception::class);
});

it('is an instance of CnpjCheckDigitsException', function () {
Expand All @@ -163,12 +162,6 @@ final class TestCnpjCheckDigitsException extends CnpjCheckDigitsException
expect($exception)->toBeInstanceOf(CnpjCheckDigitsException::class);
});

it('has the correct class name', function () {
$exception = new CnpjCheckDigitsInputLengthException('1.2.3.4.5', '12345', 12, 14);

expect($exception::class)->toBe(CnpjCheckDigitsInputLengthException::class);
});

it('sets the `actualInput` property', function () {
$exception = new CnpjCheckDigitsInputLengthException('1.2.3.4.5', '12345', 12, 14);

Expand All @@ -193,7 +186,7 @@ final class TestCnpjCheckDigitsException extends CnpjCheckDigitsException
expect($exception->maxExpectedLength)->toBe(14);
});

it('generates a message describing the exception', function () {
it('has the correct message', function () {
$actualInput = '1.2.3.4.5';
$evaluatedInput = '12345';
$minExpectedLength = 12;
Expand All @@ -209,6 +202,12 @@ final class TestCnpjCheckDigitsException extends CnpjCheckDigitsException

expect($exception->getMessage())->toBe($actualMessage);
});

it('has the correct name', function () {
$exception = new CnpjCheckDigitsInputLengthException('1.2.3.4.5', '12345', 12, 14);

expect($exception->getName())->toBe('CnpjCheckDigitsInputLengthException');
});
});
});

Expand All @@ -217,7 +216,7 @@ final class TestCnpjCheckDigitsException extends CnpjCheckDigitsException
it('is an instance of Exception', function () {
$exception = new CnpjCheckDigitsInputInvalidException('1.2.3.4.5', 'repeated digits');

expect($exception)->toBeInstanceOf(\Exception::class);
expect($exception)->toBeInstanceOf(Exception::class);
});

it('is an instance of CnpjCheckDigitsException', function () {
Expand All @@ -226,12 +225,6 @@ final class TestCnpjCheckDigitsException extends CnpjCheckDigitsException
expect($exception)->toBeInstanceOf(CnpjCheckDigitsException::class);
});

it('has the correct class name', function () {
$exception = new CnpjCheckDigitsInputInvalidException('1.2.3.4.5', 'repeated digits');

expect($exception::class)->toBe(CnpjCheckDigitsInputInvalidException::class);
});

it('sets the `actualInput` property', function () {
$exception = new CnpjCheckDigitsInputInvalidException('1.2.3.4.5', 'repeated digits');

Expand All @@ -244,14 +237,23 @@ final class TestCnpjCheckDigitsException extends CnpjCheckDigitsException
expect($exception->reason)->toBe('repeated digits');
});

it('generates a message describing the exception', function () {
it('has the correct message', function () {
$actualInput = '1.2.3.4.5';
$reason = 'repeated digits';
$actualMessage = 'CNPJ input "'.$actualInput.'" is invalid. '.$reason;

$exception = new CnpjCheckDigitsInputInvalidException($actualInput, $reason);
$exception = new CnpjCheckDigitsInputInvalidException(
$actualInput,
$reason,
);

expect($exception->getMessage())->toBe($actualMessage);
});

it('has the correct name', function () {
$exception = new CnpjCheckDigitsInputInvalidException('1.2.3.4.5', 'repeated digits');

expect($exception->getName())->toBe('CnpjCheckDigitsInputInvalidException');
});
});
});
Loading
Loading