Skip to content

Create CreditCard formatters#5

Merged
henriquemoody merged 2 commits intoRespect:mainfrom
henriquemoody:new_formatters
Feb 9, 2026
Merged

Create CreditCard formatters#5
henriquemoody merged 2 commits intoRespect:mainfrom
henriquemoody:new_formatters

Conversation

@henriquemoody
Copy link
Member

No description provided.

@henriquemoody henriquemoody changed the title Add a selection of new formatters Create CreditCard formatters Feb 8, 2026
@codecov-commenter
Copy link

codecov-commenter commented Feb 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.53%. Comparing base (faeaead) to head (315169d).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main       #5      +/-   ##
============================================
+ Coverage     99.48%   99.53%   +0.05%     
- Complexity      166      191      +25     
============================================
  Files            24       26       +2     
  Lines           388      430      +42     
============================================
+ Hits            386      428      +42     
  Misses            2        2              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@henriquemoody henriquemoody force-pushed the new_formatters branch 3 times, most recently from 1acc047 to 5602ca7 Compare February 8, 2026 23:02
@henriquemoody henriquemoody marked this pull request as ready for review February 8, 2026 23:03
@henriquemoody henriquemoody requested a review from Copilot February 8, 2026 23:03
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds new credit-card-specific formatters to the library, including a standard formatter and a “secure” masking variant, along with documentation and unit tests.

Changes:

  • Introduces CreditCardFormatter and SecureCreditCardFormatter implementations.
  • Adds PHPUnit unit test coverage for both formatters across multiple card patterns and edge cases.
  • Adds new formatter documentation pages and updates the README formatter list; extends builder/chain mixins with credit-card methods.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/CreditCardFormatter.php New formatter that cleans input and formats card numbers using patterns (incl. Amex/Diners/19-digit).
src/SecureCreditCardFormatter.php New formatter that composes CreditCardFormatter + MaskFormatter to hide sensitive digits.
src/Mixin/Builder.php Adds builder mixin methods for credit card formatters (currently contains a naming mismatch).
src/Mixin/Chain.php Adds chain mixin methods for credit card formatters (currently contains a naming mismatch).
tests/Unit/CreditCardFormatterTest.php New unit tests validating CreditCardFormatter behavior.
tests/Unit/SecureCreditCardFormatterTest.php New unit tests validating masking and custom mask character behavior.
docs/CreditCardFormatter.md New documentation page for CreditCardFormatter.
docs/SecureCreditCardFormatter.md New documentation page for SecureCreditCardFormatter (contains wording mismatch).
README.md Updates formatter list to include the new credit card formatters.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 34 to 62
public function shouldFormatCreditCards(string $input, string $expected): void
{
$formatter = new CreditCardFormatter();

$actual = $formatter->format($input);

self::assertSame($expected, $actual);
}

#[Test]
public function shouldHandleEmptyString(): void
{
$formatter = new CreditCardFormatter();

$actual = $formatter->format('');

self::assertSame(' ', $actual);
}

#[Test]
#[DataProvider('providerForVisaDifferentLengths')]
public function shouldHandleVisaDifferentLengths(string $input, string $expected): void
{
$formatter = new CreditCardFormatter();

$actual = $formatter->format($input);

self::assertSame($expected, $actual);
}
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

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

Test method naming here uses should..., while the rest of the unit test suite consistently uses the itShould... convention (e.g. tests/Unit/AreaFormatterTest.php). Consider renaming these test methods to match the established convention for consistency.

Copilot uses AI. Check for mistakes.

public static function creditCard(): FormatterBuilder;

public static function secretCreditCard(string $maskChar = '*'): FormatterBuilder;
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

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

secretCreditCard() will not work with FormatterBuilder::__call() naming convention (it reflects ucfirst($name) . 'Formatter'), so it will try to instantiate Respect\StringFormatter\SecretCreditCardFormatter, which does not exist (the class added is SecureCreditCardFormatter). Rename this method to secureCreditCard() (preferred to match the class/docs) or add a SecretCreditCardFormatter alias/wrapper class so the builder/modifier pipeline can construct the formatter.

Suggested change
public static function secretCreditCard(string $maskChar = '*'): FormatterBuilder;
public static function secureCreditCard(string $maskChar = '*'): FormatterBuilder;

Copilot uses AI. Check for mistakes.
The new CreditcardFormatter automatically detects major credit card
types (Visa, MasterCard, Amex, Discover, JCB) based on card prefix
and length, applying the appropriate formatting pattern.

This formatter is essential for payment processing applications that
need to display credit card numbers in a consistent, readable format
while supporting different card types with their specific formatting
requirements (e.g., Amex uses 4-6-5 format while others use 4-4-4-4).

Input is automatically cleaned by removing non-digit characters,
making it flexible for real-world usage where cards may have spaces,
dashes, or other separators.

Includes comprehensive tests covering all major card types, invalid
cards, custom patterns, input cleaning, and edge cases.

Assisted-by: OpenCode (GLM-4.7)
Composes CreditCardFormatter and MaskFormatter to display credit card
numbers with only the first and last digit groups visible. Automatically
detects card type and applies the appropriate mask range for each format.

Assisted-By: Claude Code (Claude Opus 4.6)
@henriquemoody henriquemoody merged commit 315169d into Respect:main Feb 9, 2026
4 checks passed
@henriquemoody henriquemoody deleted the new_formatters branch February 9, 2026 11:37
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.

3 participants