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 {@link Wordlift_Entity_Uri_Service} instance. |
||
| 39 | * |
||
| 40 | * @since 3.16.3 |
||
| 41 | * @access private |
||
| 42 | * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
||
| 43 | */ |
||
| 44 | private $entity_uri_service; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The entity post type name. |
||
| 48 | * |
||
| 49 | * @since 3.1.0 |
||
| 50 | */ |
||
| 51 | const TYPE_NAME = 'entity'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The alternative label meta key. |
||
| 55 | * |
||
| 56 | * @since 3.2.0 |
||
| 57 | */ |
||
| 58 | const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The alternative label input template. |
||
| 62 | * |
||
| 63 | * @since 3.2.0 |
||
| 64 | */ |
||
| 65 | // TODO: this should be moved to a class that deals with HTML code. |
||
| 66 | const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
||
| 67 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
||
| 68 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
||
| 69 | <button class="button wl-delete-button">%s</button> |
||
| 70 | </div>'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * A singleton instance of the Entity service. |
||
| 74 | * |
||
| 75 | * @since 3.2.0 |
||
| 76 | * @access private |
||
| 77 | * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
||
| 78 | */ |
||
| 79 | private static $instance; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Create a Wordlift_Entity_Service instance. |
||
| 83 | * |
||
| 84 | * @since 3.2.0 |
||
| 85 | * |
||
| 86 | * @param \Wordlift_UI_Service $ui_service The UI service. |
||
| 87 | * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
||
| 88 | * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
||
| 89 | */ |
||
| 90 | View Code Duplication | public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Get the singleton instance of the Entity service. |
||
| 104 | * |
||
| 105 | * @since 3.2.0 |
||
| 106 | * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
||
| 107 | */ |
||
| 108 | public static function get_instance() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Determines whether a post is an entity or not. Entity is in this context |
||
| 115 | * something which is not an article. |
||
| 116 | * |
||
| 117 | * @since 3.1.0 |
||
| 118 | * |
||
| 119 | * @param int $post_id A post id. |
||
| 120 | * |
||
| 121 | * @return bool Return true if the post is an entity otherwise false. |
||
| 122 | */ |
||
| 123 | public function is_entity( $post_id ) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get the proper classification scope for a given entity post |
||
| 141 | * |
||
| 142 | * @since 3.5.0 |
||
| 143 | * |
||
| 144 | * @param integer $post_id An entity post id. |
||
| 145 | * |
||
| 146 | * @param string $default The default classification scope, `what` if not |
||
| 147 | * provided. |
||
| 148 | * |
||
| 149 | * @return string Returns a classification scope (e.g. 'what'). |
||
| 150 | */ |
||
| 151 | public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Check whether a {@link WP_Post} is used. |
||
| 173 | * |
||
| 174 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 175 | * |
||
| 176 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
||
| 177 | */ |
||
| 178 | public function is_used( $post_id ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
||
| 223 | * |
||
| 224 | * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
||
| 225 | * @since 3.2.0 |
||
| 226 | * |
||
| 227 | * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
||
| 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 ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
||
| 240 | * we check that the post we're saving is the current post. |
||
| 241 | * |
||
| 242 | * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
||
| 243 | * |
||
| 244 | * @since 3.2.0 |
||
| 245 | * |
||
| 246 | * @param int $post_id Post ID. |
||
| 247 | * @param WP_Post $post Post object. |
||
| 248 | * @param bool $update Whether this is an existing post being updated or not. |
||
| 249 | */ |
||
| 250 | public function save_post( $post_id, $post, $update ) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Set the alternative labels. |
||
| 277 | * |
||
| 278 | * @since 3.2.0 |
||
| 279 | * |
||
| 280 | * @param int $post_id The post id. |
||
| 281 | * @param array $alt_labels An array of labels. |
||
| 282 | */ |
||
| 283 | public function set_alternative_labels( $post_id, $alt_labels ) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Retrieve the alternate labels. |
||
| 306 | * |
||
| 307 | * @since 3.2.0 |
||
| 308 | * |
||
| 309 | * @param int $post_id Post id. |
||
| 310 | * |
||
| 311 | * @return mixed An array of alternative labels. |
||
| 312 | */ |
||
| 313 | public function get_alternative_labels( $post_id ) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Retrieve the labels for an entity, i.e. the title + the synonyms. |
||
| 320 | * |
||
| 321 | * @since 3.12.0 |
||
| 322 | * |
||
| 323 | * @param int $post_id The entity {@link WP_Post} id. |
||
| 324 | * |
||
| 325 | * @return array An array with the entity title and labels. |
||
| 326 | */ |
||
| 327 | public function get_labels( $post_id ) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
||
| 334 | * |
||
| 335 | * @since 3.2.0 |
||
| 336 | * |
||
| 337 | * @param WP_Post $post Post object. |
||
| 338 | */ |
||
| 339 | public function edit_form_before_permalink( $post ) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get the URI for the entity with the specified post id. |
||
| 363 | * |
||
| 364 | * @since 3.6.0 |
||
| 365 | * |
||
| 366 | * @param int $post_id The entity post id. |
||
| 367 | * |
||
| 368 | * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
||
| 369 | */ |
||
| 370 | public function get_uri( $post_id ) { |
||
| 393 | |||
| 394 | |||
| 395 | /** |
||
| 396 | * Get the alternative label input HTML code. |
||
| 397 | * |
||
| 398 | * @since 3.2.0 |
||
| 399 | * |
||
| 400 | * @param string $value The input value. |
||
| 401 | * |
||
| 402 | * @return string The input HTML code. |
||
| 403 | */ |
||
| 404 | private function get_alternative_label_input( $value = '' ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get the number of entity posts published in this blog. |
||
| 411 | * |
||
| 412 | * @since 3.6.0 |
||
| 413 | * |
||
| 414 | * @return int The number of published entity posts. |
||
| 415 | */ |
||
| 416 | public function count() { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Add the entity filtering criterias to the arguments for a `get_posts` |
||
| 428 | * call. |
||
| 429 | * |
||
| 430 | * @since 3.15.0 |
||
| 431 | * |
||
| 432 | * @param array $args The arguments for a `get_posts` call. |
||
| 433 | * |
||
| 434 | * @return array The arguments for a `get_posts` call. |
||
| 435 | */ |
||
| 436 | View Code Duplication | public static function add_criterias( $args ) { |
|
| 465 | |||
| 466 | // /** |
||
| 467 | // * Get the entity terms IDs which represent an entity. |
||
| 468 | // * |
||
| 469 | // * @since 3.17.0 deprecated. |
||
| 470 | // * @since 3.15.0 |
||
| 471 | // * |
||
| 472 | // * @deprecated |
||
| 473 | // * @return array An array of terms' ids. |
||
| 474 | // */ |
||
| 475 | // public static function get_entity_terms() { |
||
| 476 | // |
||
| 477 | // $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
||
| 478 | // 'hide_empty' => false, |
||
| 479 | // // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
||
| 480 | // // An issue has been opened with the AAM plugin author as well. |
||
| 481 | // // |
||
| 482 | // // see https://github.com/insideout10/wordlift-plugin/issues/334 |
||
| 483 | // // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
||
| 484 | // 'fields' => 'all', |
||
| 485 | // ) ); |
||
| 486 | // |
||
| 487 | // return array_map( function ( $term ) { |
||
| 488 | // return $term->term_id; |
||
| 489 | // }, array_filter( $terms, function ( $term ) { |
||
| 490 | // return 'article' !== $term->slug; |
||
| 491 | // } ) ); |
||
| 492 | // } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Create a new entity. |
||
| 496 | * |
||
| 497 | * @since 3.9.0 |
||
| 498 | * |
||
| 499 | * @param string $name The entity name. |
||
| 500 | * @param string $type_uri The entity's type URI. |
||
| 501 | * @param null $logo The entity logo id (or NULL if none). |
||
| 502 | * @param string $status The post status, by default 'publish'. |
||
| 503 | * |
||
| 504 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
||
| 505 | */ |
||
| 506 | public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Get the entities related to the one with the specified id. By default only |
||
| 534 | * published entities will be returned. |
||
| 535 | * |
||
| 536 | * @since 3.10.0 |
||
| 537 | * |
||
| 538 | * @param int $id The post id. |
||
| 539 | * @param string $post_status The target post status (default = publish). |
||
| 540 | * |
||
| 541 | * @return array An array of post ids. |
||
| 542 | */ |
||
| 543 | public function get_related_entities( $id, $post_status = 'publish' ) { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Get the list of entities. |
||
| 550 | * |
||
| 551 | * @since 3.12.2 |
||
| 552 | * |
||
| 553 | * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
||
| 554 | * |
||
| 555 | * @return array An array of entity posts. |
||
| 556 | */ |
||
| 557 | public function get( $params = array() ) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * The list of post type names which can be used for entities |
||
| 571 | * |
||
| 572 | * Criteria is that the post type is public. The list of valid post types |
||
| 573 | * can be overridden with a filter. |
||
| 574 | * |
||
| 575 | * @since 3.15.0 |
||
| 576 | * |
||
| 577 | * @return array Array containing the names of the valid post types. |
||
| 578 | */ |
||
| 579 | static function valid_entity_post_types() { |
||
| 586 | |||
| 587 | } |
||
| 588 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.