From bc9816a0fdc1f24d89238ccadfa245670b1b9686 Mon Sep 17 00:00:00 2001
From: Alexander Schnitzler <git@alexanderschnitzler.de>
Date: Mon, 11 May 2020 17:33:41 +0200
Subject: [PATCH] [TASK] Fix phpstan checkFunctionArgumentTypes errors in
 ext:core Mail
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch fixes incompatible type usage in function arguments
and is preparatory work for introducing native type hints and
strict mode in all core files.

Releases: master, 10.4
Resolves: #92282
Change-Id: I7eac7f91fe1461a73f2f3dc5cf6988fb91274de1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65665
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Alexander Schnitzler <git@alexanderschnitzler.de>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
Reviewed-by: Alexander Schnitzler <git@alexanderschnitzler.de>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
---
 typo3/sysext/core/Classes/Mail/FileSpool.php     | 10 +++++-----
 typo3/sysext/core/Classes/Mail/MailMessage.php   |  2 +-
 typo3/sysext/core/Classes/Mail/Mailer.php        |  2 +-
 .../core/Classes/Mail/Rfc822AddressesParser.php  | 16 ++++++++--------
 4 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/typo3/sysext/core/Classes/Mail/FileSpool.php b/typo3/sysext/core/Classes/Mail/FileSpool.php
index 32a6aa044b83..3aa782b8bedf 100644
--- a/typo3/sysext/core/Classes/Mail/FileSpool.php
+++ b/typo3/sysext/core/Classes/Mail/FileSpool.php
@@ -123,7 +123,7 @@ class FileSpool extends AbstractTransport implements DelayedTransportInterface,
     public function recover(int $timeout = 900): void
     {
         foreach (new DirectoryIterator($this->path) as $file) {
-            $file = $file->getRealPath();
+            $file = (string)$file->getRealPath();
 
             if (substr($file, -16) == '.message.sending') {
                 $lockedtime = filectime($file);
@@ -144,7 +144,7 @@ class FileSpool extends AbstractTransport implements DelayedTransportInterface,
         $count = 0;
         $time = time();
         foreach ($directoryIterator as $file) {
-            $file = $file->getRealPath();
+            $file = (string)$file->getRealPath();
 
             if (substr($file, -8) != '.message') {
                 continue;
@@ -152,7 +152,7 @@ class FileSpool extends AbstractTransport implements DelayedTransportInterface,
 
             /* We try a rename, it's an atomic operation, and avoid locking the file */
             if (rename($file, $file . '.sending')) {
-                $message = unserialize(file_get_contents($file . '.sending'), [
+                $message = unserialize((string)file_get_contents($file . '.sending'), [
                     'allowedClasses' => [
                         RawMessage::class,
                         Message::class,
@@ -247,13 +247,13 @@ class FileSpool extends AbstractTransport implements DelayedTransportInterface,
         $result = '';
         $directoryIterator = new DirectoryIterator($this->path);
         foreach ($directoryIterator as $file) {
-            $file = $file->getRealPath();
+            $file = (string)$file->getRealPath();
 
             if (substr($file, -8) != '.message') {
                 continue;
             }
 
-            $result .= file_get_contents($file) . "\n";
+            $result .= (string)file_get_contents($file) . "\n";
         }
         return $result;
     }
diff --git a/typo3/sysext/core/Classes/Mail/MailMessage.php b/typo3/sysext/core/Classes/Mail/MailMessage.php
index 2952fdb99367..0975b81d0329 100644
--- a/typo3/sysext/core/Classes/Mail/MailMessage.php
+++ b/typo3/sysext/core/Classes/Mail/MailMessage.php
@@ -241,7 +241,7 @@ class MailMessage extends Email
     /**
      * Converts address from [email, name] into Address objects.
      *
-     * @param array<int,mixed> $args
+     * @param mixed ...$args
      * @return Address[]
      */
     protected function convertNamedAddress(...$args): array
diff --git a/typo3/sysext/core/Classes/Mail/Mailer.php b/typo3/sysext/core/Classes/Mail/Mailer.php
index b0a9bdf3383e..1121f4398c95 100644
--- a/typo3/sysext/core/Classes/Mail/Mailer.php
+++ b/typo3/sysext/core/Classes/Mail/Mailer.php
@@ -114,7 +114,7 @@ class Mailer implements MailerInterface
                     if ($address === 0) {
                         $replyTo = new Address($replyTo[$address]);
                     } else {
-                        $replyTo = new Address($address, reset($replyTo));
+                        $replyTo = new Address((string)$address, reset($replyTo));
                     }
                     $message->replyTo($replyTo);
                 }
diff --git a/typo3/sysext/core/Classes/Mail/Rfc822AddressesParser.php b/typo3/sysext/core/Classes/Mail/Rfc822AddressesParser.php
index 72d7972b00f5..957adca48840 100644
--- a/typo3/sysext/core/Classes/Mail/Rfc822AddressesParser.php
+++ b/typo3/sysext/core/Classes/Mail/Rfc822AddressesParser.php
@@ -179,20 +179,20 @@ class Rfc822AddressesParser
         $this->error = null;
         $this->index = null;
         // Unfold any long lines in $this->address.
-        $this->address = preg_replace('/\\r?\\n/', '
+        $this->address = (string)preg_replace('/\\r?\\n/', '
 ', $this->address);
-        $this->address = preg_replace('/\\r\\n(\\t| )+/', ' ', $this->address);
+        $this->address = (string)preg_replace('/\\r\\n(\\t| )+/', ' ', $this->address);
         while ($this->address = $this->_splitAddresses($this->address)) {
         }
         if ($this->address === false || isset($this->error)) {
-            throw new \InvalidArgumentException($this->error, 1294681466);
+            throw new \InvalidArgumentException((string)$this->error, 1294681466);
         }
         // Validate each address individually.  If we encounter an invalid
         // address, stop iterating and return an error immediately.
         foreach ($this->addresses as $address) {
             $valid = $this->_validateAddress($address);
             if ($valid === false || isset($this->error)) {
-                throw new \InvalidArgumentException($this->error, 1294681467);
+                throw new \InvalidArgumentException((string)$this->error, 1294681467);
             }
             $this->structure = array_merge($this->structure, $valid);
         }
@@ -223,7 +223,7 @@ class Rfc822AddressesParser
             return false;
         }
         // Split the string based on the above ten or so lines.
-        $parts = explode($split_char, $address);
+        $parts = explode($split_char, $address) ?: [];
         $string = $this->_splitCheck($parts, $split_char);
         // If a group...
         if ($is_group) {
@@ -376,7 +376,7 @@ class Rfc822AddressesParser
      */
     protected function _hasUnclosedBracketsSub($string, &$num, $char)
     {
-        $parts = explode($char, $string);
+        $parts = explode($char, $string) ?: [];
         $partsCounter = count($parts);
         for ($i = 0; $i < $partsCounter; $i++) {
             if (substr($parts[$i], -1) === '\\' || $this->_hasUnclosedQuotes($parts[$i])) {
@@ -535,9 +535,9 @@ class Rfc822AddressesParser
     protected function _validateQuotedString($qstring)
     {
         // Leading and trailing "
-        $qstring = substr($qstring, 1, -1);
+        $qstring = (string)substr($qstring, 1, -1);
         // Perform check, removing quoted characters first.
-        return !preg_match('/[\\x0D\\\\"]/', preg_replace('/\\\\./', '', $qstring));
+        return !preg_match('/[\\x0D\\\\"]/', (string)preg_replace('/\\\\./', '', $qstring));
     }
 
     /**
-- 
GitLab