backdrop-contrib / ubercart

A flexible but easy-to-use e-commerce system for Backdrop.
GNU General Public License v2.0
4 stars 9 forks source link

Deprecated function: Implicit conversion from float to int loses precision in UbercartEncryption->applyFudgeFactor() #485

Closed bugfolder closed 6 months ago

bugfolder commented 6 months ago

Encountered this on a dev site (PHP 8.1) doing a test checkout. Many many error messages of this at cart/checkout when clicking "review order":

Deprecated function: Implicit conversion from float 33.75 to int loses precision in UbercartEncryption->applyFudgeFactor() (line 208 of /mysite/modules/contrib/ubercart/uc_store/classes/encrypt.inc).

The offending code starting at line 208 is:

      if ($fudge % $this->mod == 0) {
        $fudge = $fudge * -1;
      }

And indeed, $fudge has a floating point value of 33.75 when the error is issued.

I don't fully follow everything going on in the UbercartEncryption class. The .75 is coming from the protected variable $adj, which ends up added to $fudge and is always initialized to 1.75 (why this particular value?).

Since the code runs correctly otherwise (and has worked for many years on lesser versions of PHP), I infer that it's OK to simply make the conversion explicit, so changing those lines to

      if (((int) $fudge) % $this->mod == 0) {
        $fudge = $fudge * -1;
      }

seems to work and eliminates the error.

argiepiano commented 6 months ago

I don't quite follow how this method (or class to be honest) does what it does, but converting the variable to int before the operation seems correct to me, as modulo is an integer operator. So, LGTM