Complex classes like Wordlift_User_Service 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 Wordlift_User_Service, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Wordlift_User_Service { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * The meta key where the user's URI is stored. |
||
| 15 | * |
||
| 16 | * @since 3.1.7 |
||
| 17 | */ |
||
| 18 | const URI_META_KEY = '_wl_uri'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The user meta key where the deny entity edit flag is stored. |
||
| 22 | * |
||
| 23 | * @since 3.14.0 |
||
| 24 | */ |
||
| 25 | const DENY_ENTITY_CREATE_META_KEY = '_wl_deny_entity_create'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The meta key holding the entity id representing a {@link WP_User}. |
||
| 29 | * |
||
| 30 | * @since 3.14.0 |
||
| 31 | */ |
||
| 32 | const ENTITY_META_KEY = '_wl_entity'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The Log service. |
||
| 36 | * |
||
| 37 | * @since 3.1.7 |
||
| 38 | * @access private |
||
| 39 | * @var \Wordlift_Log_Service $log_service The Log service. |
||
| 40 | */ |
||
| 41 | private $log_service; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The singleton instance of the User service. |
||
| 45 | * |
||
| 46 | * @since 3.1.7 |
||
| 47 | * @access private |
||
| 48 | * @var \Wordlift_User_Service $user_service The singleton instance of the User service. |
||
| 49 | */ |
||
| 50 | private static $instance; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Create an instance of the User service. |
||
| 54 | * |
||
| 55 | * @since 3.1.7 |
||
| 56 | */ |
||
| 57 | public function __construct() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get the singleton instance of the User service. |
||
| 68 | * |
||
| 69 | * @since 3.1.7 |
||
| 70 | * @return \Wordlift_User_Service The singleton instance of the User service. |
||
| 71 | */ |
||
| 72 | public static function get_instance() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get the URI for a user. |
||
| 79 | * |
||
| 80 | * @since 3.1.7 |
||
| 81 | * |
||
| 82 | * @param int $user_id The user id |
||
| 83 | * |
||
| 84 | * @return false|string The user's URI or false in case of failure. |
||
| 85 | */ |
||
| 86 | public function get_uri( $user_id ) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Receives wp_insert_post events. |
||
| 106 | * |
||
| 107 | * @since 3.1.7 |
||
| 108 | * |
||
| 109 | * @param int $post_id Post ID. |
||
| 110 | * @param WP_Post $post Post object. |
||
| 111 | * @param bool $update Whether this is an existing post being updated or not. |
||
| 112 | */ |
||
| 113 | public function wp_insert_post( $post_id, $post, $update ) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set the `id` of the entity representing a {@link WP_User}. |
||
| 142 | * |
||
| 143 | * If the `id` is set to 0 (or less) then the meta is deleted. |
||
| 144 | * |
||
| 145 | * @since 3.14.0 |
||
| 146 | * |
||
| 147 | * @param int $user_id The {@link WP_User}. |
||
| 148 | * @param int $value The entity {@link WP_Post} `id`. |
||
| 149 | * |
||
| 150 | * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
||
| 151 | */ |
||
| 152 | public function set_entity( $user_id, $value ) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get the {@link WP_Post} `id` of the entity representing a {@link WP_User}. |
||
| 161 | * |
||
| 162 | * @since 3.14.0 |
||
| 163 | * |
||
| 164 | * @param int $user_id The {@link WP_User}'s `id`. |
||
| 165 | * |
||
| 166 | * @return string The entity {@link WP_Post} `id` or an empty string if not set. |
||
| 167 | */ |
||
| 168 | public function get_entity( $user_id ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the user's URI stored in the user's meta. |
||
| 175 | * |
||
| 176 | * @since 3.1.7 |
||
| 177 | * |
||
| 178 | * @param int $user_id The user id. |
||
| 179 | * |
||
| 180 | * @return false|string The user's URI or false if not found. |
||
| 181 | */ |
||
| 182 | private function _get_uri( $user_id ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Build an URI for a user. |
||
| 195 | * |
||
| 196 | * @since 3.1.7 |
||
| 197 | * |
||
| 198 | * @param int $user_id The user's id. |
||
| 199 | * |
||
| 200 | * @return false|string The user's URI or false in case of failure. |
||
| 201 | */ |
||
| 202 | private function _build_uri( $user_id ) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Store the URI in user's meta. |
||
| 219 | * |
||
| 220 | * @since 3.1.7 |
||
| 221 | * |
||
| 222 | * @param int $user_id The user's id. |
||
| 223 | * @param string $user_uri The user's uri. |
||
| 224 | * |
||
| 225 | * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
||
| 226 | */ |
||
| 227 | private function _set_uri( $user_id, $user_uri ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get the delete query. |
||
| 234 | * |
||
| 235 | * @since 3.1.7 |
||
| 236 | * |
||
| 237 | * @param int $user_id The user id. |
||
| 238 | * |
||
| 239 | * @return false|string The delete query or false in case of failure. |
||
| 240 | */ |
||
| 241 | private function get_delete_query( $user_id ) { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get the insert query. |
||
| 270 | * |
||
| 271 | * @since 3.1.7 |
||
| 272 | * |
||
| 273 | * @param int $user_id The user id. |
||
| 274 | * |
||
| 275 | * @return false|string The insert query or false in case of failure. |
||
| 276 | */ |
||
| 277 | private function get_insert_query( $user_id ) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Mark an editor user as denied from editing entities. |
||
| 304 | * Does nothing if the user is not an editor |
||
| 305 | * |
||
| 306 | * @since 3.14.0 |
||
| 307 | * |
||
| 308 | * @param integer $user_id The ID of the user |
||
| 309 | */ |
||
| 310 | public function deny_editor_entity_create( $user_id ) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Remove the "deny entity editing" mark from an editor user. |
||
| 324 | * Does nothing if the user is not an editor |
||
| 325 | * |
||
| 326 | * @since 3.14.0 |
||
| 327 | * |
||
| 328 | * @param integer $user_id The ID of the user |
||
| 329 | */ |
||
| 330 | public function allow_editor_entity_create( $user_id ) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get whether the 'deny editor entity editing' flag is set. |
||
| 344 | * |
||
| 345 | * @since 3.14.0 |
||
| 346 | * |
||
| 347 | * @param int $user_id The {@link WP_User} `id`. |
||
| 348 | * |
||
| 349 | * @return int bool True if editing is denied otherwise false. |
||
| 350 | */ |
||
| 351 | public function is_deny_editor_entity_create( $user_id ) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Check whether the {@link WP_User} with the specified `id` is an editor, |
||
| 358 | * i.e. has the `editor` role. |
||
| 359 | * |
||
| 360 | * @since 3.14.0 |
||
| 361 | * |
||
| 362 | * @param int $user_id The {@link WP_User} `id`. |
||
| 363 | * |
||
| 364 | * @return bool True if the {@link WP_User} is an editor otherwise false. |
||
| 365 | */ |
||
| 366 | public function is_editor( $user_id ) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Check if an editor can create entities. |
||
| 377 | * |
||
| 378 | * @since 3.14.0 |
||
| 379 | * |
||
| 380 | * @param int $user_id The user id of the user being checked. |
||
| 381 | * |
||
| 382 | * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
||
| 383 | */ |
||
| 384 | public function editor_can_create_entities( $user_id ) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Filter capabilities of user. |
||
| 397 | * |
||
| 398 | * Deny the capability of managing and editing entities for some users. |
||
| 399 | * |
||
| 400 | * @since 3.14.0 |
||
| 401 | * |
||
| 402 | * @param array $allcaps All the capabilities of the user |
||
| 403 | * @param array $cap [0] Required capability |
||
| 404 | * @param array $args [0] Requested capability |
||
| 405 | * [1] User ID |
||
| 406 | * [2] Associated object ID |
||
| 407 | * |
||
| 408 | * @return array The capabilities array. |
||
| 409 | */ |
||
| 410 | public function has_cap( $allcaps, $cap, $args ) { |
||
| 445 | } |
||
| 446 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.