Skip to content

Commit 0d5d9eb

Browse files
authored
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.
1 parent ee1e56d commit 0d5d9eb

File tree

9 files changed

+23
-11
lines changed

9 files changed

+23
-11
lines changed

src/PhpSpreadsheet/Calculation/ArrayEnabled.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ trait ArrayEnabled
1212
*/
1313
private static $arrayArgumentHelper;
1414

15-
private static function initialiseHelper(array $arguments): void
15+
/**
16+
* @param array|false $arguments Can be changed to array for Php8.1+
17+
*/
18+
private static function initialiseHelper($arguments): void
1619
{
1720
if (self::$arrayArgumentHelper === null) {
1821
self::$arrayArgumentHelper = new ArrayArgumentHelper();
1922
}
20-
self::$arrayArgumentHelper->initialise($arguments);
23+
self::$arrayArgumentHelper->initialise($arguments ?: []);
2124
}
2225

2326
/**
@@ -70,6 +73,14 @@ protected static function evaluateArrayArgumentsSubset(callable $method, int $li
7073
return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments);
7174
}
7275

76+
/**
77+
* @param mixed $value
78+
*/
79+
private static function testFalse($value): bool
80+
{
81+
return $value === false;
82+
}
83+
7384
/**
7485
* Handles array argument processing when the function accepts multiple arguments,
7586
* but only the last few (from start) can be an array arguments.
@@ -85,7 +96,7 @@ protected static function evaluateArrayArgumentsSubsetFrom(callable $method, int
8596
range($start, count($arguments) - $start),
8697
array_slice($arguments, $start)
8798
);
88-
if ($arrayArgumentsSubset === false) {
99+
if (self::testFalse($arrayArgumentsSubset)) {
89100
return ['#VALUE!'];
90101
}
91102

src/PhpSpreadsheet/Calculation/Information/Value.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static function isEven($value = null)
8787
return ExcelError::VALUE();
8888
}
8989

90-
return $value % 2 == 0;
90+
return ((int) fmod($value, 2)) === 0;
9191
}
9292

9393
/**
@@ -112,7 +112,7 @@ public static function isOdd($value = null)
112112
return ExcelError::VALUE();
113113
}
114114

115-
return abs($value) % 2 == 1;
115+
return ((int) fmod($value, 2)) !== 0;
116116
}
117117

118118
/**

src/PhpSpreadsheet/Cell/Coordinate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public static function absoluteReference($cellAddress)
9292
}
9393

9494
// Create absolute coordinate
95+
$cellAddress = "$cellAddress";
9596
if (ctype_digit($cellAddress)) {
9697
return $worksheet . '$' . $cellAddress;
9798
} elseif (ctype_alpha($cellAddress)) {

src/PhpSpreadsheet/Helper/Dimension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Dimension
5858
public function __construct(string $dimension)
5959
{
6060
[$size, $unit] = sscanf($dimension, '%[1234567890.]%s');
61-
$unit = strtolower(trim($unit));
61+
$unit = strtolower(trim($unit ?? ''));
6262

6363
// If a UoM is specified, then convert the size to pixels for internal storage
6464
if (isset(self::ABSOLUTE_UNITS[$unit])) {

src/PhpSpreadsheet/Helper/Html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ protected function parseTextNode(DOMText $textNode): void
794794
$domText = preg_replace(
795795
'/\s+/u',
796796
' ',
797-
str_replace(["\r", "\n"], ' ', $textNode->nodeValue)
797+
str_replace(["\r", "\n"], ' ', $textNode->nodeValue ?: '')
798798
);
799799
$this->stringData .= $domText;
800800
$this->buildTextRun();

src/PhpSpreadsheet/Reader/Html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ protected function processDomElement(DOMNode $element, Worksheet $sheet, int &$r
619619
{
620620
foreach ($element->childNodes as $child) {
621621
if ($child instanceof DOMText) {
622-
$domText = preg_replace('/\s+/u', ' ', trim($child->nodeValue));
622+
$domText = preg_replace('/\s+/u', ' ', trim($child->nodeValue ?: ''));
623623
if (is_string($cellContent)) {
624624
// simply append the text if the cell content is a plain text string
625625
$cellContent .= $domText;

src/PhpSpreadsheet/Reader/Ods.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ private function lookForActiveSheet(DOMElement $settings, Spreadsheet $spreadshe
617617
foreach ($settings->getElementsByTagNameNS($configNs, 'config-item') as $t) {
618618
if ($t->getAttributeNs($configNs, 'name') === 'ActiveTable') {
619619
try {
620-
$spreadsheet->setActiveSheetIndexByName($t->nodeValue);
620+
$spreadsheet->setActiveSheetIndexByName($t->nodeValue ?: '');
621621
} catch (Throwable $e) {
622622
// do nothing
623623
}

src/PhpSpreadsheet/Worksheet/AutoFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ private static function filterTestInCustomDataSet($cellValue, $ruleSet)
386386
/** @var string */
387387
$ruleOperator = $rule['operator'];
388388
/** @var string */
389-
$cellValueString = $cellValue;
389+
$cellValueString = $cellValue ?? '';
390390
$retVal = false;
391391

392392
if (is_numeric($ruleValue)) {

src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ private function writeCellFormula(XMLWriter $objWriter, string $cellValue, Cell
12791279
$objWriter,
12801280
$this->getParentWriter()->getOffice2003Compatibility() === false,
12811281
'v',
1282-
($this->getParentWriter()->getPreCalculateFormulas() && !is_array($calculatedValue) && substr($calculatedValue, 0, 1) !== '#')
1282+
($this->getParentWriter()->getPreCalculateFormulas() && !is_array($calculatedValue) && substr($calculatedValue ?? '', 0, 1) !== '#')
12831283
? StringHelper::formatNumber($calculatedValue) : '0'
12841284
);
12851285
}

0 commit comments

Comments
 (0)