Merge pull request #8614 from nextcloud/strict_mail

Make \OCP\Mail strict
This commit is contained in:
Morris Jobke 2018-03-05 10:15:52 +01:00 committed by GitHub
commit 36d89a9223
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 106 additions and 94 deletions

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
@ -45,7 +46,7 @@ class Attachment implements IAttachment {
* @return $this
* @since 13.0.0
*/
public function setFilename($filename) {
public function setFilename(string $filename): IAttachment {
$this->swiftAttachment->setFilename($filename);
return $this;
}
@ -55,7 +56,7 @@ class Attachment implements IAttachment {
* @return $this
* @since 13.0.0
*/
public function setContentType($contentType) {
public function setContentType(string $contentType): IAttachment {
$this->swiftAttachment->setContentType($contentType);
return $this;
}
@ -65,7 +66,7 @@ class Attachment implements IAttachment {
* @return $this
* @since 13.0.0
*/
public function setBody($body) {
public function setBody(string $body): IAttachment {
$this->swiftAttachment->setBody($body);
return $this;
}
@ -73,7 +74,7 @@ class Attachment implements IAttachment {
/**
* @return \Swift_Mime_Attachment
*/
public function getSwiftAttachment() {
public function getSwiftAttachment(): \Swift_Mime_Attachment {
return $this->swiftAttachment;
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright 2017, Morris Jobke <hey@morrisjobke.de>
* @copyright 2017, Lukas Reschke <lukas@statuscode.ch>
@ -367,7 +368,7 @@ EOF;
*
* @param string $subject
*/
public function setSubject($subject) {
public function setSubject(string $subject) {
$this->subject = $subject;
}
@ -391,7 +392,7 @@ EOF;
* @param string|bool $plainTitle Title that is used in the plain text email
* if empty the $title is used, if false none will be used
*/
public function addHeading($title, $plainTitle = '') {
public function addHeading(string $title, $plainTitle = '') {
if ($this->footerAdded) {
return;
}
@ -424,7 +425,7 @@ EOF;
* @param string|bool $plainText Text that is used in the plain text email
* if empty the $text is used, if false none will be used
*/
public function addBodyText($text, $plainText = '') {
public function addBodyText(string $text, $plainText = '') {
if ($this->footerAdded) {
return;
}
@ -447,13 +448,13 @@ EOF;
* @param string $text Note: When $plainText falls back to this, HTML is automatically escaped in the HTML email
* @param string $metaInfo Note: When $plainMetaInfo falls back to this, HTML is automatically escaped in the HTML email
* @param string $icon Absolute path, must be 16*16 pixels
* @param string $plainText Text that is used in the plain text email
* @param string|bool $plainText Text that is used in the plain text email
* if empty the $text is used, if false none will be used
* @param string $plainMetaInfo Meta info that is used in the plain text email
* @param string|bool $plainMetaInfo Meta info that is used in the plain text email
* if empty the $metaInfo is used, if false none will be used
* @since 12.0.0
*/
public function addBodyListItem($text, $metaInfo = '', $icon = '', $plainText = '', $plainMetaInfo = '') {
public function addBodyListItem(string $text, string $metaInfo = '', string $icon = '', $plainText = '', $plainMetaInfo = '') {
$this->ensureBodyListOpened();
if ($plainText === '') {
@ -513,12 +514,12 @@ EOF;
* @param string $plainTextLeft Text of left button that is used in the plain text version - if unset the $textLeft is used
* @param string $plainTextRight Text of right button that is used in the plain text version - if unset the $textRight is used
*/
public function addBodyButtonGroup($textLeft,
$urlLeft,
$textRight,
$urlRight,
$plainTextLeft = '',
$plainTextRight = '') {
public function addBodyButtonGroup(string $textLeft,
string $urlLeft,
string $textRight,
string $urlRight,
string $plainTextLeft = '',
string $plainTextRight = '') {
if ($this->footerAdded) {
return;
}
@ -554,7 +555,7 @@ EOF;
*
* @since 12.0.0
*/
public function addBodyButton($text, $url, $plainText = '') {
public function addBodyButton(string $text, string $url, $plainText = '') {
if ($this->footerAdded) {
return;
}
@ -598,7 +599,7 @@ EOF;
*
* @param string $text If the text is empty the default "Name - Slogan<br>This is an automatically sent email" will be used
*/
public function addFooter($text = '') {
public function addFooter(string $text = '') {
if($text === '') {
$text = $this->themingDefaults->getName() . ' - ' . $this->themingDefaults->getSlogan() . '<br>' . $this->l10n->t('This is an automatically sent email, please do not reply.');
}
@ -621,7 +622,7 @@ EOF;
*
* @return string
*/
public function renderSubject() {
public function renderSubject(): string {
return $this->subject;
}
@ -630,7 +631,7 @@ EOF;
*
* @return string
*/
public function renderHtml() {
public function renderHtml(): string {
if (!$this->footerAdded) {
$this->footerAdded = true;
$this->ensureBodyIsClosed();
@ -644,7 +645,7 @@ EOF;
*
* @return string
*/
public function renderText() {
public function renderText(): string {
if (!$this->footerAdded) {
$this->footerAdded = true;
$this->ensureBodyIsClosed();

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@ -91,7 +92,7 @@ class Mailer implements IMailer {
*
* @return IMessage
*/
public function createMessage() {
public function createMessage(): IMessage {
return new Message(new \Swift_Message());
}
@ -102,7 +103,7 @@ class Mailer implements IMailer {
* @return IAttachment
* @since 13.0.0
*/
public function createAttachment($data = null, $filename = null, $contentType = null) {
public function createAttachment($data = null, $filename = null, $contentType = null): IAttachment {
return new Attachment(\Swift_Attachment::newInstance($data, $filename, $contentType));
}
@ -112,7 +113,7 @@ class Mailer implements IMailer {
* @return IAttachment
* @since 13.0.0
*/
public function createAttachmentFromPath($path, $contentType = null) {
public function createAttachmentFromPath(string $path, $contentType = null): IAttachment {
return new Attachment(\Swift_Attachment::fromPath($path, $contentType));
}
@ -124,7 +125,7 @@ class Mailer implements IMailer {
* @return IEMailTemplate
* @since 12.0.0
*/
public function createEMailTemplate($emailId, array $data = []) {
public function createEMailTemplate(string $emailId, array $data = []): IEMailTemplate {
$class = $this->config->getSystemValue('mail_template_class', '');
if ($class !== '' && class_exists($class) && is_a($class, EMailTemplate::class, true)) {
@ -156,7 +157,7 @@ class Mailer implements IMailer {
* @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address
* has been supplied.)
*/
public function send(IMessage $message) {
public function send(IMessage $message): array {
$debugMode = $this->config->getSystemValue('mail_smtpdebug', false);
if (empty($message->getFrom())) {
@ -191,7 +192,7 @@ class Mailer implements IMailer {
* @param string $email Email address to be validated
* @return bool True if the mail address is valid, false otherwise
*/
public function validateMailAddress($email) {
public function validateMailAddress(string $email): bool {
return \Swift_Validate::email($this->convertEmail($email));
}
@ -203,7 +204,7 @@ class Mailer implements IMailer {
* @param string $email
* @return string Converted mail address if `idn_to_ascii` exists
*/
protected function convertEmail($email) {
protected function convertEmail(string $email): string {
if (!function_exists('idn_to_ascii') || strpos($email, '@') === false) {
return $email;
}
@ -246,7 +247,7 @@ class Mailer implements IMailer {
*
* @return \Swift_SmtpTransport
*/
protected function getSmtpInstance() {
protected function getSmtpInstance(): \Swift_SmtpTransport {
$transport = \Swift_SmtpTransport::newInstance();
$transport->setTimeout($this->config->getSystemValue('mail_smtptimeout', 10));
$transport->setHost($this->config->getSystemValue('mail_smtphost', '127.0.0.1'));
@ -269,7 +270,7 @@ class Mailer implements IMailer {
*
* @return \Swift_SendmailTransport
*/
protected function getSendMailInstance() {
protected function getSendMailInstance(): \Swift_SendmailTransport {
switch ($this->config->getSystemValue('mail_smtpmode', 'php')) {
case 'qmail':
$binaryPath = '/var/qmail/bin/sendmail';
@ -287,7 +288,7 @@ class Mailer implements IMailer {
*
* @return \Swift_MailTransport
*/
protected function getMailInstance() {
protected function getMailInstance(): \Swift_MailTransport {
return \Swift_MailTransport::newInstance();
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@ -51,7 +52,7 @@ class Message implements IMessage {
* @return $this
* @since 13.0.0
*/
public function attach(IAttachment $attachment) {
public function attach(IAttachment $attachment): IMessage {
/** @var Attachment $attachment */
$this->swiftMessage->attach($attachment->getSwiftAttachment());
return $this;
@ -64,12 +65,12 @@ class Message implements IMessage {
* @param array $addresses Array of mail addresses, key will get converted
* @return array Converted addresses if `idn_to_ascii` exists
*/
protected function convertAddresses($addresses) {
protected function convertAddresses(array $addresses): array {
if (!function_exists('idn_to_ascii')) {
return $addresses;
}
$convertedAddresses = array();
$convertedAddresses = [];
foreach($addresses as $email => $readableName) {
if(!is_numeric($email)) {
@ -94,7 +95,7 @@ class Message implements IMessage {
* @param array $addresses Example: array('sender@domain.org', 'other@domain.org' => 'A name')
* @return $this
*/
public function setFrom(array $addresses) {
public function setFrom(array $addresses): IMessage {
$addresses = $this->convertAddresses($addresses);
$this->swiftMessage->setFrom($addresses);
@ -106,7 +107,7 @@ class Message implements IMessage {
*
* @return array
*/
public function getFrom() {
public function getFrom(): array {
return $this->swiftMessage->getFrom();
}
@ -116,7 +117,7 @@ class Message implements IMessage {
* @param array $addresses
* @return $this
*/
public function setReplyTo(array $addresses) {
public function setReplyTo(array $addresses): IMessage {
$addresses = $this->convertAddresses($addresses);
$this->swiftMessage->setReplyTo($addresses);
@ -126,9 +127,9 @@ class Message implements IMessage {
/**
* Returns the Reply-To address of this message
*
* @return array
* @return string
*/
public function getReplyTo() {
public function getReplyTo(): string {
return $this->swiftMessage->getReplyTo();
}
@ -138,7 +139,7 @@ class Message implements IMessage {
* @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
* @return $this
*/
public function setTo(array $recipients) {
public function setTo(array $recipients): IMessage {
$recipients = $this->convertAddresses($recipients);
$this->swiftMessage->setTo($recipients);
@ -150,7 +151,7 @@ class Message implements IMessage {
*
* @return array
*/
public function getTo() {
public function getTo(): array {
return $this->swiftMessage->getTo();
}
@ -160,7 +161,7 @@ class Message implements IMessage {
* @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
* @return $this
*/
public function setCc(array $recipients) {
public function setCc(array $recipients): IMessage {
$recipients = $this->convertAddresses($recipients);
$this->swiftMessage->setCc($recipients);
@ -172,7 +173,7 @@ class Message implements IMessage {
*
* @return array
*/
public function getCc() {
public function getCc(): array {
return $this->swiftMessage->getCc();
}
@ -182,7 +183,7 @@ class Message implements IMessage {
* @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
* @return $this
*/
public function setBcc(array $recipients) {
public function setBcc(array $recipients): IMessage {
$recipients = $this->convertAddresses($recipients);
$this->swiftMessage->setBcc($recipients);
@ -194,17 +195,17 @@ class Message implements IMessage {
*
* @return array
*/
public function getBcc() {
public function getBcc(): array {
return $this->swiftMessage->getBcc();
}
/**
* Set the subject of this message.
*
* @param $subject
* @return $this
* @param string $subject
* @return IMessage
*/
public function setSubject($subject) {
public function setSubject(string $subject): IMessage {
$this->swiftMessage->setSubject($subject);
return $this;
}
@ -214,7 +215,7 @@ class Message implements IMessage {
*
* @return string
*/
public function getSubject() {
public function getSubject(): string {
return $this->swiftMessage->getSubject();
}
@ -224,7 +225,7 @@ class Message implements IMessage {
* @param string $body
* @return $this
*/
public function setPlainBody($body) {
public function setPlainBody(string $body): IMessage {
$this->swiftMessage->setBody($body);
return $this;
}
@ -234,7 +235,7 @@ class Message implements IMessage {
*
* @return string
*/
public function getPlainBody() {
public function getPlainBody(): string {
return $this->swiftMessage->getBody();
}
@ -253,7 +254,7 @@ class Message implements IMessage {
* Get's the underlying SwiftMessage
* @return Swift_Message
*/
public function getSwiftMessage() {
public function getSwiftMessage(): Swift_Message {
return $this->swiftMessage;
}
@ -271,7 +272,7 @@ class Message implements IMessage {
* @param IEMailTemplate $emailTemplate
* @return $this
*/
public function useTemplate(IEMailTemplate $emailTemplate) {
public function useTemplate(IEMailTemplate $emailTemplate): IMessage {
$this->setSubject($emailTemplate->renderSubject());
$this->setPlainBody($emailTemplate->renderText());
$this->setHtmlBody($emailTemplate->renderHtml());

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
@ -33,23 +34,23 @@ interface IAttachment {
/**
* @param string $filename
* @return $this
* @return IAttachment
* @since 13.0.0
*/
public function setFilename($filename);
public function setFilename(string $filename): IAttachment;
/**
* @param string $contentType
* @return $this
* @return IAttachment
* @since 13.0.0
*/
public function setContentType($contentType);
public function setContentType(string $contentType): IAttachment;
/**
* @param string $body
* @return $this
* @return IAttachment
* @since 13.0.0
*/
public function setBody($body);
public function setBody(string $body): IAttachment;
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright 2017, Morris Jobke <hey@morrisjobke.de>
*
@ -62,7 +63,7 @@ interface IEMailTemplate {
*
* @since 13.0.0
*/
public function setSubject($subject);
public function setSubject(string $subject);
/**
* Adds a header to the email
@ -80,7 +81,7 @@ interface IEMailTemplate {
*
* @since 12.0.0
*/
public function addHeading($title, $plainTitle = '');
public function addHeading(string $title, $plainTitle = '');
/**
* Adds a paragraph to the body of the email
@ -91,7 +92,7 @@ interface IEMailTemplate {
*
* @since 12.0.0
*/
public function addBodyText($text, $plainText = '');
public function addBodyText(string $text, $plainText = '');
/**
* Adds a list item to the body of the email
@ -99,13 +100,13 @@ interface IEMailTemplate {
* @param string $text; Note: When $plainText falls back to this, HTML is automatically escaped in the HTML email
* @param string $metaInfo; Note: When $plainMetaInfo falls back to this, HTML is automatically escaped in the HTML email
* @param string $icon Absolute path, must be 16*16 pixels
* @param string $plainText Text that is used in the plain text email
* @param string|bool $plainText Text that is used in the plain text email
* if empty the $text is used, if false none will be used
* @param string $plainMetaInfo Meta info that is used in the plain text email
* @param string|bool $plainMetaInfo Meta info that is used in the plain text email
* if empty the $metaInfo is used, if false none will be used
* @since 12.0.0
*/
public function addBodyListItem($text, $metaInfo = '', $icon = '', $plainText = '', $plainMetaInfo = '');
public function addBodyListItem(string $text, string $metaInfo = '', string $icon = '', $plainText = '', $plainMetaInfo = '');
/**
* Adds a button group of two buttons to the body of the email
@ -119,7 +120,7 @@ interface IEMailTemplate {
*
* @since 12.0.0
*/
public function addBodyButtonGroup($textLeft, $urlLeft, $textRight, $urlRight, $plainTextLeft = '', $plainTextRight = '');
public function addBodyButtonGroup(string $textLeft, string $urlLeft, string $textRight, string $urlRight, string $plainTextLeft = '', string $plainTextRight = '');
/**
* Adds a button to the body of the email
@ -131,7 +132,7 @@ interface IEMailTemplate {
*
* @since 12.0.0
*/
public function addBodyButton($text, $url, $plainText = '');
public function addBodyButton(string $text, string $url, $plainText = '');
/**
* Adds a logo and a text to the footer. <br> in the text will be replaced by new lines in the plain text email
@ -140,7 +141,7 @@ interface IEMailTemplate {
*
* @since 12.0.0
*/
public function addFooter($text = '');
public function addFooter(string $text = '');
/**
* Returns the rendered email subject as string
@ -149,7 +150,7 @@ interface IEMailTemplate {
*
* @since 13.0.0
*/
public function renderSubject();
public function renderSubject(): string;
/**
* Returns the rendered HTML email as string
@ -158,7 +159,7 @@ interface IEMailTemplate {
*
* @since 12.0.0
*/
public function renderHtml();
public function renderHtml(): string;
/**
* Returns the rendered plain text email as string
@ -167,5 +168,5 @@ interface IEMailTemplate {
*
* @since 12.0.0
*/
public function renderText();
public function renderText(): string;
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@ -51,7 +52,7 @@ interface IMailer {
* @return IMessage
* @since 8.1.0
*/
public function createMessage();
public function createMessage(): IMessage;
/**
* @param string|null $data
@ -60,7 +61,7 @@ interface IMailer {
* @return IAttachment
* @since 13.0.0
*/
public function createAttachment($data = null, $filename = null, $contentType = null);
public function createAttachment($data = null, $filename = null, $contentType = null): IAttachment;
/**
* @param string $path
@ -68,7 +69,7 @@ interface IMailer {
* @return IAttachment
* @since 13.0.0
*/
public function createAttachmentFromPath($path, $contentType = null);
public function createAttachmentFromPath(string $path, $contentType = null): IAttachment;
/**
* Creates a new email template object
@ -78,7 +79,7 @@ interface IMailer {
* @return IEMailTemplate
* @since 12.0.0 Parameters added in 12.0.3
*/
public function createEMailTemplate($emailId, array $data = []);
public function createEMailTemplate(string $emailId, array $data = []): IEMailTemplate;
/**
* Send the specified message. Also sets the from address to the value defined in config.php
@ -91,7 +92,7 @@ interface IMailer {
* has been supplied.)
* @since 8.1.0
*/
public function send(IMessage $message);
public function send(IMessage $message): array;
/**
* Checks if an e-mail address is valid
@ -100,5 +101,5 @@ interface IMailer {
* @return bool True if the mail address is valid, false otherwise
* @since 8.1.0
*/
public function validateMailAddress($email);
public function validateMailAddress(string $email): bool;
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
@ -33,10 +34,10 @@ interface IMessage {
/**
* @param IAttachment $attachment
* @return $this
* @return IMessage
* @since 13.0.0
*/
public function attach(IAttachment $attachment);
public function attach(IAttachment $attachment): IMessage;
/**
* Set the from address of this message.
@ -44,51 +45,51 @@ interface IMessage {
* If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
*
* @param array $addresses Example: array('sender@domain.org', 'other@domain.org' => 'A name')
* @return $this
* @return IMessage
* @since 13.0.0
*/
public function setFrom(array $addresses);
public function setFrom(array $addresses): IMessage;
/**
* Set the Reply-To address of this message
*
* @param array $addresses
* @return $this
* @return IMessage
* @since 13.0.0
*/
public function setReplyTo(array $addresses);
public function setReplyTo(array $addresses): IMessage;
/**
* Set the to addresses of this message.
*
* @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
* @return $this
* @return IMessage
* @since 13.0.0
*/
public function setTo(array $recipients);
public function setTo(array $recipients): IMessage;
/**
* Set the CC recipients of this message.
*
* @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
* @return $this
* @return IMessage
* @since 13.0.0
*/
public function setCc(array $recipients);
public function setCc(array $recipients): IMessage;
/**
* Set the BCC recipients of this message.
*
* @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
* @return $this
* @return IMessage
* @since 13.0.0
*/
public function setBcc(array $recipients);
public function setBcc(array $recipients): IMessage;
/**
* @param IEMailTemplate $emailTemplate
* @return $this
* @return IMessage
* @since 13.0.0
*/
public function useTemplate(IEMailTemplate $emailTemplate);
public function useTemplate(IEMailTemplate $emailTemplate): IMessage;
}

View File

@ -133,6 +133,10 @@ class MailerTest extends TestCase {
}
public function testCreateEMailTemplate() {
$this->config->method('getSystemValue')
->with('mail_template_class', '')
->willReturnArgument(1);
$this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest')));
}
}

View File

@ -30,7 +30,7 @@ class MessageTest extends TestCase {
);
}
function setUp() {
public function setUp() {
parent::setUp();
$this->swiftMessage = $this->getMockBuilder('\Swift_Message')
@ -79,9 +79,9 @@ class MessageTest extends TestCase {
$this->swiftMessage
->expects($this->once())
->method('getReplyTo')
->will($this->returnValue(['lukas@owncloud.com']));
->willReturn('lukas@owncloud.com');
$this->assertSame(['lukas@owncloud.com'], $this->message->getReplyTo());
$this->assertSame('lukas@owncloud.com', $this->message->getReplyTo());
}
public function testSetTo() {