Conditions | 16 |
Paths | 2 |
Total Lines | 119 |
Code Lines | 65 |
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 |
||
79 | public function init() |
||
80 | { |
||
81 | parent::init(); |
||
82 | self::$plugin = $this; |
||
83 | |||
84 | if (!Craft::$app->plugins->isPluginInstalled('contact-form') && !Craft::$app->request->getIsConsoleRequest()) { |
||
85 | Craft::$app->session->setNotice(Craft::t('contact-form-extensions', 'The Contact Form plugin is not installed or activated, Contact Form Extensions does not work without it.')); |
||
|
|||
86 | } |
||
87 | |||
88 | Event::on(View::class, View::EVENT_BEFORE_RENDER_TEMPLATE, function (TemplateEvent $e) { |
||
89 | if ( |
||
90 | $e->template === 'settings/plugins/_settings' && |
||
91 | $e->variables['plugin'] === $this |
||
92 | ) { |
||
93 | // Add the tabs |
||
94 | $e->variables['tabs'] = [ |
||
95 | ['label' => 'Settings', 'url' => '#settings-tab-settings'], |
||
96 | ['label' => 'reCAPTCHA', 'url' => '#settings-tab-recaptcha'], |
||
97 | ]; |
||
98 | } |
||
99 | }); |
||
100 | |||
101 | Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function (RegisterUrlRulesEvent $event) { |
||
102 | $event->rules = array_merge($event->rules, [ |
||
103 | 'contact-form-extensions/submissions/<submissionId:\d+>' => 'contact-form-extensions/submissions/show-submission', |
||
104 | 'contact-form-extensions/submissions/<submissionId:\d+>/<siteHandle:{handle}>' => 'contact-form-extensions/submissions/show-submission', |
||
105 | ]); |
||
106 | }); |
||
107 | |||
108 | Event::on(Mailer::class, Mailer::EVENT_BEFORE_SEND, function (SendEvent $e) { |
||
109 | if ($e->isSpam) { |
||
110 | return; |
||
111 | } |
||
112 | |||
113 | if ($this->settings->recaptcha) { |
||
114 | $recaptcha = $this->contactFormExtensionsService->getRecaptcha(); |
||
115 | $captchaResponse = Craft::$app->request->getParam('g-recaptcha-response'); |
||
116 | |||
117 | if (!$recaptcha->verifyResponse($captchaResponse, $_SERVER['REMOTE_ADDR'])) { |
||
118 | $e->isSpam = true; |
||
119 | $e->handled = true; |
||
120 | |||
121 | return; |
||
122 | } |
||
123 | } |
||
124 | |||
125 | $submission = $e->submission; |
||
126 | if ($this->settings->enableDatabase) { |
||
127 | $this->contactFormExtensionsService->saveSubmission($submission); |
||
128 | } |
||
129 | |||
130 | // Set the overridden "toEmail" setting |
||
131 | if (is_array($e->submission->message) && array_key_exists('toEmail', $e->submission->message)) { |
||
132 | $email = Craft::$app->security->validateData($e->submission->message['toEmail']); |
||
133 | $e->toEmails = explode(',', $email); |
||
134 | } |
||
135 | |||
136 | if ($this->settings->enableTemplateOverwrite) { |
||
137 | // First set the template mode to the Site templates |
||
138 | Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE); |
||
139 | |||
140 | // Render the set template |
||
141 | $html = Craft::$app->view->renderTemplate( |
||
142 | $this->settings->notificationTemplate, |
||
143 | ['submission' => $e->submission] |
||
144 | ); |
||
145 | |||
146 | // Update the message body |
||
147 | $e->message->setHtmlBody($html); |
||
148 | |||
149 | // Set the template mode back to Control Panel |
||
150 | Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP); |
||
151 | } |
||
152 | }); |
||
153 | |||
154 | Event::on(Mailer::class, Mailer::EVENT_AFTER_SEND, function (SendEvent $e) { |
||
155 | if ($this->settings->enableConfirmationEmail) { |
||
156 | // First set the template mode to the Site templates |
||
157 | Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE); |
||
158 | |||
159 | // Check if template is overridden in form |
||
160 | $template = null; |
||
161 | if (is_array($e->submission->message) && array_key_exists('template', $e->submission->message)) { |
||
162 | $template = '_emails\\'.Craft::$app->security->validateData($e->submission->message['template']); |
||
163 | } else { |
||
164 | // Render the set template |
||
165 | $template = $this->settings->confirmationTemplate; |
||
166 | } |
||
167 | $html = Craft::$app->view->renderTemplate( |
||
168 | $template, |
||
169 | ['submission' => $e->submission] |
||
170 | ); |
||
171 | |||
172 | // Create the confirmation email |
||
173 | $message = new Message(); |
||
174 | $message->setTo($e->submission->fromEmail); |
||
175 | if (isset(App::mailSettings()->fromEmail)) { |
||
176 | $message->setFrom(App::mailSettings()->fromEmail); |
||
177 | } else { |
||
178 | $message->setFrom($e->message->getTo()); |
||
179 | } |
||
180 | $message->setHtmlBody($html); |
||
181 | $message->setSubject($this->settings->getConfirmationSubject()); |
||
182 | |||
183 | // Send the mail |
||
184 | Craft::$app->mailer->send($message); |
||
185 | |||
186 | // Set the template mode back to Control Panel |
||
187 | Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP); |
||
188 | } |
||
189 | }); |
||
190 | |||
191 | Event::on( |
||
192 | CraftVariable::class, |
||
193 | CraftVariable::EVENT_INIT, |
||
194 | function (Event $event) { |
||
195 | /** @var CraftVariable $variable */ |
||
196 | $variable = $event->sender; |
||
197 | $variable->set('contactFormExtensions', ContactFormExtensionsVariable::class); |
||
198 | } |
||
258 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.