| Conditions | 7 |
| Paths | 50 |
| Total Lines | 60 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 99 | */ |
||
| 100 | public function sendLinkShareMail($recipient, $filename, $link, $expiration) { |
||
| 101 | $subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]); |
||
| 102 | list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration); |
||
| 103 | |||
| 104 | $recipient = str_replace([', ', '; ', ',', ';', ' '], ',', $recipient); |
||
| 105 | $recipients = explode(',', $recipient); |
||
| 106 | try { |
||
| 107 | $message = $this->mailer->createMessage(); |
||
| 108 | $message->setSubject($subject); |
||
| 109 | $message->setTo($recipients); |
||
| 110 | $message->setHtmlBody($htmlBody); |
||
|
|
|||
| 111 | $message->setPlainBody($textBody); |
||
| 112 | $message->setFrom([ |
||
| 113 | Util::getDefaultEmailAddress('sharing-noreply') => |
||
| 114 | (string)$this->l->t('%s via %s', [ |
||
| 115 | $this->senderDisplayName, |
||
| 116 | $this->defaults->getName() |
||
| 117 | ]), |
||
| 118 | ]); |
||
| 119 | if(!is_null($this->replyTo)) { |
||
| 120 | $message->setReplyTo([$this->replyTo]); |
||
| 121 | } |
||
| 122 | |||
| 123 | return $this->mailer->send($message); |
||
| 124 | } catch (\Exception $e) { |
||
| 125 | $this->logger->error("Can't send mail with public link to $recipient: ".$e->getMessage(), ['app' => 'sharing']); |
||
| 126 | return [$recipient]; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * create mail body for plain text and html mail |
||
| 132 | * |
||
| 133 | * @param string $filename the shared file |
||
| 134 | * @param string $link link to the shared file |
||
| 135 | * @param int $expiration expiration date (timestamp) |
||
| 136 | * @param string $prefix prefix of mail template files |
||
| 137 | * @return array an array of the html mail body and the plain text mail body |
||
| 138 | */ |
||
| 139 | private function createMailBody($filename, $link, $expiration, $prefix = '') { |
||
| 140 | $formattedDate = $expiration ? $this->l->l('date', $expiration) : null; |
||
| 141 | |||
| 142 | $html = new \OC_Template('core', $prefix . 'mail', ''); |
||
| 143 | $html->assign ('link', $link); |
||
| 144 | $html->assign ('user_displayname', $this->senderDisplayName); |
||
| 145 | $html->assign ('filename', $filename); |
||
| 146 | $html->assign('expiration', $formattedDate); |
||
| 147 | $htmlMail = $html->fetchPage(); |
||
| 148 | |||
| 149 | $plainText = new \OC_Template('core', $prefix . 'altmail', ''); |
||
| 150 | $plainText->assign ('link', $link); |
||
| 151 | $plainText->assign ('user_displayname', $this->senderDisplayName); |
||
| 152 | $plainText->assign ('filename', $filename); |
||
| 153 | $plainText->assign('expiration', $formattedDate); |
||
| 154 | $plainTextMail = $plainText->fetchPage(); |
||
| 155 | |||
| 156 | return [$htmlMail, $plainTextMail]; |
||
| 157 | } |
||
| 158 | } |
||
| 159 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.