Skip to content

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

Merged
staabm merged 2 commits into
phpstan:2.3.xfrom
SanderMuller:trim-character-ranges
Nov 9, 2026
Merged

staabm merged 2 commits 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added TrimCharactersHelperTest with 20 cases, including the invalid ranges. I checked each expected value against real trim(). With the old /\d/ regex, 3 of the cases fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@staabm

staabm commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Thank you!

@staabm
staabm merged commit eafa5ab into phpstan:2.3.x Nov 9, 2026
890 of 894 checks passed
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