From a2299057608c69c3f931291b2609466305853aa8 Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Thu, 13 Jan 2022 22:37:18 +0100 Subject: [PATCH] MDL-78262 lib_spout: Update box/spout to address PHP 8.1 deprecation --- lib/spout/readme_moodle.txt | 7 +++++++ .../Common/Helper/GlobalFunctionsHelper.php | 2 +- .../src/Spout/Reader/CSV/RowIterator.php | 21 +++++++------------ .../src/Spout/Reader/CSV/SheetIterator.php | 21 +++++++------------ .../src/Spout/Reader/ODS/RowIterator.php | 18 ++++++---------- .../src/Spout/Reader/XLSX/RowIterator.php | 18 ++++++---------- 6 files changed, 36 insertions(+), 51 deletions(-) diff --git a/lib/spout/readme_moodle.txt b/lib/spout/readme_moodle.txt index 8b02de1fcdf..0d178e4f5cc 100644 --- a/lib/spout/readme_moodle.txt +++ b/lib/spout/readme_moodle.txt @@ -4,6 +4,13 @@ Description of Spout library import * Only include the src/Spout directory. * Update lib/thirdpartylibs.xml with the latest version. +2023/05/26 +---------- +MDL-78262 lib_spout: Update box/spout to address PHP 8.1 deprecation +This change is a direct pull from an upstream fix: +https://github.com/openspout/openspout/commit/e75f6f73012b81fd5fee6107d0af9e86c458448e +This addresses the deprecation of str_replace() in PHP 8.1. + 2022/11/25 ---------- Imported PHP 8.1 patch from OpenSpout/OpenSpout 4.8.1 diff --git a/lib/spout/src/Spout/Common/Helper/GlobalFunctionsHelper.php b/lib/spout/src/Spout/Common/Helper/GlobalFunctionsHelper.php index 08d475dbe90..1f860d78f96 100644 --- a/lib/spout/src/Spout/Common/Helper/GlobalFunctionsHelper.php +++ b/lib/spout/src/Spout/Common/Helper/GlobalFunctionsHelper.php @@ -82,7 +82,7 @@ class GlobalFunctionsHelper * @param int|null $length * @param string|null $delimiter * @param string|null $enclosure - * @return array + * @return array|false */ public function fgetcsv($handle, $length = null, $delimiter = null, $enclosure = null) { diff --git a/lib/spout/src/Spout/Reader/CSV/RowIterator.php b/lib/spout/src/Spout/Reader/CSV/RowIterator.php index c3f5592858b..3f7306c2f63 100644 --- a/lib/spout/src/Spout/Reader/CSV/RowIterator.php +++ b/lib/spout/src/Spout/Reader/CSV/RowIterator.php @@ -84,8 +84,7 @@ class RowIterator implements IteratorInterface * * @return void */ - #[\ReturnTypeWillChange] - public function rewind() + public function rewind() : void { $this->rewindAndSkipBom(); @@ -115,8 +114,7 @@ class RowIterator implements IteratorInterface * * @return bool */ - #[\ReturnTypeWillChange] - public function valid() + public function valid() : bool { return ($this->filePointer && !$this->hasReachedEndOfFile); } @@ -128,8 +126,7 @@ class RowIterator implements IteratorInterface * @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8 * @return void */ - #[\ReturnTypeWillChange] - public function next() + public function next() : void { $this->hasReachedEndOfFile = $this->globalFunctionsHelper->feof($this->filePointer); @@ -149,8 +146,8 @@ class RowIterator implements IteratorInterface } while ($this->shouldReadNextRow($rowData)); if ($rowData !== false) { - // str_replace will replace NULL values by empty strings - $rowDataBufferAsArray = \str_replace(null, null, $rowData); + // array_map will replace NULL values by empty strings + $rowDataBufferAsArray = array_map(function ($value) { return (string) $value; }, $rowData); $this->rowBuffer = $this->entityFactory->createRowFromArray($rowDataBufferAsArray); $this->numReadRows++; } else { @@ -227,8 +224,7 @@ class RowIterator implements IteratorInterface * * @return Row|null */ - #[\ReturnTypeWillChange] - public function current() + public function current() : ?Row { return $this->rowBuffer; } @@ -239,8 +235,7 @@ class RowIterator implements IteratorInterface * * @return int */ - #[\ReturnTypeWillChange] - public function key() + public function key() : int { return $this->numReadRows; } @@ -250,7 +245,7 @@ class RowIterator implements IteratorInterface * * @return void */ - public function end() + public function end() : void { // do nothing } diff --git a/lib/spout/src/Spout/Reader/CSV/SheetIterator.php b/lib/spout/src/Spout/Reader/CSV/SheetIterator.php index 27669c77c2e..84c3c809147 100644 --- a/lib/spout/src/Spout/Reader/CSV/SheetIterator.php +++ b/lib/spout/src/Spout/Reader/CSV/SheetIterator.php @@ -10,7 +10,7 @@ use Box\Spout\Reader\IteratorInterface; */ class SheetIterator implements IteratorInterface { - /** @var \Box\Spout\Reader\CSV\Sheet The CSV unique "sheet" */ + /** @var Sheet The CSV unique "sheet" */ protected $sheet; /** @var bool Whether the unique "sheet" has already been read */ @@ -30,8 +30,7 @@ class SheetIterator implements IteratorInterface * * @return void */ - #[\ReturnTypeWillChange] - public function rewind() + public function rewind() : void { $this->hasReadUniqueSheet = false; } @@ -42,8 +41,7 @@ class SheetIterator implements IteratorInterface * * @return bool */ - #[\ReturnTypeWillChange] - public function valid() + public function valid() : bool { return (!$this->hasReadUniqueSheet); } @@ -54,8 +52,7 @@ class SheetIterator implements IteratorInterface * * @return void */ - #[\ReturnTypeWillChange] - public function next() + public function next() : void { $this->hasReadUniqueSheet = true; } @@ -64,10 +61,9 @@ class SheetIterator implements IteratorInterface * Return the current element * @see http://php.net/manual/en/iterator.current.php * - * @return \Box\Spout\Reader\CSV\Sheet + * @return Sheet */ - #[\ReturnTypeWillChange] - public function current() + public function current() : Sheet { return $this->sheet; } @@ -78,8 +74,7 @@ class SheetIterator implements IteratorInterface * * @return int */ - #[\ReturnTypeWillChange] - public function key() + public function key() : int { return 1; } @@ -89,7 +84,7 @@ class SheetIterator implements IteratorInterface * * @return void */ - public function end() + public function end() : void { // do nothing } diff --git a/lib/spout/src/Spout/Reader/ODS/RowIterator.php b/lib/spout/src/Spout/Reader/ODS/RowIterator.php index 27a06001a65..a196dd8e72b 100644 --- a/lib/spout/src/Spout/Reader/ODS/RowIterator.php +++ b/lib/spout/src/Spout/Reader/ODS/RowIterator.php @@ -118,8 +118,7 @@ class RowIterator implements IteratorInterface * @throws \Box\Spout\Reader\Exception\IteratorNotRewindableException If the iterator is rewound more than once * @return void */ - #[\ReturnTypeWillChange] - public function rewind() + public function rewind() : void { // Because sheet and row data is located in the file, we can't rewind both the // sheet iterator and the row iterator, as XML file cannot be read backwards. @@ -143,8 +142,7 @@ class RowIterator implements IteratorInterface * * @return bool */ - #[\ReturnTypeWillChange] - public function valid() + public function valid() : bool { return (!$this->hasReachedEndOfFile); } @@ -157,8 +155,7 @@ class RowIterator implements IteratorInterface * @throws \Box\Spout\Common\Exception\IOException If unable to read the sheet data XML * @return void */ - #[\ReturnTypeWillChange] - public function next() + public function next() : void { if ($this->doesNeedDataForNextRowToBeProcessed()) { $this->readDataForNextRow(); @@ -359,8 +356,7 @@ class RowIterator implements IteratorInterface * * @return Row */ - #[\ReturnTypeWillChange] - public function current() + public function current() : Row { return $this->rowBuffer; } @@ -371,8 +367,7 @@ class RowIterator implements IteratorInterface * * @return int */ - #[\ReturnTypeWillChange] - public function key() + public function key() : int { return $this->lastRowIndexProcessed; } @@ -382,8 +377,7 @@ class RowIterator implements IteratorInterface * * @return void */ - #[\ReturnTypeWillChange] - public function end() + public function end() : void { $this->xmlReader->close(); } diff --git a/lib/spout/src/Spout/Reader/XLSX/RowIterator.php b/lib/spout/src/Spout/Reader/XLSX/RowIterator.php index 9c891a0415d..14be2b30e3e 100644 --- a/lib/spout/src/Spout/Reader/XLSX/RowIterator.php +++ b/lib/spout/src/Spout/Reader/XLSX/RowIterator.php @@ -139,8 +139,7 @@ class RowIterator implements IteratorInterface * @throws \Box\Spout\Common\Exception\IOException If the sheet data XML cannot be read * @return void */ - #[\ReturnTypeWillChange] - public function rewind() + public function rewind() : void { $this->xmlReader->close(); @@ -164,8 +163,7 @@ class RowIterator implements IteratorInterface * * @return bool */ - #[\ReturnTypeWillChange] - public function valid() + public function valid() : bool { return (!$this->hasReachedEndOfFile); } @@ -178,8 +176,7 @@ class RowIterator implements IteratorInterface * @throws \Box\Spout\Common\Exception\IOException If unable to read the sheet data XML * @return void */ - #[\ReturnTypeWillChange] - public function next() + public function next() : void { $this->nextRowIndexToBeProcessed++; @@ -377,8 +374,7 @@ class RowIterator implements IteratorInterface * * @return Row|null */ - #[\ReturnTypeWillChange] - public function current() + public function current() : ?Row { $rowToBeProcessed = $this->rowBuffer; @@ -403,8 +399,7 @@ class RowIterator implements IteratorInterface * * @return int */ - #[\ReturnTypeWillChange] - public function key() + public function key() : int { // TODO: This should return $this->nextRowIndexToBeProcessed // but to avoid a breaking change, the return value for @@ -419,8 +414,7 @@ class RowIterator implements IteratorInterface * * @return void */ - #[\ReturnTypeWillChange] - public function end() + public function end() : void { $this->xmlReader->close(); }