Complex classes like ElggUser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ElggUser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ElggUser extends \ElggEntity |
||
|
|||
24 | implements Friendable { |
||
1 ignored issue
–
show
|
|||
25 | |||
26 | /** |
||
27 | * Initialize the attributes array. |
||
28 | * This is vital to distinguish between metadata and base attributes. |
||
29 | * |
||
30 | * @return void |
||
31 | */ |
||
32 | 220 | protected function initializeAttributes() { |
|
38 | |||
39 | /** |
||
40 | * Get default values for attributes stored in a separate table |
||
41 | * |
||
42 | * @return array |
||
43 | * @access private |
||
44 | * |
||
45 | * @see \Elgg\Database\EntityTable::getEntities |
||
46 | */ |
||
47 | 220 | final public static function getExternalAttributes() { |
|
48 | return [ |
||
49 | 220 | 'name' => null, |
|
50 | 220 | 'username' => null, |
|
51 | 220 | 'password_hash' => null, |
|
52 | 220 | 'email' => null, |
|
53 | 220 | 'language' => null, |
|
54 | 220 | 'banned' => "no", |
|
55 | 220 | 'admin' => 'no', |
|
56 | 220 | 'prev_last_action' => null, |
|
57 | 220 | 'last_login' => null, |
|
58 | 220 | 'prev_last_login' => null, |
|
59 | 220 | ]; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Construct a new user entity |
||
64 | * |
||
65 | * Plugin developers should only use the constructor to create a new entity. |
||
66 | * To retrieve entities, use get_entity() and the elgg_get_entities* functions. |
||
67 | * |
||
68 | * @param \stdClass $row Database row result. Default is null to create a new user. |
||
69 | * |
||
70 | * @throws IOException|InvalidParameterException if there was a problem creating the user. |
||
71 | */ |
||
72 | 220 | public function __construct(\stdClass $row = null) { |
|
83 | |||
84 | /** |
||
85 | * Load the \ElggUser data from the database |
||
86 | * |
||
87 | * @param mixed $guid \ElggUser GUID or \stdClass database row from entity table |
||
88 | * |
||
89 | * @return bool |
||
90 | */ |
||
91 | 217 | protected function load($guid) { |
|
106 | |||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | protected function create() { |
||
112 | global $CONFIG; |
||
113 | |||
114 | $guid = parent::create(); |
||
115 | $name = sanitize_string($this->name); |
||
116 | $username = sanitize_string($this->username); |
||
117 | $password_hash = sanitize_string($this->password_hash); |
||
118 | $email = sanitize_string($this->email); |
||
119 | $language = sanitize_string($this->language); |
||
120 | |||
121 | $query = "INSERT into {$CONFIG->dbprefix}users_entity |
||
122 | (guid, name, username, password_hash, email, language) |
||
123 | values ($guid, '$name', '$username', '$password_hash', '$email', '$language')"; |
||
124 | |||
125 | $result = $this->getDatabase()->insertData($query); |
||
126 | if ($result === false) { |
||
127 | // TODO(evan): Throw an exception here? |
||
128 | return false; |
||
129 | } |
||
130 | |||
131 | return $guid; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | protected function update() { |
||
158 | |||
159 | /** |
||
160 | * Get user language or default to site language |
||
161 | * |
||
162 | * @param string $fallback If this is provided, it will be returned if the user doesn't have a language set. |
||
163 | * If null, the site language will be returned. |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getLanguage($fallback = null) { |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | 3 | public function getDisplayName() { |
|
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | public function setDisplayName($displayName) { |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | 219 | public function __set($name, $value) { |
|
226 | |||
227 | /** |
||
228 | * Ban this user. |
||
229 | * |
||
230 | * @param string $reason Optional reason |
||
231 | * |
||
232 | * @return bool |
||
233 | */ |
||
234 | public function ban($reason = "") { |
||
237 | |||
238 | /** |
||
239 | * Unban this user. |
||
240 | * |
||
241 | * @return bool |
||
242 | */ |
||
243 | public function unban() { |
||
246 | |||
247 | /** |
||
248 | * Is this user banned or not? |
||
249 | * |
||
250 | * @return bool |
||
251 | */ |
||
252 | 18 | public function isBanned() { |
|
255 | |||
256 | /** |
||
257 | * Is this user admin? |
||
258 | * |
||
259 | * @return bool |
||
260 | */ |
||
261 | 24 | public function isAdmin() { |
|
269 | |||
270 | /** |
||
271 | * Make the user an admin |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | public function makeAdmin() { |
||
291 | |||
292 | /** |
||
293 | * Remove the admin flag for user |
||
294 | * |
||
295 | * @return bool |
||
296 | */ |
||
297 | public function removeAdmin() { |
||
313 | |||
314 | /** |
||
315 | * Adds a user as a friend |
||
316 | * |
||
317 | * @param int $friend_guid The GUID of the user to add |
||
318 | * @param bool $create_river_item Create the river item announcing this friendship |
||
319 | * |
||
320 | * @return bool |
||
321 | */ |
||
322 | public function addFriend($friend_guid, $create_river_item = false) { |
||
342 | |||
343 | /** |
||
344 | * Removes a user as a friend |
||
345 | * |
||
346 | * @param int $friend_guid The GUID of the user to remove |
||
347 | * @return bool |
||
348 | */ |
||
349 | public function removeFriend($friend_guid) { |
||
352 | |||
353 | /** |
||
354 | * Determines whether or not this user is a friend of the currently logged in user |
||
355 | * |
||
356 | * @return bool |
||
357 | */ |
||
358 | public function isFriend() { |
||
361 | |||
362 | /** |
||
363 | * Determines whether this user is friends with another user |
||
364 | * |
||
365 | * @param int $user_guid The GUID of the user to check against |
||
366 | * |
||
367 | * @return bool |
||
368 | */ |
||
369 | public function isFriendsWith($user_guid) { |
||
372 | |||
373 | /** |
||
374 | * Determines whether or not this user is another user's friend |
||
375 | * |
||
376 | * @param int $user_guid The GUID of the user to check against |
||
377 | * |
||
378 | * @return bool |
||
379 | */ |
||
380 | public function isFriendOf($user_guid) { |
||
383 | |||
384 | /** |
||
385 | * {@inheritdoc} |
||
386 | */ |
||
387 | public function getFriends(array $options = []) { |
||
394 | |||
395 | /** |
||
396 | * {@inheritdoc} |
||
397 | */ |
||
398 | public function getFriendsOf(array $options = []) { |
||
406 | |||
407 | /** |
||
408 | * Gets the user's groups |
||
409 | * |
||
410 | * @param array $options Options array. |
||
411 | * |
||
412 | * @return array|false Array of \ElggGroup, or false, depending on success |
||
413 | */ |
||
414 | public function getGroups(array $options = []) { |
||
421 | |||
422 | /** |
||
423 | * {@inheritdoc} |
||
424 | */ |
||
425 | public function getObjects(array $options = []) { |
||
431 | |||
432 | /** |
||
433 | * {@inheritdoc} |
||
434 | */ |
||
435 | public function getFriendsObjects(array $options = []) { |
||
443 | |||
444 | /** |
||
445 | * Get a user's owner GUID |
||
446 | * |
||
447 | * Returns it's own GUID if the user is not owned. |
||
448 | * |
||
449 | * @return int |
||
450 | */ |
||
451 | public function getOwnerGUID() { |
||
458 | |||
459 | /** |
||
460 | * {@inheritdoc} |
||
461 | */ |
||
462 | protected function prepareObject($object) { |
||
470 | |||
471 | /** |
||
472 | * Can a user comment on this user? |
||
473 | * |
||
474 | * @see \ElggEntity::canComment() |
||
475 | * |
||
476 | * @param int $user_guid User guid (default is logged in user) |
||
477 | * @param bool $default Default permission |
||
478 | * @return bool |
||
479 | * @since 1.8.0 |
||
480 | */ |
||
481 | 1 | public function canComment($user_guid = 0, $default = null) { |
|
488 | |||
489 | /** |
||
490 | * Set the necessary attribute to store a hash of the user's password. |
||
491 | * |
||
492 | * @tip You must save() to persist the attribute |
||
493 | * |
||
494 | * @param string $password The password to be hashed |
||
495 | * @return void |
||
496 | * @since 1.10.0 |
||
497 | */ |
||
498 | public function setPassword($password) { |
||
501 | |||
502 | /** |
||
503 | * Enable or disable a notification delivery method |
||
504 | * |
||
505 | * @param string $method Method name |
||
506 | * @param bool $enabled Enabled or disabled |
||
507 | * @return bool |
||
508 | */ |
||
509 | 1 | public function setNotificationSetting($method, $enabled = true) { |
|
513 | |||
514 | /** |
||
515 | * Returns users's notification settings |
||
516 | * <code> |
||
517 | * [ |
||
518 | * 'email' => true, // enabled |
||
519 | * 'ajax' => false, // disabled |
||
520 | * ] |
||
521 | * </code> |
||
522 | * |
||
523 | * @return array |
||
524 | */ |
||
525 | 19 | public function getNotificationSettings() { |
|
537 | } |
||
538 |