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 |
||
34 | class WP_User { |
||
35 | /** |
||
36 | * User data container. |
||
37 | * |
||
38 | * @since 2.0.0 |
||
39 | * @var object |
||
40 | */ |
||
41 | public $data; |
||
42 | |||
43 | /** |
||
44 | * The user's ID. |
||
45 | * |
||
46 | * @since 2.1.0 |
||
47 | * @access public |
||
48 | * @var int |
||
49 | */ |
||
50 | public $ID = 0; |
||
51 | |||
52 | /** |
||
53 | * The individual capabilities the user has been given. |
||
54 | * |
||
55 | * @since 2.0.0 |
||
56 | * @access public |
||
57 | * @var array |
||
58 | */ |
||
59 | public $caps = array(); |
||
60 | |||
61 | /** |
||
62 | * User metadata option name. |
||
63 | * |
||
64 | * @since 2.0.0 |
||
65 | * @access public |
||
66 | * @var string |
||
67 | */ |
||
68 | public $cap_key; |
||
69 | |||
70 | /** |
||
71 | * The roles the user is part of. |
||
72 | * |
||
73 | * @since 2.0.0 |
||
74 | * @access public |
||
75 | * @var array |
||
76 | */ |
||
77 | public $roles = array(); |
||
78 | |||
79 | /** |
||
80 | * All capabilities the user has, including individual and role based. |
||
81 | * |
||
82 | * @since 2.0.0 |
||
83 | * @access public |
||
84 | * @var array |
||
85 | */ |
||
86 | public $allcaps = array(); |
||
87 | |||
88 | /** |
||
89 | * The filter context applied to user data fields. |
||
90 | * |
||
91 | * @since 2.9.0 |
||
92 | * @access private |
||
93 | * @var string |
||
94 | */ |
||
95 | var $filter = null; |
||
96 | |||
97 | /** |
||
98 | * @static |
||
99 | * @access private |
||
100 | * @var array |
||
101 | */ |
||
102 | private static $back_compat_keys; |
||
103 | |||
104 | /** |
||
105 | * Constructor. |
||
106 | * |
||
107 | * Retrieves the userdata and passes it to WP_User::init(). |
||
108 | * |
||
109 | * @since 2.0.0 |
||
110 | * @access public |
||
111 | * |
||
112 | * @global wpdb $wpdb WordPress database abstraction object. |
||
113 | * |
||
114 | * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB. |
||
115 | * @param string $name Optional. User's username |
||
116 | * @param int $blog_id Optional Site ID, defaults to current site. |
||
117 | */ |
||
118 | public function __construct( $id = 0, $name = '', $blog_id = '' ) { |
||
156 | |||
157 | /** |
||
158 | * Sets up object properties, including capabilities. |
||
159 | * |
||
160 | * @param object $data User DB row object. |
||
161 | * @param int $blog_id Optional. The site ID to initialize for. |
||
162 | */ |
||
163 | public function init( $data, $blog_id = '' ) { |
||
169 | |||
170 | /** |
||
171 | * Return only the main user fields |
||
172 | * |
||
173 | * @since 3.3.0 |
||
174 | * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. |
||
175 | * |
||
176 | * @static |
||
177 | * |
||
178 | * @global wpdb $wpdb WordPress database abstraction object. |
||
179 | * |
||
180 | * @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'. |
||
181 | * @param string|int $value The field value |
||
182 | * @return object|false Raw user object |
||
183 | */ |
||
184 | public static function get_data_by( $field, $value ) { |
||
243 | |||
244 | /** |
||
245 | * Makes private/protected methods readable for backwards compatibility. |
||
246 | * |
||
247 | * @since 4.3.0 |
||
248 | * @access public |
||
249 | * |
||
250 | * @param callable $name Method to call. |
||
251 | * @param array $arguments Arguments to pass when calling. |
||
252 | * @return mixed|false Return value of the callback, false otherwise. |
||
253 | */ |
||
254 | public function __call( $name, $arguments ) { |
||
260 | |||
261 | /** |
||
262 | * Magic method for checking the existence of a certain custom field. |
||
263 | * |
||
264 | * @since 3.3.0 |
||
265 | * @access public |
||
266 | * |
||
267 | * @param string $key User meta key to check if set. |
||
268 | * @return bool Whether the given user meta key is set. |
||
269 | */ |
||
270 | public function __isset( $key ) { |
||
290 | |||
291 | /** |
||
292 | * Magic method for accessing custom fields. |
||
293 | * |
||
294 | * @since 3.3.0 |
||
295 | * @access public |
||
296 | * |
||
297 | * @param string $key User meta key to retrieve. |
||
298 | * @return mixed Value of the given user meta key (if set). If `$key` is 'id', the user ID. |
||
299 | */ |
||
300 | public function __get( $key ) { |
||
326 | |||
327 | /** |
||
328 | * Magic method for setting custom user fields. |
||
329 | * |
||
330 | * This method does not update custom fields in the database. It only stores |
||
331 | * the value on the WP_User instance. |
||
332 | * |
||
333 | * @since 3.3.0 |
||
334 | * @access public |
||
335 | * |
||
336 | * @param string $key User meta key. |
||
337 | * @param mixed $value User meta value. |
||
338 | */ |
||
339 | public function __set( $key, $value ) { |
||
354 | |||
355 | /** |
||
356 | * Magic method for unsetting a certain custom field. |
||
357 | * |
||
358 | * @since 4.4.0 |
||
359 | * @access public |
||
360 | * |
||
361 | * @param string $key User meta key to unset. |
||
362 | */ |
||
363 | public function __unset( $key ) { |
||
382 | |||
383 | /** |
||
384 | * Determine whether the user exists in the database. |
||
385 | * |
||
386 | * @since 3.4.0 |
||
387 | * @access public |
||
388 | * |
||
389 | * @return bool True if user exists in the database, false if not. |
||
390 | */ |
||
391 | public function exists() { |
||
394 | |||
395 | /** |
||
396 | * Retrieve the value of a property or meta key. |
||
397 | * |
||
398 | * Retrieves from the users and usermeta table. |
||
399 | * |
||
400 | * @since 3.3.0 |
||
401 | * |
||
402 | * @param string $key Property |
||
403 | * @return mixed |
||
404 | */ |
||
405 | public function get( $key ) { |
||
408 | |||
409 | /** |
||
410 | * Determine whether a property or meta key is set |
||
411 | * |
||
412 | * Consults the users and usermeta tables. |
||
413 | * |
||
414 | * @since 3.3.0 |
||
415 | * |
||
416 | * @param string $key Property |
||
417 | * @return bool |
||
418 | */ |
||
419 | public function has_prop( $key ) { |
||
422 | |||
423 | /** |
||
424 | * Return an array representation. |
||
425 | * |
||
426 | * @since 3.5.0 |
||
427 | * |
||
428 | * @return array Array representation. |
||
429 | */ |
||
430 | public function to_array() { |
||
433 | |||
434 | /** |
||
435 | * Set up capability object properties. |
||
436 | * |
||
437 | * Will set the value for the 'cap_key' property to current database table |
||
438 | * prefix, followed by 'capabilities'. Will then check to see if the |
||
439 | * property matching the 'cap_key' exists and is an array. If so, it will be |
||
440 | * used. |
||
441 | * |
||
442 | * @access protected |
||
443 | * @since 2.1.0 |
||
444 | * |
||
445 | * @global wpdb $wpdb WordPress database abstraction object. |
||
446 | * |
||
447 | * @param string $cap_key Optional capability key |
||
448 | */ |
||
449 | protected function _init_caps( $cap_key = '' ) { |
||
464 | |||
465 | /** |
||
466 | * Retrieve all of the role capabilities and merge with individual capabilities. |
||
467 | * |
||
468 | * All of the capabilities of the roles the user belongs to are merged with |
||
469 | * the users individual roles. This also means that the user can be denied |
||
470 | * specific roles that their role might have, but the specific user isn't |
||
471 | * granted permission to. |
||
472 | * |
||
473 | * @since 2.0.0 |
||
474 | * @access public |
||
475 | * |
||
476 | * @return array List of all capabilities for the user. |
||
477 | */ |
||
478 | public function get_role_caps() { |
||
495 | |||
496 | /** |
||
497 | * Add role to user. |
||
498 | * |
||
499 | * Updates the user's meta data option with capabilities and roles. |
||
500 | * |
||
501 | * @since 2.0.0 |
||
502 | * @access public |
||
503 | * |
||
504 | * @param string $role Role name. |
||
505 | */ |
||
506 | View Code Duplication | public function add_role( $role ) { |
|
526 | |||
527 | /** |
||
528 | * Remove role from user. |
||
529 | * |
||
530 | * @since 2.0.0 |
||
531 | * @access public |
||
532 | * |
||
533 | * @param string $role Role name. |
||
534 | */ |
||
535 | View Code Duplication | public function remove_role( $role ) { |
|
553 | |||
554 | /** |
||
555 | * Set the role of the user. |
||
556 | * |
||
557 | * This will remove the previous roles of the user and assign the user the |
||
558 | * new one. You can set the role to an empty string and it will remove all |
||
559 | * of the roles from the user. |
||
560 | * |
||
561 | * @since 2.0.0 |
||
562 | * @access public |
||
563 | * |
||
564 | * @param string $role Role name. |
||
565 | */ |
||
566 | public function set_role( $role ) { |
||
596 | |||
597 | /** |
||
598 | * Choose the maximum level the user has. |
||
599 | * |
||
600 | * Will compare the level from the $item parameter against the $max |
||
601 | * parameter. If the item is incorrect, then just the $max parameter value |
||
602 | * will be returned. |
||
603 | * |
||
604 | * Used to get the max level based on the capabilities the user has. This |
||
605 | * is also based on roles, so if the user is assigned the Administrator role |
||
606 | * then the capability 'level_10' will exist and the user will get that |
||
607 | * value. |
||
608 | * |
||
609 | * @since 2.0.0 |
||
610 | * @access public |
||
611 | * |
||
612 | * @param int $max Max level of user. |
||
613 | * @param string $item Level capability name. |
||
614 | * @return int Max Level. |
||
615 | */ |
||
616 | public function level_reduction( $max, $item ) { |
||
624 | |||
625 | /** |
||
626 | * Update the maximum user level for the user. |
||
627 | * |
||
628 | * Updates the 'user_level' user metadata (includes prefix that is the |
||
629 | * database table prefix) with the maximum user level. Gets the value from |
||
630 | * the all of the capabilities that the user has. |
||
631 | * |
||
632 | * @since 2.0.0 |
||
633 | * @access public |
||
634 | * |
||
635 | * @global wpdb $wpdb WordPress database abstraction object. |
||
636 | */ |
||
637 | public function update_user_level_from_caps() { |
||
642 | |||
643 | /** |
||
644 | * Add capability and grant or deny access to capability. |
||
645 | * |
||
646 | * @since 2.0.0 |
||
647 | * @access public |
||
648 | * |
||
649 | * @param string $cap Capability name. |
||
650 | * @param bool $grant Whether to grant capability to user. |
||
651 | */ |
||
652 | public function add_cap( $cap, $grant = true ) { |
||
658 | |||
659 | /** |
||
660 | * Remove capability from user. |
||
661 | * |
||
662 | * @since 2.0.0 |
||
663 | * @access public |
||
664 | * |
||
665 | * @param string $cap Capability name. |
||
666 | */ |
||
667 | public function remove_cap( $cap ) { |
||
676 | |||
677 | /** |
||
678 | * Remove all of the capabilities of the user. |
||
679 | * |
||
680 | * @since 2.1.0 |
||
681 | * @access public |
||
682 | * |
||
683 | * @global wpdb $wpdb WordPress database abstraction object. |
||
684 | */ |
||
685 | public function remove_all_caps() { |
||
692 | |||
693 | /** |
||
694 | * Whether user has capability or role name. |
||
695 | * |
||
696 | * While checking against particular roles in place of a capability is supported |
||
697 | * in part, this practice is discouraged as it may produce unreliable results. |
||
698 | * |
||
699 | * @since 2.0.0 |
||
700 | * @access public |
||
701 | * |
||
702 | * @see map_meta_cap() |
||
703 | * |
||
704 | * @param string $cap Capability name. |
||
705 | * @param int $object_id,... Optional. ID of the specific object to check against if `$cap` is a "meta" cap. |
||
706 | * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used |
||
707 | * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts', |
||
708 | * 'edit_others_posts', etc. The parameter is accessed via func_get_args() and passed |
||
709 | * to map_meta_cap(). |
||
710 | * @return bool Whether the current user has the given capability. If `$cap` is a meta cap and `$object_id` is |
||
711 | * passed, whether the current user has the given meta capability for the given object. |
||
712 | */ |
||
713 | public function has_cap( $cap ) { |
||
754 | |||
755 | /** |
||
756 | * Convert numeric level to level capability name. |
||
757 | * |
||
758 | * Prepends 'level_' to level number. |
||
759 | * |
||
760 | * @since 2.0.0 |
||
761 | * @access public |
||
762 | * |
||
763 | * @param int $level Level number, 1 to 10. |
||
764 | * @return string |
||
765 | */ |
||
766 | public function translate_level_to_cap( $level ) { |
||
769 | |||
770 | /** |
||
771 | * Set the site to operate on. Defaults to the current site. |
||
772 | * |
||
773 | * @since 3.0.0 |
||
774 | * |
||
775 | * @global wpdb $wpdb WordPress database abstraction object. |
||
776 | * |
||
777 | * @param int $blog_id Optional. Site ID, defaults to current site. |
||
778 | */ |
||
779 | public function for_blog( $blog_id = '' ) { |
||
787 | } |
||
788 |
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..