Conditions | 12 |
Paths | 2048 |
Total Lines | 47 |
Code Lines | 28 |
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 |
||
56 | public function sendEmailFromPOST() |
||
57 | { |
||
58 | $errors = array(); |
||
59 | |||
60 | // Do not overwrite original data in $_POST |
||
61 | $data = $_POST; |
||
62 | |||
63 | // Make sure every field is set |
||
64 | $data['email'] = (isset($data['email'])) |
||
65 | ? trim((string) $data['email']) : ''; |
||
66 | $data['title'] = (isset($data['title'])) |
||
67 | ? (int) $data['title'] : 0; |
||
68 | $data['name'] = (isset($data['name'])) |
||
69 | ? trim((string) $data['name']) : ''; |
||
70 | $data['phone'] = (isset($data['phone'])) |
||
71 | ? trim((string) $data['phone']) : ''; |
||
72 | $data['callback'] = (isset($data['callback'])) |
||
73 | ? (bool) $data['callback'] : false; |
||
74 | |||
75 | // Validate input |
||
76 | $data['email'] = preg_match('/^[^@\s]+@[^@\s]+\.[^@\s.]+$/', |
||
77 | $data['email']) |
||
78 | ? $data['email'] : ''; |
||
79 | $data['title'] = (isset($this->titles[$data['title']])) |
||
80 | ? $data['title'] : 0; |
||
81 | |||
82 | // Generate error messages for fields that have to be filled |
||
83 | if ($data['email'] === '') { |
||
84 | $errors[] = 'Please provide a valid e-mail address.'; |
||
85 | } |
||
86 | if ($data['title'] === 0) { |
||
87 | $errors[] = 'Please choose a title from the list.'; |
||
88 | } |
||
89 | if ($data['name'] === '') { |
||
90 | $errors[] = 'Please enter your name.'; |
||
91 | } |
||
92 | |||
93 | // In case of error, return |
||
94 | if (!empty($errors)) { |
||
95 | return $errors; |
||
96 | } |
||
97 | |||
98 | // Otherwise, we're clear to mail the data |
||
99 | $errors = $this->doSendEmail($data); |
||
100 | |||
101 | return $errors; |
||
102 | } |
||
103 | |||
167 | } |