From 7043e2e61776836215342a67fbb627fe53ea721e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 6 Aug 2022 16:25:21 +0700 Subject: [PATCH 1/4] Apply PHP 8.0 syntax and property promotion Signed-off-by: Abdul Malik Ikhsan --- src/AddressList.php | 1 - src/Header/AbstractAddressList.php | 18 ++++------ src/Header/HeaderLocator.php | 2 +- src/Header/HeaderWrap.php | 10 +++--- src/Header/IdentificationField.php | 4 +-- src/Headers.php | 12 ++++--- src/Message.php | 4 +-- src/Protocol/AbstractProtocol.php | 20 +++-------- src/Protocol/Imap.php | 10 +++--- src/Protocol/Pop3.php | 4 +-- src/Protocol/Smtp.php | 2 +- src/Protocol/SmtpPluginManagerFactory.php | 1 - src/Storage/AbstractStorage.php | 10 +++--- src/Storage/Folder.php | 40 +++++++-------------- src/Storage/Folder/Maildir.php | 13 +++---- src/Storage/Folder/Mbox.php | 4 +-- src/Storage/Imap.php | 6 ++-- src/Storage/Maildir.php | 12 +++---- src/Storage/Mbox.php | 6 ++-- src/Storage/ParamsNormalizer.php | 3 +- src/Storage/Part.php | 14 ++++---- src/Storage/Pop3.php | 6 ++-- src/Storage/Writable/Maildir.php | 20 ++++++----- src/Transport/FileOptions.php | 4 +-- src/Transport/Sendmail.php | 4 +-- src/Transport/Smtp.php | 3 +- src/Transport/SmtpOptions.php | 1 - test/AddressListTest.php | 7 ++-- test/Header/GenericHeaderTest.php | 6 ++-- test/Header/HeaderLocatorTest.php | 4 +-- test/Header/SenderTest.php | 17 +++++---- test/HeadersTest.php | 2 +- test/MessageFactoryTest.php | 3 +- test/MessageTest.php | 3 +- test/Protocol/AbstractProtocolTest.php | 9 ++--- test/Storage/MaildirFolderTest.php | 4 +-- test/Storage/MaildirMessageOldTest.php | 2 +- test/Storage/MaildirTest.php | 2 +- test/Storage/MaildirWritableTest.php | 6 ++-- test/Storage/MboxFolderTest.php | 2 +- test/Storage/MboxTest.php | 2 +- test/Storage/MessageTest.php | 2 +- test/Storage/ParamsNormalizerTest.php | 3 +- test/TestAsset/StringSerializableObject.php | 15 +++----- test/Transport/FactoryTest.php | 5 ++- test/Transport/FileOptionsTest.php | 5 ++- test/Transport/FileTest.php | 6 ++-- 47 files changed, 142 insertions(+), 197 deletions(-) diff --git a/src/AddressList.php b/src/AddressList.php index 2a1cd16f..f34e7c5d 100644 --- a/src/AddressList.php +++ b/src/AddressList.php @@ -111,7 +111,6 @@ public function addFromString($address, $comment = null) /** * Merge another address list into this one * - * @param AddressList $addressList * @return AddressList */ public function merge(self $addressList) diff --git a/src/Header/AbstractAddressList.php b/src/Header/AbstractAddressList.php index 0e66cb31..11a9a758 100644 --- a/src/Header/AbstractAddressList.php +++ b/src/Header/AbstractAddressList.php @@ -15,8 +15,8 @@ use function preg_match_all; use function preg_replace; use function sprintf; +use function str_contains; use function str_replace; -use function strpos; use function strtolower; use function trim; @@ -94,16 +94,13 @@ public static function fromString($headerLine) $wasEncoded = false; $addresses = array_map( - function ($value) use (&$wasEncoded) { + static function ($value) use (&$wasEncoded): ?Address { $decodedValue = HeaderWrap::mimeDecodeValue($value); $wasEncoded = $wasEncoded || ($decodedValue !== $value); - - $value = trim($decodedValue); - - $comments = self::getComments($value); - $value = self::stripComments($value); - - $value = preg_replace( + $value = trim($decodedValue); + $comments = self::getComments($value); + $value = self::stripComments($value); + $value = preg_replace( [ '#(?getName(); // quote $name if value requires so - if (! empty($name) && (false !== strpos($name, ',') || false !== strpos($name, ';'))) { + if (! empty($name) && (str_contains($name, ',') || str_contains($name, ';'))) { // FIXME: what if name contains double quote? $name = sprintf('"%s"', $name); } diff --git a/src/Header/HeaderLocator.php b/src/Header/HeaderLocator.php index 7420cf17..aea47759 100644 --- a/src/Header/HeaderLocator.php +++ b/src/Header/HeaderLocator.php @@ -12,7 +12,7 @@ final class HeaderLocator implements HeaderLocatorInterface { /** @var array Pre-aliased Header plugins */ - private $plugins = [ + private array $plugins = [ 'bcc' => Bcc::class, 'cc' => Cc::class, 'contentdisposition' => ContentDisposition::class, diff --git a/src/Header/HeaderWrap.php b/src/Header/HeaderWrap.php index f33e0633..46163012 100644 --- a/src/Header/HeaderWrap.php +++ b/src/Header/HeaderWrap.php @@ -11,6 +11,8 @@ use function iconv_mime_decode; use function iconv_mime_encode; use function implode; +use function str_contains; +use function str_starts_with; use function strlen; use function strpos; use function wordwrap; @@ -119,9 +121,7 @@ public static function mimeDecodeValue($value) if (self::isNotDecoded($value, $decodedValue) && extension_loaded('imap')) { return array_reduce( imap_mime_header_decode(imap_utf8($value)), - function ($accumulator, $headerPart) { - return $accumulator . $headerPart->text; - }, + static fn($accumulator, $headerPart) => $accumulator . $headerPart->text, '' ); } @@ -131,9 +131,9 @@ function ($accumulator, $headerPart) { private static function isNotDecoded(string $originalValue, string $value): bool { - return 0 === strpos($value, '=?') + return str_starts_with($value, '=?') && strlen($value) - 2 === strpos($value, '?=') - && false !== strpos($originalValue, $value); + && str_contains($originalValue, $value); } /** diff --git a/src/Header/IdentificationField.php b/src/Header/IdentificationField.php index b8664120..79fd2e42 100644 --- a/src/Header/IdentificationField.php +++ b/src/Header/IdentificationField.php @@ -76,9 +76,7 @@ public function getFieldName() */ public function getFieldValue($format = HeaderInterface::FORMAT_RAW) { - return implode(Headers::FOLDING, array_map(function ($id) { - return sprintf('<%s>', $id); - }, $this->messageIds)); + return implode(Headers::FOLDING, array_map(static fn($id) => sprintf('<%s>', $id), $this->messageIds)); } /** diff --git a/src/Headers.php b/src/Headers.php index 3e516639..36832e1b 100644 --- a/src/Headers.php +++ b/src/Headers.php @@ -10,11 +10,13 @@ use Laminas\Loader\PluginClassLocator; use Laminas\Mail\Header\GenericHeader; use Laminas\Mail\Header\HeaderInterface; +use Laminas\Mail\Header\HeaderLocatorInterface; use ReturnTypeWillChange; use Traversable; use function array_keys; use function array_shift; +use function assert; use function count; use function current; use function explode; @@ -49,8 +51,7 @@ class Headers implements Countable, Iterator /** @var string Start of Line when folding */ public const FOLDING = "\r\n "; - /** @var null|Header\HeaderLocatorInterface */ - private $headerLocator; + private ?HeaderLocatorInterface $headerLocator = null; /** * @todo Remove for 3.0.0. @@ -193,11 +194,14 @@ public function getPluginClassLoader() * * Lazyloads a Header\HeaderLocator instance if necessary. */ - public function getHeaderLocator(): Header\HeaderLocatorInterface + public function getHeaderLocator(): HeaderLocatorInterface { if (! $this->headerLocator) { $this->setHeaderLocator(new Header\HeaderLocator()); } + + assert($this->headerLocator instanceof HeaderLocatorInterface); + return $this->headerLocator; } @@ -205,7 +209,7 @@ public function getHeaderLocator(): Header\HeaderLocatorInterface * @todo Return self when we update to 7.4 or later as minimum PHP version. * @return $this */ - public function setHeaderLocator(Header\HeaderLocatorInterface $headerLocator) + public function setHeaderLocator(HeaderLocatorInterface $headerLocator) { $this->headerLocator = $headerLocator; return $this; diff --git a/src/Message.php b/src/Message.php index eda49a69..f0e74840 100644 --- a/src/Message.php +++ b/src/Message.php @@ -305,11 +305,9 @@ public function getReplyTo() /** * setSender * - * @param mixed $emailOrAddress - * @param mixed $name * @return Message */ - public function setSender($emailOrAddress, $name = null) + public function setSender(mixed $emailOrAddress, mixed $name = null) { /** @var Sender $header */ $header = $this->getHeaderByName('sender', Sender::class); diff --git a/src/Protocol/AbstractProtocol.php b/src/Protocol/AbstractProtocol.php index d4dabd5c..9dfe67c3 100644 --- a/src/Protocol/AbstractProtocol.php +++ b/src/Protocol/AbstractProtocol.php @@ -18,10 +18,10 @@ use function restore_error_handler; use function set_error_handler; use function sprintf; +use function str_starts_with; use function stream_get_meta_data; use function stream_set_timeout; use function stream_socket_client; -use function strpos; use const E_WARNING; use const PREG_SPLIT_DELIM_CAPTURE; @@ -58,13 +58,6 @@ abstract class AbstractProtocol */ protected $host; - /** - * Port number of connection - * - * @var int - */ - protected $port; - /** * Instance of Laminas\Validator\ValidatorChain to check hostnames * @@ -95,17 +88,15 @@ abstract class AbstractProtocol /** * Log of mail requests and server responses for a session - * - * @var array */ - private $log = []; + private array $log = []; /** * @param string $host OPTIONAL Hostname of remote connection (default: 127.0.0.1) * @param int $port OPTIONAL Port number (default: null) * @throws Exception\RuntimeException */ - public function __construct($host = '127.0.0.1', $port = null) + public function __construct($host = '127.0.0.1', protected $port = null) { $this->validHost = new Validator\ValidatorChain(); $this->validHost->attach(new Validator\Hostname(Validator\Hostname::ALLOW_ALL)); @@ -115,7 +106,6 @@ public function __construct($host = '127.0.0.1', $port = null) } $this->host = $host; - $this->port = $port; } /** @@ -227,7 +217,7 @@ protected function _connect($remote) // open connection set_error_handler( - function ($error, $message = '') { + static function ($error, $message = '') { throw new Exception\RuntimeException(sprintf('Could not open socket: %s', $message), $error); }, E_WARNING @@ -359,7 +349,7 @@ protected function _expect($code, $timeout = null) } // The '-' message prefix indicates an information string instead of a response string. - } while (strpos($more, '-') === 0); + } while (str_starts_with($more, '-')); if ($errMsg !== '') { throw new Exception\RuntimeException($errMsg); diff --git a/src/Protocol/Imap.php b/src/Protocol/Imap.php index 8f75911e..d8b20724 100644 --- a/src/Protocol/Imap.php +++ b/src/Protocol/Imap.php @@ -24,7 +24,9 @@ use function next; use function preg_match; use function rtrim; +use function str_contains; use function str_replace; +use function str_starts_with; use function stream_socket_enable_crypto; use function strlen; use function strpos; @@ -154,7 +156,7 @@ protected function nextLine() protected function assumedNextLine($start) { $line = $this->nextLine(); - return strpos($line, $start) === 0; + return str_starts_with($line, $start); } /** @@ -318,7 +320,7 @@ public function readResponse($tag, $dontParse = false) // last line has response code if ($tokens[0] == 'OK') { - return $lines ? $lines : true; + return $lines ?: true; } elseif ($tokens[0] == 'NO') { return false; } @@ -385,7 +387,7 @@ public function requestAndResponse($command, $tokens = [], $dontParse = false) public function escapeString($string) { if (func_num_args() < 2) { - if (strpos($string, "\n") !== false) { + if (str_contains($string, "\n")) { return ['{' . strlen($string) . '}', $string]; } @@ -441,7 +443,7 @@ public function logout() if ($this->socket) { try { $result = $this->requestAndResponse('LOGOUT', [], true); - } catch (Exception\ExceptionInterface $e) { + } catch (Exception\ExceptionInterface) { // ignoring exception } fclose($this->socket); diff --git a/src/Protocol/Pop3.php b/src/Protocol/Pop3.php index 9ddbfa4f..a097823c 100644 --- a/src/Protocol/Pop3.php +++ b/src/Protocol/Pop3.php @@ -209,7 +209,7 @@ public function logout() if ($this->socket) { try { $this->request('QUIT'); - } catch (Exception\ExceptionInterface $e) { + } catch (Exception\ExceptionInterface) { // ignore error - we're closing the socket anyway } @@ -242,7 +242,7 @@ public function login($user, $password, $tryApop = true) try { $this->request("APOP $user " . md5($this->timestamp . $password)); return; - } catch (Exception\ExceptionInterface $e) { + } catch (Exception\ExceptionInterface) { // ignore } } diff --git a/src/Protocol/Smtp.php b/src/Protocol/Smtp.php index b8b117e0..f35ca72c 100644 --- a/src/Protocol/Smtp.php +++ b/src/Protocol/Smtp.php @@ -326,7 +326,7 @@ protected function ehlo($host) try { $this->_send('EHLO ' . $host); $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2 - } catch (Exception\ExceptionInterface $e) { + } catch (Exception\ExceptionInterface) { $this->_send('HELO ' . $host); $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2 } diff --git a/src/Protocol/SmtpPluginManagerFactory.php b/src/Protocol/SmtpPluginManagerFactory.php index 956855f9..f63b8509 100644 --- a/src/Protocol/SmtpPluginManagerFactory.php +++ b/src/Protocol/SmtpPluginManagerFactory.php @@ -47,7 +47,6 @@ public function createService(ServiceLocatorInterface $container, $name = null, /** * laminas-servicemanager v2 support for invocation options. * - * @param array $options * @psalm-param ServiceManagerConfiguration $options * @return void */ diff --git a/src/Storage/AbstractStorage.php b/src/Storage/AbstractStorage.php index 94d21c81..3c8e19ea 100644 --- a/src/Storage/AbstractStorage.php +++ b/src/Storage/AbstractStorage.php @@ -8,7 +8,7 @@ use ReturnTypeWillChange; use SeekableIterator; -use function strpos; +use function str_starts_with; use function strtolower; use function substr; @@ -67,7 +67,7 @@ abstract class AbstractStorage implements */ public function __get($var) { - if (strpos($var, 'has') === 0) { + if (str_starts_with($var, 'has')) { $var = strtolower(substr($var, 3)); return $this->has[$var] ?? null; } @@ -211,7 +211,7 @@ public function offsetExists($id) if ($this->getMessage($id)) { return true; } - } catch (Exception\ExceptionInterface $e) { + } catch (Exception\ExceptionInterface) { } return false; @@ -232,12 +232,10 @@ public function offsetGet($id) /** * ArrayAccess::offsetSet() * - * @param mixed $id - * @param mixed $value * @throws Exception\RuntimeException */ #[ReturnTypeWillChange] - public function offsetSet($id, $value) + public function offsetSet(mixed $id, mixed $value) { throw new Exception\RuntimeException('cannot write mail messages via array access'); } diff --git a/src/Storage/Folder.php b/src/Storage/Folder.php index 52053e46..29994355 100644 --- a/src/Storage/Folder.php +++ b/src/Storage/Folder.php @@ -4,28 +4,15 @@ use RecursiveIterator; use ReturnTypeWillChange; +use Stringable; use function current; use function key; use function next; use function reset; -class Folder implements RecursiveIterator +class Folder implements RecursiveIterator, Stringable { - /** - * subfolders of folder array(localName => \Laminas\Mail\Storage\Folder folder) - * - * @var array - */ - protected $folders; - - /** - * local name (name of folder in parent folder) - * - * @var string - */ - protected $localName; - /** * global name (absolute name of folder) * @@ -33,13 +20,6 @@ class Folder implements RecursiveIterator */ protected $globalName; - /** - * folder is selectable if folder is able to hold messages, otherwise it is a parent folder - * - * @var bool - */ - protected $selectable = true; - /** * create a new mail folder instance * @@ -49,12 +29,16 @@ class Folder implements RecursiveIterator * just a parent for subfolders (Default: true) * @param array $folders init with given instances of Folder as subfolders */ - public function __construct($localName, $globalName = '', $selectable = true, array $folders = []) - { - $this->localName = $localName; + public function __construct( + protected $localName, + $globalName = '', + protected $selectable = true, + /** + * subfolders of folder array(localName => \Laminas\Mail\Storage\Folder folder) + */ + protected array $folders = [] + ) { $this->globalName = $globalName ?: $localName; - $this->selectable = $selectable; - $this->folders = $folders; } /** @@ -173,7 +157,7 @@ public function __unset($name) * * @return string global name of folder */ - public function __toString() + public function __toString(): string { return (string) $this->getGlobalName(); } diff --git a/src/Storage/Folder/Maildir.php b/src/Storage/Folder/Maildir.php index 199e8045..e31fcaf9 100644 --- a/src/Storage/Folder/Maildir.php +++ b/src/Storage/Folder/Maildir.php @@ -18,8 +18,9 @@ use function readdir; use function rtrim; use function sort; +use function str_contains; +use function str_starts_with; use function strlen; -use function strpos; use function substr; use function trim; @@ -136,9 +137,9 @@ protected function buildFolderTree() foreach ($dirs as $dir) { do { - if (strpos($dir, $parent) === 0) { - $local = substr($dir, strlen($parent)); - if (strpos($local, $this->delim) !== false) { + if (str_starts_with($dir, $parent)) { + $local = substr($dir, strlen((string) $parent)); + if (str_contains($local, $this->delim)) { throw new Exception\RuntimeException('error while reading maildir'); } array_push($stack, $parent); @@ -173,14 +174,14 @@ public function getFolders($rootFolder = null) } // rootdir is same as INBOX in maildir - if (strpos($rootFolder, 'INBOX' . $this->delim) === 0) { + if (str_starts_with($rootFolder, 'INBOX' . $this->delim)) { $rootFolder = substr($rootFolder, 6); } $currentFolder = $this->rootFolder; $subname = trim($rootFolder, $this->delim); while ($currentFolder) { - if (false !== strpos($subname, $this->delim)) { + if (str_contains($subname, $this->delim)) { [$entry, $subname] = explode($this->delim, $subname, 2); } else { $entry = $subname; diff --git a/src/Storage/Folder/Mbox.php b/src/Storage/Folder/Mbox.php index 991a02e1..3b6f540a 100644 --- a/src/Storage/Folder/Mbox.php +++ b/src/Storage/Folder/Mbox.php @@ -16,7 +16,7 @@ use function readdir; use function rtrim; use function sprintf; -use function strpos; +use function str_contains; use function trim; use const DIRECTORY_SEPARATOR; @@ -149,7 +149,7 @@ public function getFolders($rootFolder = null) $currentFolder = $this->rootFolder; $subname = trim($rootFolder, DIRECTORY_SEPARATOR); while ($currentFolder) { - if (false !== strpos($subname, DIRECTORY_SEPARATOR)) { + if (str_contains($subname, DIRECTORY_SEPARATOR)) { [$entry, $subname] = explode(DIRECTORY_SEPARATOR, $subname, 2); } else { $entry = $subname; diff --git a/src/Storage/Imap.php b/src/Storage/Imap.php index 04a1e329..25dc1b13 100644 --- a/src/Storage/Imap.php +++ b/src/Storage/Imap.php @@ -12,7 +12,7 @@ use function in_array; use function is_string; use function ksort; -use function strpos; +use function str_starts_with; use function strrpos; use function substr; @@ -357,8 +357,8 @@ public function getFolders($rootFolder = null) foreach ($folders as $globalName => $data) { do { - if (! $parent || strpos($globalName, $parent) === 0) { - $pos = strrpos($globalName, $data['delim']); + if (! $parent || str_starts_with($globalName, ! is_string($parent) ? (string) $parent : $parent)) { + $pos = strrpos($globalName, (string) $data['delim']); if ($pos === false) { $localName = $globalName; } else { diff --git a/src/Storage/Maildir.php b/src/Storage/Maildir.php index 19f68e93..4936d699 100644 --- a/src/Storage/Maildir.php +++ b/src/Storage/Maildir.php @@ -25,10 +25,10 @@ use function opendir; use function readdir; use function sprintf; +use function str_contains; use function strcmp; use function stream_get_contents; use function strlen; -use function strpos; use function substr; use function trim; use function usort; @@ -332,14 +332,14 @@ protected function getMaildirFiles($dh, $dirname, $defaultFlags = []) continue; } - if (false !== strpos($entry, ':')) { + if (str_contains($entry, ':')) { [$uniq, $info] = explode(':', $entry, 2); } else { $uniq = $entry; $info = ''; } - if (false !== strpos($uniq, ',')) { + if (str_contains($uniq, ',')) { [, $size] = explode(',', $uniq, 2); } else { $size = ''; @@ -353,7 +353,7 @@ protected function getMaildirFiles($dh, $dirname, $defaultFlags = []) $size = null; } - if (false !== strpos($info, ',')) { + if (str_contains($info, ',')) { [$version, $flags] = explode(',', $info, 2); } else { $version = $info; @@ -383,9 +383,7 @@ protected function getMaildirFiles($dh, $dirname, $defaultFlags = []) $this->files[] = $data; } - usort($this->files, function ($a, $b): int { - return strcmp($a['filename'], $b['filename']); - }); + usort($this->files, static fn($a, $b): int => strcmp($a['filename'], $b['filename'])); } /** diff --git a/src/Storage/Mbox.php b/src/Storage/Mbox.php index d0f219cf..b4db7bd1 100644 --- a/src/Storage/Mbox.php +++ b/src/Storage/Mbox.php @@ -19,9 +19,9 @@ use function is_resource; use function is_subclass_of; use function range; +use function str_starts_with; use function stream_get_contents; use function strlen; -use function strpos; use function strtolower; use function trim; @@ -259,7 +259,7 @@ protected function isMboxFile($file, $fileIsString = true) $result = false; $line = fgets($file) ?: ''; - if (strpos($line, 'From ') === 0) { + if (str_starts_with($line, 'From ')) { $result = true; } @@ -307,7 +307,7 @@ protected function openMboxFile($filename) $messagePos = ['start' => ftell($this->fh), 'separator' => 0, 'end' => 0]; while (($line = fgets($this->fh)) !== false) { - if (strpos($line, 'From ') === 0) { + if (str_starts_with($line, 'From ')) { $messagePos['end'] = ftell($this->fh) - strlen($line) - 2; // + newline if (! $messagePos['separator']) { $messagePos['separator'] = $messagePos['end']; diff --git a/src/Storage/ParamsNormalizer.php b/src/Storage/ParamsNormalizer.php index c9f57b80..a6ad81c5 100644 --- a/src/Storage/ParamsNormalizer.php +++ b/src/Storage/ParamsNormalizer.php @@ -18,10 +18,9 @@ final class ParamsNormalizer { /** - * @param mixed $params * @return array */ - public static function normalizeParams($params): array + public static function normalizeParams(mixed $params): array { if ($params instanceof Traversable) { $params = iterator_to_array($params); diff --git a/src/Storage/Part.php b/src/Storage/Part.php index 0abb76d8..26b761c0 100644 --- a/src/Storage/Part.php +++ b/src/Storage/Part.php @@ -9,6 +9,7 @@ use Laminas\Mime\Exception\RuntimeException; use RecursiveIterator; use ReturnTypeWillChange; +use Stringable; use function array_map; use function count; @@ -22,7 +23,7 @@ use function strtolower; use function trim; -class Part implements RecursiveIterator, Part\PartInterface +class Part implements RecursiveIterator, Part\PartInterface, Stringable { /** * Headers of the part @@ -109,7 +110,7 @@ public function __construct(array $params) $this->messageNum = $params['id']; } - $params['strict'] = $params['strict'] ?? false; + $params['strict'] ??= false; if (isset($params['raw'])) { Mime\Decode::splitMessage( @@ -146,7 +147,7 @@ public function isMultipart() { try { return stripos($this->contentType, 'multipart/') === 0; - } catch (Exception\ExceptionInterface $e) { + } catch (Exception\ExceptionInterface) { return false; } } @@ -332,9 +333,8 @@ public function getHeader($name, $format = null) } else { $return = trim(implode( Mime\Mime::LINEEND, - array_map(static function ($header): string { - return $header->getFieldValue(HeaderInterface::FORMAT_RAW); - }, iterator_to_array($header)) + array_map(static fn($header): string + => $header->getFieldValue(HeaderInterface::FORMAT_RAW), iterator_to_array($header)) ), Mime\Mime::LINEEND); } break; @@ -411,7 +411,7 @@ public function __isset($name) * * @return string content */ - public function __toString() + public function __toString(): string { return $this->getContent(); } diff --git a/src/Storage/Pop3.php b/src/Storage/Pop3.php index 4a9e5a6f..633f3161 100644 --- a/src/Storage/Pop3.php +++ b/src/Storage/Pop3.php @@ -49,7 +49,7 @@ public function countMessages() */ public function getSize($id = 0) { - $id = $id ? $id : null; + $id = $id ?: null; return $this->protocol->getList($id); } @@ -276,7 +276,7 @@ public function __get($var) // need to make a real call, because not all server are honest in their capas try { $this->protocol->top(1, 0, false); - } catch (MailException\ExceptionInterface $e) { + } catch (MailException\ExceptionInterface) { // ignoring error } } @@ -288,7 +288,7 @@ public function __get($var) $id = null; try { $id = $this->protocol->uniqueid(1); - } catch (MailException\ExceptionInterface $e) { + } catch (MailException\ExceptionInterface) { // ignoring error } $this->has['uniqueid'] = (bool) $id; diff --git a/src/Storage/Writable/Maildir.php b/src/Storage/Writable/Maildir.php index bdab378a..fe8a162a 100644 --- a/src/Storage/Writable/Maildir.php +++ b/src/Storage/Writable/Maildir.php @@ -47,6 +47,8 @@ use function rmdir; use function rtrim; use function sleep; +use function str_contains; +use function str_starts_with; use function stream_copy_to_stream; use function strlen; use function strpos; @@ -168,18 +170,18 @@ public function createFolder($name, $parentFolder = null) $exists = null; try { $exists = $this->getFolders($folder); - } catch (MailException\ExceptionInterface $e) { + } catch (MailException\ExceptionInterface) { // ok } if ($exists) { throw new StorageException\RuntimeException('folder already exists'); } - if (strpos($folder, $this->delim . $this->delim) !== false) { + if (str_contains($folder, $this->delim . $this->delim)) { throw new StorageException\RuntimeException('invalid name - folder parts may not be empty'); } - if (strpos($folder, 'INBOX' . $this->delim) === 0) { + if (str_starts_with($folder, 'INBOX' . $this->delim)) { $folder = substr($folder, 6); } @@ -187,7 +189,7 @@ public function createFolder($name, $parentFolder = null) // check if we got tricked and would create a dir outside of the rootdir or not as direct child if ( - strpos($folder, DIRECTORY_SEPARATOR) !== false || strpos($folder, '/') !== false + str_contains($folder, DIRECTORY_SEPARATOR) || str_contains($folder, '/') || dirname($fulldir) . DIRECTORY_SEPARATOR != $this->rootdir ) { throw new StorageException\RuntimeException('invalid name - no directory separator allowed in folder name'); @@ -200,7 +202,7 @@ public function createFolder($name, $parentFolder = null) $parent = substr($folder, 0, strrpos($folder, $this->delim)); try { $this->getFolders($parent); - } catch (MailException\ExceptionInterface $e) { + } catch (MailException\ExceptionInterface) { // does not - create parent folder $this->createFolder($parent); } @@ -245,7 +247,7 @@ public function removeFolder($name) } $name = trim($name, $this->delim); - if (strpos($name, 'INBOX' . $this->delim) === 0) { + if (str_starts_with($name, 'INBOX' . $this->delim)) { $name = substr($name, 6); } @@ -316,16 +318,16 @@ public function renameFolder($oldName, $newName) } $oldName = trim($oldName, $this->delim); - if (strpos($oldName, 'INBOX' . $this->delim) === 0) { + if (str_starts_with($oldName, 'INBOX' . $this->delim)) { $oldName = substr($oldName, 6); } $newName = trim($newName, $this->delim); - if (strpos($newName, 'INBOX' . $this->delim) === 0) { + if (str_starts_with($newName, 'INBOX' . $this->delim)) { $newName = substr($newName, 6); } - if (strpos($newName, $oldName . $this->delim) === 0) { + if (str_starts_with($newName, $oldName . $this->delim)) { throw new StorageException\RuntimeException('new folder cannot be a child of old folder'); } diff --git a/src/Transport/FileOptions.php b/src/Transport/FileOptions.php index fd1c0289..6f21cb87 100644 --- a/src/Transport/FileOptions.php +++ b/src/Transport/FileOptions.php @@ -87,9 +87,7 @@ public function setCallback($callback) public function getCallback() { if (null === $this->callback) { - $this->setCallback(function () { - return 'LaminasMail_' . time() . '_' . mt_rand() . '.eml'; - }); + $this->setCallback(static fn() => 'LaminasMail_' . time() . '_' . mt_rand() . '.eml'); } return $this->callback; } diff --git a/src/Transport/Sendmail.php b/src/Transport/Sendmail.php index 76fc6763..aae76f49 100644 --- a/src/Transport/Sendmail.php +++ b/src/Transport/Sendmail.php @@ -22,8 +22,8 @@ use function restore_error_handler; use function set_error_handler; use function sprintf; +use function str_contains; use function str_replace; -use function strpos; use function strtoupper; use function substr; use function trim; @@ -240,7 +240,7 @@ protected function prepareHeaders(Mail\Message $message) $from = $headers->get('From'); if ($from) { foreach ($from->getAddressList() as $address) { - if (strpos($address->getEmail(), '\\"') !== false) { + if (str_contains($address->getEmail(), '\\"')) { throw new RuntimeException('Potential code injection in From header'); } } diff --git a/src/Transport/Smtp.php b/src/Transport/Smtp.php index 525b3c24..2188b37f 100644 --- a/src/Transport/Smtp.php +++ b/src/Transport/Smtp.php @@ -144,7 +144,6 @@ public function getAutoDisconnect() * Return an SMTP connection * * @param string $name - * @param array|null $options * @return Protocol\Smtp */ public function plugin($name, ?array $options = null) @@ -164,7 +163,7 @@ public function __destruct() try { $connection->quit(); - } catch (ProtocolException\ExceptionInterface $e) { + } catch (ProtocolException\ExceptionInterface) { // ignore } diff --git a/src/Transport/SmtpOptions.php b/src/Transport/SmtpOptions.php index 71690ebc..59984706 100644 --- a/src/Transport/SmtpOptions.php +++ b/src/Transport/SmtpOptions.php @@ -116,7 +116,6 @@ public function getConnectionConfig() /** * Set connection configuration array * - * @param array $connectionConfig * @return SmtpOptions */ public function setConnectionConfig(array $connectionConfig) diff --git a/test/AddressListTest.php b/test/AddressListTest.php index f3910278..c57f745a 100644 --- a/test/AddressListTest.php +++ b/test/AddressListTest.php @@ -18,8 +18,7 @@ */ class AddressListTest extends TestCase { - /** @var AddressList */ - private $list; + private AddressList $list; public function setUp(): void { @@ -162,7 +161,7 @@ public function testSemicolonSeparator(): void // length'; hence the try/catch block, to allow finding the root cause. try { $to = Header\To::fromString('To:' . $header); - } catch (InvalidArgumentException $e) { + } catch (InvalidArgumentException) { $this->fail('Header\To::fromString should not throw'); } $addressList = $to->getAddressList(); @@ -220,7 +219,7 @@ public function testMixedQuotesInName(): void // hence the try/catch block, to allow finding the root cause. try { $to = Header\To::fromString('To:' . $header); - } catch (InvalidArgumentException $e) { + } catch (InvalidArgumentException) { $this->fail('Header\To::fromString should not throw'); } diff --git a/test/Header/GenericHeaderTest.php b/test/Header/GenericHeaderTest.php index 9d2964a9..6d46977b 100644 --- a/test/Header/GenericHeaderTest.php +++ b/test/Header/GenericHeaderTest.php @@ -55,9 +55,8 @@ public function fieldNames(): array /** * @dataProvider fieldNames * @group ZF2015-04 - * @param mixed $fieldName */ - public function testConstructorRaisesExceptionOnInvalidFieldName($fieldName): void + public function testConstructorRaisesExceptionOnInvalidFieldName(mixed $fieldName): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('name'); @@ -68,9 +67,8 @@ public function testConstructorRaisesExceptionOnInvalidFieldName($fieldName): vo /** * @dataProvider fieldNames * @group ZF2015-04 - * @param mixed $fieldName */ - public function testSetFieldNameRaisesExceptionOnInvalidFieldName($fieldName): void + public function testSetFieldNameRaisesExceptionOnInvalidFieldName(mixed $fieldName): void { $header = new GenericHeader('Subject'); $this->expectException(InvalidArgumentException::class); diff --git a/test/Header/HeaderLocatorTest.php b/test/Header/HeaderLocatorTest.php index cf4ec08e..da8f5749 100644 --- a/test/Header/HeaderLocatorTest.php +++ b/test/Header/HeaderLocatorTest.php @@ -3,12 +3,12 @@ namespace LaminasTest\Mail\Header; use Laminas\Mail\Header; +use Laminas\Mail\Header\HeaderLocator; use PHPUnit\Framework\TestCase; class HeaderLocatorTest extends TestCase { - /** @var Header\HeaderLocator */ - private $headerLocator; + private HeaderLocator $headerLocator; public function setUp(): void { diff --git a/test/Header/SenderTest.php b/test/Header/SenderTest.php index eb43c075..20a7001b 100644 --- a/test/Header/SenderTest.php +++ b/test/Header/SenderTest.php @@ -142,16 +142,15 @@ public function validSenderDataProvider(): array public function validSenderHeaderDataProvider(): array { - return array_merge(array_map(function ($parameters) { - return array_slice($parameters, 2); - }, $this->validSenderDataProvider()), [ + return array_merge(array_map(static fn($parameters) + => array_slice($parameters, 2), $this->validSenderDataProvider()), [ // Per RFC 2822, 3.4 and 3.6.2, "Sender: foo@bar" is valid. - 'Unbracketed email' => [ - '', - 'foo@bar', - 'ASCII', - ], - ]); + 'Unbracketed email' => [ + '', + 'foo@bar', + 'ASCII', + ], + ]); } public function invalidSenderDataProvider(): array diff --git a/test/HeadersTest.php b/test/HeadersTest.php index d0762b0d..d2994cfb 100644 --- a/test/HeadersTest.php +++ b/test/HeadersTest.php @@ -46,7 +46,7 @@ public function tearDown(): void public function setDeprecationErrorHandler(): void { $this->originalErrorHandler = set_error_handler( - function (int $errno, string $errstr, string $errfile, int $errline): void { + static function (int $errno, string $errstr, string $errfile, int $errline): void { /** @psalm-suppress InternalMethod */ throw new Deprecated($errstr, $errno, $errfile, $errline); }, diff --git a/test/MessageFactoryTest.php b/test/MessageFactoryTest.php index 2bb74ee7..11da700c 100644 --- a/test/MessageFactoryTest.php +++ b/test/MessageFactoryTest.php @@ -115,9 +115,8 @@ public function invalidMessageOptions(): array /** * @dataProvider invalidMessageOptions - * @param mixed $options */ - public function testExceptionForOptionsNotArrayOrTraversable($options): void + public function testExceptionForOptionsNotArrayOrTraversable(mixed $options): void { $this->expectException(Exception\InvalidArgumentException::class); MessageFactory::getInstance($options); diff --git a/test/MessageTest.php b/test/MessageTest.php index 272a19fe..f81ecdd7 100644 --- a/test/MessageTest.php +++ b/test/MessageTest.php @@ -539,9 +539,8 @@ public static function invalidBodyValues(): array /** * @dataProvider invalidBodyValues - * @param mixed $body */ - public function testSettingNonScalarNonMimeNonStringSerializableValueForBodyRaisesException($body): void + public function testSettingNonScalarNonMimeNonStringSerializableValueForBodyRaisesException(mixed $body): void { $this->expectException(Exception\InvalidArgumentException::class); $this->message->setBody($body); diff --git a/test/Protocol/AbstractProtocolTest.php b/test/Protocol/AbstractProtocolTest.php index aca421b6..5adc4b23 100644 --- a/test/Protocol/AbstractProtocolTest.php +++ b/test/Protocol/AbstractProtocolTest.php @@ -12,7 +12,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Process\Process; -use function strpos; +use function str_contains; use const PHP_BINARY; @@ -22,8 +22,7 @@ */ final class AbstractProtocolTest extends TestCase { - /** @var Process */ - private $process; + private Process $process; protected function setUp(): void { @@ -35,9 +34,7 @@ protected function setUp(): void __DIR__ . '/HttpStatusService', ]); $this->process->start(); - $this->process->waitUntil(static function (string $type, string $output): bool { - return false !== strpos($output, 'started'); - }); + $this->process->waitUntil(static fn(string $type, string $output): bool => str_contains($output, 'started')); } protected function tearDown(): void diff --git a/test/Storage/MaildirFolderTest.php b/test/Storage/MaildirFolderTest.php index 680649e8..4d37da40 100644 --- a/test/Storage/MaildirFolderTest.php +++ b/test/Storage/MaildirFolderTest.php @@ -79,7 +79,7 @@ public function setUp(): void $phar->extractTo($originalMaildir); // This empty directory is in the tar, but not unpacked by PharData mkdir($originalMaildir . '.subfolder/cur'); - } catch (\Exception $e) { + } catch (\Exception) { // intentionally empty catch block } } @@ -354,7 +354,7 @@ public function testNotReadableFolder(): void $check = false; try { $mail = new Folder\Maildir($this->params); - } catch (\Exception $e) { + } catch (\Exception) { $check = true; // test ok } diff --git a/test/Storage/MaildirMessageOldTest.php b/test/Storage/MaildirMessageOldTest.php index 71445f50..3b0bb00f 100644 --- a/test/Storage/MaildirMessageOldTest.php +++ b/test/Storage/MaildirMessageOldTest.php @@ -67,7 +67,7 @@ public function setUp(): void try { $phar = new PharData($originalMaildir . 'maildir.tar'); $phar->extractTo($originalMaildir); - } catch (Exception $e) { + } catch (Exception) { // intentionally empty catch block } } diff --git a/test/Storage/MaildirTest.php b/test/Storage/MaildirTest.php index e3b27941..6cc92053 100644 --- a/test/Storage/MaildirTest.php +++ b/test/Storage/MaildirTest.php @@ -74,7 +74,7 @@ public function setUp(): void try { $phar = new PharData($originalMaildir . 'maildir.tar'); $phar->extractTo($originalMaildir); - } catch (\Exception $e) { + } catch (\Exception) { // intentionally empty catch block } } diff --git a/test/Storage/MaildirWritableTest.php b/test/Storage/MaildirWritableTest.php index fa0bc156..6fce264d 100644 --- a/test/Storage/MaildirWritableTest.php +++ b/test/Storage/MaildirWritableTest.php @@ -77,7 +77,7 @@ public function setUp(): void try { $phar = new PharData($originalMaildir . 'maildir.tar'); $phar->extractTo($originalMaildir); - } catch (\Exception $e) { + } catch (\Exception) { // intentionally empty catch block } } @@ -575,7 +575,7 @@ public function testInit(): void try { $mail = new Writable\Maildir($this->params); $this->fail('empty maildir should not be accepted'); - } catch (\Exception $e) { + } catch (\Exception) { } Writable\Maildir::initMaildir($this->params['dirname']); @@ -592,7 +592,7 @@ public function testCreate(): void try { $mail = new Writable\Maildir($this->params); $this->fail('empty maildir should not be accepted'); - } catch (\Exception $e) { + } catch (\Exception) { } $this->params['create'] = true; diff --git a/test/Storage/MboxFolderTest.php b/test/Storage/MboxFolderTest.php index 0acbdb00..eab4359b 100644 --- a/test/Storage/MboxFolderTest.php +++ b/test/Storage/MboxFolderTest.php @@ -329,7 +329,7 @@ public function testNotReadableFolder(): void $check = false; try { $mail = new Folder\Mbox($this->params); - } catch (\Exception $e) { + } catch (\Exception) { $check = true; // test ok } diff --git a/test/Storage/MboxTest.php b/test/Storage/MboxTest.php index 8876e42c..52f5a667 100644 --- a/test/Storage/MboxTest.php +++ b/test/Storage/MboxTest.php @@ -303,7 +303,7 @@ public function testSleepWakeRemoved(): void $check = false; try { $mail = unserialize($serialzed); - } catch (\Exception $e) { + } catch (\Exception) { $check = true; // test ok } diff --git a/test/Storage/MessageTest.php b/test/Storage/MessageTest.php index 2ba8e16a..cfb07989 100644 --- a/test/Storage/MessageTest.php +++ b/test/Storage/MessageTest.php @@ -317,7 +317,7 @@ public function testEmptyBody(): void $part = null; try { $part = $message->getPart(1); - } catch (Exception\RuntimeException $e) { + } catch (Exception\RuntimeException) { // ok } if ($part) { diff --git a/test/Storage/ParamsNormalizerTest.php b/test/Storage/ParamsNormalizerTest.php index 0aec4cac..76d1b8e1 100644 --- a/test/Storage/ParamsNormalizerTest.php +++ b/test/Storage/ParamsNormalizerTest.php @@ -22,9 +22,8 @@ public function invalidParams(): iterable /** * @dataProvider invalidParams - * @param mixed $params */ - public function testRaisesErrorOnInvalidParamsTypes($params): void + public function testRaisesErrorOnInvalidParamsTypes(mixed $params): void { $this->expectException(InvalidArgumentException::class); ParamsNormalizer::normalizeParams($params); diff --git a/test/TestAsset/StringSerializableObject.php b/test/TestAsset/StringSerializableObject.php index ecd7dfc3..69fd6ab5 100644 --- a/test/TestAsset/StringSerializableObject.php +++ b/test/TestAsset/StringSerializableObject.php @@ -2,20 +2,15 @@ namespace LaminasTest\Mail\TestAsset; -class StringSerializableObject -{ - /** @var string */ - private $message; +use Stringable; - public function __construct(string $message) +class StringSerializableObject implements Stringable +{ + public function __construct(private string $message) { - $this->message = $message; } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->message; } diff --git a/test/Transport/FactoryTest.php b/test/Transport/FactoryTest.php index 22a619ec..f23ca16f 100644 --- a/test/Transport/FactoryTest.php +++ b/test/Transport/FactoryTest.php @@ -27,9 +27,8 @@ class FactoryTest extends TestCase { /** * @dataProvider invalidSpecTypeProvider - * @param mixed $spec */ - public function testInvalidSpecThrowsInvalidArgumentException($spec): void + public function testInvalidSpecThrowsInvalidArgumentException(mixed $spec): void { $this->expectException(Exception\InvalidArgumentException::class); Factory::create($spec); @@ -56,7 +55,7 @@ public function testDefaultTypeIsSendmail(): void */ public function testCanCreateClassUsingTypeKey(string $type): void { - set_error_handler(function ($code, $message): void { + set_error_handler(static function ($code, $message): void { // skip deprecation notices }, E_USER_DEPRECATED); $transport = Factory::create([ diff --git a/test/Transport/FileOptionsTest.php b/test/Transport/FileOptionsTest.php index 1e03c4c1..212fb5ea 100644 --- a/test/Transport/FileOptionsTest.php +++ b/test/Transport/FileOptionsTest.php @@ -14,8 +14,7 @@ */ class FileOptionsTest extends TestCase { - /** @var FileOptions */ - private $options; + private FileOptions $options; public function setUp(): void { @@ -47,7 +46,7 @@ public function testPathIsMutable(): void public function testCallbackIsMutable(): void { $original = $this->options->getCallback(); - $new = function ($transport): void { + $new = static function ($transport): void { }; $this->options->setCallback($new); diff --git a/test/Transport/FileTest.php b/test/Transport/FileTest.php index 3869d067..3c579e6d 100644 --- a/test/Transport/FileTest.php +++ b/test/Transport/FileTest.php @@ -8,7 +8,6 @@ use PHPUnit\Framework\TestCase; use function file_get_contents; -use function get_class; use function glob; use function is_dir; use function mkdir; @@ -22,8 +21,7 @@ */ class FileTest extends TestCase { - /** @var string */ - private $tempDir; + private string $tempDir; public function setUp(): void { @@ -87,6 +85,6 @@ public function testReceivesMailArtifacts(): void public function testConstructorNoOptions(): void { $transport = new File(); - $this->assertSame(FileOptions::class, get_class($transport->getOptions())); + $this->assertSame(FileOptions::class, $transport->getOptions()::class); } } From f258ba091963a1b702c7cb5353a65ea51a2966d4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 6 Aug 2022 19:20:18 +0700 Subject: [PATCH 2/4] move comment to method doc Signed-off-by: Abdul Malik Ikhsan --- src/Storage/Folder.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Storage/Folder.php b/src/Storage/Folder.php index 29994355..c39fec10 100644 --- a/src/Storage/Folder.php +++ b/src/Storage/Folder.php @@ -23,19 +23,16 @@ class Folder implements RecursiveIterator, Stringable /** * create a new mail folder instance * - * @param string $localName name of folder in current subdirectory + * @param string $localName local name (name of folder in parent folder) * @param string $globalName absolute name of folder * @param bool $selectable if true folder holds messages, if false it's * just a parent for subfolders (Default: true) - * @param array $folders init with given instances of Folder as subfolders + * @param array subfolders of folder array(localName => \Laminas\Mail\Storage\Folder folder) */ public function __construct( protected $localName, $globalName = '', protected $selectable = true, - /** - * subfolders of folder array(localName => \Laminas\Mail\Storage\Folder folder) - */ protected array $folders = [] ) { $this->globalName = $globalName ?: $localName; From b0af22be41403100fe116c93065ef5c22cb16558 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 6 Aug 2022 19:22:45 +0700 Subject: [PATCH 3/4] add missing parameter name in doc Signed-off-by: Abdul Malik Ikhsan --- src/Storage/Folder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/Folder.php b/src/Storage/Folder.php index c39fec10..c59df1fb 100644 --- a/src/Storage/Folder.php +++ b/src/Storage/Folder.php @@ -27,7 +27,7 @@ class Folder implements RecursiveIterator, Stringable * @param string $globalName absolute name of folder * @param bool $selectable if true folder holds messages, if false it's * just a parent for subfolders (Default: true) - * @param array subfolders of folder array(localName => \Laminas\Mail\Storage\Folder folder) + * @param array $folders subfolders of folder array(localName => \Laminas\Mail\Storage\Folder folder) */ public function __construct( protected $localName, From aa6071b2c871ca0c31006b6647fca633c85980f3 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 6 Aug 2022 19:25:03 +0700 Subject: [PATCH 4/4] cs fix Signed-off-by: Abdul Malik Ikhsan --- src/Storage/Folder.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Storage/Folder.php b/src/Storage/Folder.php index c59df1fb..ab89a153 100644 --- a/src/Storage/Folder.php +++ b/src/Storage/Folder.php @@ -27,7 +27,8 @@ class Folder implements RecursiveIterator, Stringable * @param string $globalName absolute name of folder * @param bool $selectable if true folder holds messages, if false it's * just a parent for subfolders (Default: true) - * @param array $folders subfolders of folder array(localName => \Laminas\Mail\Storage\Folder folder) + * @param array $folders subfolders of + * folder array(localName => \Laminas\Mail\Storage\Folder folder) */ public function __construct( protected $localName,