| Conditions | 9 |
| Paths | 30 |
| Total Lines | 70 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 113 | public function sendMail( |
||
| 114 | int|string $reportId, |
||
| 115 | string $content, |
||
| 116 | string $object, |
||
| 117 | string|array $receiverAddress, |
||
| 118 | array $data = [], |
||
| 119 | array $attachments = [], |
||
| 120 | string $senderAddress = '', |
||
| 121 | string $senderName = '' |
||
| 122 | ): bool { |
||
| 123 | // @codeCoverageIgnoreStart |
||
| 124 | if ($this->isEnabled() === false) { |
||
|
|
|||
| 125 | return true; |
||
| 126 | } |
||
| 127 | // @codeCoverageIgnoreEnd |
||
| 128 | |||
| 129 | if (empty($receiverAddress)) { |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | $mainInformations = $this->printHelper->getMainData(); |
||
| 134 | if (empty($senderAddress)) { |
||
| 135 | $senderAddress = $this->getSenderEmail(); |
||
| 136 | } |
||
| 137 | |||
| 138 | if (empty($senderName)) { |
||
| 139 | $senderName = $this->config->get('app.name'); |
||
| 140 | } |
||
| 141 | |||
| 142 | $reportData = $data + $mainInformations; |
||
| 143 | |||
| 144 | $receivers = Arr::wrap($receiverAddress); |
||
| 145 | |||
| 146 | $mailer = new Mailer($this->transport); |
||
| 147 | |||
| 148 | foreach ($receivers as $receiver) { |
||
| 149 | /* |
||
| 150 | * TODO: normally the next lines should not be added in the loop |
||
| 151 | * but in order to customized the message content based on receiver user |
||
| 152 | * we had it here in the loop the in the mail template we can know which user is |
||
| 153 | * the current receiver and adapt the mail content based on the receiver email |
||
| 154 | * address. |
||
| 155 | */ |
||
| 156 | $reportData['receiver_email'] = $receiver; |
||
| 157 | $mailBody = $this->template->renderString($content, $reportData); |
||
| 158 | // If need debug |
||
| 159 | $this->printHelper->debugReport($reportId, $reportData); |
||
| 160 | |||
| 161 | |||
| 162 | $message = new Message(); |
||
| 163 | $message->setFrom($senderAddress, $senderName) |
||
| 164 | ->setTo($receiver) |
||
| 165 | ->setSubject($object) |
||
| 166 | ->setBody($mailBody) |
||
| 167 | ->setHtml(); |
||
| 168 | |||
| 169 | foreach ($attachments as $name => $path) { |
||
| 170 | if (is_string($name)) { |
||
| 171 | $message->addAttachment($path, $name); |
||
| 172 | } else { |
||
| 173 | $message->addAttachment($path); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | if ($mailer->send($message) === false) { |
||
| 178 | return false; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | return true; |
||
| 183 | } |
||
| 203 |