From 0a531cf1cd875e1922bd3fc6a2a3fdbbcf8716d3 Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Sat, 30 Apr 2022 18:36:24 -0700 Subject: [PATCH] Replace 'self' with self::class in 2 Modules (#2773) PHP 8.2 is supposed to deprecate the use of `['self', 'functionname']` for callables, suggesting the use of `[self::class, 'functionname']` instead. We made this change in a recent PR, and, while I'm thinking about it, I'll fix the remaining 2 modules with this construction. Vlookup is already adequately covered in unit tests. Reader/Xls/MD5 is not; a unit test is added. --- phpstan-baseline.neon | 50 ------------------- .../Calculation/LookupRef/VLookup.php | 6 ++- src/PhpSpreadsheet/Reader/Xls/MD5.php | 19 +++---- .../Reader/Xls/Md5Test.php | 17 +++++++ 4 files changed, 31 insertions(+), 61 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Reader/Xls/Md5Test.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index bdbbf4a8..fc010b55 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -790,26 +790,6 @@ parameters: count: 3 path: src/PhpSpreadsheet/Calculation/LookupRef/RowColumnInformation.php - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\LookupRef\\\\VLookup\\:\\:vlookupSort\\(\\) has no return type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/LookupRef/VLookup.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\LookupRef\\\\VLookup\\:\\:vlookupSort\\(\\) has parameter \\$a with no type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/LookupRef/VLookup.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\LookupRef\\\\VLookup\\:\\:vlookupSort\\(\\) has parameter \\$b with no type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/LookupRef/VLookup.php - - - - message: "#^Parameter \\#2 \\$callback of function uasort expects callable\\(T, T\\)\\: int, array\\{'self', 'vlookupSort'\\} given\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/LookupRef/VLookup.php - - message: "#^Binary operation \"/\" between array\\|float\\|int\\|string and array\\|float\\|int\\|string results in an error\\.$#" count: 2 @@ -2370,36 +2350,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/Reader/Xls/ErrorCode.php - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\MD5\\:\\:f\\(\\) has no return type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Reader/Xls/MD5.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\MD5\\:\\:g\\(\\) has no return type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Reader/Xls/MD5.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\MD5\\:\\:h\\(\\) has no return type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Reader/Xls/MD5.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\MD5\\:\\:i\\(\\) has no return type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Reader/Xls/MD5.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\MD5\\:\\:rotate\\(\\) has no return type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Reader/Xls/MD5.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\MD5\\:\\:step\\(\\) has parameter \\$func with no type specified\\.$#" - count: 1 - path: src/PhpSpreadsheet/Reader/Xls/MD5.php - - message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\RC4\\:\\:\\$i has no type specified\\.$#" count: 1 diff --git a/src/PhpSpreadsheet/Calculation/LookupRef/VLookup.php b/src/PhpSpreadsheet/Calculation/LookupRef/VLookup.php index bc8624f3..52d5a191 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef/VLookup.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef/VLookup.php @@ -49,7 +49,9 @@ class VLookup extends LookupBase $firstColumn = array_shift($columnKeys) ?? 1; if (!$notExactMatch) { - uasort($lookupArray, ['self', 'vlookupSort']); + /** @var callable */ + $callable = [self::class, 'vlookupSort']; + uasort($lookupArray, $callable); } $rowNumber = self::vLookupSearch($lookupValue, $lookupArray, $firstColumn, $notExactMatch); @@ -62,7 +64,7 @@ class VLookup extends LookupBase return ExcelError::NA(); } - private static function vlookupSort($a, $b) + private static function vlookupSort(array $a, array $b): int { reset($a); $firstColumn = key($a); diff --git a/src/PhpSpreadsheet/Reader/Xls/MD5.php b/src/PhpSpreadsheet/Reader/Xls/MD5.php index 14b6bc54..947b5c44 100644 --- a/src/PhpSpreadsheet/Reader/Xls/MD5.php +++ b/src/PhpSpreadsheet/Reader/Xls/MD5.php @@ -79,10 +79,10 @@ class MD5 $C = $this->c; $D = $this->d; - $F = ['self', 'f']; - $G = ['self', 'g']; - $H = ['self', 'h']; - $I = ['self', 'i']; + $F = [self::class, 'f']; + $G = [self::class, 'g']; + $H = [self::class, 'h']; + $I = [self::class, 'i']; // ROUND 1 self::step($F, $A, $B, $C, $D, $words[0], 7, 0xd76aa478); @@ -162,33 +162,34 @@ class MD5 $this->d = ($this->d + $D) & 0xffffffff; } - private static function f(int $X, int $Y, int $Z) + private static function f(int $X, int $Y, int $Z): int { return ($X & $Y) | ((~$X) & $Z); // X AND Y OR NOT X AND Z } - private static function g(int $X, int $Y, int $Z) + private static function g(int $X, int $Y, int $Z): int { return ($X & $Z) | ($Y & (~$Z)); // X AND Z OR Y AND NOT Z } - private static function h(int $X, int $Y, int $Z) + private static function h(int $X, int $Y, int $Z): int { return $X ^ $Y ^ $Z; // X XOR Y XOR Z } - private static function i(int $X, int $Y, int $Z) + private static function i(int $X, int $Y, int $Z): int { return $Y ^ ($X | (~$Z)); // Y XOR (X OR NOT Z) } - private static function step($func, int &$A, int $B, int $C, int $D, int $M, int $s, int $t): void + private static function step(callable $func, int &$A, int $B, int $C, int $D, int $M, int $s, int $t): void { $A = ($A + call_user_func($func, $B, $C, $D) + $M + $t) & 0xffffffff; $A = self::rotate($A, $s); $A = ($B + $A) & 0xffffffff; } + /** @return float|int */ private static function rotate(int $decimal, int $bits) { $binary = str_pad(decbin($decimal), 32, '0', STR_PAD_LEFT); diff --git a/tests/PhpSpreadsheetTests/Reader/Xls/Md5Test.php b/tests/PhpSpreadsheetTests/Reader/Xls/Md5Test.php new file mode 100644 index 00000000..f86d1c55 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/Xls/Md5Test.php @@ -0,0 +1,17 @@ +add('123456789a123456789b123456789c123456789d123456789e123456789f1234'); + $context = $md5->getContext(); + self::assertSame('0761293f016b925b0bca11b34f1ed613', bin2hex($context)); + } +}