Conditions | 3 |
Paths | 3 |
Total Lines | 61 |
Code Lines | 39 |
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 |
||
32 | public function index() |
||
33 | { |
||
34 | $contact = [ |
||
35 | 'schema' => [ |
||
36 | 'name' => [ |
||
37 | 'type' => 'string', |
||
38 | 'length' => 100 |
||
39 | ], |
||
40 | 'email' => [ |
||
41 | 'type' => 'email', |
||
42 | 'length' => 100 |
||
43 | ], |
||
44 | 'subject' => [ |
||
45 | 'type' => 'string', |
||
46 | 'length' => 255 |
||
47 | ], |
||
48 | 'message' => [ |
||
49 | 'type' => 'string' |
||
50 | ] |
||
51 | ], |
||
52 | 'required' => [ |
||
53 | 'name' => 1, |
||
54 | 'email' => 1, |
||
55 | 'message' => 1 |
||
56 | ] |
||
57 | ]; |
||
58 | |||
59 | if ($this->request->is('post')) { |
||
60 | $validator = new Validator(); |
||
61 | $validator |
||
62 | ->notEmpty('email', __('You need to put your E-mail.')) |
||
63 | ->add('email', 'validFormat', [ |
||
64 | 'rule' => 'email', |
||
65 | 'message' => __("You must specify a valid E-mail address.") |
||
66 | ]) |
||
67 | ->notEmpty('name', __('You need to put your name.')) |
||
68 | ->notEmpty('message', __("You need to give a message.")) |
||
69 | ->add('message', 'minLength', [ |
||
70 | 'rule' => ['minLength', 10], |
||
71 | 'message' => __("Your message can not contain less than {0} characters.", 10) |
||
72 | ]); |
||
73 | |||
74 | $contact['errors'] = $validator->errors($this->request->data()); |
||
75 | |||
76 | if (empty($contact['errors'])) { |
||
77 | $viewVars = [ |
||
78 | 'ip' => $this->request->clientIp() |
||
79 | ]; |
||
80 | |||
81 | $viewVars = array_merge($this->request->data(), $viewVars); |
||
82 | |||
83 | $this->getMailer('Contact')->send('contact', [$viewVars]); |
||
84 | |||
85 | $this->Flash->success(__("Your message has been sent successfully, you will get a response shortly !")); |
||
86 | |||
87 | return $this->redirect(['controller' => 'pages', 'action' => 'home']); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | $this->set(compact('contact')); |
||
92 | } |
||
93 | } |
||
94 |