Skip to content

Commit b0bfdde

Browse files
authored
Cleanup After Phpstan Upgrade (#2800)
After Phpstan 1.6.3 upgrade, clean up some new problems that will show up if it runs under Php 8+.
1 parent 4d5a482 commit b0bfdde

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,6 @@ parameters:
360360
count: 1
361361
path: src/PhpSpreadsheet/Calculation/Engineering/ErfC.php
362362

363-
-
364-
message: "#^Parameter \\#1 \\$callback of function set_error_handler expects \\(callable\\(int, string, string, int, array\\)\\: bool\\)\\|null, array\\{'PhpOffice\\\\\\\\PhpSpreadsheet\\\\\\\\Calculation\\\\\\\\Exception', 'errorHandlerCallback'\\} given\\.$#"
365-
count: 1
366-
path: src/PhpSpreadsheet/Calculation/ExceptionHandler.php
367-
368363
-
369364
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\:\\:ISPMT\\(\\) has no return type specified\\.$#"
370365
count: 1

phpstan-conditional.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
'path' => __DIR__ . '/src/PhpSpreadsheet/Shared/StringHelper.php',
4747
'count' => 1,
4848
];
49+
} else {
50+
// Flagged in Php8+ - unsure how to correct code
51+
$config['parameters']['ignoreErrors'][] = [
52+
'message' => '#^Binary operation "/" between float and array[|]float[|]int[|]string results in an error.#',
53+
'path' => __DIR__ . '/src/PhpSpreadsheet/Calculation/MathTrig/Combinations.php',
54+
'count' => 2,
55+
];
4956
}
5057

5158
return $config;

src/PhpSpreadsheet/Calculation/ExceptionHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ class ExceptionHandler
99
*/
1010
public function __construct()
1111
{
12-
set_error_handler([Exception::class, 'errorHandlerCallback'], E_ALL);
12+
/** @var callable */
13+
$callable = [Exception::class, 'errorHandlerCallback'];
14+
set_error_handler($callable, E_ALL);
1315
}
1416

1517
/**

src/PhpSpreadsheet/Reader/Slk.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ private function processPRecord(array $rowData, Spreadsheet &$spreadsheet): void
450450

451451
break;
452452
case 'M':
453-
$formatArray['font']['size'] = substr($rowDatum, 1) / 20;
453+
$formatArray['font']['size'] = ((float) substr($rowDatum, 1)) / 20;
454454

455455
break;
456456
case 'L':

src/PhpSpreadsheet/Reader/Xls/MD5.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
class MD5
66
{
7-
// Context
8-
97
/**
108
* @var int
119
*/
@@ -26,11 +24,17 @@ class MD5
2624
*/
2725
private $d;
2826

27+
/**
28+
* @var int
29+
*/
30+
private static $allOneBits;
31+
2932
/**
3033
* MD5 stream constructor.
3134
*/
3235
public function __construct()
3336
{
37+
self::$allOneBits = self::signedInt(0xffffffff);
3438
$this->reset();
3539
}
3640

@@ -40,8 +44,8 @@ public function __construct()
4044
public function reset(): void
4145
{
4246
$this->a = 0x67452301;
43-
$this->b = 0xEFCDAB89;
44-
$this->c = 0x98BADCFE;
47+
$this->b = self::signedInt(0xEFCDAB89);
48+
$this->c = self::signedInt(0x98BADCFE);
4549
$this->d = 0x10325476;
4650
}
4751

@@ -156,10 +160,10 @@ public function add(string $data): void
156160
self::step($I, $C, $D, $A, $B, $words[2], 15, 0x2ad7d2bb);
157161
self::step($I, $B, $C, $D, $A, $words[9], 21, 0xeb86d391);
158162

159-
$this->a = ($this->a + $A) & 0xffffffff;
160-
$this->b = ($this->b + $B) & 0xffffffff;
161-
$this->c = ($this->c + $C) & 0xffffffff;
162-
$this->d = ($this->d + $D) & 0xffffffff;
163+
$this->a = ($this->a + $A) & self::$allOneBits;
164+
$this->b = ($this->b + $B) & self::$allOneBits;
165+
$this->c = ($this->c + $C) & self::$allOneBits;
166+
$this->d = ($this->d + $D) & self::$allOneBits;
163167
}
164168

165169
private static function f(int $X, int $Y, int $Z): int
@@ -182,18 +186,25 @@ private static function i(int $X, int $Y, int $Z): int
182186
return $Y ^ ($X | (~$Z)); // Y XOR (X OR NOT Z)
183187
}
184188

185-
private static function step(callable $func, int &$A, int $B, int $C, int $D, int $M, int $s, int $t): void
189+
/** @param float|int $t may be float on 32-bit system */
190+
private static function step(callable $func, int &$A, int $B, int $C, int $D, int $M, int $s, $t): void
186191
{
187-
$A = ($A + call_user_func($func, $B, $C, $D) + $M + $t) & 0xffffffff;
192+
$t = self::signedInt($t);
193+
$A = ($A + call_user_func($func, $B, $C, $D) + $M + $t) & self::$allOneBits;
188194
$A = self::rotate($A, $s);
189-
$A = ($B + $A) & 0xffffffff;
195+
$A = ($B + $A) & self::$allOneBits;
196+
}
197+
198+
/** @param float|int $result may be float on 32-bit system */
199+
private static function signedInt($result): int
200+
{
201+
return is_int($result) ? $result : (int) (PHP_INT_MIN + $result - 1 - PHP_INT_MAX);
190202
}
191203

192-
/** @return float|int */
193-
private static function rotate(int $decimal, int $bits)
204+
private static function rotate(int $decimal, int $bits): int
194205
{
195206
$binary = str_pad(decbin($decimal), 32, '0', STR_PAD_LEFT);
196207

197-
return bindec(substr($binary, $bits) . substr($binary, 0, $bits));
208+
return self::signedInt(bindec(substr($binary, $bits) . substr($binary, 0, $bits)));
198209
}
199210
}

0 commit comments

Comments
 (0)