Add maskRegex and maskPartialRegex by PhantomWatson · Pull Request #19526 · cakephp/cakephp · GitHub
Skip to content

Add maskRegex and maskPartialRegex#19526

Open
PhantomWatson wants to merge 1 commit into
cakephp:5.nextfrom
PhantomWatson:5.next-match-regex
Open

Add maskRegex and maskPartialRegex#19526
PhantomWatson wants to merge 1 commit into
cakephp:5.nextfrom
PhantomWatson:5.next-match-regex

Conversation

@PhantomWatson

Copy link
Copy Markdown
Contributor

Add Text::maskRegex() and Text::maskRegexPartial() static methods to Cake\Utility\Text for masking portions of strings by regex instead of by specific substrings.

See issue #19525

Copilot AI review requested due to automatic review settings June 30, 2026 19:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR extends Cake\Utility\Text with two new masking helpers that operate on regular expressions, enabling masking of dynamic/sensitive substrings (e.g., numbers, words) without needing exact substring needles.

Changes:

  • Added Text::maskRegex() to mask full regex matches by replacing each match with repeated mask characters.
  • Added Text::maskPartialRegex() to mask regex matches while preserving configurable leading/trailing characters.
  • Added PHPUnit coverage for both methods, including multi-pattern and multibyte scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/Utility/Text.php Adds the new regex-based masking APIs to the Text utility class.
tests/TestCase/Utility/TextTest.php Adds test cases/data providers covering expected behaviors and invalid inputs.

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

Comment thread src/Utility/Text.php
Comment on lines +1288 to +1292
* @param string $string The input string.
* @param string[]|string $patterns One or more regex patterns
* @param string $maskCharacter Single masking character.
* @return string
*/
Comment thread src/Utility/Text.php
Comment on lines +1326 to +1328
* Examples:
* - maskPartialRegex('Card used: 4242424242424242', '/\d{16}/', 0, 4) => Credit card used: ************4242
* - maskPartialRegex('Secret Codeword', '/\b\w+\b/', 1, 1) => S****t C********d
Comment thread src/Utility/Text.php
Comment on lines +1334 to +1336
* @param string $maskCharacter Single masking character.
* @throws \InvalidArgumentException If $maskCharacter is not exactly a single character.
* @return string
Comment thread src/Utility/Text.php
Comment on lines +1338 to +1339
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.

2 participants