vinkla / hashids

A small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user.
https://hashids.org/php
MIT License
5.28k stars 418 forks source link

Missing BC Math or GMP extension #104

Closed dakujem closed 6 years ago

dakujem commented 6 years ago

Hello. Just run into an issue on Heroku hosted app.

Version 2.0.3 runs fine, version 2.0.4 crashes with this:

RuntimeException

Missing BC Math or GMP extension.

    /app/vendor/hashids/hashids/src/Math.php in mod at line 113

        if (function_exists('bcmod')) {
            return bcmod($n, $d);
        }
        throw new RuntimeException('Missing BC Math or GMP extension.');
    }
    /**
     * Compares two arbitrary-length integers.
     *
d   100
n   1

It is confusing... by looking at the code it looks like version 2.0.3 would crash on "bad method call" or similar as well, but it really does not crash with 2.0.3. I'm using the

I have not delved any deeper.


The app is really simple and this is how I use hashids:

        $hashFilters = [
            Route::FILTER_IN => function($device) {
                return $this->hs->decode($device)[0];
            },
            Route::FILTER_OUT => function($device) {
                return $this->hs->encode($device);
            },
        ];

where $this->hs holds an instance of Hashids\Hashids class.

vinkla commented 6 years ago

You need to install either the BC Math or GMP extension with PHP to be able to use this package.

dakujem commented 6 years ago

Well I understand the error. But how is it possible, that with 2.0.3 which I am using, I do not need it either?

vinkla commented 6 years ago

We updated the error messages a while back to make it possible to use either GMP or BC.

dakujem commented 6 years ago

Anyway, I wanted to say that somehow it worked with 2.0.3 and not with 2.0.4. Maybe you made some changes that should be noted in a changelog or so. For me it was an update that broke the app. Just to let you know. I do not wish to poke, just trying to help. The lib itself is awesome. Thanks.

vinkla commented 6 years ago

It is noted in the changelog https://github.com/ivanakimov/hashids.php/releases/tag/2.0.4

dakujem commented 6 years ago

Fair enough. Though the fact that the change breaks a functioning app remains.

I'd have written: "BC Break: blah requirement removed, install either A or B manually."

Never mind. Sorry for wasting your time.

vinkla commented 6 years ago

This isn’t a breaking change. If it were we would have it released as a new major version.

What PHP extensions do you have installed in your environment?

dakujem commented 6 years ago

Well I'm guessing this is only a problem with Heroku (and similar platforms). Since 2.0.3 had that in composer.json, heroku installed the bcmath extension automatically. 2.0.4 does not. That is why it did not get isntalled. I guess.

vinkla commented 6 years ago

Then you can add ext-bcmath to the required object in your composer.json file and you should be good to go.

dakujem commented 6 years ago

Yes, thanks for the help. Though i solved the issue even before posting this issue :-) All I wanted to note was what I wrote earlier: It surprised me that after doing a simple "patch" update the app started crashing.

vinkla commented 6 years ago

We're sorry about that but we can't support all deployment platforms. If you know of a better way to support two math libraries without forcing to install one of them we would be happy to look at any pull request you send our way.

dakujem commented 6 years ago

Sorry, i do not.

antonioreyna commented 6 years ago

hello, i am getting the same error, i installed the bcmath but still is not working

dakujem commented 6 years ago

@antonioreyna You are experiencing a different problem. This issue was about a changed behaviour between 2.0.3 and 2.0.4 versions.

Your problem should be fixed by installing either GMP or BCMath. If you are using Heroku (or a similar platform), all you need to do is add either ext-bcmath or ext-gmp into the requirements section of your composer.json file.