Complex classes like UsersTable 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 UsersTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class UsersTable { |
||
23 | |||
24 | use \Elgg\TimeUsing; |
||
25 | |||
26 | /** |
||
27 | * @var Conf |
||
28 | */ |
||
29 | protected $config; |
||
30 | |||
31 | /** |
||
32 | * @var Database |
||
33 | */ |
||
34 | protected $db; |
||
35 | |||
36 | /** |
||
37 | * @var MetadataTable |
||
38 | */ |
||
39 | protected $metadata; |
||
40 | |||
41 | /** |
||
42 | * @var EntityCache |
||
43 | */ |
||
44 | protected $entity_cache; |
||
45 | |||
46 | /** |
||
47 | * @var EventsService |
||
48 | */ |
||
49 | protected $events; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $table; |
||
55 | |||
56 | /** |
||
57 | * Constructor |
||
58 | * |
||
59 | * @param Conf $config Config |
||
60 | * @param Database $db Database |
||
61 | * @param MetadataTable $metadata Metadata table |
||
62 | * @param EntityCache $cache Entity cache |
||
63 | * @param EventsService $events Event service |
||
64 | */ |
||
65 | 3711 | public function __construct( |
|
74 | |||
75 | /** |
||
76 | * Get user by username |
||
77 | * |
||
78 | * @param string $username The user's username |
||
79 | * |
||
80 | * @return ElggUser|false Depending on success |
||
81 | */ |
||
82 | 286 | public function getByUsername($username) { |
|
111 | |||
112 | /** |
||
113 | * Get an array of users from an email address |
||
114 | * |
||
115 | * @param string $email Email address |
||
116 | * @return array |
||
117 | */ |
||
118 | 63 | public function getByEmail($email) { |
|
136 | |||
137 | /** |
||
138 | * Return users (or the number of them) who have been active within a recent period. |
||
139 | * |
||
140 | * @param array $options Array of options with keys: |
||
141 | * |
||
142 | * seconds (int) => Length of period (default 600 = 10min) |
||
143 | * limit (int) => Limit (default 10) |
||
144 | * offset (int) => Offset (default 0) |
||
145 | * count (bool) => Return a count instead of users? (default false) |
||
146 | * |
||
147 | * @return \ElggUser[]|int |
||
148 | */ |
||
149 | public function findActive(array $options = []) { |
||
187 | |||
188 | /** |
||
189 | * Registers a user, returning false if the username already exists |
||
190 | * |
||
191 | * @param string $username The username of the new user |
||
192 | * @param string $password The password |
||
193 | * @param string $name The user's display name |
||
194 | * @param string $email The user's email address |
||
195 | * @param bool $allow_multiple_emails Allow the same email address to be |
||
196 | * registered multiple times? |
||
197 | * @param string $subtype Subtype of the user entity |
||
198 | * |
||
199 | * @return int|false The new user's GUID; false on failure |
||
200 | * @throws RegistrationException |
||
201 | */ |
||
202 | 63 | public function register($username, $password, $name, $email, $allow_multiple_emails = false, $subtype = null) { |
|
270 | |||
271 | /** |
||
272 | * Generates a unique invite code for a user |
||
273 | * |
||
274 | * @param string $username The username of the user sending the invitation |
||
275 | * |
||
276 | * @return string Invite code |
||
277 | * @see validateInviteCode |
||
278 | */ |
||
279 | public function generateInviteCode($username) { |
||
283 | |||
284 | /** |
||
285 | * Validate a user's invite code |
||
286 | * |
||
287 | * @param string $username The username |
||
288 | * @param string $code The invite code |
||
289 | * |
||
290 | * @return bool |
||
291 | * @see generateInviteCode |
||
292 | */ |
||
293 | public function validateInviteCode($username, $code) { |
||
303 | |||
304 | /** |
||
305 | * Set the validation status for a user. |
||
306 | * |
||
307 | * @param int $user_guid The user's GUID |
||
308 | * @param bool $status Validated (true) or unvalidated (false) |
||
309 | * @param string $method Optional method to say how a user was validated |
||
310 | * @return bool |
||
311 | */ |
||
312 | 63 | public function setValidationStatus($user_guid, $status, $method = '') { |
|
331 | |||
332 | /** |
||
333 | * Gets the validation status of a user. |
||
334 | * |
||
335 | * @param int $user_guid The user's GUID |
||
336 | * @return bool|null Null means status was not set for this user. |
||
337 | */ |
||
338 | public function getValidationStatus($user_guid) { |
||
345 | |||
346 | /** |
||
347 | * Sets the last action time of the given user to right now. |
||
348 | * |
||
349 | * @see _elgg_session_boot The session boot calls this at the beginning of every request |
||
350 | * |
||
351 | * @param ElggUser $user User entity |
||
352 | * @return void |
||
353 | */ |
||
354 | public function setLastAction(ElggUser $user) { |
||
372 | |||
373 | /** |
||
374 | * Sets the last logon time of the given user to right now. |
||
375 | * |
||
376 | * @param ElggUser $user User entity |
||
377 | * @return void |
||
378 | */ |
||
379 | public function setLastLogin(ElggUser $user) { |
||
392 | } |
||
393 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.