Conditions | 20 |
Paths | 1120 |
Total Lines | 99 |
Code Lines | 47 |
Lines | 30 |
Ratio | 30.3 % |
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 |
||
170 | * user type. |
||
171 | * |
||
172 | * @since 2.4 |
||
173 | * @return boolean |
||
174 | */ |
||
175 | public function isAuthor() |
||
176 | { |
||
177 | return ($this->get('user_type') == 'author'); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Returns boolean if the current Author's authentication token is valid. |
||
182 | * |
||
183 | * @return boolean |
||
184 | */ |
||
185 | public function isTokenActive() |
||
186 | { |
||
187 | return !empty($this->get('auth_token')); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Returns the current Author's auth token. |
||
192 | * |
||
193 | * @return mixed |
||
194 | * The auth token or null |
||
195 | */ |
||
196 | public function getAuthToken() |
||
197 | { |
||
198 | return $this->get('auth_token'); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * A convenience method that returns an Authors full name |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | public function getFullName() |
||
207 | { |
||
208 | return $this->get('first_name') . ' ' . $this->get('last_name'); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Prior to saving an Author object, the validate function ensures that |
||
213 | * the values in `$this->fields` array are correct. As of Symphony 2.3 |
||
214 | * Authors must have unique username AND email address. This function returns |
||
215 | * boolean, with an `$errors` array provided by reference to the callee |
||
216 | * function. |
||
217 | * |
||
218 | * @param array $errors |
||
219 | * @return boolean |
||
220 | */ |
||
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() |
||
359 |