davideme / libphonenumber-for-PHP

PHP version of Google's phone number handling library
125 stars 71 forks source link

Call to a member function matcher() on a non-object in libphonenumber-for-php/PhoneNumberUtil.php on line 942 #27

Open michaellenahan opened 10 years ago

michaellenahan commented 10 years ago

Hello, I'm using libphonenumber-for-php as part of the Drupal phone module (https://drupal.org/project/phone).

I got this error on a fairly standard looking number formatted as follows: 0054 11 2222 3333

Call to a member function matcher() on a non-object in libphonenumber-for-php/PhoneNumberUtil.php on line 942

The error occurs in this function: PhoneNumberUtil->maybeStripNationalPrefixAndCarrierCode()

The work-around was to add an is_object() check so the matcher member function does not get called, but I don't know what the root cause of this problem could have been.

Code before:

                $transformedNumber = $number;
                $transformedNumber =  substr_replace($transformedNumber, $prefixMatcher->replaceFirst($transformRule), 0, $numberLength);
                if ($isViableOriginalNumber &&
                    !$nationalNumberRule->matcher($transformedNumber->toString())->matches()) {
                    return false;
                }

Code after:

                $transformedNumber = $number;
                $transformedNumber =  substr_replace($transformedNumber, $prefixMatcher->replaceFirst($transformRule), 0, $numberLength);
                if ($isViableOriginalNumber && is_object($nationalNumberRule) &&
                    !$nationalNumberRule->matcher($transformedNumber->toString())->matches()) {
                    return false;
                }
giggsey commented 10 years ago

Take a look at https://github.com/giggsey/libphonenumber-for-php/pull/3 which also contains a similar fix to yours.