Commit 7b4d1cd5 authored by crell's avatar crell Committed by Nikita Hovratov
Browse files

[!!!][TASK] Add native types to MailUtility

And remove phpstan exceptions along the way.

Resolves: #97377
Related: #97210
Related: #97372
Releases: main
Change-Id: I7769527eaf8e39150928402037e68b95c2be9cc1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/74290

Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Oliver Klee's avatarOliver Klee <typo3-coding@oliverklee.de>
Tested-by: Nikita Hovratov's avatarNikita Hovratov <nikita.h@live.de>
Reviewed-by: Oliver Klee's avatarOliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Nikita Hovratov's avatarNikita Hovratov <nikita.h@live.de>
parent 902126db
...@@ -1675,16 +1675,6 @@ parameters: ...@@ -1675,16 +1675,6 @@ parameters:
count: 1 count: 1
path: ../../typo3/sysext/core/Classes/Utility/IpAnonymizationUtility.php path: ../../typo3/sysext/core/Classes/Utility/IpAnonymizationUtility.php
-
message: "#^Method TYPO3\\\\CMS\\\\Core\\\\Utility\\\\MailUtility\\:\\:getSystemFrom\\(\\) should return array but returns null\\.$#"
count: 1
path: ../../typo3/sysext/core/Classes/Utility/MailUtility.php
-
message: "#^Method TYPO3\\\\CMS\\\\Core\\\\Utility\\\\MailUtility\\:\\:getSystemFromName\\(\\) should return string but returns null\\.$#"
count: 1
path: ../../typo3/sysext/core/Classes/Utility/MailUtility.php
- -
message: "#^Method TYPO3Fluid\\\\Fluid\\\\View\\\\ViewInterface\\:\\:render\\(\\) invoked with 1 parameter, 0 required\\.$#" message: "#^Method TYPO3Fluid\\\\Fluid\\\\View\\\\ViewInterface\\:\\:render\\(\\) invoked with 1 parameter, 0 required\\.$#"
count: 1 count: 1
......
<?php <?php
declare(strict_types=1);
/* /*
* This file is part of the TYPO3 CMS project. * This file is part of the TYPO3 CMS project.
* *
...@@ -27,9 +29,14 @@ class MailUtility ...@@ -27,9 +29,14 @@ class MailUtility
* *
* Ready to be passed to $mail->setFrom() * Ready to be passed to $mail->setFrom()
* *
* @return array key=Valid email address which can be used as sender, value=Valid name which can be used as a sender. NULL if no address is configured * This method can return three different variants:
* 1. An assoc. array: key => Valid email address which can be used as sender; value => Valid name which can be used as a sender
* 2. A numeric array with one entry: Valid email address which can be used as sender
* 3. Null, if no address is configured
*
* @return array<string|int, string>|null
*/ */
public static function getSystemFrom() public static function getSystemFrom(): ?array
{ {
$address = self::getSystemFromAddress(); $address = self::getSystemFromAddress();
$name = self::getSystemFromName(); $name = self::getSystemFromName();
...@@ -47,14 +54,13 @@ class MailUtility ...@@ -47,14 +54,13 @@ class MailUtility
* *
* As configured in Install Tool. * As configured in Install Tool.
* *
* @return string The name (unquoted, unformatted). NULL if none is set * @return string|null The name (unquoted, unformatted). NULL if none is set or an invalid non-string value.
*/ */
public static function getSystemFromName() public static function getSystemFromName(): ?string
{ {
if ($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']) { $name = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'] ?? null;
return $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
} return (!empty($name) && is_string($name)) ? $name : null;
return null;
} }
/** /**
...@@ -70,11 +76,11 @@ class MailUtility ...@@ -70,11 +76,11 @@ class MailUtility
* *
* @return string An email address * @return string An email address
*/ */
public static function getSystemFromAddress() public static function getSystemFromAddress(): string
{ {
// default, first check the localconf setting $address = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] ?? null;
$address = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
if (!GeneralUtility::validEmail($address)) { if (!is_string($address) || !GeneralUtility::validEmail($address)) {
// still nothing, get host name from server // still nothing, get host name from server
$address = 'no-reply@' . php_uname('n'); $address = 'no-reply@' . php_uname('n');
if (!GeneralUtility::validEmail($address)) { if (!GeneralUtility::validEmail($address)) {
...@@ -90,12 +96,18 @@ class MailUtility ...@@ -90,12 +96,18 @@ class MailUtility
* *
* Ready to be passed to $mail->setReplyTo() * Ready to be passed to $mail->setReplyTo()
* *
* @return array List of email-addresses. Specifying a realname can be done in the form of "replyToName <replyTo@example.com>". * This method returns a list of email addresses, but depending on the existence of "defaultMailReplyToName"
* the array can have a different shape:
*
* 1. An assoc. array: key => a valid reply-to address which can be used as sender; value => a valid reply-to name which can be used as a sender
* 2. A numeric array with one entry: a valid reply-to address which can be used as sender
*
* @return array<string|int, string>
*/ */
public static function getSystemReplyTo(): array public static function getSystemReplyTo(): array
{ {
$mailConfiguration = $GLOBALS['TYPO3_CONF_VARS']['MAIL']; $mailConfiguration = $GLOBALS['TYPO3_CONF_VARS']['MAIL'] ?? [];
$replyToAddress = $mailConfiguration['defaultMailReplyToAddress']; $replyToAddress = $mailConfiguration['defaultMailReplyToAddress'] ?? null;
if (empty($replyToAddress) || !GeneralUtility::validEmail($replyToAddress)) { if (empty($replyToAddress) || !GeneralUtility::validEmail($replyToAddress)) {
return []; return [];
} }
...@@ -118,14 +130,14 @@ class MailUtility ...@@ -118,14 +130,14 @@ class MailUtility
* @param int $lineWidth The line width * @param int $lineWidth The line width
* @return string Reformatted text * @return string Reformatted text
*/ */
public static function breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76) public static function breakLinesForEmail(string $str, string $newlineChar = LF, int $lineWidth = 76): string
{ {
$lines = []; $lines = [];
$substrStart = 0; $substrStart = 0;
while (strlen($str) > $substrStart) { while (strlen($str) > $substrStart) {
$substr = substr($str, $substrStart, $lineWidth); $substr = substr($str, $substrStart, $lineWidth);
// has line exceeded (reached) the maximum width? // has line exceeded (reached) the maximum width?
if (strlen($substr) == $lineWidth) { if (strlen($substr) === $lineWidth) {
// find last space-char // find last space-char
$spacePos = strrpos(rtrim($substr), ' '); $spacePos = strrpos(rtrim($substr), ' ');
// space-char found? // space-char found?
...@@ -168,7 +180,7 @@ class MailUtility ...@@ -168,7 +180,7 @@ class MailUtility
* @param string $rawAddresses Comma separated list of email addresses (optionally with display name) * @param string $rawAddresses Comma separated list of email addresses (optionally with display name)
* @return array Parsed list of addresses. * @return array Parsed list of addresses.
*/ */
public static function parseAddresses($rawAddresses) public static function parseAddresses(string $rawAddresses): array
{ {
/** @var \TYPO3\CMS\Core\Mail\Rfc822AddressesParser $addressParser */ /** @var \TYPO3\CMS\Core\Mail\Rfc822AddressesParser $addressParser */
$addressParser = GeneralUtility::makeInstance( $addressParser = GeneralUtility::makeInstance(
......
...@@ -20,6 +20,7 @@ expectations and existing behavior. ...@@ -20,6 +20,7 @@ expectations and existing behavior.
- :php:`\TYPO3\CMS\Core\Utility\DebugUtility` - :php:`\TYPO3\CMS\Core\Utility\DebugUtility`
- :php:`\TYPO3\CMS\Core\Utility\DiffUtility` - :php:`\TYPO3\CMS\Core\Utility\DiffUtility`
- :php:`\TYPO3\CMS\Core\Utility\ExtensionManagementUtility` - :php:`\TYPO3\CMS\Core\Utility\ExtensionManagementUtility`
- :php:`\TYPO3\CMS\Core\Utility\MailUtility`
- :php:`\TYPO3\CMS\Core\Utility\MathUtility` - :php:`\TYPO3\CMS\Core\Utility\MathUtility`
- :php:`\TYPO3\CMS\Core\Utility\PathUtility` - :php:`\TYPO3\CMS\Core\Utility\PathUtility`
- :php:`\TYPO3\CMS\Core\Utility\RootlineUtility` - :php:`\TYPO3\CMS\Core\Utility\RootlineUtility`
......
...@@ -21,7 +21,7 @@ use TYPO3\CMS\Core\Utility\MailUtility; ...@@ -21,7 +21,7 @@ use TYPO3\CMS\Core\Utility\MailUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase; use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/** /**
* Testcase for the \TYPO3\CMS\Core\Utility\MailUtility class. * @covers \TYPO3\CMS\Core\Utility\MailUtility
*/ */
class MailUtilityTest extends UnitTestCase class MailUtilityTest extends UnitTestCase
{ {
...@@ -87,8 +87,6 @@ class MailUtilityTest extends UnitTestCase ...@@ -87,8 +87,6 @@ class MailUtilityTest extends UnitTestCase
/** /**
* Data provider for parseAddressesTest * Data provider for parseAddressesTest
*
* @return array Data sets
*/ */
public function parseAddressesProvider(): array public function parseAddressesProvider(): array
{ {
...@@ -116,15 +114,12 @@ class MailUtilityTest extends UnitTestCase ...@@ -116,15 +114,12 @@ class MailUtilityTest extends UnitTestCase
* @test * @test
* @dataProvider parseAddressesProvider * @dataProvider parseAddressesProvider
*/ */
public function parseAddressesTest($source, $addressList): void public function parseAddressesTest(string $source, array $addressList): void
{ {
$returnArray = MailUtility::parseAddresses($source); $returnArray = MailUtility::parseAddresses($source);
self::assertEquals($addressList, $returnArray); self::assertEquals($addressList, $returnArray);
} }
/**
* @return array
*/
public function replyToProvider(): array public function replyToProvider(): array
{ {
return [ return [
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment