| Conditions | 14 |
| Paths | 12 |
| Total Lines | 66 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 2 | Features | 3 |
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 |
||
| 64 | public function getContent(): string { |
||
| 65 | $template = new DwooTemplate("pages/sendmail"); |
||
| 66 | |||
| 67 | $template->assign("form_action", DomainUtils::generateURL("admin/sendmail")); |
||
| 68 | $template->assign("content", ""); |
||
| 69 | |||
| 70 | if (isset($_REQUEST['submit'])) { |
||
| 71 | //first, check csrf token |
||
| 72 | if (!Security::checkCSRFToken()) { |
||
| 73 | $template->assign("error_message", "Wrong CSRF token!"); |
||
| 74 | |||
| 75 | if (isset($_POST['content'])) { |
||
| 76 | $template->assign("content", $_POST['content']); |
||
| 77 | } |
||
| 78 | } else { |
||
| 79 | $required_fields = array("to_mail", "subject", "content"); |
||
| 80 | |||
| 81 | foreach ($required_fields as $field) { |
||
| 82 | if (!isset($_POST[$field]) || empty($_POST[$field])) { |
||
| 83 | $template->assign("error_message", "Please complete form!"); |
||
| 84 | |||
| 85 | if (isset($_POST['content'])) { |
||
| 86 | $template->assign("content", $_POST['content']); |
||
| 87 | } |
||
| 88 | |||
| 89 | return $template->getCode(); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | //form is complete |
||
| 94 | |||
| 95 | //get values |
||
| 96 | $to_mail = $_POST['to_mail']; |
||
| 97 | $subject = $_POST['subject']; |
||
| 98 | $content = $_POST['content']; |
||
| 99 | |||
| 100 | //check, if mail is valide |
||
| 101 | if (!(new Validator_Mail())->isValide($to_mail)) { |
||
| 102 | $template->assign("error_message", "Mail is not valide!"); |
||
| 103 | |||
| 104 | if (isset($_POST['content'])) { |
||
| 105 | $template->assign("content", $_POST['content']); |
||
| 106 | } |
||
| 107 | } else if (!(new Validator_String())->isValide($subject)) { |
||
| 108 | $template->assign("error_message", "Subject is not valide!"); |
||
| 109 | |||
| 110 | if (isset($_POST['content'])) { |
||
| 111 | $template->assign("content", $_POST['content']); |
||
| 112 | } |
||
| 113 | } else { |
||
| 114 | //parameters are valide, send mail |
||
| 115 | |||
| 116 | if (MailApi::sendHTMLMail($to_mail, $subject, $content)) { |
||
| 117 | $template->assign("success_message", "Mail sended successfully!"); |
||
| 118 | } else { |
||
| 119 | $template->assign("error_message", "Sending of mail failed!"); |
||
| 120 | |||
| 121 | if (isset($_POST['content'])) { |
||
| 122 | $template->assign("content", $_POST['content']); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | return $template->getCode(); |
||
| 130 | } |
||
| 139 |
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.