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:
count: 1
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\\.$#"
count: 1
......
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
......@@ -27,9 +29,14 @@ class MailUtility
*
* 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();
$name = self::getSystemFromName();
......@@ -47,14 +54,13 @@ class MailUtility
*
* 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']) {
return $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
}
return null;
$name = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'] ?? null;
return (!empty($name) && is_string($name)) ? $name : null;
}
/**
......@@ -70,11 +76,11 @@ class MailUtility
*
* @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'];
if (!GeneralUtility::validEmail($address)) {
$address = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] ?? null;
if (!is_string($address) || !GeneralUtility::validEmail($address)) {
// still nothing, get host name from server
$address = 'no-reply@' . php_uname('n');
if (!GeneralUtility::validEmail($address)) {
......@@ -90,12 +96,18 @@ class MailUtility
*
* 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
{
$mailConfiguration = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
$replyToAddress = $mailConfiguration['defaultMailReplyToAddress'];
$mailConfiguration = $GLOBALS['TYPO3_CONF_VARS']['MAIL'] ?? [];
$replyToAddress = $mailConfiguration['defaultMailReplyToAddress'] ?? null;
if (empty($replyToAddress) || !GeneralUtility::validEmail($replyToAddress)) {
return [];
}
......@@ -118,14 +130,14 @@ class MailUtility
* @param int $lineWidth The line width
* @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 = [];
$substrStart = 0;
while (strlen($str) > $substrStart) {
$substr = substr($str, $substrStart, $lineWidth);
// has line exceeded (reached) the maximum width?
if (strlen($substr) == $lineWidth) {
if (strlen($substr) === $lineWidth) {
// find last space-char
$spacePos = strrpos(rtrim($substr), ' ');
// space-char found?
......@@ -168,7 +180,7 @@ class MailUtility
* @param string $rawAddresses Comma separated list of email addresses (optionally with display name)
* @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 */
$addressParser = GeneralUtility::makeInstance(
......
......@@ -20,6 +20,7 @@ expectations and existing behavior.
- :php:`\TYPO3\CMS\Core\Utility\DebugUtility`
- :php:`\TYPO3\CMS\Core\Utility\DiffUtility`
- :php:`\TYPO3\CMS\Core\Utility\ExtensionManagementUtility`
- :php:`\TYPO3\CMS\Core\Utility\MailUtility`
- :php:`\TYPO3\CMS\Core\Utility\MathUtility`
- :php:`\TYPO3\CMS\Core\Utility\PathUtility`
- :php:`\TYPO3\CMS\Core\Utility\RootlineUtility`
......
......@@ -21,7 +21,7 @@ use TYPO3\CMS\Core\Utility\MailUtility;
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
{
......@@ -87,8 +87,6 @@ class MailUtilityTest extends UnitTestCase
/**
* Data provider for parseAddressesTest
*
* @return array Data sets
*/
public function parseAddressesProvider(): array
{
......@@ -116,15 +114,12 @@ class MailUtilityTest extends UnitTestCase
* @test
* @dataProvider parseAddressesProvider
*/
public function parseAddressesTest($source, $addressList): void
public function parseAddressesTest(string $source, array $addressList): void
{
$returnArray = MailUtility::parseAddresses($source);
self::assertEquals($addressList, $returnArray);
}
/**
* @return array
*/
public function replyToProvider(): array
{
return [
......
Markdown is supported
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