libphonenumber-for-php icon indicating copy to clipboard operation
libphonenumber-for-php copied to clipboard

Method maybeExtractCountryCode very slowly

Open anboo opened this issue 4 years ago • 3 comments

$ composer show giggsey/libphonenumber-for-php

name     : giggsey/libphonenumber-for-php
descrip. : PHP Port of Google's libphonenumber
keywords : geocoding, geolocation, libphonenumber, mobile, phonenumber, validation
versions : * 8.12.19
php -v

PHP 7.4.3 (cli) (built: Feb 20 2020 21:53:46) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
    with Xdebug v2.9.8, Copyright (c) 2002-2020, by Derick Rethans

callgraph callgraph (1) callgraph (2)

Maybe need add mechanism for cache of parsing? How I can cache or serialize phonenumber phone for cache?

anboo avatar Jun 02 '21 10:06 anboo

        $phoneNumberUtil = PhoneNumberUtil::getInstance();

        $phoneNumberObject = $phoneNumberUtil->parse('+79636417683');
        $serializedPhoneNumber = serialize($phoneNumberObject);

        echo $serializedPhoneNumber.PHP_EOL;

        $phoneNumberObjectFromUnserialized = unserialize($serializedPhoneNumber);
        dump($phoneNumberObjectFromUnserialized);


        $this->assertEquals(
            $phoneNumberUtil->getNumberType($phoneNumberObject),
            $phoneNumberUtil->getNumberType($phoneNumberObjectFromUnserialized)
        );
        $this->assertEquals(
            $phoneNumberUtil->format($phoneNumberObject, PhoneNumberFormat::E164),
            $phoneNumberUtil->format($phoneNumberObjectFromUnserialized, PhoneNumberFormat::E164)
        );
        $this->assertEquals(
            $phoneNumberUtil->canBeInternationallyDialled($phoneNumberObject),
            $phoneNumberUtil->canBeInternationallyDialled($phoneNumberObjectFromUnserialized)
        );
        $this->assertEquals(
            $phoneNumberUtil->truncateTooLongNumber($phoneNumberObject),
            $phoneNumberUtil->truncateTooLongNumber($phoneNumberObjectFromUnserialized)
        );
        $this->assertEquals(
            $phoneNumberUtil->truncateTooLongNumber($phoneNumberObject),
            $phoneNumberUtil->truncateTooLongNumber($phoneNumberObjectFromUnserialized)
        );

It's working ok Serialized phone number object is

C:26:"libphonenumber\PhoneNumber":76:{a:8:{i:0;i:7;i:1;s:10:"9636417683";i:2;N;i:3;N;i:4;i:1;i:5;N;i:6;i:4;i:7;N;}}

Dump after unserialize:

^ libphonenumber\PhoneNumber^ {#384
  #countryCode: 7
  #nationalNumber: "9636417683"
  #extension: null
  #italianLeadingZero: null
  #rawInput: null
  #countryCodeSource: 4
  #preferredDomesticCarrierCode: null
  #hasNumberOfLeadingZeros: false
  #numberOfLeadingZeros: 1
}

Can you tell me if there could be any problems when using the serialize phone number object function?

anboo avatar Jun 02 '21 11:06 anboo

Serialization is unit tested, so it should be absolutely fine for your needs there.

As for speed improvements, MRs are welcome, providing it doesn't deviate too much from the Java version of Google's library. Just looking through the code, there are a bunch of calls if (mb_strlen($variable) === 0) that can be replaced with if ($variable === '') for a minor improvement.

giggsey avatar Jun 02 '21 11:06 giggsey

i replaced mb_strlen === 0 and mb_strlen > 0 by string comparison.

dahse89 avatar Oct 20 '21 17:10 dahse89