| Conditions | 10 |
| Paths | 129 |
| Total Lines | 100 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 39 |
| CRAP Score | 11.3027 |
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 |
||
| 105 | 15 | public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
|
| 106 | { |
||
| 107 | 15 | $failedRecipients = (array)$failedRecipients; |
|
| 108 | |||
| 109 | 15 | $evt = $this->_eventDispatcher->createSendEvent($this, $message); |
|
| 110 | 15 | if ($evt) { |
|
| 111 | |||
| 112 | $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed'); |
||
| 113 | if ($evt->bubbleCancelled()) { |
||
| 114 | return 0; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | $count = ( |
||
| 119 | 15 | count((array)$message->getTo()) |
|
| 120 | 15 | + count((array)$message->getCc()) |
|
| 121 | 15 | + count((array)$message->getBcc()) |
|
| 122 | ); |
||
| 123 | |||
| 124 | /* |
||
| 125 | // TODO: check if we need this check, breaks "Mockery"-Tests |
||
| 126 | if ($count == 0) { |
||
| 127 | $this->_throwException(new Swift_TransportException('Cannot send message without a recipient')); |
||
| 128 | } |
||
| 129 | */ |
||
| 130 | |||
| 131 | 15 | $toHeader = $message->getHeaders()->get('To'); |
|
| 132 | 15 | $subjectHeader = $message->getHeaders()->get('Subject'); |
|
| 133 | |||
| 134 | 15 | if (!$toHeader) { |
|
| 135 | $this->_throwException(new Swift_TransportException('Cannot send message without a recipient')); |
||
| 136 | } |
||
| 137 | |||
| 138 | 15 | $to = $toHeader->getFieldBody(); |
|
| 139 | 15 | $subject = $subjectHeader ? $subjectHeader->getFieldBody() : ''; |
|
| 140 | |||
| 141 | 15 | $reversePath = $this->_getReversePath($message); |
|
| 142 | |||
| 143 | // Remove headers that would otherwise be duplicated |
||
| 144 | 15 | $message->getHeaders()->remove('To'); |
|
| 145 | 15 | $message->getHeaders()->remove('Subject'); |
|
| 146 | |||
| 147 | 15 | $messageStr = $message->toString(); |
|
| 148 | |||
| 149 | 15 | $message->getHeaders()->set($toHeader); |
|
| 150 | 15 | $message->getHeaders()->set($subjectHeader); |
|
| 151 | |||
| 152 | // Separate headers from body |
||
| 153 | 15 | if (false !== $endHeaders = strpos($messageStr, "\r\n\r\n")) { |
|
| 154 | 3 | $headers = substr($messageStr, 0, $endHeaders) . "\r\n"; // Keep last EOL |
|
| 155 | 3 | $body = substr($messageStr, $endHeaders + 4); |
|
| 156 | } else { |
||
| 157 | 12 | $headers = $messageStr . "\r\n"; |
|
| 158 | 12 | $body = ''; |
|
| 159 | } |
||
| 160 | |||
| 161 | 15 | unset($messageStr); |
|
| 162 | |||
| 163 | 15 | if ("\r\n" != PHP_EOL) { |
|
| 164 | // Non-windows (not using SMTP) |
||
| 165 | 15 | $headers = str_replace("\r\n", PHP_EOL, $headers); |
|
| 166 | 15 | $subject = str_replace("\r\n", PHP_EOL, $subject); |
|
| 167 | 15 | $body = str_replace("\r\n", PHP_EOL, $body); |
|
| 168 | } else { |
||
| 169 | // Windows, using SMTP |
||
| 170 | $headers = str_replace("\r\n.", "\r\n..", $headers); |
||
| 171 | $subject = str_replace("\r\n.", "\r\n..", $subject); |
||
| 172 | $body = str_replace("\r\n.", "\r\n..", $body); |
||
| 173 | } |
||
| 174 | |||
| 175 | 15 | if ($this->mail($to, $subject, $body, $headers, $this->_formatExtraParams($this->_extraParams, $reversePath))) { |
|
| 176 | 1 | if ($evt) { |
|
| 177 | $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS); |
||
| 178 | $evt->setFailedRecipients($failedRecipients); |
||
| 179 | 1 | $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed'); |
|
| 180 | } |
||
| 181 | } else { |
||
| 182 | 14 | $failedRecipients = array_merge( |
|
| 183 | $failedRecipients, |
||
| 184 | 14 | array_keys((array)$message->getTo()), |
|
| 185 | 14 | array_keys((array)$message->getCc()), |
|
| 186 | 14 | array_keys((array)$message->getBcc()) |
|
| 187 | ); |
||
| 188 | |||
| 189 | 14 | if ($evt) { |
|
| 190 | $evt->setResult(Swift_Events_SendEvent::RESULT_FAILED); |
||
| 191 | $evt->setFailedRecipients($failedRecipients); |
||
| 192 | $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed'); |
||
| 193 | } |
||
| 194 | |||
| 195 | // TODO: Why do we do this only on error? |
||
| 196 | // -> take a look at "AbstractSmtpTransport" |
||
| 197 | // |
||
| 198 | 14 | $message->generateId(); // Make sure a new Message ID is used |
|
| 199 | |||
| 200 | 14 | $count = 0; |
|
| 201 | } |
||
| 202 | |||
| 203 | 15 | return $count; |
|
| 204 | } |
||
| 205 | |||
| 305 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.