Complex classes like Wordlift_Entity_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_Entity_Service, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Wordlift_Entity_Service { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The Log service. |
||
| 23 | * |
||
| 24 | * @since 3.2.0 |
||
| 25 | * @access private |
||
| 26 | * @var \Wordlift_Log_Service $log The Log service. |
||
| 27 | */ |
||
| 28 | private $log; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The UI service. |
||
| 32 | * |
||
| 33 | * @since 3.2.0 |
||
| 34 | * @access private |
||
| 35 | * @var \Wordlift_UI_Service $ui_service The UI service. |
||
| 36 | */ |
||
| 37 | private $ui_service; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The {@link Wordlift_Relation_Service} instance. |
||
| 41 | * |
||
| 42 | * @since 3.15.0 |
||
| 43 | * @access private |
||
| 44 | * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
||
| 45 | */ |
||
| 46 | private $relation_service; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The {@link Wordlift_Entity_Uri_Service} instance. |
||
| 50 | * |
||
| 51 | * @since 3.16.3 |
||
| 52 | * @access private |
||
| 53 | * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
||
| 54 | */ |
||
| 55 | private $entity_uri_service; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The entity post type name. |
||
| 59 | * |
||
| 60 | * @since 3.1.0 |
||
| 61 | */ |
||
| 62 | const TYPE_NAME = 'entity'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The alternative label meta key. |
||
| 66 | * |
||
| 67 | * @since 3.2.0 |
||
| 68 | */ |
||
| 69 | const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The alternative label input template. |
||
| 73 | * |
||
| 74 | * @since 3.2.0 |
||
| 75 | */ |
||
| 76 | // TODO: this should be moved to a class that deals with HTML code. |
||
| 77 | const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
||
| 78 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
||
| 79 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
||
| 80 | <button class="button wl-delete-button">%s</button> |
||
| 81 | </div>'; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * A singleton instance of the Entity service. |
||
| 85 | * |
||
| 86 | * @since 3.2.0 |
||
| 87 | * @access private |
||
| 88 | * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
||
| 89 | */ |
||
| 90 | private static $instance; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Create a Wordlift_Entity_Service instance. |
||
| 94 | * |
||
| 95 | * @param \Wordlift_UI_Service $ui_service The UI service. |
||
| 96 | * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
||
| 97 | * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
||
| 98 | * |
||
| 99 | * @since 3.2.0 |
||
| 100 | * |
||
| 101 | */ |
||
| 102 | public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get the singleton instance of the Entity service. |
||
| 116 | * |
||
| 117 | * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
||
| 118 | * @since 3.2.0 |
||
| 119 | */ |
||
| 120 | public static function get_instance() { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Determines whether a post is an entity or not. Entity is in this context |
||
| 127 | * something which is not an article. |
||
| 128 | * |
||
| 129 | * @param int $post_id A post id. |
||
| 130 | * |
||
| 131 | * @return bool Return true if the post is an entity otherwise false. |
||
| 132 | * @since 3.1.0 |
||
| 133 | * |
||
| 134 | */ |
||
| 135 | public function is_entity( $post_id ) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get the proper classification scope for a given entity post |
||
| 167 | * |
||
| 168 | * @param integer $post_id An entity post id. |
||
| 169 | * |
||
| 170 | * @param string $default The default classification scope, `what` if not |
||
| 171 | * provided. |
||
| 172 | * |
||
| 173 | * @return string Returns a classification scope (e.g. 'what'). |
||
| 174 | * @since 3.5.0 |
||
| 175 | * |
||
| 176 | */ |
||
| 177 | public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Check whether a {@link WP_Post} is used. |
||
| 199 | * |
||
| 200 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 201 | * |
||
| 202 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
||
| 203 | */ |
||
| 204 | public function is_used( $post_id ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
||
| 249 | * |
||
| 250 | * @param string $uri The entity URI. |
||
| 251 | * |
||
| 252 | * @return WP_Post|null A WP_Post instance or null if not found. |
||
| 253 | * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
||
| 254 | * |
||
| 255 | * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
||
| 256 | * @since 3.2.0 |
||
| 257 | * |
||
| 258 | */ |
||
| 259 | public function get_entity_post_by_uri( $uri ) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
||
| 266 | * we check that the post we're saving is the current post. |
||
| 267 | * |
||
| 268 | * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
||
| 269 | * |
||
| 270 | * @since 3.2.0 |
||
| 271 | * |
||
| 272 | * @param int $post_id Post ID. |
||
| 273 | * @param WP_Post $post Post object. |
||
| 274 | * @param bool $update Whether this is an existing post being updated or not. |
||
| 275 | */ |
||
| 276 | public function save_post( $post_id, $post, $update ) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Set the alternative labels. |
||
| 306 | * |
||
| 307 | * @param int $post_id The post id. |
||
| 308 | * @param array $alt_labels An array of labels. |
||
| 309 | * |
||
| 310 | * @since 3.2.0 |
||
| 311 | * |
||
| 312 | */ |
||
| 313 | public function set_alternative_labels( $post_id, $alt_labels ) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Retrieve the alternate labels. |
||
| 342 | * |
||
| 343 | * @param int $post_id Post id. |
||
| 344 | * |
||
| 345 | * @return mixed An array of alternative labels. |
||
| 346 | * @since 3.2.0 |
||
| 347 | * |
||
| 348 | */ |
||
| 349 | public function get_alternative_labels( $post_id ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Retrieve the labels for an entity, i.e. the title + the synonyms. |
||
| 356 | * |
||
| 357 | * @param int $post_id The entity {@link WP_Post} id. |
||
| 358 | * |
||
| 359 | * @return array An array with the entity title and labels. |
||
| 360 | * @since 3.12.0 |
||
| 361 | * |
||
| 362 | */ |
||
| 363 | public function get_labels( $post_id ) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
||
| 370 | * |
||
| 371 | * @param WP_Post $post Post object. |
||
| 372 | * |
||
| 373 | * @since 3.2.0 |
||
| 374 | * |
||
| 375 | */ |
||
| 376 | public function edit_form_before_permalink( $post ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get the URI for the entity with the specified post id. |
||
| 400 | * |
||
| 401 | * @param int $post_id The entity post id. |
||
| 402 | * |
||
| 403 | * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
||
| 404 | * @since 3.6.0 |
||
| 405 | * |
||
| 406 | */ |
||
| 407 | private function get_uri_for_post( $post_id ) { |
||
| 443 | |||
| 444 | public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Get the alternative label input HTML code. |
||
| 473 | * |
||
| 474 | * @param string $value The input value. |
||
| 475 | * |
||
| 476 | * @return string The input HTML code. |
||
| 477 | * @since 3.2.0 |
||
| 478 | * |
||
| 479 | */ |
||
| 480 | private function get_alternative_label_input( $value = '' ) { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Get the number of entity posts published in this blog. |
||
| 487 | * |
||
| 488 | * @return int The number of published entity posts. |
||
| 489 | * @since 3.6.0 |
||
| 490 | * |
||
| 491 | */ |
||
| 492 | public function count() { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Add the entity filtering criterias to the arguments for a `get_posts` |
||
| 521 | * call. |
||
| 522 | * |
||
| 523 | * @param array $args The arguments for a `get_posts` call. |
||
| 524 | * |
||
| 525 | * @return array The arguments for a `get_posts` call. |
||
| 526 | * @since 3.15.0 |
||
| 527 | * |
||
| 528 | */ |
||
| 529 | public static function add_criterias( $args ) { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Create a new entity. |
||
| 563 | * |
||
| 564 | * @param string $name The entity name. |
||
| 565 | * @param string $type_uri The entity's type URI. |
||
| 566 | * @param null $logo The entity logo id (or NULL if none). |
||
| 567 | * @param string $status The post status, by default 'publish'. |
||
| 568 | * |
||
| 569 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
||
| 570 | * @since 3.9.0 |
||
| 571 | * |
||
| 572 | */ |
||
| 573 | public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Get the entities related to the one with the specified id. By default only |
||
| 601 | * published entities will be returned. |
||
| 602 | * |
||
| 603 | * @param int $id The post id. |
||
| 604 | * @param string $post_status The target post status (default = publish). |
||
| 605 | * |
||
| 606 | * @return array An array of post ids. |
||
| 607 | * @since 3.10.0 |
||
| 608 | * |
||
| 609 | */ |
||
| 610 | public function get_related_entities( $id, $post_status = 'publish' ) { |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Get the list of entities. |
||
| 617 | * |
||
| 618 | * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
||
| 619 | * |
||
| 620 | * @return array An array of entity posts. |
||
| 621 | * @since 3.12.2 |
||
| 622 | * |
||
| 623 | */ |
||
| 624 | public function get( $params = array() ) { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * The list of post type names which can be used for entities |
||
| 638 | * |
||
| 639 | * Criteria is that the post type is public. The list of valid post types |
||
| 640 | * can be overridden with a filter. |
||
| 641 | * |
||
| 642 | * @return array Array containing the names of the valid post types. |
||
| 643 | * @since 3.15.0 |
||
| 644 | * |
||
| 645 | */ |
||
| 646 | static function valid_entity_post_types() { |
||
| 653 | |||
| 654 | } |
||
| 655 |
This check looks for function calls that miss required arguments.