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

Add type hints and return types #183

Merged
merged 1 commit into from
Sep 26, 2017
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
31 changes: 15 additions & 16 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Connection
*
* @throws \InvalidArgumentException
*/
public function __construct($resource, $server)
public function __construct($resource, string $server)
{
if (!is_resource($resource)) {
throw new \InvalidArgumentException('$resource must be a resource');
Expand All @@ -40,9 +40,10 @@ public function __construct($resource, $server)
*
* @return Mailbox[]
*/
public function getMailboxes()
public function getMailboxes(): array
{
if (null === $this->mailboxes) {
$this->mailboxes = [];
foreach ($this->getMailboxNames() as $mailboxName) {
$this->mailboxes[] = $this->getMailbox($mailboxName);
}
Expand All @@ -58,7 +59,7 @@ public function getMailboxes()
*
* @return bool
*/
public function hasMailbox($name)
public function hasMailbox(string $name): bool
{
return in_array($name, $this->getMailboxNames());
}
Expand All @@ -72,7 +73,7 @@ public function hasMailbox($name)
*
* @return Mailbox
*/
public function getMailbox($name)
public function getMailbox(string $name): Mailbox
{
if (!$this->hasMailbox($name)) {
throw new MailboxDoesNotExistException(sprintf(
Expand Down Expand Up @@ -103,15 +104,15 @@ public function count()
*
* @return Mailbox
*/
public function createMailbox($name)
public function createMailbox(string $name): Mailbox
{
if (imap_createmailbox($this->resource, $this->server . $name)) {
$this->mailboxNames = $this->mailboxes = null;

return $this->getMailbox($name);
if (false === imap_createmailbox($this->resource, $this->server . $name)) {
throw new Exception("Can not create '{$name}' mailbox at '{$this->server}'");
}

throw new Exception("Can not create '{$name}' mailbox at '{$this->server}'");
$this->mailboxNames = $this->mailboxes = null;

return $this->getMailbox($name);
}

/**
Expand All @@ -121,17 +122,14 @@ public function createMailbox($name)
*
* @return bool
*/
public function close($flag = 0)
public function close(int $flag = 0): bool
{
return imap_close($this->resource, $flag);
}

public function deleteMailbox(Mailbox $mailbox)
{
if (false === imap_deletemailbox(
$this->resource,
$this->server . $mailbox->getName()
)) {
if (false === imap_deletemailbox($this->resource, $this->server . $mailbox->getName())) {
throw new Exception('Mailbox ' . $mailbox->getName() . ' could not be deleted');
}

Expand All @@ -153,9 +151,10 @@ public function getResource()
*
* @return array
*/
private function getMailboxNames()
private function getMailboxNames(): array
{
if (null === $this->mailboxNames) {
$this->mailboxNames = [];
$mailboxes = imap_getmailboxes($this->resource, $this->server, '*');
foreach ($mailboxes as $mailbox) {
$this->mailboxNames[] = imap_utf7_decode(str_replace($this->server, '', $mailbox->name));
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Exception extends \RuntimeException
E_USER_DEPRECATED => 'E_USER_DEPRECATED',
];

final public function __construct($message, $code = 0, $previous = null)
final public function __construct(string $message, int $code = 0, \Throwable $previous = null)
{
$errorType = '';
if (is_int($code) && isset(self::$errorLabels[$code])) {
Expand Down
16 changes: 7 additions & 9 deletions src/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Mailbox implements \Countable, \IteratorAggregate
* @param string $name Mailbox name
* @param Connection $connection IMAP connection
*/
public function __construct($name, Connection $connection)
public function __construct(string $name, Connection $connection)
{
$this->mailbox = $name;
$this->connection = $connection;
Expand All @@ -31,7 +31,7 @@ public function __construct($name, Connection $connection)
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
Expand All @@ -55,7 +55,7 @@ public function count()
*
* @return Message[]|MessageIterator
*/
public function getMessages(SearchExpression $search = null)
public function getMessages(SearchExpression $search = null): MessageIterator
{
$this->init();

Expand All @@ -77,7 +77,7 @@ public function getMessages(SearchExpression $search = null)
*
* @return Message
*/
public function getMessage($number)
public function getMessage(int $number): Message
{
$this->init();

Expand All @@ -89,10 +89,8 @@ public function getMessage($number)
*
* @return MessageIterator
*/
public function getIterator()
public function getIterator(): MessageIterator
{
$this->init();

return $this->getMessages();
}

Expand All @@ -109,7 +107,7 @@ public function delete()
*
* @return Mailbox
*/
public function expunge()
public function expunge(): self
{
$this->init();

Expand All @@ -125,7 +123,7 @@ public function expunge()
*
* @return bool
*/
public function addMessage($message)
public function addMessage($message): bool
{
return imap_append($this->connection->getResource(), $this->mailbox, $message);
}
Expand Down
24 changes: 12 additions & 12 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Message extends Message\Part
* @param resource $stream IMAP stream
* @param int $messageNumber Message number
*/
public function __construct($stream, $messageNumber)
public function __construct($stream, int $messageNumber)
{
$this->stream = $stream;
$this->messageNumber = $messageNumber;
Expand All @@ -43,7 +43,7 @@ public function __construct($stream, $messageNumber)
*
* @return string
*/
public function getId()
public function getId(): string
{
return $this->getHeaders()->get('message_id');
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public function getCc()
*
* @return int
*/
public function getNumber()
public function getNumber(): int
{
return $this->messageNumber;
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public function getSize()
*
* @return string
*/
public function getContent($keepUnseen = false)
public function getContent(bool $keepUnseen = false): string
{
// Null headers, so subsequent calls to getHeaders() will return
// updated seen flag
Expand Down Expand Up @@ -159,7 +159,7 @@ public function isDraft()
*
* @return bool
*/
public function isSeen()
public function isSeen(): bool
{
return
'R' === $this->getHeaders()->get('recent')
Expand All @@ -182,7 +182,7 @@ public function getSubject()
*
* @return Message\Headers
*/
public function getHeaders()
public function getHeaders(): Message\Headers
{
if (null === $this->headers) {
// imap_header is much faster than imap_fetchheader
Expand Down Expand Up @@ -215,7 +215,7 @@ public function getBodyHtml()
*
* @return string
*/
public function getBodyText()
public function getBodyText(): string
{
$iterator = new \RecursiveIteratorIterator($this, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $part) {
Expand All @@ -233,7 +233,7 @@ public function getBodyText()
*
* @return Message\Attachment[]
*/
public function getAttachments()
public function getAttachments(): array
{
if (null === $this->attachments) {
$this->attachments = [];
Expand All @@ -259,7 +259,7 @@ public function getAttachments()
*
* @return bool
*/
public function hasAttachments()
public function hasAttachments(): bool
{
return count($this->getAttachments()) > 0;
}
Expand Down Expand Up @@ -291,7 +291,7 @@ public function delete()
*
* @return Message
*/
public function move(Mailbox $mailbox)
public function move(Mailbox $mailbox): self
{
if (!imap_mail_move($this->stream, $this->messageNumber, $mailbox->getName(), \CP_UID)) {
throw new MessageMoveException(sprintf(
Expand All @@ -313,9 +313,9 @@ public function move(Mailbox $mailbox)
*
* @return Message
*/
public function keepUnseen($bool = true)
public function keepUnseen(bool $bool = true): self
{
$this->keepUnseen = (bool) $bool;
$this->keepUnseen = $bool;

return $this;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Message/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class EmailAddress
private $name;
private $address;

public function __construct($mailbox, $hostname = null, $name = null)
public function __construct(string $mailbox, string $hostname = null, string $name = null)
{
$this->mailbox = $mailbox;
$this->hostname = $hostname;
Expand All @@ -35,7 +35,7 @@ public function getAddress()
*
* @return string
*/
public function getFullAddress()
public function getFullAddress(): string
{
if ($this->name) {
$address = sprintf('%s <%s@%s>', $this->name, $this->mailbox, $this->hostname);
Expand All @@ -46,7 +46,7 @@ public function getFullAddress()
return $address;
}

public function getMailbox()
public function getMailbox(): string
{
return $this->mailbox;
}
Expand Down
11 changes: 3 additions & 8 deletions src/Message/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,16 @@ public function __construct(\stdClass $headers)
*
* @return string
*/
public function get($key)
public function get(string $key)
{
return parent::get(strtolower($key));
}

private function parseHeader($key, $value)
private function parseHeader(string $key, $value)
{
switch ($key) {
case 'msgno':
return (int) $value;
case 'answered':
case 'deleted':
case 'draft':
case 'unseen':
return $value;
case 'date':
$value = $this->decode($value);
$value = preg_replace('/([^\(]*)\(.*\)/', '$1', $value);
Expand All @@ -70,7 +65,7 @@ private function parseHeader($key, $value)
}
}

private function decodeEmailAddress($value)
private function decodeEmailAddress(\stdClass $value): EmailAddress
{
return new EmailAddress(
$value->mailbox,
Expand Down
Loading