From 9c0f083defa3596a0fde6b2bca5774738d999f44 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Tue, 26 Sep 2017 09:39:22 +0200 Subject: [PATCH] Add type hints and return types --- src/Connection.php | 31 ++++++++++----------- src/Exception/Exception.php | 2 +- src/Mailbox.php | 16 +++++------ src/Message.php | 24 ++++++++-------- src/Message/EmailAddress.php | 6 ++-- src/Message/Headers.php | 11 ++------ src/Message/Part.php | 18 ++++++------ src/MessageIterator.php | 2 +- src/Parameters.php | 4 +-- src/Search/AbstractCondition.php | 2 +- src/Search/Date/After.php | 2 +- src/Search/Date/Before.php | 2 +- src/Search/Date/On.php | 2 +- src/Search/Email/AbstractEmail.php | 4 +-- src/Search/Email/FromAddress.php | 2 +- src/Search/Email/To.php | 2 +- src/Search/Flag/Answered.php | 2 +- src/Search/Flag/Flagged.php | 2 +- src/Search/Flag/Recent.php | 2 +- src/Search/Flag/Seen.php | 2 +- src/Search/Flag/Unanswered.php | 2 +- src/Search/Flag/Unflagged.php | 2 +- src/Search/Flag/Unseen.php | 2 +- src/Search/LogicalOperator/All.php | 2 +- src/Search/LogicalOperator/OrConditions.php | 2 +- src/Search/State/Deleted.php | 2 +- src/Search/State/NewMessage.php | 2 +- src/Search/State/Old.php | 2 +- src/Search/State/Undeleted.php | 2 +- src/Search/Text/AbstractText.php | 4 +-- src/Search/Text/Body.php | 2 +- src/Search/Text/Keyword.php | 2 +- src/Search/Text/Subject.php | 2 +- src/Search/Text/Text.php | 2 +- src/Search/Text/Unkeyword.php | 2 +- src/SearchExpression.php | 4 +-- src/Server.php | 12 ++++---- tests/AbstractTest.php | 8 +++--- tests/ConnectionTest.php | 12 ++++---- tests/MailboxTest.php | 2 +- tests/MessageTest.php | 8 +++--- 41 files changed, 104 insertions(+), 112 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index c6b68644..48e51dc9 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -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'); @@ -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); } @@ -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()); } @@ -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( @@ -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); } /** @@ -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'); } @@ -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)); diff --git a/src/Exception/Exception.php b/src/Exception/Exception.php index 28a47fe6..e6a7e374 100644 --- a/src/Exception/Exception.php +++ b/src/Exception/Exception.php @@ -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])) { diff --git a/src/Mailbox.php b/src/Mailbox.php index 1ce8af27..fea4f0c6 100644 --- a/src/Mailbox.php +++ b/src/Mailbox.php @@ -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; @@ -31,7 +31,7 @@ public function __construct($name, Connection $connection) * * @return string */ - public function getName() + public function getName(): string { return $this->name; } @@ -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(); @@ -77,7 +77,7 @@ public function getMessages(SearchExpression $search = null) * * @return Message */ - public function getMessage($number) + public function getMessage(int $number): Message { $this->init(); @@ -89,10 +89,8 @@ public function getMessage($number) * * @return MessageIterator */ - public function getIterator() + public function getIterator(): MessageIterator { - $this->init(); - return $this->getMessages(); } @@ -109,7 +107,7 @@ public function delete() * * @return Mailbox */ - public function expunge() + public function expunge(): self { $this->init(); @@ -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); } diff --git a/src/Message.php b/src/Message.php index 3b056fef..bf3baf73 100644 --- a/src/Message.php +++ b/src/Message.php @@ -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; @@ -43,7 +43,7 @@ public function __construct($stream, $messageNumber) * * @return string */ - public function getId() + public function getId(): string { return $this->getHeaders()->get('message_id'); } @@ -83,7 +83,7 @@ public function getCc() * * @return int */ - public function getNumber() + public function getNumber(): int { return $this->messageNumber; } @@ -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 @@ -159,7 +159,7 @@ public function isDraft() * * @return bool */ - public function isSeen() + public function isSeen(): bool { return 'R' === $this->getHeaders()->get('recent') @@ -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 @@ -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) { @@ -233,7 +233,7 @@ public function getBodyText() * * @return Message\Attachment[] */ - public function getAttachments() + public function getAttachments(): array { if (null === $this->attachments) { $this->attachments = []; @@ -259,7 +259,7 @@ public function getAttachments() * * @return bool */ - public function hasAttachments() + public function hasAttachments(): bool { return count($this->getAttachments()) > 0; } @@ -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( @@ -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; } diff --git a/src/Message/EmailAddress.php b/src/Message/EmailAddress.php index a25b5430..54e5f9e9 100644 --- a/src/Message/EmailAddress.php +++ b/src/Message/EmailAddress.php @@ -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; @@ -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); @@ -46,7 +46,7 @@ public function getFullAddress() return $address; } - public function getMailbox() + public function getMailbox(): string { return $this->mailbox; } diff --git a/src/Message/Headers.php b/src/Message/Headers.php index a9e9a2bd..32f7be8b 100644 --- a/src/Message/Headers.php +++ b/src/Message/Headers.php @@ -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); @@ -70,7 +65,7 @@ private function parseHeader($key, $value) } } - private function decodeEmailAddress($value) + private function decodeEmailAddress(\stdClass $value): EmailAddress { return new EmailAddress( $value->mailbox, diff --git a/src/Message/Part.php b/src/Message/Part.php index 5e3794b8..c0ef57a0 100644 --- a/src/Message/Part.php +++ b/src/Message/Part.php @@ -95,8 +95,8 @@ class Part implements \RecursiveIterator */ public function __construct( $stream, - $messageNumber, - $partNumber = null, + int $messageNumber, + string $partNumber = null, \stdClass $structure = null ) { $this->stream = $stream; @@ -148,7 +148,7 @@ public function getParameters() * * @return string */ - public function getContent($keepUnseen = false) + public function getContent(bool $keepUnseen = false): string { if (null === $this->content) { $this->content = $this->doGetContent($keepUnseen); @@ -164,7 +164,7 @@ public function getContent($keepUnseen = false) * * @return string */ - public function getDecodedContent($keepUnseen = false) + public function getDecodedContent(bool $keepUnseen = false): string { if (null === $this->decodedContent) { switch ($this->getEncoding()) { @@ -206,7 +206,7 @@ public function getStructure() return $this->structure; } - protected function fetchStructure($partNumber = null) + protected function fetchStructure(int $partNumber = null) { if (null === $this->structure) { $this->loadStructure(); @@ -252,7 +252,7 @@ protected function parseStructure(\stdClass $structure) if (isset($structure->parts)) { foreach ($structure->parts as $key => $partStructure) { if (null === $this->partNumber) { - $partNumber = ($key + 1); + $partNumber = (string) ($key + 1); } else { $partNumber = (string) ($this->partNumber . '.' . ($key + 1)); } @@ -271,7 +271,7 @@ protected function parseStructure(\stdClass $structure) * * @return self[] */ - public function getParts() + public function getParts(): array { return $this->parts; } @@ -325,7 +325,7 @@ public function getDisposition() * * @return string */ - protected function doGetContent($keepUnseen = false) + protected function doGetContent(bool $keepUnseen = false) { return imap_fetchbody( $this->stream, @@ -335,7 +335,7 @@ protected function doGetContent($keepUnseen = false) ); } - private function isAttachment($part) + private function isAttachment(\stdClass $part) { // Attachment with correct Content-Disposition header if (isset($part->disposition)) { diff --git a/src/MessageIterator.php b/src/MessageIterator.php index 2dc38016..e00a86c6 100644 --- a/src/MessageIterator.php +++ b/src/MessageIterator.php @@ -26,7 +26,7 @@ public function __construct($stream, array $messageNumbers) * * @return Message */ - public function current() + public function current(): Message { return new Message($this->stream, parent::current()); } diff --git a/src/Parameters.php b/src/Parameters.php index dfae9e2d..4514872d 100644 --- a/src/Parameters.php +++ b/src/Parameters.php @@ -24,7 +24,7 @@ public function add(array $parameters = []) } } - public function get($key) + public function get(string $key) { if (isset($this->parameters[$key])) { return $this->parameters[$key]; @@ -33,7 +33,7 @@ public function get($key) return; } - protected function decode($value) + protected function decode(string $value): string { $decoded = ''; $parts = imap_mime_header_decode($value); diff --git a/src/Search/AbstractCondition.php b/src/Search/AbstractCondition.php index 6df99cf1..a6ccf7c0 100644 --- a/src/Search/AbstractCondition.php +++ b/src/Search/AbstractCondition.php @@ -24,5 +24,5 @@ public function __toString() * * @return string */ - abstract protected function getKeyword(); + abstract protected function getKeyword(): string; } diff --git a/src/Search/Date/After.php b/src/Search/Date/After.php index 0e6f72bd..242ddd76 100644 --- a/src/Search/Date/After.php +++ b/src/Search/Date/After.php @@ -15,7 +15,7 @@ class After extends AbstractDate * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'SINCE'; } diff --git a/src/Search/Date/Before.php b/src/Search/Date/Before.php index 6db74738..14ff8c43 100644 --- a/src/Search/Date/Before.php +++ b/src/Search/Date/Before.php @@ -15,7 +15,7 @@ class Before extends AbstractDate * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'BEFORE'; } diff --git a/src/Search/Date/On.php b/src/Search/Date/On.php index eeacb7e0..166ba568 100644 --- a/src/Search/Date/On.php +++ b/src/Search/Date/On.php @@ -15,7 +15,7 @@ class On extends AbstractDate * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'ON'; } diff --git a/src/Search/Email/AbstractEmail.php b/src/Search/Email/AbstractEmail.php index fba37ade..ef073e6a 100644 --- a/src/Search/Email/AbstractEmail.php +++ b/src/Search/Email/AbstractEmail.php @@ -23,7 +23,7 @@ abstract class AbstractEmail extends AbstractCondition * * @param string $email optional email address for the condition */ - public function __construct($email = null) + public function __construct(string $email = null) { if ($email) { $this->setEmail($email); @@ -35,7 +35,7 @@ public function __construct($email = null) * * @param string $email */ - public function setEmail($email) + public function setEmail(string $email) { $this->email = $email; } diff --git a/src/Search/Email/FromAddress.php b/src/Search/Email/FromAddress.php index 3cd0e5b6..419eeb5a 100644 --- a/src/Search/Email/FromAddress.php +++ b/src/Search/Email/FromAddress.php @@ -15,7 +15,7 @@ class FromAddress extends AbstractEmail * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'FROM'; } diff --git a/src/Search/Email/To.php b/src/Search/Email/To.php index 4e417ad3..265d7714 100644 --- a/src/Search/Email/To.php +++ b/src/Search/Email/To.php @@ -16,7 +16,7 @@ class To extends AbstractEmail * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'TO'; } diff --git a/src/Search/Flag/Answered.php b/src/Search/Flag/Answered.php index 3a829cac..179e458c 100644 --- a/src/Search/Flag/Answered.php +++ b/src/Search/Flag/Answered.php @@ -17,7 +17,7 @@ class Answered extends AbstractCondition * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'ANSWERED'; } diff --git a/src/Search/Flag/Flagged.php b/src/Search/Flag/Flagged.php index 1e971d58..4c7a8faa 100644 --- a/src/Search/Flag/Flagged.php +++ b/src/Search/Flag/Flagged.php @@ -17,7 +17,7 @@ class Flagged extends AbstractCondition * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'FLAGGED'; } diff --git a/src/Search/Flag/Recent.php b/src/Search/Flag/Recent.php index 8dca2f1b..2e1befe4 100644 --- a/src/Search/Flag/Recent.php +++ b/src/Search/Flag/Recent.php @@ -17,7 +17,7 @@ class Recent extends AbstractCondition * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'RECENT'; } diff --git a/src/Search/Flag/Seen.php b/src/Search/Flag/Seen.php index 72235960..179d2cc0 100644 --- a/src/Search/Flag/Seen.php +++ b/src/Search/Flag/Seen.php @@ -17,7 +17,7 @@ class Seen extends AbstractCondition * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'SEEN'; } diff --git a/src/Search/Flag/Unanswered.php b/src/Search/Flag/Unanswered.php index 7ef6b04c..3e8fae9b 100644 --- a/src/Search/Flag/Unanswered.php +++ b/src/Search/Flag/Unanswered.php @@ -17,7 +17,7 @@ class Unanswered extends AbstractCondition * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'UNANSWERED'; } diff --git a/src/Search/Flag/Unflagged.php b/src/Search/Flag/Unflagged.php index fb6e7a1e..5082e0ca 100644 --- a/src/Search/Flag/Unflagged.php +++ b/src/Search/Flag/Unflagged.php @@ -17,7 +17,7 @@ class Unflagged extends AbstractCondition * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'UNFLAGGED'; } diff --git a/src/Search/Flag/Unseen.php b/src/Search/Flag/Unseen.php index 050869ad..30414060 100644 --- a/src/Search/Flag/Unseen.php +++ b/src/Search/Flag/Unseen.php @@ -17,7 +17,7 @@ class Unseen extends AbstractCondition * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'UNSEEN'; } diff --git a/src/Search/LogicalOperator/All.php b/src/Search/LogicalOperator/All.php index a082f9dd..48e58d17 100644 --- a/src/Search/LogicalOperator/All.php +++ b/src/Search/LogicalOperator/All.php @@ -17,7 +17,7 @@ class All extends AbstractCondition * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'ALL'; } diff --git a/src/Search/LogicalOperator/OrConditions.php b/src/Search/LogicalOperator/OrConditions.php index af7f579b..391fb4c2 100644 --- a/src/Search/LogicalOperator/OrConditions.php +++ b/src/Search/LogicalOperator/OrConditions.php @@ -17,7 +17,7 @@ class OrConditions extends AbstractCondition * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'OR'; } diff --git a/src/Search/State/Deleted.php b/src/Search/State/Deleted.php index d0956b67..cb3431a0 100644 --- a/src/Search/State/Deleted.php +++ b/src/Search/State/Deleted.php @@ -17,7 +17,7 @@ class Deleted extends AbstractCondition * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'DELETED'; } diff --git a/src/Search/State/NewMessage.php b/src/Search/State/NewMessage.php index 872a4ee4..f245e19d 100644 --- a/src/Search/State/NewMessage.php +++ b/src/Search/State/NewMessage.php @@ -16,7 +16,7 @@ class NewMessage extends AbstractCondition * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'NEW'; } diff --git a/src/Search/State/Old.php b/src/Search/State/Old.php index bf034930..3a145ad7 100644 --- a/src/Search/State/Old.php +++ b/src/Search/State/Old.php @@ -16,7 +16,7 @@ class Old extends AbstractCondition * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'OLD'; } diff --git a/src/Search/State/Undeleted.php b/src/Search/State/Undeleted.php index c01b9f8f..1ffade18 100644 --- a/src/Search/State/Undeleted.php +++ b/src/Search/State/Undeleted.php @@ -17,7 +17,7 @@ class Undeleted extends AbstractCondition * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'UNDELETED'; } diff --git a/src/Search/Text/AbstractText.php b/src/Search/Text/AbstractText.php index 885956d1..d466b220 100644 --- a/src/Search/Text/AbstractText.php +++ b/src/Search/Text/AbstractText.php @@ -24,7 +24,7 @@ abstract class AbstractText extends AbstractCondition * * @param string $text optional text for the condition */ - public function __construct($text = null) + public function __construct(string $text = null) { if (null !== $text && strlen($text) > 0) { $this->setText($text); @@ -36,7 +36,7 @@ public function __construct($text = null) * * @param string $text */ - public function setText($text) + public function setText(string $text) { $this->text = $text; } diff --git a/src/Search/Text/Body.php b/src/Search/Text/Body.php index b74e916d..ce4ae3f2 100644 --- a/src/Search/Text/Body.php +++ b/src/Search/Text/Body.php @@ -15,7 +15,7 @@ class Body extends Text * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'BODY'; } diff --git a/src/Search/Text/Keyword.php b/src/Search/Text/Keyword.php index 5ab95b04..7daae24d 100644 --- a/src/Search/Text/Keyword.php +++ b/src/Search/Text/Keyword.php @@ -15,7 +15,7 @@ class Keyword extends Text * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'KEYWORD'; } diff --git a/src/Search/Text/Subject.php b/src/Search/Text/Subject.php index 41f8bd40..874a56ce 100644 --- a/src/Search/Text/Subject.php +++ b/src/Search/Text/Subject.php @@ -15,7 +15,7 @@ class Subject extends Text * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'SUBJECT'; } diff --git a/src/Search/Text/Text.php b/src/Search/Text/Text.php index e5d2a495..bed51d5c 100644 --- a/src/Search/Text/Text.php +++ b/src/Search/Text/Text.php @@ -15,7 +15,7 @@ class Text extends AbstractText * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'TEXT'; } diff --git a/src/Search/Text/Unkeyword.php b/src/Search/Text/Unkeyword.php index 61bcec28..0ccfc268 100644 --- a/src/Search/Text/Unkeyword.php +++ b/src/Search/Text/Unkeyword.php @@ -15,7 +15,7 @@ class Unkeyword extends Text * * @return string */ - public function getKeyword() + public function getKeyword(): string { return 'UNKEYWORD'; } diff --git a/src/SearchExpression.php b/src/SearchExpression.php index 9857ca2a..d25c10a7 100644 --- a/src/SearchExpression.php +++ b/src/SearchExpression.php @@ -25,7 +25,7 @@ class SearchExpression * * @return SearchExpression */ - public function addCondition(AbstractCondition $condition) + public function addCondition(AbstractCondition $condition): self { $this->conditions[] = $condition; @@ -37,7 +37,7 @@ public function addCondition(AbstractCondition $condition) * * @return string */ - public function __toString() + public function __toString(): string { return implode(' ', $this->conditions); } diff --git a/src/Server.php b/src/Server.php index a5ac3e64..ba839b9c 100644 --- a/src/Server.php +++ b/src/Server.php @@ -46,10 +46,10 @@ class Server * @param array $parameters Connection parameters */ public function __construct( - $hostname, - $port = 993, - $flags = '/imap/ssl/validate-cert', - $parameters = [] + string $hostname, + string $port = '993', + string $flags = '/imap/ssl/validate-cert', + array $parameters = [] ) { if (!function_exists('imap_open')) { throw new \RuntimeException('IMAP extension must be enabled'); @@ -71,7 +71,7 @@ public function __construct( * * @return Connection */ - public function authenticate($username, $password) + public function authenticate(string $username, string $password): Connection { // Wrap imap_open, which gives notices instead of exceptions set_error_handler(function ($nr, $message) use ($username) { @@ -113,7 +113,7 @@ public function authenticate($username, $password) * * @return string */ - private function getServerString() + private function getServerString(): string { return sprintf( '{%s:%s%s}', diff --git a/tests/AbstractTest.php b/tests/AbstractTest.php index a68a02fd..909e6911 100644 --- a/tests/AbstractTest.php +++ b/tests/AbstractTest.php @@ -10,7 +10,7 @@ abstract class AbstractTest extends PHPUnit_Framework_TestCase { - protected function getConnection() + final protected function getConnection() { static $connection; if (null === $connection) { @@ -21,14 +21,14 @@ protected function getConnection() return $connection; } - protected function createMailbox() + final protected function createMailbox() { $this->mailboxName = uniqid('mailbox_'); return $this->getConnection()->createMailbox($this->mailboxName); } - protected function createTestMessage( + final protected function createTestMessage( Mailbox $mailbox, $subject = 'Don\'t panic!', $contents = 'Don\'t forget your towel', @@ -44,7 +44,7 @@ protected function createTestMessage( $mailbox->addMessage($message); } - protected function getFixture($fixture) + final protected function getFixture($fixture) { return file_get_contents(__DIR__ . '/fixtures/' . $fixture); } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 0a9e865a..afe96270 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -4,6 +4,8 @@ namespace Ddeboer\Imap\Tests; +use Ddeboer\Imap\Exception\MailboxDoesNotExistException; + class ConnectionTest extends AbstractTest { public function testCount() @@ -27,9 +29,6 @@ public function testGetMailbox() $this->assertInstanceOf('\Ddeboer\Imap\Mailbox', $mailbox); } - /** - * @expectedException \Ddeboer\Imap\Exception\MailboxDoesNotExistException - */ public function testCreateMailbox() { $connection = static::getConnection(); @@ -48,14 +47,15 @@ public function testCreateMailbox() ); $mailbox->delete(); + + $this->expectException(MailboxDoesNotExistException::class); + $connection->getMailbox($name); } - /** - * @expectedException \Ddeboer\Imap\Exception\MailboxDoesNotExistException - */ public function testGetInvalidMailbox() { + $this->expectException(MailboxDoesNotExistException::class); static::getConnection()->getMailbox('does-not-exist'); } } diff --git a/tests/MailboxTest.php b/tests/MailboxTest.php index 9334fef3..d6577492 100644 --- a/tests/MailboxTest.php +++ b/tests/MailboxTest.php @@ -17,7 +17,7 @@ class MailboxTest extends AbstractTest */ protected $mailbox; - public function setUp() + protected function setUp() { $this->mailbox = $this->createMailbox(); diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 567941e8..559a8105 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -11,7 +11,7 @@ class MessageTest extends AbstractTest */ protected $mailbox; - public function setUp() + protected function setUp() { $this->mailbox = $this->createMailbox(); } @@ -115,10 +115,10 @@ public function testDelete() /** * @dataProvider getAttachmentFixture */ - public function testGetAttachments() + public function testGetAttachments(string $fixture) { $this->mailbox->addMessage( - $this->getFixture('attachment_encoded_filename') + $this->getFixture($fixture) ); $message = $this->mailbox->getMessage(1); @@ -130,7 +130,7 @@ public function testGetAttachments() ); } - public function getAttachmentFixture() + public function getAttachmentFixture(): array { return [ ['attachment_no_disposition'],