| Conditions | 19 |
| Paths | 64 |
| Total Lines | 67 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 72 | protected function mapEmail(Email $email, PHPMailer $mailer) |
||
| 73 | { |
||
| 74 | // Addresses |
||
| 75 | $address = $email->getFromAddress(); |
||
| 76 | $mailer->setFrom($address->getEmail(), (string) $address->getDisplayName()); |
||
| 77 | |||
| 78 | $list = $email->getToAddresses(); |
||
| 79 | foreach ($list->getEachAddress() as $address) { |
||
| 80 | $mailer->addAddress($address->getEmail(), (string) $address->getDisplayName()); |
||
| 81 | } |
||
| 82 | |||
| 83 | $list = $email->getReplyToAddresses(); |
||
| 84 | if (null !== $list) { |
||
| 85 | foreach ($list->getEachAddress() as $address) { |
||
| 86 | $mailer->addReplyTo($address->getEmail(), (string)$address->getDisplayName()); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | $list = $email->getCcAddresses(); |
||
| 91 | if (null !== $list) { |
||
| 92 | foreach ($list->getEachAddress() as $address) { |
||
| 93 | $mailer->addCC($address->getEmail(), (string)$address->getDisplayName()); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | $list = $email->getBccAddresses(); |
||
| 98 | if (null !== $list) { |
||
| 99 | foreach ($list->getEachAddress() as $address) { |
||
| 100 | $mailer->addBCC($address->getEmail(), (string)$address->getDisplayName()); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | // Attachments |
||
| 105 | $attachmentSet = $email->getAttachments(); |
||
| 106 | if (null !== $attachmentSet) { |
||
| 107 | foreach ($attachmentSet->getEachAttachment() as $attachment) { |
||
| 108 | $file = $attachment->getFilename(); |
||
| 109 | $body = $attachment->getStringBody(); |
||
| 110 | $name = (string) $attachment->getName(); |
||
| 111 | $type = (string) $attachment->getMimeType(); |
||
| 112 | $inline = $attachment->getInlineAttribute(); |
||
| 113 | if (null !== $file && !$inline) { |
||
| 114 | $mailer->addAttachment($file, $name, 'base64', $type); |
||
| 115 | } elseif (null === $file && !$inline) { |
||
| 116 | $mailer->addStringAttachment($body, $name, 'base64', $type); |
||
| 117 | } elseif (null !== $file && $inline) { |
||
| 118 | $mailer->addEmbeddedImage($file, $name, $name, 'base64', $type); |
||
| 119 | } elseif (null === $file && $inline) { |
||
| 120 | $mailer->addStringEmbeddedImage($body, $name, $name, 'base64', $type); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | $mailer->CharSet = 'UTF-8'; |
||
| 126 | $mailer->Subject = $email->getSubject(); |
||
| 127 | |||
| 128 | if (null !== $email->getHtmlBody()) { |
||
| 129 | $mailer->Body = $email->getHtmlBody(); |
||
| 130 | $mailer->AltBody = $email->getBody(); |
||
| 131 | $mailer->isHTML(true); |
||
| 132 | return $mailer; |
||
| 133 | } |
||
| 134 | |||
| 135 | $mailer->isHTML(false); |
||
| 136 | $mailer->Body= $email->getBody(); |
||
| 137 | |||
| 138 | return $mailer; |
||
| 139 | } |
||
| 177 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: