From 1e45f3def46f57cb7d2c72dd64cfb5c600f7b2de Mon Sep 17 00:00:00 2001 From: Owen Leibman Date: Wed, 2 Jun 2021 06:06:31 -0700 Subject: [PATCH] PHP8.1 Deprecation Passing Null to String Function For each of the files in this PR, one or more statements can pass a null to string functions like strlower. This is deprecated in PHP8.1, and, when deprecated messages are enabled, causes many tests to error out. In every case, use coercion to pass null string rather than null. --- src/PhpSpreadsheet/Calculation/Calculation.php | 6 +++--- .../Calculation/Database/DatabaseAbstract.php | 2 +- src/PhpSpreadsheet/Calculation/DateTimeExcel/DateValue.php | 2 +- src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeValue.php | 2 +- src/PhpSpreadsheet/Calculation/TextData/Extract.php | 6 +++--- src/PhpSpreadsheet/Calculation/TextData/Text.php | 2 +- src/PhpSpreadsheet/Reader/Html.php | 2 +- src/PhpSpreadsheet/Reader/Xml/PageSettings.php | 2 +- src/PhpSpreadsheet/Shared/StringHelper.php | 2 +- src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php | 2 +- tests/PhpSpreadsheetTests/Calculation/FunctionsTest.php | 2 +- 11 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/Calculation.php b/src/PhpSpreadsheet/Calculation/Calculation.php index ec85df69af..981e17e212 100644 --- a/src/PhpSpreadsheet/Calculation/Calculation.php +++ b/src/PhpSpreadsheet/Calculation/Calculation.php @@ -4562,7 +4562,7 @@ private function processTokenStack($tokens, $cellID = null, ?Cell $pCell = null) } else { $this->executeNumericBinaryOperation($multiplier, $arg, '*', 'arrayTimesEquals', $stack); } - } elseif (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/i', $token, $matches)) { + } elseif (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/i', $token ?? '', $matches)) { $cellRef = null; if (isset($matches[8])) { if ($pCell === null) { @@ -4637,7 +4637,7 @@ private function processTokenStack($tokens, $cellID = null, ?Cell $pCell = null) } // if the token is a function, pop arguments off the stack, hand them to the function, and push the result back on - } elseif (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/miu', $token, $matches)) { + } elseif (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/miu', $token ?? '', $matches)) { if ($pCellParent) { $pCell->attach($pCellParent); } @@ -4727,7 +4727,7 @@ private function processTokenStack($tokens, $cellID = null, ?Cell $pCell = null) } } else { // if the token is a number, boolean, string or an Excel error, push it onto the stack - if (isset(self::$excelConstants[strtoupper($token)])) { + if (isset(self::$excelConstants[strtoupper($token ?? '')])) { $excelConstant = strtoupper($token); $stack->push('Constant Value', self::$excelConstants[$excelConstant]); if (isset($storeKey)) { diff --git a/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php b/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php index 2c9bb2de14..b2b0e6691e 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php +++ b/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php @@ -27,7 +27,7 @@ abstract public static function evaluate($database, $field, $criteria); */ protected static function fieldExtract(array $database, $field): ?int { - $field = strtoupper(Functions::flattenSingleValue($field)); + $field = strtoupper(Functions::flattenSingleValue($field ?? '')); if ($field === '') { return null; } diff --git a/src/PhpSpreadsheet/Calculation/DateTimeExcel/DateValue.php b/src/PhpSpreadsheet/Calculation/DateTimeExcel/DateValue.php index 86b8d3d9a0..be94c64433 100644 --- a/src/PhpSpreadsheet/Calculation/DateTimeExcel/DateValue.php +++ b/src/PhpSpreadsheet/Calculation/DateTimeExcel/DateValue.php @@ -37,7 +37,7 @@ public static function funcDateValue($dateValue) { $dti = new DateTimeImmutable(); $baseYear = Date::getExcelCalendar(); - $dateValue = trim(Functions::flattenSingleValue($dateValue), '"'); + $dateValue = trim(Functions::flattenSingleValue($dateValue ?? ''), '"'); // Strip any ordinals because they're allowed in Excel (English only) $dateValue = preg_replace('/(\d)(st|nd|rd|th)([ -\/])/Ui', '$1$3', $dateValue); // Convert separators (/ . or space) to hyphens (should also handle dot used for ordinals in some countries, e.g. Denmark, Germany) diff --git a/src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeValue.php b/src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeValue.php index 2366b1d69f..72e55d2c42 100644 --- a/src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeValue.php +++ b/src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeValue.php @@ -31,7 +31,7 @@ class TimeValue */ public static function funcTimeValue($timeValue) { - $timeValue = trim(Functions::flattenSingleValue($timeValue), '"'); + $timeValue = trim(Functions::flattenSingleValue($timeValue ?? ''), '"'); $timeValue = str_replace(['/', '.'], '-', $timeValue); $arraySplit = preg_split('/[\/:\-\s]/', $timeValue); diff --git a/src/PhpSpreadsheet/Calculation/TextData/Extract.php b/src/PhpSpreadsheet/Calculation/TextData/Extract.php index 2f994858ec..015fabfb84 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Extract.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Extract.php @@ -26,7 +26,7 @@ public static function left($value = '', $chars = 1): string $value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE(); } - return mb_substr($value, 0, $chars, 'UTF-8'); + return mb_substr($value ?? '', 0, $chars, 'UTF-8'); } /** @@ -50,7 +50,7 @@ public static function mid($value = '', $start = 1, $chars = null): string $value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE(); } - return mb_substr($value, --$start, $chars, 'UTF-8'); + return mb_substr($value ?? '', --$start, $chars, 'UTF-8'); } /** @@ -72,6 +72,6 @@ public static function right($value = '', $chars = 1): string $value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE(); } - return mb_substr($value, mb_strlen($value, 'UTF-8') - $chars, $chars, 'UTF-8'); + return mb_substr($value ?? '', mb_strlen($value ?? '', 'UTF-8') - $chars, $chars, 'UTF-8'); } } diff --git a/src/PhpSpreadsheet/Calculation/TextData/Text.php b/src/PhpSpreadsheet/Calculation/TextData/Text.php index 6e408891a2..f3da4bb339 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Text.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Text.php @@ -20,7 +20,7 @@ public static function length($value = ''): int $value = ($value) ? Calculation::getTRUE() : Calculation::getFALSE(); } - return mb_strlen($value, 'UTF-8'); + return mb_strlen($value ?? '', 'UTF-8'); } /** diff --git a/src/PhpSpreadsheet/Reader/Html.php b/src/PhpSpreadsheet/Reader/Html.php index f235f9b1b1..df6d343c8a 100644 --- a/src/PhpSpreadsheet/Reader/Html.php +++ b/src/PhpSpreadsheet/Reader/Html.php @@ -914,7 +914,7 @@ private function applyInlineStyle(&$sheet, $row, $column, $attributeArray): void */ public function getStyleColor($value) { - if (strpos($value, '#') === 0) { + if (strpos($value ?? '', '#') === 0) { return substr($value, 1); } diff --git a/src/PhpSpreadsheet/Reader/Xml/PageSettings.php b/src/PhpSpreadsheet/Reader/Xml/PageSettings.php index e56ac331f3..62e1e3851d 100644 --- a/src/PhpSpreadsheet/Reader/Xml/PageSettings.php +++ b/src/PhpSpreadsheet/Reader/Xml/PageSettings.php @@ -115,7 +115,7 @@ private function printSetup(SimpleXMLElement $xmlX, stdClass $printDefaults): st private function setLayout(stdClass $printDefaults, SimpleXMLElement $pageSetupAttributes): void { - $printDefaults->orientation = (string) strtolower($pageSetupAttributes->Orientation) ?: PageSetup::ORIENTATION_PORTRAIT; + $printDefaults->orientation = (string) strtolower($pageSetupAttributes->Orientation ?? '') ?: PageSetup::ORIENTATION_PORTRAIT; $printDefaults->horizontalCentered = (bool) $pageSetupAttributes->CenterHorizontal ?: false; $printDefaults->verticalCentered = (bool) $pageSetupAttributes->CenterVertical ?: false; } diff --git a/src/PhpSpreadsheet/Shared/StringHelper.php b/src/PhpSpreadsheet/Shared/StringHelper.php index e85ce55d2d..0e8eb8a5f3 100644 --- a/src/PhpSpreadsheet/Shared/StringHelper.php +++ b/src/PhpSpreadsheet/Shared/StringHelper.php @@ -502,7 +502,7 @@ public static function strToUpper($pValue) */ public static function strToLower($pValue) { - return mb_convert_case($pValue, MB_CASE_LOWER, 'UTF-8'); + return mb_convert_case($pValue ?? '', MB_CASE_LOWER, 'UTF-8'); } /** diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php index 3978eb6f04..ae76674a38 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -1249,7 +1249,7 @@ private function writeCellFormula(XMLWriter $objWriter, string $cellValue, Cell $objWriter, $this->getParentWriter()->getOffice2003Compatibility() === false, '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' ); } diff --git a/tests/PhpSpreadsheetTests/Calculation/FunctionsTest.php b/tests/PhpSpreadsheetTests/Calculation/FunctionsTest.php index 4e8335f173..ea629183cd 100644 --- a/tests/PhpSpreadsheetTests/Calculation/FunctionsTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/FunctionsTest.php @@ -341,7 +341,7 @@ public function testIsFormula($expectedResult, $reference, $value = 'undefined') ->disableOriginalConstructor() ->getMock(); $remoteCell->method('isFormula') - ->willReturn(substr($value, 0, 1) == '='); + ->willReturn(substr($value ?? '', 0, 1) == '='); $remoteSheet = $this->getMockBuilder(Worksheet::class) ->disableOriginalConstructor()