Skip to content

Do not skip the character after a match in PhoneNumberMatcher - #773

Merged
giggsey merged 2 commits into
giggsey:masterfrom
wleeaf:fix-matcher-skipped-character
Oct 26, 2026
Merged

giggsey merged 2 commits into
giggsey:masterfrom
wleeaf:fix-matcher-skipped-character

Conversation

@wleeaf

@wleeaf wleeaf commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

When a number starts right after the previous match, findNumbers() drops its first character:

foreach ($util->findNumbers('Call 033316005+64 3 331 6005', 'NZ') as $m) {
    echo $m->start(), ' ', $m->rawString(), PHP_EOL;
}
// 5 033316005
// 15 64 3 331 6005     Java: 14 +64 3 331 6005

Without its + (or its opening (, or a leading digit), the second number can be parsed with the wrong country calling code. For example, findNumbers('2024-01-02+4930123456', 'GB', Leniency::POSSIBLE()) returns 4930123456 as +444930123456, where Java returns +4930123456.

PhoneNumberMatcher::next() sets $this->searchIndex = $this->lastMatch->end() and then always increments it, so the next search starts one character after the end of the previous match. In Java's hasNext() there is no increment:

lastMatch = find(searchIndex);
if (lastMatch == null) {
  state = State.DONE;
} else {
  searchIndex = lastMatch.end();
  state = State.READY;
}

This removes the increment. A match is never empty, so the search still moves forward.

testSequenceStartingRightAfterPreviousMatch follows testSequences, with no separator between the two numbers. It fails without the change (start 15, raw string 64 3 331 6005) and passes with it.

I compared this branch with Java libphonenumber 9.0.41 on 20,000 generated findNumbers() texts with numbers back to back. Measured on top of the companion STRICT_GROUPING fix, 297 of the 316 findNumbers() mismatches left after that fix are gone, and no new ones appear. The remaining ones involve trailing extension markers such as ext and x.

vendor/bin/phpunit, php-cs-fixer fix --dry-run and phpstan pass. The one exception is LocaleTest data set #174 (SH, "St. Helena"), which also fails on master.

next() moved searchIndex to the end of the last match and then always
incremented it, so the search for the next number started one character
late. Java sets searchIndex = lastMatch.end() without the increment. When a
number starts right after the previous one, its first character was lost:
in "033316005+64 3 331 6005" the second match was "64 3 331 6005" instead
of "+64 3 331 6005", and a number without its plus sign can be parsed with
the wrong country calling code.
Copilot AI balanced review requested due to automatic review settings October 25, 2026 00:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.12%. Comparing base (318d9b7) to head (9f3dd3b).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #773      +/-   ##
============================================
- Coverage     86.12%   86.12%   -0.01%     
  Complexity     1320     1320              
============================================
  Files            31       31              
  Lines          3445     3444       -1     
============================================
- Hits           2967     2966       -1     
  Misses          478      478              

☔ View full report in Codecov by Harness.
📢 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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused fix corrects the boundary error, preserves forward progress, and includes a regression test for the reported case.

No open issues 🎉

Review effort: Balanced

@giggsey
giggsey merged commit 0bb5aea into giggsey:master Oct 26, 2026
22 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.

3 participants