diff --git a/src/Sender/MailToFileSender.php b/src/Sender/MailToFileSender.php index 536347b..b05d93c 100644 --- a/src/Sender/MailToFileSender.php +++ b/src/Sender/MailToFileSender.php @@ -47,15 +47,17 @@ public function send(iterable $frames): void /** * Get normalized email address for file or directory name. * - * @return non-empty-string + * @return non-empty-string|null */ - private static function normalizeEmail(string $email): string + private static function normalizeEmail(?string $email): ?string { - return \preg_replace( + $normalized = \preg_replace( ['/[^a-z0-9.\\- @]/i', '/\s+/'], ['!', '_'], - $email, + (string) $email, ); + + return $normalized === '' ? null : $normalized; } /** @@ -63,14 +65,14 @@ private static function normalizeEmail(string $email): string */ private static function fetchDirectories(Message\Smtp $message): array { - return - \array_filter( - \array_unique( - \array_map( - static fn(Contact $c) => self::normalizeEmail($c->email), - \array_merge($message->getBcc(), $message->getTo()), - ), + return \array_values(\array_filter( + \array_unique( + \array_map( + static fn(Contact $c): ?string => self::normalizeEmail($c->email), + \array_merge($message->getBcc(), $message->getTo()), ), - ); + ), + static fn(?string $dir): bool => $dir !== null, + )); } } diff --git a/src/Traffic/Message/Smtp.php b/src/Traffic/Message/Smtp.php index 0cd1659..b6ebf3e 100644 --- a/src/Traffic/Message/Smtp.php +++ b/src/Traffic/Message/Smtp.php @@ -133,7 +133,7 @@ public function getSender(): array { $addrs = \array_unique(\array_merge((array) ($this->protocol['FROM'] ?? []), $this->getHeader('From'))); - return \array_map([$this, 'parseContact'], $addrs); + return \array_map(self::parseContact(...), $addrs); } /** @@ -141,7 +141,7 @@ public function getSender(): array */ public function getTo(): array { - return $this->normalizeAddressList($this->getHeader('To')); + return self::normalizeAddressList($this->getHeader('To')); } /** @@ -149,7 +149,7 @@ public function getTo(): array */ public function getCc(): array { - return $this->normalizeAddressList($this->getHeader('Cc')); + return self::normalizeAddressList($this->getHeader('Cc')); } /** @@ -160,7 +160,7 @@ public function getCc(): array */ public function getBcc(): array { - return $this->normalizeAddressList($this->protocol['BCC'] ?? []); + return self::normalizeAddressList($this->protocol['BCC'] ?? []); } /** @@ -168,7 +168,7 @@ public function getBcc(): array */ public function getReplyTo(): array { - return $this->normalizeAddressList($this->getHeader('Reply-To')); + return self::normalizeAddressList($this->getHeader('Reply-To')); } public function getSubject(): string @@ -187,7 +187,7 @@ public function getMessage(MessageFormat $type): ?Field return null; } - private function parseContact(string $line): Contact + private static function parseContact(string $line): Contact { if (\preg_match('/^\s*+(?.*?)\s*<(?.*)>\s*$/', $line, $matches) === 1) { $name = match (true) { @@ -206,26 +206,27 @@ private function parseContact(string $line): Contact } /** - * @return array + * @return list */ - private function parseDestinationAddress(string $line): array + private static function parseDestinationAddress(string $line): array { // if this is a group recipient if (\preg_match('/^[^"]+:(.*);$/', $line, $matches) === 1) { $line = $matches[1]; } - $emailList = \array_map('trim', \explode(',', $line)); - return \array_map([$this, 'parseContact'], $emailList); + $emailList = \array_map(\trim(...), \explode(',', $line)); + return \array_map(self::parseContact(...), $emailList); } /** - * @return array + * @param list $param + * @return list */ - private function normalizeAddressList(array $param): array + private static function normalizeAddressList(array $param): array { return \array_merge( - ...\array_map([$this, 'parseDestinationAddress'], $param), + ...\array_map(self::parseDestinationAddress(...), $param), ); } }