Conditions | 23 |
Paths | 2898 |
Total Lines | 105 |
Lines | 55 |
Ratio | 52.38 % |
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 |
||
98 | function change_user_email() { |
||
99 | $email = get_input('email'); |
||
100 | $user_guid = get_input('guid'); |
||
101 | |||
102 | if ($user_guid) { |
||
103 | $user = get_user($user_guid); |
||
104 | } else { |
||
105 | $user = elgg_get_logged_in_user_entity(); |
||
106 | } |
||
107 | |||
108 | if (!is_email_address($email)) { |
||
109 | register_error(elgg_echo('email:save:fail')); |
||
110 | return false; |
||
111 | } |
||
112 | |||
113 | elgg_load_library('c_ext_lib'); |
||
114 | $isValid = false; |
||
115 | |||
116 | View Code Duplication | if ($email) { |
|
117 | // cyu - check if the email is in the list of exceptions |
||
118 | $user_email = explode('@',$email); |
||
119 | $list_of_domains = getExtension(); |
||
120 | |||
121 | if( elgg_is_active_plugin('c_email_extensions') ){ |
||
122 | // Checks against the domain manager list... |
||
123 | $wildcard_query = "SELECT ext FROM email_extensions WHERE ext LIKE '%*%'"; |
||
124 | $wildcard_emails = get_data($wildcard_query); |
||
125 | |||
126 | if( $wildcard_emails ){ |
||
|
|||
127 | foreach($wildcard_emails as $wildcard){ |
||
128 | $regex = str_replace(".", "\.", $wildcard->ext); |
||
129 | $regex = str_replace("*", "[\w-.]+", $regex); |
||
130 | $regex = "/^@" . $regex . "$/"; |
||
131 | if(preg_match($regex, "@".$user_email[1]) || strtolower(str_replace("*.", "", $wildcard->ext)) == strtolower($user_email[1])){ |
||
132 | $isValid = true; |
||
133 | break; |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | if( elgg_is_active_plugin('gcRegistration_invitation') ){ |
||
140 | // Checks against the email invitation list... |
||
141 | $invitation_query = "SELECT email FROM email_invitations WHERE email = '{$email}'"; |
||
142 | $result = get_data($invitation_query); |
||
143 | |||
144 | if( count($result) > 0 ) |
||
145 | $isValid = true; |
||
146 | } |
||
147 | |||
148 | if (count($list_of_domains) > 0) { |
||
149 | while ($row = mysqli_fetch_array($list_of_domains)) { |
||
150 | if (strtolower($row['ext']) === strtolower($user_email[1])) { |
||
151 | $isValid = true; |
||
152 | break; |
||
153 | } |
||
154 | } |
||
155 | $error_message = elgg_echo('gcc_profile:error').elgg_echo('gcc_profile:notaccepted'); |
||
156 | } |
||
157 | |||
158 | // cyu - check if domain is gc.ca |
||
159 | if (!$isValid) { |
||
160 | $govt_domain = explode('.',$user_email[1]); |
||
161 | $govt_domain_len = count($govt_domain) - 1; |
||
162 | |||
163 | if ($govt_domain[$govt_domain_len - 1].'.'.$govt_domain[$govt_domain_len] === 'gc.ca') { |
||
164 | $isValid = true; |
||
165 | } else { |
||
166 | $isValid = false; |
||
167 | $error_message = elgg_echo('gcc_profile:error').elgg_echo('gcc_profile:notaccepted'); |
||
168 | } |
||
169 | } |
||
170 | } |
||
171 | |||
172 | if( !$isValid ){ |
||
173 | if( elgg_is_active_plugin('b_extended_profile_collab') ){ |
||
174 | prepare_email_change($user_guid, $email); |
||
175 | } else { |
||
176 | register_error($error_message); |
||
177 | } |
||
178 | } else { |
||
179 | if ($user) { |
||
180 | if (strcmp($email, $user->email) != 0) { |
||
181 | if (!get_user_by_email($email)) { |
||
182 | if ($user->email != $email) { |
||
183 | |||
184 | $user->email = $email; |
||
185 | if ($user->save()) { |
||
186 | system_message(elgg_echo('email:save:success')); |
||
187 | return true; |
||
188 | } else { |
||
189 | register_error(elgg_echo('email:save:fail')); |
||
190 | } |
||
191 | } |
||
192 | } else { |
||
193 | register_error(elgg_echo('registration:dupeemail')); |
||
194 | } |
||
195 | } |
||
196 | } else { |
||
197 | register_error(elgg_echo('email:save:fail')); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | return false; |
||
202 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.