Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WP_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 WP_User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class WP_User { |
||
36 | /** |
||
37 | * User data container. |
||
38 | * |
||
39 | * @since 2.0.0 |
||
40 | * @var object |
||
41 | */ |
||
42 | public $data; |
||
43 | |||
44 | /** |
||
45 | * The user's ID. |
||
46 | * |
||
47 | * @since 2.1.0 |
||
48 | * @access public |
||
49 | * @var int |
||
50 | */ |
||
51 | public $ID = 0; |
||
52 | |||
53 | /** |
||
54 | * The individual capabilities the user has been given. |
||
55 | * |
||
56 | * @since 2.0.0 |
||
57 | * @access public |
||
58 | * @var array |
||
59 | */ |
||
60 | public $caps = array(); |
||
61 | |||
62 | /** |
||
63 | * User metadata option name. |
||
64 | * |
||
65 | * @since 2.0.0 |
||
66 | * @access public |
||
67 | * @var string |
||
68 | */ |
||
69 | public $cap_key; |
||
70 | |||
71 | /** |
||
72 | * The roles the user is part of. |
||
73 | * |
||
74 | * @since 2.0.0 |
||
75 | * @access public |
||
76 | * @var array |
||
77 | */ |
||
78 | public $roles = array(); |
||
79 | |||
80 | /** |
||
81 | * All capabilities the user has, including individual and role based. |
||
82 | * |
||
83 | * @since 2.0.0 |
||
84 | * @access public |
||
85 | * @var array |
||
86 | */ |
||
87 | public $allcaps = array(); |
||
88 | |||
89 | /** |
||
90 | * The filter context applied to user data fields. |
||
91 | * |
||
92 | * @since 2.9.0 |
||
93 | * @access private |
||
94 | * @var string |
||
95 | */ |
||
96 | var $filter = null; |
||
97 | |||
98 | /** |
||
99 | * @static |
||
100 | * @access private |
||
101 | * @var array |
||
102 | */ |
||
103 | private static $back_compat_keys; |
||
104 | |||
105 | /** |
||
106 | * @since 4.7.0 |
||
107 | * @access protected |
||
108 | * @var wpdb |
||
109 | */ |
||
110 | protected $db; |
||
111 | |||
112 | /** |
||
113 | * Constructor. |
||
114 | * |
||
115 | * Retrieves the userdata and passes it to WP_User::init(). |
||
116 | * |
||
117 | * @since 2.0.0 |
||
118 | * @access public |
||
119 | * |
||
120 | * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB. |
||
121 | * @param string $name Optional. User's username |
||
122 | * @param int $blog_id Optional Site ID, defaults to current site. |
||
123 | */ |
||
124 | public function __construct( $id = 0, $name = '', $blog_id = '' ) { |
||
164 | |||
165 | /** |
||
166 | * Sets up object properties, including capabilities. |
||
167 | * |
||
168 | * @param object $data User DB row object. |
||
169 | * @param int $blog_id Optional. The site ID to initialize for. |
||
170 | */ |
||
171 | public function init( $data, $blog_id = '' ) { |
||
177 | |||
178 | /** |
||
179 | * Return only the main user fields |
||
180 | * |
||
181 | * @since 3.3.0 |
||
182 | * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. |
||
183 | * |
||
184 | * @static |
||
185 | * |
||
186 | * @global wpdb $wpdb WordPress database abstraction object. |
||
187 | * |
||
188 | * @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'. |
||
189 | * @param string|int $value The field value |
||
190 | * @return object|false Raw user object |
||
191 | */ |
||
192 | public static function get_data_by( $field, $value ) { |
||
251 | |||
252 | /** |
||
253 | * Makes private/protected methods readable for backward compatibility. |
||
254 | * |
||
255 | * @since 4.3.0 |
||
256 | * @access public |
||
257 | * |
||
258 | * @param callable $name Method to call. |
||
259 | * @param array $arguments Arguments to pass when calling. |
||
260 | * @return mixed|false Return value of the callback, false otherwise. |
||
261 | */ |
||
262 | public function __call( $name, $arguments ) { |
||
268 | |||
269 | /** |
||
270 | * Magic method for checking the existence of a certain custom field. |
||
271 | * |
||
272 | * @since 3.3.0 |
||
273 | * @access public |
||
274 | * |
||
275 | * @param string $key User meta key to check if set. |
||
276 | * @return bool Whether the given user meta key is set. |
||
277 | */ |
||
278 | public function __isset( $key ) { |
||
298 | |||
299 | /** |
||
300 | * Magic method for accessing custom fields. |
||
301 | * |
||
302 | * @since 3.3.0 |
||
303 | * @access public |
||
304 | * |
||
305 | * @param string $key User meta key to retrieve. |
||
306 | * @return mixed Value of the given user meta key (if set). If `$key` is 'id', the user ID. |
||
307 | */ |
||
308 | public function __get( $key ) { |
||
334 | |||
335 | /** |
||
336 | * Magic method for setting custom user fields. |
||
337 | * |
||
338 | * This method does not update custom fields in the database. It only stores |
||
339 | * the value on the WP_User instance. |
||
340 | * |
||
341 | * @since 3.3.0 |
||
342 | * @access public |
||
343 | * |
||
344 | * @param string $key User meta key. |
||
345 | * @param mixed $value User meta value. |
||
346 | */ |
||
347 | public function __set( $key, $value ) { |
||
362 | |||
363 | /** |
||
364 | * Magic method for unsetting a certain custom field. |
||
365 | * |
||
366 | * @since 4.4.0 |
||
367 | * @access public |
||
368 | * |
||
369 | * @param string $key User meta key to unset. |
||
370 | */ |
||
371 | public function __unset( $key ) { |
||
390 | |||
391 | /** |
||
392 | * Determine whether the user exists in the database. |
||
393 | * |
||
394 | * @since 3.4.0 |
||
395 | * @access public |
||
396 | * |
||
397 | * @return bool True if user exists in the database, false if not. |
||
398 | */ |
||
399 | public function exists() { |
||
402 | |||
403 | /** |
||
404 | * Retrieve the value of a property or meta key. |
||
405 | * |
||
406 | * Retrieves from the users and usermeta table. |
||
407 | * |
||
408 | * @since 3.3.0 |
||
409 | * |
||
410 | * @param string $key Property |
||
411 | * @return mixed |
||
412 | */ |
||
413 | public function get( $key ) { |
||
416 | |||
417 | /** |
||
418 | * Determine whether a property or meta key is set |
||
419 | * |
||
420 | * Consults the users and usermeta tables. |
||
421 | * |
||
422 | * @since 3.3.0 |
||
423 | * |
||
424 | * @param string $key Property |
||
425 | * @return bool |
||
426 | */ |
||
427 | public function has_prop( $key ) { |
||
430 | |||
431 | /** |
||
432 | * Return an array representation. |
||
433 | * |
||
434 | * @since 3.5.0 |
||
435 | * |
||
436 | * @return array Array representation. |
||
437 | */ |
||
438 | public function to_array() { |
||
441 | |||
442 | /** |
||
443 | * Set up capability object properties. |
||
444 | * |
||
445 | * Will set the value for the 'cap_key' property to current database table |
||
446 | * prefix, followed by 'capabilities'. Will then check to see if the |
||
447 | * property matching the 'cap_key' exists and is an array. If so, it will be |
||
448 | * used. |
||
449 | * |
||
450 | * @access protected |
||
451 | * @since 2.1.0 |
||
452 | * |
||
453 | * @param string $cap_key Optional capability key |
||
454 | */ |
||
455 | protected function _init_caps( $cap_key = '' ) { |
||
468 | |||
469 | /** |
||
470 | * Retrieve all of the role capabilities and merge with individual capabilities. |
||
471 | * |
||
472 | * All of the capabilities of the roles the user belongs to are merged with |
||
473 | * the users individual roles. This also means that the user can be denied |
||
474 | * specific roles that their role might have, but the specific user isn't |
||
475 | * granted permission to. |
||
476 | * |
||
477 | * @since 2.0.0 |
||
478 | * @access public |
||
479 | * |
||
480 | * @return array List of all capabilities for the user. |
||
481 | */ |
||
482 | public function get_role_caps() { |
||
499 | |||
500 | /** |
||
501 | * Add role to user. |
||
502 | * |
||
503 | * Updates the user's meta data option with capabilities and roles. |
||
504 | * |
||
505 | * @since 2.0.0 |
||
506 | * @access public |
||
507 | * |
||
508 | * @param string $role Role name. |
||
509 | */ |
||
510 | View Code Duplication | public function add_role( $role ) { |
|
530 | |||
531 | /** |
||
532 | * Remove role from user. |
||
533 | * |
||
534 | * @since 2.0.0 |
||
535 | * @access public |
||
536 | * |
||
537 | * @param string $role Role name. |
||
538 | */ |
||
539 | View Code Duplication | public function remove_role( $role ) { |
|
557 | |||
558 | /** |
||
559 | * Set the role of the user. |
||
560 | * |
||
561 | * This will remove the previous roles of the user and assign the user the |
||
562 | * new one. You can set the role to an empty string and it will remove all |
||
563 | * of the roles from the user. |
||
564 | * |
||
565 | * @since 2.0.0 |
||
566 | * @access public |
||
567 | * |
||
568 | * @param string $role Role name. |
||
569 | */ |
||
570 | public function set_role( $role ) { |
||
600 | |||
601 | /** |
||
602 | * Choose the maximum level the user has. |
||
603 | * |
||
604 | * Will compare the level from the $item parameter against the $max |
||
605 | * parameter. If the item is incorrect, then just the $max parameter value |
||
606 | * will be returned. |
||
607 | * |
||
608 | * Used to get the max level based on the capabilities the user has. This |
||
609 | * is also based on roles, so if the user is assigned the Administrator role |
||
610 | * then the capability 'level_10' will exist and the user will get that |
||
611 | * value. |
||
612 | * |
||
613 | * @since 2.0.0 |
||
614 | * @access public |
||
615 | * |
||
616 | * @param int $max Max level of user. |
||
617 | * @param string $item Level capability name. |
||
618 | * @return int Max Level. |
||
619 | */ |
||
620 | public function level_reduction( $max, $item ) { |
||
628 | |||
629 | /** |
||
630 | * Update the maximum user level for the user. |
||
631 | * |
||
632 | * Updates the 'user_level' user metadata (includes prefix that is the |
||
633 | * database table prefix) with the maximum user level. Gets the value from |
||
634 | * the all of the capabilities that the user has. |
||
635 | * |
||
636 | * @since 2.0.0 |
||
637 | * @access public |
||
638 | */ |
||
639 | public function update_user_level_from_caps() { |
||
643 | |||
644 | /** |
||
645 | * Add capability and grant or deny access to capability. |
||
646 | * |
||
647 | * @since 2.0.0 |
||
648 | * @access public |
||
649 | * |
||
650 | * @param string $cap Capability name. |
||
651 | * @param bool $grant Whether to grant capability to user. |
||
652 | */ |
||
653 | public function add_cap( $cap, $grant = true ) { |
||
659 | |||
660 | /** |
||
661 | * Remove capability from user. |
||
662 | * |
||
663 | * @since 2.0.0 |
||
664 | * @access public |
||
665 | * |
||
666 | * @param string $cap Capability name. |
||
667 | */ |
||
668 | public function remove_cap( $cap ) { |
||
677 | |||
678 | /** |
||
679 | * Remove all of the capabilities of the user. |
||
680 | * |
||
681 | * @since 2.1.0 |
||
682 | * @access public |
||
683 | */ |
||
684 | public function remove_all_caps() { |
||
690 | |||
691 | /** |
||
692 | * Whether user has capability or role name. |
||
693 | * |
||
694 | * While checking against particular roles in place of a capability is supported |
||
695 | * in part, this practice is discouraged as it may produce unreliable results. |
||
696 | * |
||
697 | * @since 2.0.0 |
||
698 | * @access public |
||
699 | * |
||
700 | * @see map_meta_cap() |
||
701 | * |
||
702 | * @param string $cap Capability name. |
||
703 | * @param int $object_id,... Optional. ID of the specific object to check against if `$cap` is a "meta" cap. |
||
704 | * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used |
||
705 | * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts', |
||
706 | * 'edit_others_posts', etc. The parameter is accessed via func_get_args() and passed |
||
707 | * to map_meta_cap(). |
||
708 | * @return bool Whether the current user has the given capability. If `$cap` is a meta cap and `$object_id` is |
||
709 | * passed, whether the current user has the given meta capability for the given object. |
||
710 | */ |
||
711 | public function has_cap( $cap ) { |
||
752 | |||
753 | /** |
||
754 | * Convert numeric level to level capability name. |
||
755 | * |
||
756 | * Prepends 'level_' to level number. |
||
757 | * |
||
758 | * @since 2.0.0 |
||
759 | * @access public |
||
760 | * |
||
761 | * @param int $level Level number, 1 to 10. |
||
762 | * @return string |
||
763 | */ |
||
764 | public function translate_level_to_cap( $level ) { |
||
767 | |||
768 | /** |
||
769 | * Set the site to operate on. Defaults to the current site. |
||
770 | * |
||
771 | * @since 3.0.0 |
||
772 | * |
||
773 | * @param int $blog_id Optional. Site ID, defaults to current site. |
||
774 | */ |
||
775 | public function for_blog( $blog_id = '' ) { |
||
783 | } |
||
784 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..