From a340240a3fd3e793011e8a16da52e2f3fd3c42e4 Mon Sep 17 00:00:00 2001 From: oleibman Date: Sat, 5 Jun 2021 06:14:23 -0700 Subject: [PATCH] PHP8.1 Deprecation Passing Null to String Function (#2137) 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. --- .../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 +- 10 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php b/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php index 2c9bb2de..b2b0e669 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php +++ b/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php @@ -27,7 +27,7 @@ abstract class DatabaseAbstract */ 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 de767fca..d8e88a21 100644 --- a/src/PhpSpreadsheet/Calculation/DateTimeExcel/DateValue.php +++ b/src/PhpSpreadsheet/Calculation/DateTimeExcel/DateValue.php @@ -37,7 +37,7 @@ class DateValue { $dti = new DateTimeImmutable(); $baseYear = SharedDateHelper::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 73b7ba91..2a294344 100644 --- a/src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeValue.php +++ b/src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeValue.php @@ -31,7 +31,7 @@ class TimeValue */ public static function fromString($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 2f994858..015fabfb 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Extract.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Extract.php @@ -26,7 +26,7 @@ class Extract $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 @@ class Extract $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 @@ class Extract $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 6e408891..f3da4bb3 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Text.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Text.php @@ -20,7 +20,7 @@ class Text $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 6e6155c2..ced65e97 100644 --- a/src/PhpSpreadsheet/Reader/Html.php +++ b/src/PhpSpreadsheet/Reader/Html.php @@ -914,7 +914,7 @@ class Html extends BaseReader */ 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 e56ac331..62e1e385 100644 --- a/src/PhpSpreadsheet/Reader/Xml/PageSettings.php +++ b/src/PhpSpreadsheet/Reader/Xml/PageSettings.php @@ -115,7 +115,7 @@ class PageSettings 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 e85ce55d..0e8eb8a5 100644 --- a/src/PhpSpreadsheet/Shared/StringHelper.php +++ b/src/PhpSpreadsheet/Shared/StringHelper.php @@ -502,7 +502,7 @@ class StringHelper */ 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 38dead4f..493fd07e 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -1256,7 +1256,7 @@ class Worksheet extends WriterPart $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 4e8335f1..ea629183 100644 --- a/tests/PhpSpreadsheetTests/Calculation/FunctionsTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/FunctionsTest.php @@ -341,7 +341,7 @@ class FunctionsTest extends TestCase ->disableOriginalConstructor() ->getMock(); $remoteCell->method('isFormula') - ->willReturn(substr($value, 0, 1) == '='); + ->willReturn(substr($value ?? '', 0, 1) == '='); $remoteSheet = $this->getMockBuilder(Worksheet::class) ->disableOriginalConstructor()