Skip to content

Retrieved version number and PHP 8 #94

Open
@chrisgraham

Description

@chrisgraham

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions