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

Retrieved version number and PHP 8 #94

Open
chrisgraham opened this issue Oct 21, 2020 · 2 comments
Open

Retrieved version number and PHP 8 #94

chrisgraham opened this issue Oct 21, 2020 · 2 comments

Comments

@chrisgraham
Copy link

The getVersion method is documented to return only a single period:

    /**
     * The version of the browser.
     * @return string Version of the browser (will only contain alpha-numeric characters and a period)
     */
    public function getVersion()
    {
        return $this->_version;
    }

However, it may contain multiple. E.g. '15.1.4'.

This was not really an obvious issue, until PHP 8, which changes how automatic casting works...

php -r 'var_dump("15.1.4" == 15.1);'
bool(false)

It returns false as it now does a string comparison rather than an automatic cast, if it doesn't fully match the pattern of a float (I think!!). I was relying on this, as I'm sure other users were.

This change to the setVersion method fixes it by making it only have one period section:

    /**
     * Set the version of the browser
     * @param string $version The version of the Browser
     */
    public function setVersion($version)
    {
        $this->_version = preg_replace('#^([^\.]*\.[^\.]*)\..*#', '$1', preg_replace('/[^0-9,.,a-z,A-Z-]/', '', $version));
    }

This provides us a return value that continues to work...

$ php -r 'var_dump("15.1" == 15.1);'
bool(true)
@cbschuld
Copy link
Owner

@chrisgraham definitely an issue - I hear you. For an acceptable stopgap in PHP8 you could cast to a double; I'd really like to not change the setter/getter scenario of the version if we can (just due to usage)

php -r 'var_dump((double)"15.1.4" == (double)15.1);'

Here is an execution from 7.3.11:

php -r 'var_dump((double)"15.1.4" == (double)15.1);'
bool(true)

and one from 8.0.0 CR2

PHP 8.0.0RC2 (cli) (built: Oct 22 2020 06:35:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
/ # php -r 'var_dump((double)"15.1.4" == (double)15.1);'
bool(true)

@chrisgraham
Copy link
Author

chrisgraham commented Oct 28, 2020

That's fine, thanks for the reply.
If you don't want to go my way for now, I suggest just securing the way you're doing it. Fix "will only contain alpha-numeric characters and a period" to "will only contain alpha-numeric characters and periods, e.g. 15.1.4", and actually I'd advise people to use PHP's version_compare function.

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

No branches or pull requests

2 participants