Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply PHP 8.0 syntax and property promotion #204

Merged
merged 4 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/AddressList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 7 additions & 11 deletions src/Header/AbstractAddressList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(
[
'#(?<!\\\)"(.*)(?<!\\\)"#', // quoted-text
'#\\\([\x01-\x09\x0b\x0c\x0e-\x7f])#', // quoted-pair
Expand All @@ -114,7 +111,6 @@ function ($value) use (&$wasEncoded) {
],
$value
);

return empty($value) ? null : Address::fromString($value, $comments);
},
$values
Expand Down Expand Up @@ -183,7 +179,7 @@ public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
$name = $address->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, ';'))) {
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
// FIXME: what if name contains double quote?
$name = sprintf('"%s"', $name);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Header/HeaderLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions src/Header/HeaderWrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
''
);
}
Expand All @@ -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);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Header/IdentificationField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -193,19 +194,22 @@ 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;
}

/**
* @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;
Expand Down
4 changes: 1 addition & 3 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
{
/** @var Sender $header */
$header = $this->getHeaderByName('sender', Sender::class);
Expand Down
20 changes: 5 additions & 15 deletions src/Protocol/AbstractProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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));
Expand All @@ -115,7 +106,6 @@ public function __construct($host = '127.0.0.1', $port = null)
}

$this->host = $host;
$this->port = $port;
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions src/Protocol/Imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
}

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Protocol/Pop3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion src/Protocol/SmtpPluginManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
10 changes: 4 additions & 6 deletions src/Storage/AbstractStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use ReturnTypeWillChange;
use SeekableIterator;

use function strpos;
use function str_starts_with;
use function strtolower;
use function substr;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -211,7 +211,7 @@ public function offsetExists($id)
if ($this->getMessage($id)) {
return true;
}
} catch (Exception\ExceptionInterface $e) {
} catch (Exception\ExceptionInterface) {
}

return false;
Expand All @@ -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)
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
{
throw new Exception\RuntimeException('cannot write mail messages via array access');
}
Expand Down
Loading