Skip to content

Fix STRICT_GROUPING rejecting numbers written without formatting - #772

Merged
giggsey merged 2 commits into
giggsey:masterfrom
wleeaf:fix-strict-grouping-national-number
Oct 26, 2026
Merged

giggsey merged 2 commits into
giggsey:masterfrom
wleeaf:fix-strict-grouping-national-number

Conversation

@wleeaf

@wleeaf wleeaf commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

findNumbers() with Leniency::STRICT_GROUPING() skips numbers written as one block of digits in regions that have a national prefix, although it finds them with the stricter EXACT_GROUPING():

$util->findNumbers('02071234567', 'GB', Leniency::STRICT_GROUPING());  // no match; Java finds 02071234567
$util->findNumbers('6502530000', 'US', Leniency::STRICT_GROUPING());   // no match; Java finds 6502530000
$util->findNumbers('+442071234567', 'ZZ', Leniency::STRICT_GROUPING()); // no match; Java finds it
$util->findNumbers('02071234567', 'GB', Leniency::EXACT_GROUPING());   // match

allNumberGroupsRemainGrouped() ports this check from PhoneNumberMatcher.java:

return normalizedCandidate.substring(fromIndex - formattedNumberGroups[i].length())
    .startsWith(nationalSignificantNumber);

as mb_substr($candidate, mb_strlen($nationalSignificantNumber)) === $nationalSignificantNumber. That compares the text after the first strlen(nsn) characters with the NSN, not the start of the text, so the check almost never passes. It now uses str_starts_with().

The existing tests didn't catch this because the matcher data providers combine their lists with the array union operator (self::dataStrictGroupingCases() + self::dataExactGroupingCases()). For lists with integer keys, + keeps the first list's keys and drops the overlapping ones from the next, so several cases never ran at the leniencies they were meant for. Among them were the unformatted exact-grouping cases (4156667777, 4156667777 x 123, 4156667777) under STRICT_GROUPING. The providers now use array_merge(), which brings PhoneNumberMatcherTest from 218 to 338 tests. Without the fix, the 3 cases above fail; with it, all 338 pass.

I compared this branch with Java libphonenumber 9.0.41 on about 90,000 generated parse, format, as-you-type, short-number and findNumbers() calls. The 57 STRICT_GROUPING mismatches from the first run are gone, and no new ones appear.

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 with the current giggsey/locale.

allNumberGroupsRemainGrouped() ports Java's
normalizedCandidate.substring(...).startsWith(nationalSignificantNumber)
as mb_substr($candidate, strlen($nsn)) === $nsn, which compares the text
after the national significant number instead of its start. In regions
with a national prefix this made findNumbers() with STRICT_GROUPING skip
numbers written as one block, such as 4156667777 (US) or 02071234567 (GB),
which Java finds and which EXACT_GROUPING already accepts.

The matcher test data providers combined the case lists with the array
union operator, which keeps only the first list's keys, so most cases
never ran at the leniencies they were meant for, including the
unformatted exact-grouping cases under STRICT_GROUPING. They now use
array_merge(), which runs 338 matcher tests instead of 218.
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.

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 correction matches the intended grouping behavior, and the provider changes restore relevant regression coverage with no identified blockers.

No open issues 🎉

Review effort: Balanced

@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 (caf036f) to head (318d9b7).
⚠️ Report is 9 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #772      +/-   ##
============================================
+ Coverage     85.86%   86.12%   +0.26%     
- Complexity     1319     1320       +1     
============================================
  Files            31       31              
  Lines          3445     3445              
============================================
+ Hits           2958     2967       +9     
+ Misses          487      478       -9     

☔ 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.

@giggsey
giggsey merged commit 0f0e014 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