Conditions | 20 |
Paths | 1120 |
Total Lines | 102 |
Code Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
221 | public function validate(&$errors) |
||
222 | { |
||
223 | $errors = array(); |
||
224 | $current_author = null; |
||
225 | |||
226 | if (is_null($this->get('first_name'))) { |
||
227 | $errors['first_name'] = __('First name is required'); |
||
228 | } |
||
229 | |||
230 | if (is_null($this->get('last_name'))) { |
||
231 | $errors['last_name'] = __('Last name is required'); |
||
232 | } |
||
233 | |||
234 | if ($this->get('id')) { |
||
235 | $current_author = Symphony::Database() |
||
236 | ->select(['email', 'username']) |
||
237 | ->from('tbl_authors') |
||
238 | ->where(['id' => $this->get('id')]) |
||
239 | ->execute() |
||
240 | ->next(); |
||
241 | } |
||
242 | |||
243 | // Include validators |
||
244 | include TOOLKIT . '/util.validators.php'; |
||
245 | |||
246 | // Check that Email is provided |
||
247 | if (is_null($this->get('email'))) { |
||
248 | $errors['email'] = __('E-mail address is required'); |
||
249 | |||
250 | // Check Email is valid |
||
251 | } elseif (isset($validators['email']) && !General::validateString($this->get('email'), $validators['email'])) { |
||
252 | $errors['email'] = __('E-mail address entered is invalid'); |
||
253 | |||
254 | // Check Email is valid, fallback when no validator found |
||
255 | } elseif (!isset($validators['email']) && !filter_var($email, FILTER_VALIDATE_EMAIL)) { |
||
256 | $errors['email'] = __('E-mail address entered is invalid'); |
||
257 | |||
258 | // Check that if an existing Author changes their email address that |
||
259 | // it is not already used by another Author |
||
260 | } elseif ($this->get('id')) { |
||
261 | if ( |
||
262 | $current_author['email'] !== $this->get('email') && |
||
263 | (int)Symphony::Database() |
||
264 | ->select() |
||
265 | ->count() |
||
266 | ->from('tbl_authors') |
||
267 | ->where(['email' => $this->get('email')]) |
||
268 | ->limit(1) |
||
269 | ->execute() |
||
270 | ->variable(0) !== 0 |
||
271 | ) { |
||
272 | $errors['email'] = __('E-mail address is already taken'); |
||
273 | } |
||
274 | |||
275 | // Check that Email is not in use by another Author |
||
276 | } elseif (Symphony::Database() |
||
277 | ->select(['id']) |
||
278 | ->from('tbl_authors') |
||
279 | ->where(['email' => $this->get('email')]) |
||
280 | ->limit(1) |
||
281 | ->execute() |
||
282 | ->variable('id')) { |
||
283 | $errors['email'] = __('E-mail address is already taken'); |
||
284 | } |
||
285 | |||
286 | // Check the username exists |
||
287 | if (is_null($this->get('username'))) { |
||
288 | $errors['username'] = __('Username is required'); |
||
289 | |||
290 | // Check that if it's an existing Author that the username is not already |
||
291 | // in use by another Author if they are trying to change it. |
||
292 | } elseif ($this->get('id')) { |
||
293 | if ( |
||
294 | $current_author['username'] !== $this->get('username') && |
||
295 | (int)Symphony::Database() |
||
296 | ->select() |
||
297 | ->count() |
||
298 | ->from('tbl_authors') |
||
299 | ->where(['username' => $this->get('username')]) |
||
300 | ->limit(1) |
||
301 | ->execute() |
||
302 | ->variable(0) !== 0 |
||
303 | ) { |
||
304 | $errors['username'] = __('Username is already taken'); |
||
305 | } |
||
306 | |||
307 | // Check that the username is unique |
||
308 | } elseif (Symphony::Database() |
||
309 | ->select(['id']) |
||
310 | ->from('tbl_authors') |
||
311 | ->where(['username' => $this->get('username')]) |
||
312 | ->limit(1) |
||
313 | ->execute() |
||
314 | ->variable('id')) { |
||
315 | $errors['username'] = __('Username is already taken'); |
||
316 | } |
||
317 | |||
318 | if (is_null($this->get('password'))) { |
||
319 | $errors['password'] = __('Password is required'); |
||
320 | } |
||
321 | |||
322 | return (empty($errors) ? true : false); |
||
323 | } |
||
359 |