backdrop-contrib / demo

Manage snapshots of a Backdrop site for demo purposes.
https://backdropcms.org/project/demo
GNU General Public License v2.0
1 stars 3 forks source link

Deprecated function: uasort() with php 8.1.x #17

Closed izmeez closed 1 year ago

izmeez commented 1 year ago

With php 8.1.17 the following error message appears: Deprecated function: uasort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero in demo_get_dumps() (line 497 of .../demo.admin.inc).

robertgarrigos commented 1 year ago

I cannot reproduce this, @izmeez. What exactly do you do to get this error?

argiepiano commented 1 year ago

Using this type of comparison in PHP 8.1 WILL produce the deprecated message:

uasort($files, function($a, $b) {return ($a->filemtime < $b->filemtime);});

The problem here is that the < comparison returns a boolean, and uasort() will only accept ints such as 1, 0 or -1.

Typically you would fix this with the "spaceship" operator (<=>), BUT it doesn't work in PHP 5.6, which is still supported by Backdrop

So, the only possible solution is:

[EDITED]

uasort($files, function($a, $b) {
  return ($a->filemtime == $b->filemtime) ? 0 : (($a->filemtime < $b->filemtime) ? 1 : -1);
});
argiepiano commented 1 year ago

Sorry, there are some inaccuracies in my answer. I'll edit soon.

[EDIT]: I fixed the answer above.

robertgarrigos commented 1 year ago

@argiepiano, I understand the problem. I will fix it with your code. However, I'm not getting any error on php 8.1.20. at least on Backdrop's log.

robertgarrigos commented 1 year ago

@izmeez, as I cannot reproduce the error, would you check the pr for this fix? Thanks.

izmeez commented 1 year ago

Yes, the fix works with php 8.1.17