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 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 |
||
| 8 | class Wordlift_Entity_Service { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * The Log service. |
||
| 12 | * |
||
| 13 | * @since 3.2.0 |
||
| 14 | * @access private |
||
| 15 | * @var \Wordlift_Log_Service $log The Log service. |
||
| 16 | */ |
||
| 17 | private $log; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The UI service. |
||
| 21 | * |
||
| 22 | * @since 3.2.0 |
||
| 23 | * @access private |
||
| 24 | * @var \Wordlift_UI_Service $ui_service The UI service. |
||
| 25 | */ |
||
| 26 | private $ui_service; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The {@link Wordlift_Relation_Service} instance. |
||
| 30 | * |
||
| 31 | * @since 3.15.0 |
||
| 32 | * @access private |
||
| 33 | * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
||
| 34 | */ |
||
| 35 | private $relation_service; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The entity post type name. |
||
| 39 | * |
||
| 40 | * @since 3.1.0 |
||
| 41 | */ |
||
| 42 | const TYPE_NAME = 'entity'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The alternative label meta key. |
||
| 46 | * |
||
| 47 | * @since 3.2.0 |
||
| 48 | */ |
||
| 49 | const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The alternative label input template. |
||
| 53 | * |
||
| 54 | * @since 3.2.0 |
||
| 55 | */ |
||
| 56 | // TODO: this should be moved to a class that deals with HTML code. |
||
| 57 | const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
||
| 58 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
||
| 59 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
||
| 60 | <button class="button wl-delete-button">%s</button> |
||
| 61 | </div>'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * A singleton instance of the Entity service. |
||
| 65 | * |
||
| 66 | * @since 3.2.0 |
||
| 67 | * @access private |
||
| 68 | * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
||
| 69 | */ |
||
| 70 | private static $instance; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Create a Wordlift_Entity_Service instance. |
||
| 74 | * |
||
| 75 | * @since 3.2.0 |
||
| 76 | * |
||
| 77 | * @param \Wordlift_UI_Service $ui_service The UI service. |
||
| 78 | * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
||
| 79 | */ |
||
| 80 | public function __construct( $ui_service, $relation_service ) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get the singleton instance of the Entity service. |
||
| 93 | * |
||
| 94 | * @since 3.2.0 |
||
| 95 | * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
||
| 96 | */ |
||
| 97 | public static function get_instance() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Determines whether a post is an entity or not. Entity is in this context |
||
| 104 | * something which is not an article. |
||
| 105 | * |
||
| 106 | * @since 3.1.0 |
||
| 107 | * |
||
| 108 | * @param int $post_id A post id. |
||
| 109 | * |
||
| 110 | * @return bool Return true if the post is an entity otherwise false. |
||
| 111 | */ |
||
| 112 | public function is_entity( $post_id ) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get the proper classification scope for a given entity post |
||
| 130 | * |
||
| 131 | * @since 3.5.0 |
||
| 132 | * |
||
| 133 | * @param integer $post_id An entity post id. |
||
| 134 | * |
||
| 135 | * @param string $default The default classification scope, `what` if not |
||
| 136 | * provided. |
||
| 137 | * |
||
| 138 | * @return string Returns a classification scope (e.g. 'what'). |
||
| 139 | */ |
||
| 140 | public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Check whether a {@link WP_Post} is used. |
||
| 162 | * |
||
| 163 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 164 | * |
||
| 165 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
||
| 166 | */ |
||
| 167 | public function is_used( $post_id ) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Determines whether a given uri is an internal uri or not. |
||
| 212 | * |
||
| 213 | * @since 3.3.2 |
||
| 214 | * |
||
| 215 | * @param int $uri An uri. |
||
| 216 | * |
||
| 217 | * @return true if the uri internal to the current dataset otherwise false. |
||
| 218 | */ |
||
| 219 | public function is_internal_uri( $uri ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
||
| 226 | * |
||
| 227 | * @since 3.2.0 |
||
| 228 | * |
||
| 229 | * @param string $uri The entity URI. |
||
| 230 | * |
||
| 231 | * @return WP_Post|null A WP_Post instance or null if not found. |
||
| 232 | */ |
||
| 233 | public function get_entity_post_by_uri( $uri ) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
||
| 310 | * we check that the post we're saving is the current post. |
||
| 311 | * |
||
| 312 | * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
||
| 313 | * |
||
| 314 | * @since 3.2.0 |
||
| 315 | * |
||
| 316 | * @param int $post_id Post ID. |
||
| 317 | * @param WP_Post $post Post object. |
||
| 318 | * @param bool $update Whether this is an existing post being updated or not. |
||
| 319 | */ |
||
| 320 | public function save_post( $post_id, $post, $update ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Set the alternative labels. |
||
| 347 | * |
||
| 348 | * @since 3.2.0 |
||
| 349 | * |
||
| 350 | * @param int $post_id The post id. |
||
| 351 | * @param array $alt_labels An array of labels. |
||
| 352 | */ |
||
| 353 | public function set_alternative_labels( $post_id, $alt_labels ) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Retrieve the alternate labels. |
||
| 376 | * |
||
| 377 | * @since 3.2.0 |
||
| 378 | * |
||
| 379 | * @param int $post_id Post id. |
||
| 380 | * |
||
| 381 | * @return mixed An array of alternative labels. |
||
| 382 | */ |
||
| 383 | public function get_alternative_labels( $post_id ) { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Retrieve the labels for an entity, i.e. the title + the synonyms. |
||
| 390 | * |
||
| 391 | * @since 3.12.0 |
||
| 392 | * |
||
| 393 | * @param int $post_id The entity {@link WP_Post} id. |
||
| 394 | * |
||
| 395 | * @return array An array with the entity title and labels. |
||
| 396 | */ |
||
| 397 | public function get_labels( $post_id ) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
||
| 404 | * |
||
| 405 | * @since 3.2.0 |
||
| 406 | * |
||
| 407 | * @param WP_Post $post Post object. |
||
| 408 | */ |
||
| 409 | public function edit_form_before_permalink( $post ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get the URI for the entity with the specified post id. |
||
| 433 | * |
||
| 434 | * @since 3.6.0 |
||
| 435 | * |
||
| 436 | * @param int $post_id The entity post id. |
||
| 437 | * |
||
| 438 | * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
||
| 439 | */ |
||
| 440 | public function get_uri( $post_id ) { |
||
| 463 | |||
| 464 | |||
| 465 | /** |
||
| 466 | * Get the alternative label input HTML code. |
||
| 467 | * |
||
| 468 | * @since 3.2.0 |
||
| 469 | * |
||
| 470 | * @param string $value The input value. |
||
| 471 | * |
||
| 472 | * @return string The input HTML code. |
||
| 473 | */ |
||
| 474 | private function get_alternative_label_input( $value = '' ) { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Get the number of entity posts published in this blog. |
||
| 481 | * |
||
| 482 | * @since 3.6.0 |
||
| 483 | * |
||
| 484 | * @return int The number of published entity posts. |
||
| 485 | */ |
||
| 486 | public function count() { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Add the entity filtering criterias to the arguments for a `get_posts` |
||
| 498 | * call. |
||
| 499 | * |
||
| 500 | * @since 3.15.0 |
||
| 501 | * |
||
| 502 | * @param array $args The arguments for a `get_posts` call. |
||
| 503 | * |
||
| 504 | * @return array The arguments for a `get_posts` call. |
||
| 505 | */ |
||
| 506 | View Code Duplication | public static function add_criterias( $args ) { |
|
| 535 | |||
| 536 | // /** |
||
| 537 | // * Get the entity terms IDs which represent an entity. |
||
| 538 | // * |
||
| 539 | // * @since 3.17.0 deprecated. |
||
| 540 | // * @since 3.15.0 |
||
| 541 | // * |
||
| 542 | // * @deprecated |
||
| 543 | // * @return array An array of terms' ids. |
||
| 544 | // */ |
||
| 545 | // public static function get_entity_terms() { |
||
| 546 | // |
||
| 547 | // $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
||
| 548 | // 'hide_empty' => false, |
||
| 549 | // // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
||
| 550 | // // An issue has been opened with the AAM plugin author as well. |
||
| 551 | // // |
||
| 552 | // // see https://github.com/insideout10/wordlift-plugin/issues/334 |
||
| 553 | // // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
||
| 554 | // 'fields' => 'all', |
||
| 555 | // ) ); |
||
| 556 | // |
||
| 557 | // return array_map( function ( $term ) { |
||
| 558 | // return $term->term_id; |
||
| 559 | // }, array_filter( $terms, function ( $term ) { |
||
| 560 | // return 'article' !== $term->slug; |
||
| 561 | // } ) ); |
||
| 562 | // } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Create a new entity. |
||
| 566 | * |
||
| 567 | * @since 3.9.0 |
||
| 568 | * |
||
| 569 | * @param string $name The entity name. |
||
| 570 | * @param string $type_uri The entity's type URI. |
||
| 571 | * @param null $logo The entity logo id (or NULL if none). |
||
| 572 | * @param string $status The post status, by default 'publish'. |
||
| 573 | * |
||
| 574 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
||
| 575 | */ |
||
| 576 | public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Get the entities related to the one with the specified id. By default only |
||
| 604 | * published entities will be returned. |
||
| 605 | * |
||
| 606 | * @since 3.10.0 |
||
| 607 | * |
||
| 608 | * @param int $id The post id. |
||
| 609 | * @param string $post_status The target post status (default = publish). |
||
| 610 | * |
||
| 611 | * @return array An array of post ids. |
||
| 612 | */ |
||
| 613 | public function get_related_entities( $id, $post_status = 'publish' ) { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Get the list of entities. |
||
| 620 | * |
||
| 621 | * @since 3.12.2 |
||
| 622 | * |
||
| 623 | * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
||
| 624 | * |
||
| 625 | * @return array An array of entity posts. |
||
| 626 | */ |
||
| 627 | public function get( $params = array() ) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * The list of post type names which can be used for entities |
||
| 641 | * |
||
| 642 | * Criteria is that the post type is public. The list of valid post types |
||
| 643 | * can be overridden with a filter. |
||
| 644 | * |
||
| 645 | * @since 3.15.0 |
||
| 646 | * |
||
| 647 | * @return array Array containing the names of the valid post types. |
||
| 648 | */ |
||
| 649 | static function valid_entity_post_types() { |
||
| 656 | |||
| 657 | } |
||
| 658 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state