Skip to content

Expand character ranges when trimming a numeric-string - #7013

Open
SanderMuller wants to merge 1 commit into
phpstan:2.3.xfrom
SanderMuller:trim-character-ranges
Open

SanderMuller wants to merge 1 commit into
phpstan:2.3.xfrom
SanderMuller:trim-character-ranges

Conversation

@SanderMuller

Copy link
Copy Markdown
Contributor

For a numeric-string, the trim extensions keep numeric-string when the character list cannot remove a digit. The old check only looked for a literal digit, so it missed .. ranges: '/..:' covers 0 to 9, and trim('12', '/..:') returns ''.

TrimCharactersHelper::containsDigit() reads the character list the same way as PHP's php_charmask(). A valid range adds every byte between its ends. An invalid range, such as '..:' or ':../', adds no range, so those lists still keep numeric-string.

I compared the helper with PHP's own trim() on 300,000 random character lists. The lists had dots, digits, /, : and random bytes. The helper gave the same answer for every list. The old regex gave a different answer for 2,781 of them.

Closes phpstan/phpstan#15683

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Reads the character list like PHP's php_charmask(): "a..z" stands for
* every byte from "a" to "z", so "/..:" contains the digits.
*/
public function containsDigit(string $characters): bool

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.

this new method should have some unit tests

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.

trim() keeps numeric-string when a character range includes digits

2 participants