Clean up missing or inconsistent types in Mail, SMS, Push classes

This commit is contained in:
Kijin Sung 2023-10-03 15:05:06 +09:00
parent 7c844c7e24
commit 27c8d32dc5
4 changed files with 121 additions and 108 deletions

View file

@ -27,10 +27,10 @@ class Mail
/**
* Set the default driver.
*
* @param object $driver
* @param Drivers\MailInterface $driver
* @return void
*/
public static function setDefaultDriver(Drivers\MailInterface $driver)
public static function setDefaultDriver(Drivers\MailInterface $driver): void
{
self::$default_driver = $driver;
}
@ -38,9 +38,9 @@ class Mail
/**
* Get the default driver.
*
* @return object
* @return Drivers\MailInterface
*/
public static function getDefaultDriver()
public static function getDefaultDriver(): Drivers\MailInterface
{
if (!self::$default_driver)
{
@ -61,8 +61,11 @@ class Mail
/**
* Add a custom mail driver.
*
* @param Drivers\MailInterface $driver
* @return void
*/
public static function addDriver(Drivers\MailInterface $driver)
public static function addDriver(Drivers\MailInterface $driver): void
{
self::$custom_drivers[] = $driver;
}
@ -72,7 +75,7 @@ class Mail
*
* @return array
*/
public static function getSupportedDrivers()
public static function getSupportedDrivers(): array
{
$result = array();
foreach (Storage::readDirectory(__DIR__ . '/drivers/mail', false) as $filename)
@ -123,7 +126,7 @@ class Mail
* @param string $name Name (optional)
* @return bool
*/
public function setFrom($email, $name = null)
public function setFrom(string $email, ?string $name = null): bool
{
try
{
@ -142,7 +145,7 @@ class Mail
*
* @return string|null
*/
public function getFrom()
public function getFrom(): ?string
{
$list = $this->message->getFrom();
return $list ? array_first($this->formatAddresses($list)) : null;
@ -155,7 +158,7 @@ class Mail
* @param string $name Name (optional)
* @return bool
*/
public function addTo($email, $name = null)
public function addTo(string $email, ?string $name = null): bool
{
try
{
@ -176,7 +179,7 @@ class Mail
* @param string $name Name (optional)
* @return bool
*/
public function addCc($email, $name = null)
public function addCc(string $email, ?string $name = null): bool
{
try
{
@ -197,7 +200,7 @@ class Mail
* @param string $name Name (optional)
* @return bool
*/
public function addBcc($email, $name = null)
public function addBcc(string $email, ?string $name = null): bool
{
try
{
@ -214,21 +217,21 @@ class Mail
/**
* Get the list of recipients.
*
* @return array();
* @return array
*/
public function getRecipients()
public function getRecipients(): array
{
$result = array();
foreach ($this->formatAddresses($this->message->getTo()) as $address)
foreach ($this->formatAddresses($this->message->getTo() ?: []) as $address)
{
$result[] = $address;
}
foreach ($this->formatAddresses($this->message->getCc()) as $address)
foreach ($this->formatAddresses($this->message->getCc() ?: []) as $address)
{
$result[] = $address;
}
foreach ($this->formatAddresses($this->message->getBcc()) as $address)
foreach ($this->formatAddresses($this->message->getBcc() ?: []) as $address)
{
$result[] = $address;
}
@ -242,7 +245,7 @@ class Mail
* @param string $replyTo
* @return bool
*/
public function setReplyTo($replyTo)
public function setReplyTo(string $replyTo): bool
{
try
{
@ -262,7 +265,7 @@ class Mail
* @param string $returnPath
* @return bool
*/
public function setReturnPath($returnPath)
public function setReturnPath(string $returnPath): bool
{
try
{
@ -279,15 +282,15 @@ class Mail
/**
* Set the Message ID.
*
* @param string $messageId
* @param string $message_id
* @return bool
*/
public function setMessageID($messageId)
public function setMessageID(string $message_id): bool
{
try
{
$headers = $this->message->getHeaders();
$headers->get('Message-ID')->setId($messageId);
$headers->get('Message-ID')->setId($message_id);
return true;
}
catch (\Exception $e)
@ -300,15 +303,15 @@ class Mail
/**
* Set the In-Reply-To: header.
*
* @param string $inReplyTo
* @param string $in_reply_to
* @return bool
*/
public function setInReplyTo($inReplyTo)
public function setInReplyTo(string $in_reply_to): bool
{
try
{
$headers = $this->message->getHeaders();
$headers->addTextHeader('In-Reply-To', $inReplyTo);
$headers->addTextHeader('In-Reply-To', $in_reply_to);
return true;
}
catch (\Exception $e)
@ -324,7 +327,7 @@ class Mail
* @param string $references
* @return bool
*/
public function setReferences($references)
public function setReferences(string $references): bool
{
try
{
@ -345,11 +348,11 @@ class Mail
* @param string $subject
* @return bool
*/
public function setSubject($subject)
public function setSubject(string $subject): bool
{
try
{
$this->message->setSubject(strval($subject));
$this->message->setSubject($subject);
return true;
}
catch (\Exception $e)
@ -364,7 +367,7 @@ class Mail
*
* @return string
*/
public function getSubject()
public function getSubject(): string
{
return $this->message->getSubject();
}
@ -375,7 +378,7 @@ class Mail
* @param string $subject
* @return bool
*/
public function setTitle($subject)
public function setTitle(string $subject): bool
{
return $this->setSubject($subject);
}
@ -385,7 +388,7 @@ class Mail
*
* @return string
*/
public function getTitle()
public function getTitle(): string
{
return $this->getSubject();
}
@ -397,7 +400,7 @@ class Mail
* @param string $content_type (optional)
* @return void
*/
public function setBody($content, $content_type = null)
public function setBody(string $content, ?string $content_type = null): void
{
if ($content_type !== null)
{
@ -417,7 +420,7 @@ class Mail
*
* @return string
*/
public function getBody()
public function getBody(): string
{
return $this->message->getBody();
}
@ -429,9 +432,9 @@ class Mail
* @param string $content_type (optional)
* @return void
*/
public function setContent($content, $content_type = null)
public function setContent(string $content, ?string $content_type = null): void
{
return $this->setBody($content, $content_type);
$this->setBody($content, $content_type);
}
/**
@ -439,7 +442,7 @@ class Mail
*
* @return string
*/
public function getContent()
public function getContent(): string
{
return $this->getBody();
}
@ -450,7 +453,7 @@ class Mail
* @param string $mode The type
* @return void
*/
public function setContentType($type = 'text/html')
public function setContentType(string $type = 'text/html'): void
{
$this->content_type = (strpos($type, 'html') !== false) ? 'text/html' : ((strpos($type, '/') !== false) ? $type : 'text/plain');
}
@ -460,7 +463,7 @@ class Mail
*
* @return string
*/
public function getContentType()
public function getContentType(): string
{
return $this->content_type;
}
@ -472,7 +475,7 @@ class Mail
* @param string $display_filename (optional)
* @return bool
*/
public function attach($local_filename, $display_filename = null)
public function attach(string $local_filename, ?string $display_filename = null): bool
{
if ($display_filename === null)
{
@ -510,7 +513,7 @@ class Mail
* @param string $cid (optional)
* @return string|false
*/
public function embed($local_filename, $cid = null)
public function embed(string $local_filename, ?string $cid = null)
{
if (!Storage::exists($local_filename))
{
@ -545,7 +548,7 @@ class Mail
*
* @return array
*/
public function getAttachments()
public function getAttachments(): array
{
return $this->attachments;
}
@ -555,7 +558,7 @@ class Mail
*
* @return bool
*/
public function send()
public function send(): bool
{
// Get caller information.
$backtrace = debug_backtrace(0);
@ -602,7 +605,7 @@ class Mail
*
* @return bool
*/
public function isSent()
public function isSent(): bool
{
return $this->sent;
}
@ -612,7 +615,7 @@ class Mail
*
* @return string
*/
public function getCaller()
public function getCaller(): string
{
return $this->caller;
}
@ -622,7 +625,7 @@ class Mail
*
* @return array
*/
public function getErrors()
public function getErrors(): array
{
return $this->errors;
}
@ -634,7 +637,7 @@ class Mail
* @param array $matches Match info.
* @return string
*/
protected function convertImageURLs(array $matches)
protected function convertImageURLs(array $matches): string
{
return Filters\HTMLFilter::fixRelativeUrls($matches[0]);
}
@ -645,7 +648,7 @@ class Mail
* @param array $addresses
* @return array
*/
protected function formatAddresses($addresses)
protected function formatAddresses(array $addresses): array
{
$result = array();

View file

@ -35,7 +35,7 @@ class Push
* @param object $driver
* @return void
*/
public static function addDriver(string $name, Drivers\PushInterface $driver)
public static function addDriver(string $name, Drivers\PushInterface $driver): void
{
self::$_drivers[$name] = $driver;
}
@ -44,9 +44,9 @@ class Push
* Get the default driver.
*
* @param string $name
* @return object|null
* @return ?object
*/
public static function getDriver(string $name)
public static function getDriver(string $name): ?object
{
if (isset(self::$_drivers[$name]))
{
@ -464,34 +464,41 @@ class Push
* Delete the device toekn
*
* @param array
* @return void
* @return bool
*/
protected function _deleteInvalidTokens(array $invalid_tokens)
protected function _deleteInvalidTokens(array $invalid_tokens): bool
{
if(!count($invalid_tokens))
{
return;
return true;
}
$args = new \stdClass;
$args->device_token = $invalid_tokens;
executeQueryArray('member.deleteMemberDevice', $args);
$output = executeQueryArray('member.deleteMemberDevice', $args);
return $output->toBool();
}
/**
* Update the device toekn
*
* @param array
* @return void
* @return bool
*/
protected function _updateDeviceTokens(array $update_tokens)
protected function _updateDeviceTokens(array $update_tokens): bool
{
$args = new \stdClass;
$result = true;
foreach($update_tokens as $key => $value)
{
$args->old_token = $key;
$args->new_token = $value;
executeQueryArray('member.updateMemberDevice', $args);
$output = executeQueryArray('member.updateMemberDevice', $args);
if (!$output->toBool())
{
$result = false;
}
}
return $result;
}
/**
@ -560,7 +567,7 @@ class Push
* @param string $message
* @return void
*/
public function addError(string $message)
public function addError(string $message): void
{
$this->errors[] = $message;
}

View file

@ -34,10 +34,10 @@ class SMS
/**
* Set the default driver.
*
* @param object $driver
* @param Drivers\SMSInterface $driver
* @return void
*/
public static function setDefaultDriver(Drivers\SMSInterface $driver)
public static function setDefaultDriver(Drivers\SMSInterface $driver): void
{
self::$default_driver = $driver;
}
@ -45,9 +45,9 @@ class SMS
/**
* Get the default driver.
*
* @return object
* @return Drivers\SMSInterface
*/
public static function getDefaultDriver()
public static function getDefaultDriver(): Drivers\SMSInterface
{
if (!self::$default_driver)
{
@ -68,8 +68,11 @@ class SMS
/**
* Add a custom mail driver.
*
* @param Drivers\SMSInterface $driver
* @return void
*/
public static function addDriver(Drivers\SMSInterface $driver)
public static function addDriver(Drivers\SMSInterface $driver): void
{
self::$custom_drivers[] = $driver;
}
@ -79,7 +82,7 @@ class SMS
*
* @return array
*/
public static function getSupportedDrivers()
public static function getSupportedDrivers(): array
{
$result = array();
foreach (Storage::readDirectory(__DIR__ . '/drivers/sms', false) as $filename)
@ -132,7 +135,7 @@ class SMS
* @param string $number Phone number
* @return bool
*/
public function setFrom($number)
public function setFrom(string $number): bool
{
$this->from = preg_replace('/[^0-9]/', '', $number);
return true;
@ -143,7 +146,7 @@ class SMS
*
* @return string|null
*/
public function getFrom()
public function getFrom(): ?string
{
return $this->from;
}
@ -155,7 +158,7 @@ class SMS
* @param string $country Country code (optional)
* @return bool
*/
public function addTo($number, $country = 0)
public function addTo(string $number, string $country = '0'): bool
{
$this->to[] = (object)array(
'number' => preg_replace('/[^0-9]/', '', $number),
@ -169,7 +172,7 @@ class SMS
*
* @return array
*/
public function getRecipients()
public function getRecipients(): array
{
return array_map(function($recipient) {
return $recipient->number;
@ -181,7 +184,7 @@ class SMS
*
* @return array
*/
public function getRecipientsWithCountry()
public function getRecipientsWithCountry(): array
{
return $this->to;
}
@ -191,7 +194,7 @@ class SMS
*
* @return array
*/
public function getRecipientsGroupedByCountry()
public function getRecipientsGroupedByCountry(): array
{
$result = array();
foreach ($this->to as $recipient)
@ -207,7 +210,7 @@ class SMS
* @param string $subject
* @return bool
*/
public function setSubject($subject)
public function setSubject(string $subject): bool
{
$this->subject = utf8_trim(utf8_clean($subject));
return true;
@ -218,7 +221,7 @@ class SMS
*
* @return string
*/
public function getSubject()
public function getSubject(): string
{
return $this->subject;
}
@ -229,7 +232,7 @@ class SMS
* @param string $subject
* @return bool
*/
public function setTitle($subject)
public function setTitle(string $subject): bool
{
return $this->setSubject($subject);
}
@ -239,7 +242,7 @@ class SMS
*
* @return string
*/
public function getTitle()
public function getTitle(): string
{
return $this->getSubject();
}
@ -250,7 +253,7 @@ class SMS
* @param string $content
* @return bool
*/
public function setBody($content)
public function setBody(string $content): bool
{
$this->content = utf8_trim(utf8_clean($content));
$this->content = strtr($this->content, array("\r\n" => "\n"));
@ -262,7 +265,7 @@ class SMS
*
* @return string
*/
public function getBody()
public function getBody(): string
{
return $this->content;
}
@ -271,9 +274,9 @@ class SMS
* Set the content (alias to setBody).
*
* @param string $content
* @return void
* @return bool
*/
public function setContent($content)
public function setContent(string $content): bool
{
return $this->setBody($content);
}
@ -283,7 +286,7 @@ class SMS
*
* @return string
*/
public function getContent()
public function getContent(): string
{
return $this->getBody();
}
@ -295,7 +298,7 @@ class SMS
* @param string $display_filename (optional)
* @return bool
*/
public function attach($local_filename, $display_filename = null)
public function attach(string $local_filename, ?string $display_filename = null): bool
{
if ($display_filename === null)
{
@ -320,7 +323,7 @@ class SMS
*
* @return array
*/
public function getAttachments()
public function getAttachments(): array
{
return $this->attachments;
}
@ -332,7 +335,7 @@ class SMS
* @param mixed $value
* @return void
*/
public function setExtraVar($key, $value)
public function setExtraVar(string $key, $value): void
{
$this->extra_vars[$key] = $value;
}
@ -343,7 +346,7 @@ class SMS
* @param string $key
* @return mixed
*/
public function getExtraVar($key)
public function getExtraVar(string $key)
{
return isset($this->extra_vars[$key]) ? $this->extra_vars[$key] : null;
}
@ -352,9 +355,9 @@ class SMS
* Get all extra variables.
*
* @param string $key
* @return mixed
* @return array
*/
public function getExtraVars()
public function getExtraVars(): array
{
return $this->extra_vars;
}
@ -365,7 +368,7 @@ class SMS
* @param array $vars
* @return void
*/
public function setExtraVars(array $vars)
public function setExtraVars(array $vars): void
{
$this->extra_vars = $vars;
}
@ -381,7 +384,7 @@ class SMS
* @param int $when Unix timestamp
* @return bool
*/
public function setDelay($when)
public function setDelay(int $when): bool
{
if ($when <= (86400 * 365))
{
@ -392,8 +395,8 @@ class SMS
$when = 0;
}
$this->delay_timestamp = intval($when);
return true;
$this->delay_timestamp = $when;
return $when > 0;
}
/**
@ -406,7 +409,7 @@ class SMS
*
* @return int
*/
public function getDelay()
public function getDelay(): int
{
return $this->delay_timestamp;
}
@ -416,7 +419,7 @@ class SMS
*
* @return void
*/
public function forceSMS()
public function forceSMS(): void
{
$this->force_sms = true;
}
@ -426,7 +429,7 @@ class SMS
*
* @return void
*/
public function unforceSMS()
public function unforceSMS(): void
{
$this->force_sms = false;
}
@ -436,7 +439,7 @@ class SMS
*
* @return bool
*/
public function isForceSMS()
public function isForceSMS(): bool
{
return $this->force_sms;
}
@ -446,7 +449,7 @@ class SMS
*
* @return void
*/
public function allowSplitSMS()
public function allowSplitSMS(): void
{
$this->allow_split_sms = true;
}
@ -456,7 +459,7 @@ class SMS
*
* @return void
*/
public function allowSplitLMS()
public function allowSplitLMS(): void
{
$this->allow_split_lms = true;
}
@ -466,7 +469,7 @@ class SMS
*
* @return void
*/
public function disallowSplitSMS()
public function disallowSplitSMS(): void
{
$this->allow_split_sms = false;
}
@ -476,7 +479,7 @@ class SMS
*
* @return void
*/
public function disallowSplitLMS()
public function disallowSplitLMS(): void
{
$this->allow_split_lms = false;
}
@ -486,7 +489,7 @@ class SMS
*
* @return bool
*/
public function isSplitSMSAllowed()
public function isSplitSMSAllowed(): bool
{
return $this->allow_split_sms;
}
@ -496,7 +499,7 @@ class SMS
*
* @return bool
*/
public function isSplitLMSAllowed()
public function isSplitLMSAllowed(): bool
{
return $this->allow_split_lms;
}
@ -506,7 +509,7 @@ class SMS
*
* @return bool
*/
public function send()
public function send(): bool
{
// Get caller information.
$backtrace = debug_backtrace(0);
@ -568,7 +571,7 @@ class SMS
*
* @return bool
*/
public function isSent()
public function isSent(): bool
{
return $this->sent;
}
@ -578,7 +581,7 @@ class SMS
*
* @return string
*/
public function getCaller()
public function getCaller(): string
{
return $this->caller;
}
@ -588,7 +591,7 @@ class SMS
*
* @return array
*/
public function getErrors()
public function getErrors(): array
{
return $this->errors;
}
@ -599,7 +602,7 @@ class SMS
* @param string $message
* @return void
*/
public function addError($message)
public function addError(string $message): void
{
$this->errors[] = $message;
}
@ -610,7 +613,7 @@ class SMS
* @param array $spec API specifications
* @return array
*/
protected function _formatSpec(array $spec)
protected function _formatSpec(array $spec): array
{
// Initialize the return array.
$result = array();
@ -771,9 +774,9 @@ class SMS
*
* @param string $str String to measure
* @param string $charset Character set to measure length
* @return
* @return int
*/
protected function _getLengthInCharset($str, $charset)
protected function _getLengthInCharset(string $str, string $charset): int
{
$str = @iconv('UTF-8', $charset . '//IGNORE', $str);
return strlen($str);
@ -787,7 +790,7 @@ class SMS
* @param string $charset Character set to measure length
* @return array
*/
protected function _splitString($str, $max_length, $charset)
protected function _splitString(string $str, int $max_length, string $charset): array
{
$str = utf8_trim(utf8_normalize_spaces($str, true));
$chars = preg_split('//u', $str, -1, \PREG_SPLIT_NO_EMPTY);

View file

@ -135,7 +135,7 @@ class SMSTest extends \Codeception\TestCase\Test
$this->assertLessThanOrEqual(time() + $delay_relative + 1, $sms->getDelay());
$delay_relative = 86400 * 3650;
$this->assertTrue($sms->setDelay($delay_relative));
$this->assertFalse($sms->setDelay($delay_relative));
$this->assertEquals(0, $sms->getDelay());
}