Php8.1/Phpstan Problems (#2649)

I made some changes to make our code ready when Phpstan is configured to run on Php8. These were regressed. I reinstalled. All was good for an hour or so till Dependabot pushed a version change for Phpstan. Checking that out, it runs clean on Php7, has 3 errors on Php8.0, and 100 errors on Php8.1. Although that number seems alarming, most of these were caused because Phpstan was reporting the same problem in a trait everywhere it was used, and a single change sufficed to eliminate most of those.

The remainder, some of which caused run-time problems with Php8.1 on my system but which are not yet showing up on Github, are mostly the usual "null supplied where string expected". However, a new problem showed up - `float % 2` gets an "implicit conversion from float to int" warning (and Phpunit issues an extraordinarily long message when this happens). There was also a totally undocumented Php change where `array_combine` throws an exception in Php8 when it would have returned `false` as it does in Php7. These problems are all addressed with very minor changes in this PR.
This commit is contained in:
oleibman 2022-03-09 18:05:36 -08:00 committed by GitHub
parent ee1e56d8e7
commit 0d5d9eb0ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 23 additions and 11 deletions

View File

@ -12,12 +12,15 @@ trait ArrayEnabled
*/ */
private static $arrayArgumentHelper; private static $arrayArgumentHelper;
private static function initialiseHelper(array $arguments): void /**
* @param array|false $arguments Can be changed to array for Php8.1+
*/
private static function initialiseHelper($arguments): void
{ {
if (self::$arrayArgumentHelper === null) { if (self::$arrayArgumentHelper === null) {
self::$arrayArgumentHelper = new ArrayArgumentHelper(); self::$arrayArgumentHelper = new ArrayArgumentHelper();
} }
self::$arrayArgumentHelper->initialise($arguments); self::$arrayArgumentHelper->initialise($arguments ?: []);
} }
/** /**
@ -70,6 +73,14 @@ trait ArrayEnabled
return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments); return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments);
} }
/**
* @param mixed $value
*/
private static function testFalse($value): bool
{
return $value === false;
}
/** /**
* Handles array argument processing when the function accepts multiple arguments, * Handles array argument processing when the function accepts multiple arguments,
* but only the last few (from start) can be an array arguments. * but only the last few (from start) can be an array arguments.
@ -85,7 +96,7 @@ trait ArrayEnabled
range($start, count($arguments) - $start), range($start, count($arguments) - $start),
array_slice($arguments, $start) array_slice($arguments, $start)
); );
if ($arrayArgumentsSubset === false) { if (self::testFalse($arrayArgumentsSubset)) {
return ['#VALUE!']; return ['#VALUE!'];
} }

View File

@ -87,7 +87,7 @@ class Value
return ExcelError::VALUE(); return ExcelError::VALUE();
} }
return $value % 2 == 0; return ((int) fmod($value, 2)) === 0;
} }
/** /**
@ -112,7 +112,7 @@ class Value
return ExcelError::VALUE(); return ExcelError::VALUE();
} }
return abs($value) % 2 == 1; return ((int) fmod($value, 2)) !== 0;
} }
/** /**

View File

@ -92,6 +92,7 @@ abstract class Coordinate
} }
// Create absolute coordinate // Create absolute coordinate
$cellAddress = "$cellAddress";
if (ctype_digit($cellAddress)) { if (ctype_digit($cellAddress)) {
return $worksheet . '$' . $cellAddress; return $worksheet . '$' . $cellAddress;
} elseif (ctype_alpha($cellAddress)) { } elseif (ctype_alpha($cellAddress)) {

View File

@ -58,7 +58,7 @@ class Dimension
public function __construct(string $dimension) public function __construct(string $dimension)
{ {
[$size, $unit] = sscanf($dimension, '%[1234567890.]%s'); [$size, $unit] = sscanf($dimension, '%[1234567890.]%s');
$unit = strtolower(trim($unit)); $unit = strtolower(trim($unit ?? ''));
// If a UoM is specified, then convert the size to pixels for internal storage // If a UoM is specified, then convert the size to pixels for internal storage
if (isset(self::ABSOLUTE_UNITS[$unit])) { if (isset(self::ABSOLUTE_UNITS[$unit])) {

View File

@ -794,7 +794,7 @@ class Html
$domText = preg_replace( $domText = preg_replace(
'/\s+/u', '/\s+/u',
' ', ' ',
str_replace(["\r", "\n"], ' ', $textNode->nodeValue) str_replace(["\r", "\n"], ' ', $textNode->nodeValue ?: '')
); );
$this->stringData .= $domText; $this->stringData .= $domText;
$this->buildTextRun(); $this->buildTextRun();

View File

@ -619,7 +619,7 @@ class Html extends BaseReader
{ {
foreach ($element->childNodes as $child) { foreach ($element->childNodes as $child) {
if ($child instanceof DOMText) { if ($child instanceof DOMText) {
$domText = preg_replace('/\s+/u', ' ', trim($child->nodeValue)); $domText = preg_replace('/\s+/u', ' ', trim($child->nodeValue ?: ''));
if (is_string($cellContent)) { if (is_string($cellContent)) {
// simply append the text if the cell content is a plain text string // simply append the text if the cell content is a plain text string
$cellContent .= $domText; $cellContent .= $domText;

View File

@ -617,7 +617,7 @@ class Ods extends BaseReader
foreach ($settings->getElementsByTagNameNS($configNs, 'config-item') as $t) { foreach ($settings->getElementsByTagNameNS($configNs, 'config-item') as $t) {
if ($t->getAttributeNs($configNs, 'name') === 'ActiveTable') { if ($t->getAttributeNs($configNs, 'name') === 'ActiveTable') {
try { try {
$spreadsheet->setActiveSheetIndexByName($t->nodeValue); $spreadsheet->setActiveSheetIndexByName($t->nodeValue ?: '');
} catch (Throwable $e) { } catch (Throwable $e) {
// do nothing // do nothing
} }

View File

@ -386,7 +386,7 @@ class AutoFilter
/** @var string */ /** @var string */
$ruleOperator = $rule['operator']; $ruleOperator = $rule['operator'];
/** @var string */ /** @var string */
$cellValueString = $cellValue; $cellValueString = $cellValue ?? '';
$retVal = false; $retVal = false;
if (is_numeric($ruleValue)) { if (is_numeric($ruleValue)) {

View File

@ -1279,7 +1279,7 @@ class Worksheet extends WriterPart
$objWriter, $objWriter,
$this->getParentWriter()->getOffice2003Compatibility() === false, $this->getParentWriter()->getOffice2003Compatibility() === false,
'v', 'v',
($this->getParentWriter()->getPreCalculateFormulas() && !is_array($calculatedValue) && substr($calculatedValue, 0, 1) !== '#') ($this->getParentWriter()->getPreCalculateFormulas() && !is_array($calculatedValue) && substr($calculatedValue ?? '', 0, 1) !== '#')
? StringHelper::formatNumber($calculatedValue) : '0' ? StringHelper::formatNumber($calculatedValue) : '0'
); );
} }