Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class User extends \SerializableObject |
||
22 | { |
||
23 | /** |
||
24 | * An array to cache the title to string mappings so that they don't need to be pulled from the database |
||
25 | * everytime |
||
26 | */ |
||
27 | public static $titlenames = null; |
||
28 | |||
29 | /** |
||
30 | * Is this user in the Group or a child of that group? |
||
31 | * |
||
32 | * @param string $name The name of the group to check if the user is in |
||
33 | * |
||
34 | * @return boolean True if the user is in the group, false otherwise |
||
35 | */ |
||
36 | public function isInGroupNamed($name) |
||
40 | |||
41 | public function __get($propName) |
||
45 | |||
46 | public function __set($propName, $value) |
||
49 | |||
50 | /** |
||
51 | * The email address for the user |
||
52 | * |
||
53 | * @return boolean|string The user's email address |
||
54 | */ |
||
55 | public function getEmail() |
||
59 | |||
60 | /** |
||
61 | * The user ID for the user |
||
62 | * |
||
63 | * @return boolean|string The user's ID or username |
||
64 | */ |
||
65 | public function getUid() |
||
69 | |||
70 | /** |
||
71 | * The photo for the user |
||
72 | * |
||
73 | * @return boolean|string The user's photo as a binary string |
||
74 | */ |
||
75 | public function getPhoto() |
||
79 | |||
80 | /** |
||
81 | * The phone number for the user |
||
82 | * |
||
83 | * @return boolean|string The user's phone number |
||
84 | */ |
||
85 | public function getPhoneNumber() |
||
89 | |||
90 | /** |
||
91 | * The organziation for the user |
||
92 | * |
||
93 | * @return boolean|string The user's organization |
||
94 | */ |
||
95 | public function getOrganization() |
||
99 | |||
100 | /** |
||
101 | * The list of titles for the user |
||
102 | * |
||
103 | * @return boolean|array The user's title(s) in short format |
||
104 | */ |
||
105 | public function getTitles() |
||
109 | |||
110 | /** |
||
111 | * The list of titles for the user |
||
112 | * |
||
113 | * @return boolean|array The user's title(s) in user friendly strings |
||
114 | * |
||
115 | * @SuppressWarnings("StaticAccess") |
||
116 | */ |
||
117 | public function getTitleNames() |
||
147 | |||
148 | /** |
||
149 | * The state the user's mailing address is in |
||
150 | * |
||
151 | * @return boolean|string The user's state from their mailing address |
||
152 | */ |
||
153 | public function getState() |
||
157 | |||
158 | /** |
||
159 | * The city the user's mailing address is in |
||
160 | * |
||
161 | * @return boolean|string The user's city from their mailing address |
||
162 | */ |
||
163 | public function getCity() |
||
167 | |||
168 | /** |
||
169 | * The last name for the user |
||
170 | * |
||
171 | * @return boolean|string The user's last name |
||
172 | */ |
||
173 | public function getLastName() |
||
177 | |||
178 | /** |
||
179 | * The nick name for the user |
||
180 | * |
||
181 | * @return boolean|string The user's nick name |
||
182 | */ |
||
183 | public function getNickName() |
||
187 | |||
188 | /** |
||
189 | * The street address for the user |
||
190 | * |
||
191 | * @return boolean|string The user's street address |
||
192 | */ |
||
193 | public function getAddress() |
||
197 | |||
198 | /** |
||
199 | * The postal (zip) code for the user's mailing address |
||
200 | * |
||
201 | * @return boolean|string The user's postal code |
||
202 | */ |
||
203 | public function getPostalCode() |
||
207 | |||
208 | /** |
||
209 | * The country the user's mailing address is in |
||
210 | * |
||
211 | * @return boolean|string The user's country from their mailing address |
||
212 | */ |
||
213 | public function getCountry() |
||
217 | |||
218 | /** |
||
219 | * The organizational units the user is in |
||
220 | * |
||
221 | * This is the same as Areas in Flipside speak. |
||
222 | * |
||
223 | * @return boolean|array The user's orgnaiational units |
||
224 | */ |
||
225 | public function getOrganizationUnits() |
||
229 | |||
230 | /** |
||
231 | * The supplemental login types that the user can use to login |
||
232 | * |
||
233 | * @return boolean|array The user's login providers |
||
234 | */ |
||
235 | public function getLoginProviders() |
||
239 | |||
240 | /** |
||
241 | * The groups the user is a part of |
||
242 | * |
||
243 | * @return boolean|array The user's Auth\Group structures |
||
244 | */ |
||
245 | public function getGroups() |
||
249 | |||
250 | /** |
||
251 | * Add a supplemental login type that the user can use to login |
||
252 | * |
||
253 | * @param string $provider The hostname for the provider |
||
254 | * |
||
255 | * @return boolean true if the addition worked, false otherwise |
||
256 | */ |
||
257 | public function addLoginProvider($provider) |
||
261 | |||
262 | /** |
||
263 | * Can the user login with this provider? |
||
264 | * |
||
265 | * @param string $provider The hostname for the provider |
||
266 | * |
||
267 | * @return boolean true if they can login with the provider, false otherwise |
||
268 | */ |
||
269 | public function canLoginWith($provider) |
||
286 | |||
287 | /** |
||
288 | * Set the user's password without verifying the current password |
||
289 | * |
||
290 | * @param string $password The new user password |
||
291 | * |
||
292 | * @return boolean true if the user's password was changed, false otherwise |
||
293 | */ |
||
294 | protected function setPass($password) |
||
298 | |||
299 | /** |
||
300 | * Has the user completely filled out their user profile? |
||
301 | * |
||
302 | * @return boolean true if the user's profile is complete, false otherwise |
||
303 | */ |
||
304 | public function isProfileComplete() |
||
314 | |||
315 | /** |
||
316 | * Validate that the user's password is the specified password |
||
317 | * |
||
318 | * @param string $password The user's current password |
||
319 | * |
||
320 | * @return boolean true if the user's password is correct, false otherwise |
||
321 | * |
||
322 | * @SuppressWarnings("UnusedFormalParameter") |
||
323 | */ |
||
324 | public function validate_password($password) |
||
328 | |||
329 | /** |
||
330 | * Validate that the user's reset hash is the sepcified hash |
||
331 | * |
||
332 | * @param string $hash The user's reset hash |
||
333 | * |
||
334 | * @return boolean true if the user's hash is correct, false otherwise |
||
335 | * |
||
336 | * @SuppressWarnings("UnusedFormalParameter") |
||
337 | */ |
||
338 | public function validate_reset_hash($hash) |
||
342 | |||
343 | /** |
||
344 | * Change the user's password, validating the old password or reset hash |
||
345 | * |
||
346 | * @param string $oldpass The user's original password or reset hash if $isHash is true |
||
347 | * @param string $newpass The user's new password |
||
348 | * @param boolean $isHash Is $old_pass a password or a hash |
||
349 | * |
||
350 | * @return boolean true if the user's password was changed, false otherwise |
||
351 | */ |
||
352 | public function change_pass($oldpass, $newpass, $isHash = false) |
||
368 | |||
369 | /** |
||
370 | * Set the user's display name |
||
371 | * |
||
372 | * @param string $name The user's new display name |
||
373 | * |
||
374 | * @return boolean true if the user's display name was changed, false otherwise |
||
375 | */ |
||
376 | public function setDisplayName($name) |
||
380 | |||
381 | /** |
||
382 | * Set the user's given (first) name |
||
383 | * |
||
384 | * @param string $name The user's new given name |
||
385 | * |
||
386 | * @return boolean true if the user's given name was changed, false otherwise |
||
387 | */ |
||
388 | public function setGivenName($name) |
||
392 | |||
393 | /** |
||
394 | * Set the user's email address |
||
395 | * |
||
396 | * @param string $email The user's new email address |
||
397 | * |
||
398 | * @return boolean true if the user's email address was changed, false otherwise |
||
399 | * |
||
400 | * @SuppressWarnings("UnusedFormalParameter") |
||
401 | */ |
||
402 | public function setEmail($email) |
||
406 | |||
407 | /** |
||
408 | * Set the user's user ID or user name |
||
409 | * |
||
410 | * @param string $uid The user's new user ID |
||
411 | * |
||
412 | * @return boolean true if the user's ID was changed, false otherwise |
||
413 | * |
||
414 | * @SuppressWarnings("UnusedFormalParameter") |
||
415 | */ |
||
416 | public function setUid($uid) |
||
420 | |||
421 | /** |
||
422 | * Set the user's photo |
||
423 | * |
||
424 | * @param string $photo The user's new photo as a binary string |
||
425 | * |
||
426 | * @return boolean true if the user's photo was changed, false otherwise |
||
427 | * |
||
428 | * @SuppressWarnings("UnusedFormalParameter") |
||
429 | */ |
||
430 | public function setPhoto($photo) |
||
434 | |||
435 | /** |
||
436 | * Set the user's phone number |
||
437 | * |
||
438 | * @param string $phone The user's new phonew number |
||
439 | * |
||
440 | * @return boolean true if the user's phone number was changed, false otherwise |
||
441 | * |
||
442 | * @SuppressWarnings("UnusedFormalParameter") |
||
443 | */ |
||
444 | public function setPhoneNumber($phone) |
||
448 | |||
449 | /** |
||
450 | * Set the user's organization |
||
451 | * |
||
452 | * @param string $org The user's new organization |
||
453 | * |
||
454 | * @return boolean true if the user's organization was changed, false otherwise |
||
455 | * |
||
456 | * @SuppressWarnings("UnusedFormalParameter") |
||
457 | */ |
||
458 | public function setOrganization($org) |
||
462 | |||
463 | /** |
||
464 | * Set the user's titles |
||
465 | * |
||
466 | * @param string $titles The user's new titles |
||
467 | * |
||
468 | * @return boolean true if the user's titles were changed, false otherwise |
||
469 | * |
||
470 | * @SuppressWarnings("UnusedFormalParameter") |
||
471 | */ |
||
472 | public function setTitles($titles) |
||
476 | |||
477 | /** |
||
478 | * Set the user's state |
||
479 | * |
||
480 | * @param string $state The user's new state |
||
481 | * |
||
482 | * @return boolean true if the user's state was changed, false otherwise |
||
483 | * |
||
484 | * @SuppressWarnings("UnusedFormalParameter") |
||
485 | */ |
||
486 | public function setState($state) |
||
490 | |||
491 | /** |
||
492 | * Set the user's city |
||
493 | * |
||
494 | * @param string $city The user's new city |
||
495 | * |
||
496 | * @return boolean true if the user's city was changed, false otherwise |
||
497 | * |
||
498 | * @SuppressWarnings("UnusedFormalParameter") |
||
499 | */ |
||
500 | public function setCity($city) |
||
504 | |||
505 | /** |
||
506 | * Set the user's last name |
||
507 | * |
||
508 | * @param string $sn The user's new last name |
||
509 | * |
||
510 | * @return boolean true if the user's last name was changed, false otherwise |
||
511 | * |
||
512 | * @SuppressWarnings("UnusedFormalParameter") |
||
513 | */ |
||
514 | public function setLastName($sn) |
||
518 | |||
519 | /** |
||
520 | * Set the user's nick name |
||
521 | * |
||
522 | * @param string $displayName The user's new nick name |
||
523 | * |
||
524 | * @return boolean true if the user's nick name was changed, false otherwise |
||
525 | */ |
||
526 | public function setNickName($displayName) |
||
530 | |||
531 | /** |
||
532 | * Set the user's mailing address |
||
533 | * |
||
534 | * @param string $address The user's new mailing address |
||
535 | * |
||
536 | * @return boolean true if the user's mailing address was changed, false otherwise |
||
537 | * |
||
538 | * @SuppressWarnings("UnusedFormalParameter") |
||
539 | */ |
||
540 | public function setAddress($address) |
||
544 | |||
545 | /** |
||
546 | * Set the user's postal or zip code |
||
547 | * |
||
548 | * @param string $postalcode The user's new postal code |
||
549 | * |
||
550 | * @return boolean true if the user's postal code was changed, false otherwise |
||
551 | * |
||
552 | * @SuppressWarnings("UnusedFormalParameter") |
||
553 | */ |
||
554 | public function setPostalCode($postalcode) |
||
558 | |||
559 | /** |
||
560 | * Set the user's country |
||
561 | * |
||
562 | * @param string $country The user's new country |
||
563 | * |
||
564 | * @return boolean true if the user's country was changed, false otherwise |
||
565 | * |
||
566 | * @SuppressWarnings("UnusedFormalParameter") |
||
567 | */ |
||
568 | public function setCountry($country) |
||
572 | |||
573 | /** |
||
574 | * Set the user's organizations |
||
575 | * |
||
576 | * @param string $ous The user's new organizations |
||
577 | * |
||
578 | * @return boolean true if the user's organizations was changed, false otherwise |
||
579 | * |
||
580 | * @SuppressWarnings("UnusedFormalParameter") |
||
581 | */ |
||
582 | public function setOrganizationUnits($ous) |
||
586 | |||
587 | /** |
||
588 | * Allow write for the user |
||
589 | */ |
||
590 | protected function enableReadWrite() |
||
600 | |||
601 | /** |
||
602 | * Update the user password if required |
||
603 | */ |
||
604 | private function editUserPassword($data) |
||
621 | |||
622 | private function editNames($data) |
||
645 | |||
646 | private function checkForUnsettableElements($data) |
||
665 | |||
666 | private function editAddressElements($data) |
||
694 | |||
695 | private function editOrganizationElements($data) |
||
713 | |||
714 | /** |
||
715 | * Modify the user given the provided data object |
||
716 | * |
||
717 | * @param stdClass $data The user's new data |
||
718 | * |
||
719 | * @return boolean true if the user's data was changed, false otherwise |
||
720 | */ |
||
721 | public function editUser($data) |
||
742 | |||
743 | /** |
||
744 | * Obtain the user's password reset hash |
||
745 | * |
||
746 | * @return string|false A hash if available, false otherwise |
||
747 | */ |
||
748 | public function getPasswordResetHash() |
||
752 | |||
753 | /** |
||
754 | * Serialize the user data into a format usable by the json_encode method |
||
755 | * |
||
756 | * @return array A simple keyed array representing the user |
||
757 | */ |
||
758 | public function jsonSerialize() |
||
782 | |||
783 | /** |
||
784 | * Serialize the user data into a VCARD 2.1 format |
||
785 | * |
||
786 | * @return string The VCARD for the user |
||
787 | */ |
||
788 | public function getVcard() |
||
804 | } |
||
805 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
806 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.