giggsey / libphonenumber-for-php

PHP version of Google's phone number handling library
https://giggsey.com/libphonenumber/
Apache License 2.0
4.76k stars 468 forks source link

Fatal uncaught error when testing a number. #600

Closed tree28 closed 1 year ago

tree28 commented 1 year ago

Hello,

An observation – When testing a number that does not have a correct format the following fatal error occurs. It would be beneficial to alter the code so that non-compliant numbers were returned with a usable error code or message. The return could be used to provide feedback back to the user, for example telling them that the number was invalid, while also preventing the fatal error from halting the system.

A reasonable change if this utility exists to do analysis on numbers from the wild.

The error – Fatal error: Uncaught Error type: 0. Missing or invalid default region. thrown in /var/www/vendor/giggsey/libphonenumber-for-php/src/PhoneNumberUtil.php on line 1682

giggsey commented 1 year ago

Can you supply some example code?

From the error message, I'm guessing you are trying to parse() some numbers without passing the region. The library needs to know a default region for numbers that are not in a known international format. This is because numbers could be valid in multiple different countries.

As a side note, catching exceptions is generally recommended, this would stop it triggering a fatal error.

On Sun, 15 Oct 2023, 15:59 tree28, @.***> wrote:

Hello,

An observation – When testing a number that does not have a correct format the following fatal error occurs. It would be beneficial to alter the code so that non-compliant numbers were returned with a usable error code or message. The return could be used to provide feedback back to the user, for example telling them that the number was invalid, while also preventing the fatal error from halting the system.

A reasonable change if this utility exists to do analysis on numbers from the wild.

The error – Fatal error: Uncaught Error type: 0. Missing or invalid default region. thrown in /var/www/vendor/giggsey/libphonenumber-for-php/src/PhoneNumberUtil.php on line 1682

— Reply to this email directly, view it on GitHub https://github.com/giggsey/libphonenumber-for-php/issues/600, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACKUQXXT2ZQT3AOWTS6M4DX7P26TANCNFSM6AAAAAA6BCARN4 . You are receiving this because you are subscribed to this thread.Message ID: @.***>

tree28 commented 1 year ago

The code – $phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance(); $phoneNumberObject = $phoneNumberUtil->parse('a wild number',null);

An observation – This seems to be best and maybe a usable example.

$phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance(); $filter = array('us','au','gb'); for($i = 0; $i < count($filter); $i++) { $phoneNumberObject = $phoneNumberUtil->parse('a wild number',$filter[$i]); if($phoneNumberUtil->getRegionCodeForNumber($phoneNumberObject)) { $cciso2 = $phoneNumberUtil->getRegionCodeForNumber($phoneNumberObject); break; } }

giggsey commented 1 year ago

Yes, looping around possible regions based on your data will be a good idea.

Just as a general PHP note: using foreach might be better here. And generally it's not advised to put count() in the middle part of a for loop (as it'll run that function on every iteration).

On Sun, 15 Oct 2023, 16:41 tree28, @.***> wrote:

The code:

$phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance(); $phoneNumberObject = $phoneNumberUtil->parse('a wild number',null);

An observation: This seems to be best and maybe a usable example.

$phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance(); $filter = array('us','au','gb'); for($i = 0; $i < count($filter); $i++) { $phoneNumberObject = $phoneNumberUtil->parse('a wild number',$filter[$i]); if($phoneNumberUtil->getRegionCodeForNumber($phoneNumberObject)) { $cciso2 = $phoneNumberUtil->getRegionCodeForNumber($phoneNumberObject); break; } }

— Reply to this email directly, view it on GitHub https://github.com/giggsey/libphonenumber-for-php/issues/600#issuecomment-1763427951, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACKUQTVFT2SPVLXNNXKLMLX7P73PANCNFSM6AAAAAA6BCARN4 . You are receiving this because you commented.Message ID: @.***>

tree28 commented 1 year ago

Thanks for the library – appreciate the work.