| Conditions | 9 |
| Paths | 28 |
| Total Lines | 93 |
| 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 |
||
| 59 | public function action_contact(): void |
||
| 60 | { |
||
| 61 | global $context, $txt, $modSettings; |
||
| 62 | |||
| 63 | // Users have no need to use this, just send a PM |
||
| 64 | // Disabled, you cannot enter. |
||
| 65 | if ($this->user->is_guest === false || empty($modSettings['enable_contactform']) || $modSettings['enable_contactform'] === 'disabled') |
||
|
|
|||
| 66 | { |
||
| 67 | redirectexit(); |
||
| 68 | } |
||
| 69 | |||
| 70 | Txt::load('Login'); |
||
| 71 | theme()->getTemplates()->load('Register'); |
||
| 72 | |||
| 73 | // Submitted the contact form? |
||
| 74 | if (isset($this->_req->post->send)) |
||
| 75 | { |
||
| 76 | checkSession(); |
||
| 77 | validateToken('contact'); |
||
| 78 | |||
| 79 | // Can't send a lot of these in a row, no sir! |
||
| 80 | spamProtection('contact'); |
||
| 81 | |||
| 82 | // No errors, yet. |
||
| 83 | $context['errors'] = []; |
||
| 84 | Txt::load('Errors'); |
||
| 85 | |||
| 86 | // Could they get the right send topic verification code? |
||
| 87 | require_once(SUBSDIR . '/Members.subs.php'); |
||
| 88 | |||
| 89 | // Form validation |
||
| 90 | $validator = new DataValidator(); |
||
| 91 | $validator->sanitation_rules([ |
||
| 92 | 'emailaddress' => 'trim', |
||
| 93 | 'contactmessage' => 'trim' |
||
| 94 | ]); |
||
| 95 | $validator->validation_rules([ |
||
| 96 | 'emailaddress' => 'required|valid_email', |
||
| 97 | 'contactmessage' => 'required' |
||
| 98 | ]); |
||
| 99 | $validator->text_replacements([ |
||
| 100 | 'emailaddress' => $txt['error_email'], |
||
| 101 | 'contactmessage' => $txt['error_message'] |
||
| 102 | ]); |
||
| 103 | |||
| 104 | // Any form errors |
||
| 105 | if (!$validator->validate($this->_req->post)) |
||
| 106 | { |
||
| 107 | $context['errors'] = $validator->validation_errors(); |
||
| 108 | } |
||
| 109 | |||
| 110 | // Get the clean data |
||
| 111 | $this->_req->post = new ArrayObject($validator->validation_data(), ArrayObject::ARRAY_AS_PROPS); |
||
| 112 | |||
| 113 | // Trigger the verify contact event for captcha checks |
||
| 114 | $this->_events->trigger('verify_contact', []); |
||
| 115 | |||
| 116 | // No errors, then send the PM to the admins |
||
| 117 | if (empty($context['errors'])) |
||
| 118 | { |
||
| 119 | $admins = admins(); |
||
| 120 | if (!empty($admins)) |
||
| 121 | { |
||
| 122 | require_once(SUBSDIR . '/PersonalMessage.subs.php'); |
||
| 123 | sendpm(['to' => array_keys($admins), 'bcc' => []], $txt['contact_subject'], $this->_req->post->contactmessage, false, ['id' => 0, 'name' => $this->_req->post->emailaddress, 'username' => $this->_req->post->emailaddress]); |
||
| 124 | } |
||
| 125 | |||
| 126 | // Send the PM |
||
| 127 | redirectexit('action=about;sa=contact;done'); |
||
| 128 | } |
||
| 129 | else |
||
| 130 | { |
||
| 131 | $context['emailaddress'] = $this->_req->post->emailaddress; |
||
| 132 | $context['contactmessage'] = $this->_req->post->contactmessage; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | // Show the contact done form or the form itself |
||
| 137 | if (isset($this->_req->query->done)) |
||
| 138 | { |
||
| 139 | $context['sub_template'] = 'contact_form_done'; |
||
| 140 | } |
||
| 141 | else |
||
| 142 | { |
||
| 143 | loadJavascriptFile('ext/mailcheck.min.js'); |
||
| 144 | $context['sub_template'] = 'contact_form'; |
||
| 145 | $context['page_title'] = $txt['admin_contact_form']; |
||
| 146 | |||
| 147 | // Setup any contract form events, like validation |
||
| 148 | $this->_events->trigger('setup_contact', []); |
||
| 149 | } |
||
| 150 | |||
| 151 | createToken('contact'); |
||
| 152 | } |
||
| 299 |