Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method maybeExtractCountryCode very slowly #444

Open
anboo opened this issue Jun 2, 2021 · 3 comments
Open

Method maybeExtractCountryCode very slowly #444

anboo opened this issue Jun 2, 2021 · 3 comments

Comments

@anboo
Copy link

anboo commented Jun 2, 2021

$ 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
Copy link
Author

anboo commented Jun 2, 2021

        $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?

@giggsey
Copy link
Owner

giggsey commented Jun 2, 2021

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.

dahse89 pushed a commit to dahse89/libphonenumber-for-php that referenced this issue Oct 20, 2021
Replace all checks of mb_strlen is 0 by direct compare to empty string

closes giggsey#444
dahse89 pushed a commit to dahse89/libphonenumber-for-php that referenced this issue Oct 20, 2021
…son)

Replace all checks of mb_strlen is greater than 0 by direct compare to not equal to empty string

closes giggsey#444
@dahse89
Copy link
Contributor

dahse89 commented Oct 20, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants