Skip to content

Commit

Permalink
test(smtp): fix psalm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 19, 2025
1 parent 64dc421 commit 7f6ad5b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
26 changes: 14 additions & 12 deletions src/Sender/MailToFileSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,32 @@ 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;
}

/**
* @return list<non-empty-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,
));
}
}
27 changes: 14 additions & 13 deletions src/Traffic/Message/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,23 @@ 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);

Check warning on line 136 in src/Traffic/Message/Smtp.php

View check run for this annotation

Codecov / codecov/patch

src/Traffic/Message/Smtp.php#L136

Added line #L136 was not covered by tests
}

/**
* @return Contact[]
*/
public function getTo(): array
{
return $this->normalizeAddressList($this->getHeader('To'));
return self::normalizeAddressList($this->getHeader('To'));
}

/**
* @return Contact[]
*/
public function getCc(): array
{
return $this->normalizeAddressList($this->getHeader('Cc'));
return self::normalizeAddressList($this->getHeader('Cc'));
}

/**
Expand All @@ -160,15 +160,15 @@ public function getCc(): array
*/
public function getBcc(): array
{
return $this->normalizeAddressList($this->protocol['BCC'] ?? []);
return self::normalizeAddressList($this->protocol['BCC'] ?? []);
}

/**
* @return Contact[]
*/
public function getReplyTo(): array
{
return $this->normalizeAddressList($this->getHeader('Reply-To'));
return self::normalizeAddressList($this->getHeader('Reply-To'));
}

public function getSubject(): string
Expand All @@ -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*+(?<name>.*?)\s*<(?<email>.*)>\s*$/', $line, $matches) === 1) {
$name = match (true) {
Expand All @@ -206,26 +206,27 @@ private function parseContact(string $line): Contact
}

/**
* @return array<Contact>
* @return list<Contact>
*/
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<Contact>
* @param list<string> $param
* @return list<Contact>
*/
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),
);
}
}

0 comments on commit 7f6ad5b

Please sign in to comment.