Skip to content

Treat an unset extension as empty in containsOnlyValidXChars - #774

Merged
giggsey merged 2 commits into
giggsey:masterfrom
wleeaf:fix-matcher-x-without-extension
Oct 26, 2026
Merged

giggsey merged 2 commits into
giggsey:masterfrom
wleeaf:fix-matcher-x-without-extension

Conversation

@wleeaf

@wleeaf wleeaf commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

PhoneNumberMatcher::containsOnlyValidXChars() ports this check from PhoneNumberMatcher.java:

} else if (!PhoneNumberUtil.normalizeDigitsOnly(candidate.substring(index)).equals(
    number.getExtension())) {
  return false;
}

In Java, getExtension() of a number without an extension is the empty string, the protobuf default. In PHP, PhoneNumber::getExtension() returns null, so the port's strict !== comparison fails whenever an x in the candidate is followed by no digits. That covers an x that starts the letters ext. Such candidates are rejected with VALID and stricter leniencies, where Java accepts them:

$util->findNumbers('x +6913201ext. 12234)', 'DE', Leniency::VALID()); // no match; Java finds "+6913201ext" (+6913201398)
$util->findNumbers('/0037799123ext. 12456.', 'GB', Leniency::VALID()); // no match; Java finds "0037799123ext"

The comparison now treats a null extension as '', as Java does. An x that is followed by digits still has to be followed by the extension.

The new testContainsOnlyValidXCharsWithoutExtension checks both cases for a number without an extension. The x. case fails without the change.

I compared findNumbers() with Java libphonenumber 9.0.41 on 20,000 generated texts. This change fixes 10 of the mismatches that remain with #772 and #773 applied, and adds none. Full vendor/bin/phpunit passes (6178 tests) except LocaleTest data set #174 (SH, 'St. Helena'), which fails on master too with the current giggsey/locale. php-cs-fixer fix --dry-run reports 0 files and phpstan reports no errors.

Java compares the digits after an x with number.getExtension(), which is
an empty string when the number has no extension. PhoneNumber::getExtension()
returns null instead, so an x followed by no digits made the check fail and
findNumbers dropped candidates that Java keeps.
Copilot AI balanced review requested due to automatic review settings October 25, 2026 01:12

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 (9f3dd3b) to head (83f5ca9).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master     #774   +/-   ##
=========================================
  Coverage     86.12%   86.12%           
  Complexity     1320     1320           
=========================================
  Files            31       31           
  Lines          3444     3444           
=========================================
  Hits           2966     2966           
  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 narrowly scoped correction includes targeted regression coverage and has no identified blocking issues.

No open issues 🎉

Review effort: Balanced

@giggsey
giggsey merged commit 1ad4042 into giggsey:master Oct 26, 2026
22 checks passed
@giggsey

giggsey commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Thank you for these 3 PRs!

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