Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EasySwift often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EasySwift, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 32 | class EasySwift  | 
            ||
| 33 | { | 
            ||
| 34 | /**  | 
            ||
| 35 | * The instance of Swift this class wrappers  | 
            ||
| 36 | * @var Swift  | 
            ||
| 37 | */  | 
            ||
| 38 | public $swift = null;  | 
            ||
| 39 | /**  | 
            ||
| 40 | * This value becomes set to true when Swift fails  | 
            ||
| 41 | * @var boolean  | 
            ||
| 42 | */  | 
            ||
| 43 | public $failed = false;  | 
            ||
| 44 | /**  | 
            ||
| 45 | * The number of loaded plugins  | 
            ||
| 46 | * @var int  | 
            ||
| 47 | */  | 
            ||
| 48 | protected $pluginCount = 0;  | 
            ||
| 49 | /**  | 
            ||
| 50 | * An instance of Swift_Message  | 
            ||
| 51 | * @var Swift_Message  | 
            ||
| 52 | */  | 
            ||
| 53 | public $message = null;  | 
            ||
| 54 | /**  | 
            ||
| 55 | * An address list to send to (Cc, Bcc, To..)  | 
            ||
| 56 | * @var Swift_RecipientList  | 
            ||
| 57 | */  | 
            ||
| 58 | public $recipients = null;  | 
            ||
| 59 | /**  | 
            ||
| 60 | * If all recipients should get the same copy of the message, including headers  | 
            ||
| 61 | * This is already implied if any Cc or Bcc recipients are set  | 
            ||
| 62 | * @var boolean  | 
            ||
| 63 | */  | 
            ||
| 64 | protected $exactCopy = false;  | 
            ||
| 65 | /**  | 
            ||
| 66 | * If EasySwift should get rid of the message and recipients once it's done sending  | 
            ||
| 67 | * @var boolean  | 
            ||
| 68 | */  | 
            ||
| 69 | protected $autoFlush = true;  | 
            ||
| 70 | /**  | 
            ||
| 71 | * A list of the IDs of all parts added to the message  | 
            ||
| 72 | * @var array  | 
            ||
| 73 | */  | 
            ||
| 74 | protected $partIds = array();  | 
            ||
| 75 | /**  | 
            ||
| 76 | * A list of all the IDs of the attachments add to the message  | 
            ||
| 77 | * @var array  | 
            ||
| 78 | */  | 
            ||
| 79 | protected $attachmentIds = array();  | 
            ||
| 80 | /**  | 
            ||
| 81 | * The last response received from the server  | 
            ||
| 82 | * @var string  | 
            ||
| 83 | */  | 
            ||
| 84 | public $lastResponse = "";  | 
            ||
| 85 | /**  | 
            ||
| 86 | * The 3 digit code in the last response received from the server  | 
            ||
| 87 | * @var int  | 
            ||
| 88 | */  | 
            ||
| 89 | public $responseCode = 0;  | 
            ||
| 90 | /**  | 
            ||
| 91 | * The list of errors handled at runtime  | 
            ||
| 92 | * @var array  | 
            ||
| 93 | */  | 
            ||
| 94 | public $errors = array();  | 
            ||
| 95 | /**  | 
            ||
| 96 | * The last error received  | 
            ||
| 97 | * @var string  | 
            ||
| 98 | */  | 
            ||
| 99 | public $lastError = null;  | 
            ||
| 100 | |||
| 101 | /**  | 
            ||
| 102 | * Constructor  | 
            ||
| 103 | * @param Swift_Connection The connection to use  | 
            ||
| 104 | * @param string The domain name of this server (not the SMTP server)  | 
            ||
| 105 | */  | 
            ||
| 106 | public function __construct(Swift_Connection $connection, $domain=null)  | 
            ||
| 119 | /**  | 
            ||
| 120 | * Set an error message  | 
            ||
| 121 | * @param string Error message  | 
            ||
| 122 | * @param string $msg  | 
            ||
| 123 | */  | 
            ||
| 124 | public function setError($msg)  | 
            ||
| 128 | /**  | 
            ||
| 129 | * Get the full list of errors  | 
            ||
| 130 | * @return array  | 
            ||
| 131 | */  | 
            ||
| 132 | public function getErrors()  | 
            ||
| 136 | /**  | 
            ||
| 137 | * Get the last error that occured  | 
            ||
| 138 | * @return string  | 
            ||
| 139 | */  | 
            ||
| 140 | public function getLastError()  | 
            ||
| 144 | /**  | 
            ||
| 145 | * Clear the current list of errors  | 
            ||
| 146 | */  | 
            ||
| 147 | public function flushErrors()  | 
            ||
| 152 | /**  | 
            ||
| 153 | * Turn automatic flsuhing on or off.  | 
            ||
| 154 | * This in ON by deault. It removes the message and all parts after sending.  | 
            ||
| 155 | * @param boolean  | 
            ||
| 156 | */  | 
            ||
| 157 | public function autoFlush($flush=true)  | 
            ||
| 161 | /**  | 
            ||
| 162 | * Set the maximum size of the log  | 
            ||
| 163 | * @param int  | 
            ||
| 164 | */  | 
            ||
| 165 | public function setMaxLogSize($size)  | 
            ||
| 170 | /**  | 
            ||
| 171 | * Turn logging on or off (saves memory)  | 
            ||
| 172 | * @param boolean  | 
            ||
| 173 | */  | 
            ||
| 174 | public function useLogging($use=true)  | 
            ||
| 180 | /**  | 
            ||
| 181 | * Enable line resizing (on 1000 by default)  | 
            ||
| 182 | * @param int The number of characters allowed on a line  | 
            ||
| 183 | */  | 
            ||
| 184 | public function useAutoLineResizing($size=1000)  | 
            ||
| 188 | /**  | 
            ||
| 189 | * Dump the log contents  | 
            ||
| 190 | * @deprecated  | 
            ||
| 191 | */  | 
            ||
| 192 | public function getTransactions()  | 
            ||
| 196 | /**  | 
            ||
| 197 | * Dump the contents of the log to the browser  | 
            ||
| 198 | * The log contains some < and > characters so you may need to view source  | 
            ||
| 199 | * Note that this method dumps data to the browser, it does NOT return anything.  | 
            ||
| 200 | */  | 
            ||
| 201 | public function dumpLog()  | 
            ||
| 206 | /**  | 
            ||
| 207 | * This method should be called if you do not wish to send messages in batch mode (i.e. if all recipients should see each others' addresses)  | 
            ||
| 208 | * @param boolean If this mode should be used  | 
            ||
| 209 | */  | 
            ||
| 210 | public function useExactCopy($bool=true)  | 
            ||
| 214 | /**  | 
            ||
| 215 | * Reset the current message and start a fresh one  | 
            ||
| 216 | */  | 
            ||
| 217 | public function newMessage($msg=false)  | 
            ||
| 224 | /**  | 
            ||
| 225 | * Clear out all message parts  | 
            ||
| 226 | * @return boolean  | 
            ||
| 227 | */  | 
            ||
| 228 | View Code Duplication | public function flushParts()  | 
            |
| 243 | /**  | 
            ||
| 244 | * Clear out all attachments  | 
            ||
| 245 | * @return boolean  | 
            ||
| 246 | */  | 
            ||
| 247 | View Code Duplication | public function flushAttachments()  | 
            |
| 262 | /**  | 
            ||
| 263 | * Clear out all message headers  | 
            ||
| 264 | * @deprecated  | 
            ||
| 265 | */  | 
            ||
| 266 | public function flushHeaders()  | 
            ||
| 270 | /**  | 
            ||
| 271 | * Reset the current list of recipients and start a new one  | 
            ||
| 272 | */  | 
            ||
| 273 | public function newRecipientList($list=false)  | 
            ||
| 278 | /**  | 
            ||
| 279 | * Check if Swift has failed or not  | 
            ||
| 280 | * This facade stops processing if so  | 
            ||
| 281 | * @return boolean  | 
            ||
| 282 | */  | 
            ||
| 283 | public function hasFailed()  | 
            ||
| 287 | /**  | 
            ||
| 288 | * Check if the current connection is open or not  | 
            ||
| 289 | * @return boolean  | 
            ||
| 290 | */  | 
            ||
| 291 | public function isConnected()  | 
            ||
| 295 | /**  | 
            ||
| 296 | * Connect to the MTA if not already connected  | 
            ||
| 297 | */  | 
            ||
| 298 | View Code Duplication | public function connect()  | 
            |
| 312 | /**  | 
            ||
| 313 | * Perform the SMTP greeting process (don't do this unless you understand why you're doing it)  | 
            ||
| 314 | */  | 
            ||
| 315 | public function handshake()  | 
            ||
| 319 | /**  | 
            ||
| 320 | * Close the connection to the MTA  | 
            ||
| 321 | * @return boolean  | 
            ||
| 322 | */  | 
            ||
| 323 | View Code Duplication | public function close()  | 
            |
| 336 | /**  | 
            ||
| 337 | * Send a command to Swift and get a response  | 
            ||
| 338 | * @param string The command to send (leave of CRLF)  | 
            ||
| 339 | * @return string  | 
            ||
| 340 | */  | 
            ||
| 341 | public function command($command)  | 
            ||
| 353 | /**  | 
            ||
| 354 | * Add a new plugin to respond to events  | 
            ||
| 355 | * @param Swift_Events_Listener The plugin to load  | 
            ||
| 356 | * @param string The ID to identify the plugin by if needed  | 
            ||
| 357 | * @return string The ID of the plugin  | 
            ||
| 358 | */  | 
            ||
| 359 | public function loadPlugin(Swift_Events_Listener $plugin, $name=null)  | 
            ||
| 366 | /**  | 
            ||
| 367 | * Get a reference to the plugin identified by $name  | 
            ||
| 368 | * @param string the ID of the plugin  | 
            ||
| 369 | * @return Swift_Event_Listener|null  | 
            ||
| 370 | */  | 
            ||
| 371 | public function getPlugin($name)  | 
            ||
| 380 | /**  | 
            ||
| 381 | * Remove the plugin identified by $name  | 
            ||
| 382 | * @param string The ID of the plugin  | 
            ||
| 383 | * @return boolean  | 
            ||
| 384 | */  | 
            ||
| 385 | public function removePlugin($name)  | 
            ||
| 394 | /**  | 
            ||
| 395 | * Load in a new authentication mechanism for SMTP  | 
            ||
| 396 | * This needn't be called since Swift will locate any available in Swift/Authenticator/*.php  | 
            ||
| 397 | * @param Swift_Authenticator The authentication mechanism to load  | 
            ||
| 398 | * @throws Exception If the wrong connection is used  | 
            ||
| 399 | */  | 
            ||
| 400 | public function loadAuthenticator(Swift_Authenticator $auth)  | 
            ||
| 408 | /**  | 
            ||
| 409 | * Authenticate with SMTP authentication  | 
            ||
| 410 | * @param string The SMTP username  | 
            ||
| 411 | * @param string The SMTP password  | 
            ||
| 412 | * @return boolean  | 
            ||
| 413 | * @throws Exception If the wrong connection is used  | 
            ||
| 414 | */  | 
            ||
| 415 | public function authenticate($username, $password)  | 
            ||
| 429 | /**  | 
            ||
| 430 | * Turn a string representation of an email address into a Swift_Address object  | 
            ||
| 431 | * @paramm string The email address  | 
            ||
| 432 | * @param string $string  | 
            ||
| 433 | * @return Swift_Address  | 
            ||
| 434 | */  | 
            ||
| 435 | public function stringToAddress($string)  | 
            ||
| 457 | /**  | 
            ||
| 458 | * Set the encoding used in the message header  | 
            ||
| 459 | * The encoding can be one of Q (quoted-printable) or B (base64)  | 
            ||
| 460 | * @param string The encoding to use  | 
            ||
| 461 | */  | 
            ||
| 462 | public function setHeaderEncoding($mode="B")  | 
            ||
| 473 | /**  | 
            ||
| 474 | * Set the return path address (where bounces go to)  | 
            ||
| 475 | * @param mixed The address as a string or Swift_Address  | 
            ||
| 476 | */  | 
            ||
| 477 | public function setReturnPath($address)  | 
            ||
| 481 | /**  | 
            ||
| 482 | * Request for a read recipient to be sent to the reply-to address  | 
            ||
| 483 | * @param boolean  | 
            ||
| 484 | */  | 
            ||
| 485 | public function requestReadReceipt($request=true)  | 
            ||
| 489 | /**  | 
            ||
| 490 | * Set the message priority  | 
            ||
| 491 | * This is an integer between 1 (high) and 5 (low)  | 
            ||
| 492 | * @param int The level of priority to use  | 
            ||
| 493 | */  | 
            ||
| 494 | public function setPriority($priority)  | 
            ||
| 498 | /**  | 
            ||
| 499 | * Get the return-path address as a string  | 
            ||
| 500 | * @return string  | 
            ||
| 501 | */  | 
            ||
| 502 | public function getReturnPath()  | 
            ||
| 510 | /**  | 
            ||
| 511 | * Set the reply-to header  | 
            ||
| 512 | * @param mixed The address replies come to. String, or Swift_Address, or an array of either.  | 
            ||
| 513 | */  | 
            ||
| 514 | public function setReplyTo($address)  | 
            ||
| 518 | /**  | 
            ||
| 519 | * Get the reply-to address(es) as an array of strings  | 
            ||
| 520 | * @return array  | 
            ||
| 521 | */  | 
            ||
| 522 | public function getReplyTo()  | 
            ||
| 530 | /**  | 
            ||
| 531 | * Add To: recipients to the email  | 
            ||
| 532 | * @param mixed To address(es)  | 
            ||
| 533 | * @return boolean  | 
            ||
| 534 | */  | 
            ||
| 535 | public function addTo($address)  | 
            ||
| 539 | /**  | 
            ||
| 540 | * Get an array of To addresses  | 
            ||
| 541 | * This currently returns an array of Swift_Address objects and may be simplified to an array of strings in later versions  | 
            ||
| 542 | * @return array  | 
            ||
| 543 | */  | 
            ||
| 544 | public function getToAddresses()  | 
            ||
| 548 | /**  | 
            ||
| 549 | * Clear out all To: recipients  | 
            ||
| 550 | */  | 
            ||
| 551 | public function flushTo()  | 
            ||
| 555 | /**  | 
            ||
| 556 | * Add Cc: recipients to the email  | 
            ||
| 557 | * @param mixed Cc address(es)  | 
            ||
| 558 | * @return boolean  | 
            ||
| 559 | */  | 
            ||
| 560 | public function addCc($address)  | 
            ||
| 564 | /**  | 
            ||
| 565 | * Get an array of Cc addresses  | 
            ||
| 566 | * This currently returns an array of Swift_Address objects and may be simplified to an array of strings in later versions  | 
            ||
| 567 | * @return array  | 
            ||
| 568 | */  | 
            ||
| 569 | public function getCcAddresses()  | 
            ||
| 573 | /**  | 
            ||
| 574 | * Clear out all Cc: recipients  | 
            ||
| 575 | */  | 
            ||
| 576 | public function flushCc()  | 
            ||
| 580 | /**  | 
            ||
| 581 | * Add Bcc: recipients to the email  | 
            ||
| 582 | * @param mixed Bcc address(es)  | 
            ||
| 583 | * @return boolean  | 
            ||
| 584 | */  | 
            ||
| 585 | public function addBcc($address)  | 
            ||
| 589 | /**  | 
            ||
| 590 | * Get an array of Bcc addresses  | 
            ||
| 591 | * This currently returns an array of Swift_Address objects and may be simplified to an array of strings in later versions  | 
            ||
| 592 | * @return array  | 
            ||
| 593 | */  | 
            ||
| 594 | public function getBccAddresses()  | 
            ||
| 598 | /**  | 
            ||
| 599 | * Clear out all Bcc: recipients  | 
            ||
| 600 | */  | 
            ||
| 601 | public function flushBcc()  | 
            ||
| 605 | /**  | 
            ||
| 606 | * Add recipients to the email  | 
            ||
| 607 | * @param mixed Address(es)  | 
            ||
| 608 | * @param string Recipient type (To, Cc, Bcc)  | 
            ||
| 609 | * @param string $type  | 
            ||
| 610 | * @return boolean  | 
            ||
| 611 | */  | 
            ||
| 612 | protected function addRecipients($address, $type)  | 
            ||
| 650 | /**  | 
            ||
| 651 | * Flush message, recipients and headers  | 
            ||
| 652 | */  | 
            ||
| 653 | public function flush()  | 
            ||
| 658 | /**  | 
            ||
| 659 | * Get a list of any addresses which have failed since instantiation  | 
            ||
| 660 | * @return array  | 
            ||
| 661 | */  | 
            ||
| 662 | public function getFailedRecipients()  | 
            ||
| 667 | /**  | 
            ||
| 668 | * Set the multipart MIME warning message (only seen by old clients)  | 
            ||
| 669 | * @param string The message to show  | 
            ||
| 670 | */  | 
            ||
| 671 | public function setMimeWarning($text)  | 
            ||
| 675 | /**  | 
            ||
| 676 | * Get the currently set MIME warning (seen by old clients)  | 
            ||
| 677 | * @return string  | 
            ||
| 678 | */  | 
            ||
| 679 | public function getMimeWarning()  | 
            ||
| 683 | /**  | 
            ||
| 684 | * Set the charset of the charset to use in the message  | 
            ||
| 685 | * @param string The charset (e.g. utf-8, iso-8859-1 etc)  | 
            ||
| 686 | * @return boolean  | 
            ||
| 687 | */  | 
            ||
| 688 | public function setCharset($charset)  | 
            ||
| 698 | /**  | 
            ||
| 699 | * Get the charset of the charset to use in the message  | 
            ||
| 700 | * @return string  | 
            ||
| 701 | */  | 
            ||
| 702 | public function getCharset()  | 
            ||
| 706 | /**  | 
            ||
| 707 | * Add a new MIME part to the message  | 
            ||
| 708 | * @param mixed The part to add. If this is a string it's used as the body. If it's an instance of Swift_Message_Part it's used as the entire part  | 
            ||
| 709 | * @param string Content-type, default text/plain  | 
            ||
| 710 | * @param string The encoding used (default is to let Swift decide)  | 
            ||
| 711 | * @param string The charset to use (default is to let swift decide)  | 
            ||
| 712 | */  | 
            ||
| 713 | public function addPart($body, $type="text/plain", $encoding=null, $charset=null)  | 
            ||
| 734 | /**  | 
            ||
| 735 | * Add a new attachment to the message  | 
            ||
| 736 | * @param mixed The attachment to add. If this is a string it's used as the file contents. If it's an instance of Swift_Message_Attachment it's used as the entire part. If it's an instance of Swift_File it's used as the contents.  | 
            ||
| 737 | * @param string Filename, optional  | 
            ||
| 738 | * @param string Content-type. Default application/octet-stream  | 
            ||
| 739 | * @param string The encoding used (default is base64)  | 
            ||
| 740 | * @return boolean  | 
            ||
| 741 | */  | 
            ||
| 742 | public function addAttachment($data, $filename=null, $type="application/octet-stream", $encoding=null)  | 
            ||
| 767 | /**  | 
            ||
| 768 | * Embed an image into the message and get the src attribute for HTML  | 
            ||
| 769 | * Returns FALSE on failure  | 
            ||
| 770 | * @param mixed The path to the image, a Swift_Message_Image object or a Swift_File object  | 
            ||
| 771 | * @return string  | 
            ||
| 772 | */  | 
            ||
| 773 | public function addImage($input)  | 
            ||
| 811 | /**  | 
            ||
| 812 | * Embed an inline file into the message, such as a Image or MIDI file  | 
            ||
| 813 | * @param mixed The file contents, Swift_File object or Swift_Message_EmbeddedFile object  | 
            ||
| 814 | * @param string The content-type of the file, optional  | 
            ||
| 815 | * @param string The filename to use, optional  | 
            ||
| 816 | * @param string the Content-ID to use, optional  | 
            ||
| 817 | * @return string  | 
            ||
| 818 | */  | 
            ||
| 819 | public function embedFile($data, $type="application/octet-stream", $filename=null, $cid=null)  | 
            ||
| 857 | /**  | 
            ||
| 858 | * Add headers to the message  | 
            ||
| 859 | * @param string The message headers to append, separated by CRLF  | 
            ||
| 860 | * @deprecated  | 
            ||
| 861 | */  | 
            ||
| 862 | public function addHeaders($string)  | 
            ||
| 894 | /**  | 
            ||
| 895 | * Set a header in the message  | 
            ||
| 896 | * @param string The name of the header  | 
            ||
| 897 | * @param string The value of the header (without attributes)  | 
            ||
| 898 |    * @see {addHeaderAttribute} | 
            ||
| 899 | */  | 
            ||
| 900 | public function setHeader($name, $value)  | 
            ||
| 904 | /**  | 
            ||
| 905 | * Set an attribute in the message headers  | 
            ||
| 906 |    * For example charset in Content-Type: text/html; charset=utf-8 set by $swift->setHeaderAttribute("Content-Type", "charset", "utf-8") | 
            ||
| 907 | * @param string The name of the header  | 
            ||
| 908 | * @param string The name of the attribute  | 
            ||
| 909 | * @param string The value of the attribute  | 
            ||
| 910 | */  | 
            ||
| 911 | public function setHeaderAttribute($name, $attribute, $value)  | 
            ||
| 916 | /**  | 
            ||
| 917 | * Send an email to a number of recipients  | 
            ||
| 918 | * Returns the number of successful recipients, or FALSE on failure  | 
            ||
| 919 | * @param mixed The recipients to send to. One of string, array, 2-dimensional array or Swift_Address  | 
            ||
| 920 | * @param mixed The address to send from. string or Swift_Address  | 
            ||
| 921 | * @param string The message subject  | 
            ||
| 922 | * @param string The message body, optional  | 
            ||
| 923 | * @return int  | 
            ||
| 924 | */  | 
            ||
| 925 | public function send($recipients, $from, $subject, $body=null)  | 
            ||
| 952 | }  | 
            ||
| 953 | 
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..