From 847f08dcd4a5076a5872d9fdb650c9b426fdfd71 Mon Sep 17 00:00:00 2001
From: Marina Glancy <marina@moodle.com>
Date: Mon, 28 Nov 2022 15:56:35 +0100
Subject: [PATCH] MDL-76494 libs: patch Box::Spout for PHP 8.1 compatibility

---
 lib/spout/readme_moodle.txt                       | 1 +
 lib/spout/src/Spout/Reader/CSV/RowIterator.php    | 5 +++++
 lib/spout/src/Spout/Reader/CSV/SheetIterator.php  | 5 +++++
 lib/spout/src/Spout/Reader/ODS/RowIterator.php    | 6 ++++++
 lib/spout/src/Spout/Reader/ODS/SheetIterator.php  | 6 ++++++
 lib/spout/src/Spout/Reader/Wrapper/XMLReader.php  | 2 ++
 lib/spout/src/Spout/Reader/XLSX/RowIterator.php   | 6 ++++++
 lib/spout/src/Spout/Reader/XLSX/SheetIterator.php | 6 ++++++
 8 files changed, 37 insertions(+)

diff --git a/lib/spout/readme_moodle.txt b/lib/spout/readme_moodle.txt
index 2fab36e584b..b2812a56a4e 100644
--- a/lib/spout/readme_moodle.txt
+++ b/lib/spout/readme_moodle.txt
@@ -12,6 +12,7 @@ MDL-73624 needs to fix with a couple of minor changes to
 Writer/WriterAbstract.php. The changes replace rawurldecode() with
 rawurlencode() in lines 143 and 144.
 by Meirza <meirza.arson@moodle.com>
+MDL-76494 compatibility for PHP 8.1
 
 2021/09/01
 ----------
diff --git a/lib/spout/src/Spout/Reader/CSV/RowIterator.php b/lib/spout/src/Spout/Reader/CSV/RowIterator.php
index 78e66501320..c3f5592858b 100644
--- a/lib/spout/src/Spout/Reader/CSV/RowIterator.php
+++ b/lib/spout/src/Spout/Reader/CSV/RowIterator.php
@@ -84,6 +84,7 @@ class RowIterator implements IteratorInterface
      *
      * @return void
      */
+    #[\ReturnTypeWillChange]
     public function rewind()
     {
         $this->rewindAndSkipBom();
@@ -114,6 +115,7 @@ class RowIterator implements IteratorInterface
      *
      * @return bool
      */
+    #[\ReturnTypeWillChange]
     public function valid()
     {
         return ($this->filePointer && !$this->hasReachedEndOfFile);
@@ -126,6 +128,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()
     {
         $this->hasReachedEndOfFile = $this->globalFunctionsHelper->feof($this->filePointer);
@@ -224,6 +227,7 @@ class RowIterator implements IteratorInterface
      *
      * @return Row|null
      */
+    #[\ReturnTypeWillChange]
     public function current()
     {
         return $this->rowBuffer;
@@ -235,6 +239,7 @@ class RowIterator implements IteratorInterface
      *
      * @return int
      */
+    #[\ReturnTypeWillChange]
     public function key()
     {
         return $this->numReadRows;
diff --git a/lib/spout/src/Spout/Reader/CSV/SheetIterator.php b/lib/spout/src/Spout/Reader/CSV/SheetIterator.php
index 69eb58a0ba9..27669c77c2e 100644
--- a/lib/spout/src/Spout/Reader/CSV/SheetIterator.php
+++ b/lib/spout/src/Spout/Reader/CSV/SheetIterator.php
@@ -30,6 +30,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return void
      */
+    #[\ReturnTypeWillChange]
     public function rewind()
     {
         $this->hasReadUniqueSheet = false;
@@ -41,6 +42,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return bool
      */
+    #[\ReturnTypeWillChange]
     public function valid()
     {
         return (!$this->hasReadUniqueSheet);
@@ -52,6 +54,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return void
      */
+    #[\ReturnTypeWillChange]
     public function next()
     {
         $this->hasReadUniqueSheet = true;
@@ -63,6 +66,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return \Box\Spout\Reader\CSV\Sheet
      */
+    #[\ReturnTypeWillChange]
     public function current()
     {
         return $this->sheet;
@@ -74,6 +78,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return int
      */
+    #[\ReturnTypeWillChange]
     public function key()
     {
         return 1;
diff --git a/lib/spout/src/Spout/Reader/ODS/RowIterator.php b/lib/spout/src/Spout/Reader/ODS/RowIterator.php
index 81f0953d811..27a06001a65 100644
--- a/lib/spout/src/Spout/Reader/ODS/RowIterator.php
+++ b/lib/spout/src/Spout/Reader/ODS/RowIterator.php
@@ -118,6 +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()
     {
         // Because sheet and row data is located in the file, we can't rewind both the
@@ -142,6 +143,7 @@ class RowIterator implements IteratorInterface
      *
      * @return bool
      */
+    #[\ReturnTypeWillChange]
     public function valid()
     {
         return (!$this->hasReachedEndOfFile);
@@ -155,6 +157,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()
     {
         if ($this->doesNeedDataForNextRowToBeProcessed()) {
@@ -356,6 +359,7 @@ class RowIterator implements IteratorInterface
      *
      * @return Row
      */
+    #[\ReturnTypeWillChange]
     public function current()
     {
         return $this->rowBuffer;
@@ -367,6 +371,7 @@ class RowIterator implements IteratorInterface
      *
      * @return int
      */
+    #[\ReturnTypeWillChange]
     public function key()
     {
         return $this->lastRowIndexProcessed;
@@ -377,6 +382,7 @@ class RowIterator implements IteratorInterface
      *
      * @return void
      */
+    #[\ReturnTypeWillChange]
     public function end()
     {
         $this->xmlReader->close();
diff --git a/lib/spout/src/Spout/Reader/ODS/SheetIterator.php b/lib/spout/src/Spout/Reader/ODS/SheetIterator.php
index c7b8cd9dcf8..7dd420bc6c2 100644
--- a/lib/spout/src/Spout/Reader/ODS/SheetIterator.php
+++ b/lib/spout/src/Spout/Reader/ODS/SheetIterator.php
@@ -79,6 +79,7 @@ class SheetIterator implements IteratorInterface
      * @throws \Box\Spout\Common\Exception\IOException If unable to open the XML file containing sheets' data
      * @return void
      */
+    #[\ReturnTypeWillChange]
     public function rewind()
     {
         $this->xmlReader->close();
@@ -131,6 +132,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return bool
      */
+    #[\ReturnTypeWillChange]
     public function valid()
     {
         return $this->hasFoundSheet;
@@ -142,6 +144,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return void
      */
+    #[\ReturnTypeWillChange]
     public function next()
     {
         $this->hasFoundSheet = $this->xmlReader->readUntilNodeFound(self::XML_NODE_TABLE);
@@ -157,6 +160,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return \Box\Spout\Reader\ODS\Sheet
      */
+    #[\ReturnTypeWillChange]
     public function current()
     {
         $escapedSheetName = $this->xmlReader->getAttribute(self::XML_ATTRIBUTE_TABLE_NAME);
@@ -214,6 +218,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return int
      */
+    #[\ReturnTypeWillChange]
     public function key()
     {
         return $this->currentSheetIndex + 1;
@@ -224,6 +229,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return void
      */
+    #[\ReturnTypeWillChange]
     public function end()
     {
         $this->xmlReader->close();
diff --git a/lib/spout/src/Spout/Reader/Wrapper/XMLReader.php b/lib/spout/src/Spout/Reader/Wrapper/XMLReader.php
index 6f85f320c8f..a05b72d85ed 100644
--- a/lib/spout/src/Spout/Reader/Wrapper/XMLReader.php
+++ b/lib/spout/src/Spout/Reader/Wrapper/XMLReader.php
@@ -83,6 +83,7 @@ class XMLReader extends \XMLReader
      * @throws \Box\Spout\Reader\Exception\XMLProcessingException If an error/warning occurred
      * @return bool TRUE on success or FALSE on failure
      */
+    #[\ReturnTypeWillChange]
     public function read()
     {
         $this->useXMLInternalErrors();
@@ -119,6 +120,7 @@ class XMLReader extends \XMLReader
      * @throws \Box\Spout\Reader\Exception\XMLProcessingException If an error/warning occurred
      * @return bool TRUE on success or FALSE on failure
      */
+    #[\ReturnTypeWillChange]
     public function next($localName = null)
     {
         $this->useXMLInternalErrors();
diff --git a/lib/spout/src/Spout/Reader/XLSX/RowIterator.php b/lib/spout/src/Spout/Reader/XLSX/RowIterator.php
index a54b8b192b0..9c891a0415d 100644
--- a/lib/spout/src/Spout/Reader/XLSX/RowIterator.php
+++ b/lib/spout/src/Spout/Reader/XLSX/RowIterator.php
@@ -139,6 +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()
     {
         $this->xmlReader->close();
@@ -163,6 +164,7 @@ class RowIterator implements IteratorInterface
      *
      * @return bool
      */
+    #[\ReturnTypeWillChange]
     public function valid()
     {
         return (!$this->hasReachedEndOfFile);
@@ -176,6 +178,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()
     {
         $this->nextRowIndexToBeProcessed++;
@@ -374,6 +377,7 @@ class RowIterator implements IteratorInterface
      *
      * @return Row|null
      */
+    #[\ReturnTypeWillChange]
     public function current()
     {
         $rowToBeProcessed = $this->rowBuffer;
@@ -399,6 +403,7 @@ class RowIterator implements IteratorInterface
      *
      * @return int
      */
+    #[\ReturnTypeWillChange]
     public function key()
     {
         // TODO: This should return $this->nextRowIndexToBeProcessed
@@ -414,6 +419,7 @@ class RowIterator implements IteratorInterface
      *
      * @return void
      */
+    #[\ReturnTypeWillChange]
     public function end()
     {
         $this->xmlReader->close();
diff --git a/lib/spout/src/Spout/Reader/XLSX/SheetIterator.php b/lib/spout/src/Spout/Reader/XLSX/SheetIterator.php
index 81f481c3cdc..aae8897eef4 100644
--- a/lib/spout/src/Spout/Reader/XLSX/SheetIterator.php
+++ b/lib/spout/src/Spout/Reader/XLSX/SheetIterator.php
@@ -38,6 +38,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return void
      */
+    #[\ReturnTypeWillChange]
     public function rewind()
     {
         $this->currentSheetIndex = 0;
@@ -49,6 +50,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return bool
      */
+    #[\ReturnTypeWillChange]
     public function valid()
     {
         return ($this->currentSheetIndex < \count($this->sheets));
@@ -60,6 +62,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return void
      */
+    #[\ReturnTypeWillChange]
     public function next()
     {
         // Using isset here because it is way faster than array_key_exists...
@@ -77,6 +80,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return \Box\Spout\Reader\XLSX\Sheet
      */
+    #[\ReturnTypeWillChange]
     public function current()
     {
         return $this->sheets[$this->currentSheetIndex];
@@ -88,6 +92,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return int
      */
+    #[\ReturnTypeWillChange]
     public function key()
     {
         return $this->currentSheetIndex + 1;
@@ -98,6 +103,7 @@ class SheetIterator implements IteratorInterface
      *
      * @return void
      */
+    #[\ReturnTypeWillChange]
     public function end()
     {
         // make sure we are not leaking memory in case the iteration stopped before the end