| Conditions | 20 |
| Paths | 1120 |
| Total Lines | 98 |
| Code Lines | 58 |
| 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 |
||
| 170 | public function validate(&$errors) |
||
| 171 | { |
||
| 172 | $errors = array(); |
||
| 173 | $current_author = null; |
||
| 174 | |||
| 175 | if (is_null($this->get('first_name'))) { |
||
| 176 | $errors['first_name'] = __('First name is required'); |
||
| 177 | } |
||
| 178 | |||
| 179 | if (is_null($this->get('last_name'))) { |
||
| 180 | $errors['last_name'] = __('Last name is required'); |
||
| 181 | } |
||
| 182 | |||
| 183 | if ($this->get('id')) { |
||
| 184 | $current_author = Symphony::Database()->fetchRow(0, sprintf( |
||
| 185 | "SELECT `email`, `username` |
||
| 186 | FROM `tbl_authors` |
||
| 187 | WHERE `id` = %d", |
||
| 188 | $this->get('id') |
||
| 189 | )); |
||
| 190 | } |
||
| 191 | |||
| 192 | // Include validators |
||
| 193 | include TOOLKIT . '/util.validators.php'; |
||
| 194 | |||
| 195 | // Check that Email is provided |
||
| 196 | if (is_null($this->get('email'))) { |
||
| 197 | $errors['email'] = __('E-mail address is required'); |
||
| 198 | |||
| 199 | // Check Email is valid |
||
| 200 | } elseif (isset($validators['email']) && !General::validateString($this->get('email'), $validators['email'])) { |
||
| 201 | $errors['email'] = __('E-mail address entered is invalid'); |
||
| 202 | |||
| 203 | // Check Email is valid, fallback when no validator found |
||
| 204 | } elseif (!isset($validators['email']) && !filter_var($email, FILTER_VALIDATE_EMAIL)) { |
||
| 205 | $errors['email'] = __('E-mail address entered is invalid'); |
||
| 206 | |||
| 207 | // Check that if an existing Author changes their email address that |
||
| 208 | // it is not already used by another Author |
||
| 209 | } elseif ($this->get('id')) { |
||
| 210 | if ( |
||
| 211 | $current_author['email'] !== $this->get('email') && |
||
| 212 | Symphony::Database()->fetchVar('count', 0, sprintf( |
||
| 213 | "SELECT COUNT(`id`) as `count` |
||
| 214 | FROM `tbl_authors` |
||
| 215 | WHERE `email` = '%s'", |
||
| 216 | Symphony::Database()->cleanValue($this->get('email')) |
||
| 217 | )) != 0 |
||
| 218 | ) { |
||
| 219 | $errors['email'] = __('E-mail address is already taken'); |
||
| 220 | } |
||
| 221 | |||
| 222 | // Check that Email is not in use by another Author |
||
| 223 | } elseif (Symphony::Database()->fetchVar('id', 0, sprintf( |
||
| 224 | "SELECT `id` |
||
| 225 | FROM `tbl_authors` |
||
| 226 | WHERE `email` = '%s' |
||
| 227 | LIMIT 1", |
||
| 228 | Symphony::Database()->cleanValue($this->get('email')) |
||
| 229 | ))) { |
||
| 230 | $errors['email'] = __('E-mail address is already taken'); |
||
| 231 | } |
||
| 232 | |||
| 233 | // Check the username exists |
||
| 234 | if (is_null($this->get('username'))) { |
||
| 235 | $errors['username'] = __('Username is required'); |
||
| 236 | |||
| 237 | // Check that if it's an existing Author that the username is not already |
||
| 238 | // in use by another Author if they are trying to change it. |
||
| 239 | } elseif ($this->get('id')) { |
||
| 240 | if ( |
||
| 241 | $current_author['username'] !== $this->get('username') && |
||
| 242 | Symphony::Database()->fetchVar('count', 0, sprintf( |
||
| 243 | "SELECT COUNT(`id`) as `count` |
||
| 244 | FROM `tbl_authors` |
||
| 245 | WHERE `username` = '%s'", |
||
| 246 | Symphony::Database()->cleanValue($this->get('username')) |
||
| 247 | )) != 0 |
||
| 248 | ) { |
||
| 249 | $errors['username'] = __('Username is already taken'); |
||
| 250 | } |
||
| 251 | |||
| 252 | // Check that the username is unique |
||
| 253 | } elseif (Symphony::Database()->fetchVar('id', 0, sprintf( |
||
| 254 | "SELECT `id` |
||
| 255 | FROM `tbl_authors` |
||
| 256 | WHERE `username` = '%s' |
||
| 257 | LIMIT 1", |
||
| 258 | Symphony::Database()->cleanValue($this->get('username')) |
||
| 259 | ))) { |
||
| 260 | $errors['username'] = __('Username is already taken'); |
||
| 261 | } |
||
| 262 | |||
| 263 | if (is_null($this->get('password'))) { |
||
| 264 | $errors['password'] = __('Password is required'); |
||
| 265 | } |
||
| 266 | |||
| 267 | return (empty($errors) ? true : false); |
||
| 268 | } |
||
| 304 |