Skip to content

Commit

Permalink
ReverseSort bug, exposed but not caused by PHP8 (PHPOffice#1660)
Browse files Browse the repository at this point in the history
Some tests of ReferenceHelper functions columnReverseSort and
cellReverseSort which passed with PHP7 fail with PHP8.
Both functions use the following construction:
  return 1 - strcasecmp(whatever);
The "1" seems very mysterious. I believe that the correct code should be:
  return -strcasecmp(whatever);
It appears in particular that PHP7 strcasecmp was never returning a
value of 1 for the tests in question, but PHP8 strcasecmp does so.
With the corrected code, the tests pass in both PHP7 and PHP8.
  • Loading branch information
oleibman authored and Gianluca Giovinazzo committed Dec 14, 2020
1 parent 91cdec8 commit 98b81f1
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/ReferenceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function columnSort($a, $b)
*/
public static function columnReverseSort($a, $b)
{
return 1 - strcasecmp(strlen($a) . $a, strlen($b) . $b);
return -strcasecmp(strlen($a) . $a, strlen($b) . $b);
}

/**
Expand Down Expand Up @@ -108,7 +108,7 @@ public static function cellReverseSort($a, $b)
[$bc, $br] = sscanf($b, '%[A-Z]%d');

if ($ar === $br) {
return 1 - strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc);
return -strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc);
}

return ($ar < $br) ? 1 : -1;
Expand Down

0 comments on commit 98b81f1

Please sign in to comment.