MailAddress->toString(): Reduce complexity by inverting ifs
authorReedy <reedy@wikimedia.org>
Sat, 20 Oct 2018 22:31:00 +0000 (23:31 +0100)
committerJames D. Forrester <jforrester@wikimedia.org>
Thu, 25 Oct 2018 17:30:11 +0000 (10:30 -0700)
Change-Id: I5185c0231a0944f36d960211e78cf36273c94d52

includes/mail/MailAddress.php

index 000bbe3..63a114d 100644 (file)
@@ -50,7 +50,7 @@ class MailAddress {
         * @param string|null $name Human-readable name if a string address is given
         * @param string|null $realName Human-readable real name if a string address is given
         */
-       function __construct( $address, $name = null, $realName = null ) {
+       public function __construct( $address, $name = null, $realName = null ) {
                $this->address = strval( $address );
                $this->name = strval( $name );
                $this->realName = strval( $realName );
@@ -72,25 +72,26 @@ class MailAddress {
         * @return string
         */
        function toString() {
+               if ( !$this->address ) {
+                       return '';
+               }
+
                # PHP's mail() implementation under Windows is somewhat shite, and
                # can't handle "Joe Bloggs <joe@bloggs.com>" format email addresses,
                # so don't bother generating them
-               if ( $this->address ) {
-                       if ( $this->name != '' && !wfIsWindows() ) {
-                               global $wgEnotifUseRealName;
-                               $name = ( $wgEnotifUseRealName && $this->realName !== '' ) ? $this->realName : $this->name;
-                               $quoted = UserMailer::quotedPrintable( $name );
-                               // Must only be quoted if string does not use =? encoding (T191931)
-                               if ( $quoted === $name ) {
-                                       $quoted = '"' . addslashes( $quoted ) . '"';
-                               }
-                               return "$quoted <{$this->address}>";
-                       } else {
-                               return $this->address;
-                       }
-               } else {
-                       return "";
+               if ( $this->name === '' || wfIsWindows() ) {
+                       return $this->address;
                }
+
+               global $wgEnotifUseRealName;
+               $name = ( $wgEnotifUseRealName && $this->realName !== '' ) ? $this->realName : $this->name;
+               $quoted = UserMailer::quotedPrintable( $name );
+               // Must only be quoted if string does not use =? encoding (T191931)
+               if ( $quoted === $name ) {
+                       $quoted = '"' . addslashes( $quoted ) . '"';
+               }
+
+               return "$quoted <{$this->address}>";
        }
 
        function __toString() {