From 0d5cb18b8d9bc05a8a27643dcf7a37b6b3f06da1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Fri, 17 Nov 2017 02:10:25 +0200 Subject: [PATCH] general code style fixes (#1241) * use instanceof instead of is_a is_a is deprecated since 5.0.0 in favour of instanceof operator http://php.net/is_a this allows using class aliases instead of strings * do not assign null to class properties that is default behaviour of php engine * use stripos instead of stristr * use faster $array[]= $value instead of array_push * add SuspiciousAssignmentsInspection noinspection to shut up ide * use call_user_func with defined args * avoid overwriting $error parameter * apply more yoda-style --- src/OAuth.php | 4 ++-- src/PHPMailer.php | 43 ++++++++++++++++++++++--------------------- src/POP3.php | 4 ++-- src/SMTP.php | 2 +- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/OAuth.php b/src/OAuth.php index b2309a1fc..0bce7e346 100644 --- a/src/OAuth.php +++ b/src/OAuth.php @@ -39,14 +39,14 @@ class OAuth * * @var AbstractProvider */ - protected $provider = null; + protected $provider; /** * The current OAuth access token. * * @var AccessToken */ - protected $oauthToken = null; + protected $oauthToken; /** * The user's email address, usually used as the login ID diff --git a/src/PHPMailer.php b/src/PHPMailer.php index baf933496..397adf3ff 100644 --- a/src/PHPMailer.php +++ b/src/PHPMailer.php @@ -37,7 +37,7 @@ class PHPMailer * * @var int */ - public $Priority = null; + public $Priority; /** * The character set of the message. @@ -314,7 +314,7 @@ class PHPMailer * * @var OAuth */ - protected $oauth = null; + protected $oauth; /** * The SMTP server timeout in seconds. @@ -504,7 +504,7 @@ class PHPMailer * * @var SMTP */ - protected $smtp = null; + protected $smtp; /** * The array of 'to' names and addresses. @@ -786,7 +786,7 @@ protected function edebug($str) return; } //Is this a PSR-3 logger? - if (is_a($this->Debugoutput, 'Psr\Log\LoggerInterface')) { + if ($this->Debugoutput instanceof \Psr\Log\LoggerInterface) { $this->Debugoutput->debug($str); return; @@ -866,7 +866,7 @@ public function isSendmail() { $ini_sendmail_path = ini_get('sendmail_path'); - if (!stristr($ini_sendmail_path, 'sendmail')) { + if (false === stripos($ini_sendmail_path, 'sendmail')) { $this->Sendmail = '/usr/sbin/sendmail'; } else { $this->Sendmail = $ini_sendmail_path; @@ -881,7 +881,7 @@ public function isQmail() { $ini_sendmail_path = ini_get('sendmail_path'); - if (!stristr($ini_sendmail_path, 'qmail')) { + if (false === stripos($ini_sendmail_path, 'qmail')) { $this->Sendmail = '/var/qmail/bin/qmail-inject'; } else { $this->Sendmail = $ini_sendmail_path; @@ -1039,7 +1039,7 @@ protected function addAnAddress($kind, $address, $name = '') } if ('Reply-To' != $kind) { if (!array_key_exists(strtolower($address), $this->all_recipients)) { - array_push($this->$kind, [$address, $name]); + $this->{$kind}[] = [$address, $name]; $this->all_recipients[strtolower($address)] = true; return true; @@ -1890,7 +1890,7 @@ public function smtpConnect($options = null) // * we have openssl extension // * we are not already using SSL // * the server offers STARTTLS - if ($this->SMTPAutoTLS and $sslext and $secure != 'ssl' and $this->smtp->getServerExt('STARTTLS')) { + if ($this->SMTPAutoTLS and $sslext and 'ssl' != $secure and $this->smtp->getServerExt('STARTTLS')) { $tls = true; } if ($tls) { @@ -2089,7 +2089,7 @@ public function wrapText($message, $length, $qp_mode = false) } // If utf-8 encoding is used, we will need to make sure we don't // split multibyte characters when we wrap - $is_utf8 = (strtolower($this->CharSet) == 'utf-8'); + $is_utf8 = 'utf-8' == strtolower($this->CharSet); $lelen = strlen(static::$LE); $crlflen = strlen(static::$LE); @@ -2115,9 +2115,9 @@ public function wrapText($message, $length, $qp_mode = false) $len = $space_left; if ($is_utf8) { $len = $this->utf8CharBoundary($word, $len); - } elseif (substr($word, $len - 1, 1) == '=') { + } elseif ('=' == substr($word, $len - 1, 1)) { --$len; - } elseif (substr($word, $len - 2, 1) == '=') { + } elseif ('=' == substr($word, $len - 2, 1)) { $len -= 2; } $part = substr($word, 0, $len); @@ -2136,9 +2136,9 @@ public function wrapText($message, $length, $qp_mode = false) $len = $length; if ($is_utf8) { $len = $this->utf8CharBoundary($word, $len); - } elseif (substr($word, $len - 1, 1) == '=') { + } elseif ('=' == substr($word, $len - 1, 1)) { --$len; - } elseif (substr($word, $len - 2, 1) == '=') { + } elseif ('=' == substr($word, $len - 2, 1)) { $len -= 2; } $part = substr($word, 0, $len); @@ -2157,7 +2157,7 @@ public function wrapText($message, $length, $qp_mode = false) } $buf .= $word; - if (strlen($buf) > $length and $buf_o != '') { + if (strlen($buf) > $length and '' != $buf_o) { $message .= $buf_o . $soft_break; $buf = $word; } @@ -3202,6 +3202,7 @@ public function encodeQ($str, $position = 'text') default: // RFC 2047 section 5.1 // Replace every high ascii, control, =, ? and _ characters + /** @noinspection SuspiciousAssignmentsInspection */ $pattern = '\000-\011\013\014\016-\037\075\077\137\177-\377' . $pattern; break; } @@ -3381,7 +3382,7 @@ protected function cidExists($cid) public function inlineImageExists() { foreach ($this->attachment as $attachment) { - if ($attachment[6] == 'inline') { + if ('inline' == $attachment[6]) { return true; } } @@ -3397,7 +3398,7 @@ public function inlineImageExists() public function attachmentExists() { foreach ($this->attachment as $attachment) { - if ($attachment[6] == 'attachment') { + if ('attachment' == $attachment[6]) { return true; } } @@ -3691,7 +3692,7 @@ public function msgHTML($message, $basedir = '', $advanced = false) { preg_match_all('/(src|background)=["\'](.*)["\']/Ui', $message, $images); if (array_key_exists(2, $images)) { - if (strlen($basedir) > 1 && substr($basedir, -1) != '/') { + if (strlen($basedir) > 1 && '/' != substr($basedir, -1)) { // Ensure $basedir has a trailing / $basedir .= '/'; } @@ -3726,7 +3727,7 @@ public function msgHTML($message, $basedir = '', $advanced = false) // Ignore URLs containing parent dir traversal (..) and (strpos($url, '..') === false) // Do not change urls that are already inline images - and substr($url, 0, 4) !== 'cid:' + and 0 !== strpos($url, 'cid:') // Do not change absolute URLs, including anonymous protocol and !preg_match('#^[a-z][a-z0-9+.-]*:?//#i', $url) ) { @@ -3736,10 +3737,10 @@ public function msgHTML($message, $basedir = '', $advanced = false) $directory = ''; } $cid = hash('sha256', $url) . '@phpmailer.0'; // RFC2392 S 2 - if (strlen($basedir) > 1 and substr($basedir, -1) != '/') { + if (strlen($basedir) > 1 and '/' != substr($basedir, -1)) { $basedir .= '/'; } - if (strlen($directory) > 1 and substr($directory, -1) != '/') { + if (strlen($directory) > 1 and '/' != substr($directory, -1)) { $directory .= '/'; } if ($this->addEmbeddedImage( @@ -4378,7 +4379,7 @@ public function getAllRecipientAddresses() protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body, $from, $extra) { if (!empty($this->action_function) and is_callable($this->action_function)) { - call_user_func_array($this->action_function, [$isSent, $to, $cc, $bcc, $subject, $body, $from, $extra]); + call_user_func($this->action_function, $isSent, $to, $cc, $bcc, $subject, $body, $from, $extra); } } diff --git a/src/POP3.php b/src/POP3.php index 4e8a56253..9ab47192a 100644 --- a/src/POP3.php +++ b/src/POP3.php @@ -384,8 +384,8 @@ protected function setError($error) $this->errors[] = $error; if ($this->do_debug >= 1) { echo '
';
-            foreach ($this->errors as $error) {
-                print_r($error);
+            foreach ($this->errors as $e) {
+                print_r($e);
             }
             echo '
'; } diff --git a/src/SMTP.php b/src/SMTP.php index 223514f01..697bf19ca 100644 --- a/src/SMTP.php +++ b/src/SMTP.php @@ -232,7 +232,7 @@ protected function edebug($str, $level = 0) return; } //Is this a PSR-3 logger? - if (is_a($this->Debugoutput, 'Psr\Log\LoggerInterface')) { + if ($this->Debugoutput instanceof \Psr\Log\LoggerInterface) { $this->Debugoutput->debug($str); return;