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 entity post type name. |
||
| 30 | * |
||
| 31 | * @since 3.1.0 |
||
| 32 | */ |
||
| 33 | const TYPE_NAME = 'entity'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The alternative label meta key. |
||
| 37 | * |
||
| 38 | * @since 3.2.0 |
||
| 39 | */ |
||
| 40 | const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The alternative label input template. |
||
| 44 | * |
||
| 45 | * @since 3.2.0 |
||
| 46 | */ |
||
| 47 | // TODO: this should be moved to a class that deals with HTML code. |
||
| 48 | const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
||
| 49 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
||
| 50 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
||
| 51 | <button class="button wl-delete-button">%s</button> |
||
| 52 | </div>'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * A singleton instance of the Entity service. |
||
| 56 | * |
||
| 57 | * @since 3.2.0 |
||
| 58 | * @access private |
||
| 59 | * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
||
| 60 | */ |
||
| 61 | private static $instance; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Create a Wordlift_Entity_Service instance. |
||
| 65 | * |
||
| 66 | * @since 3.2.0 |
||
| 67 | * |
||
| 68 | * @param \Wordlift_UI_Service $ui_service The UI service. |
||
| 69 | */ |
||
| 70 | public function __construct( $ui_service ) { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get the singleton instance of the Entity service. |
||
| 84 | * |
||
| 85 | * @since 3.2.0 |
||
| 86 | * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
||
| 87 | */ |
||
| 88 | public static function get_instance() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Determines whether a post is an entity or not. |
||
| 95 | * |
||
| 96 | * @since 3.1.0 |
||
| 97 | * |
||
| 98 | * @param int $post_id A post id. |
||
| 99 | * |
||
| 100 | * @return bool Return true if the post is an entity otherwise false. |
||
| 101 | */ |
||
| 102 | public function is_entity( $post_id ) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get the proper classification scope for a given entity post |
||
| 109 | * |
||
| 110 | * @since 3.5.0 |
||
| 111 | * |
||
| 112 | * @param integer $post_id An entity post id. |
||
| 113 | * |
||
| 114 | * @return string Returns an uri. |
||
| 115 | */ |
||
| 116 | public function get_classification_scope_for( $post_id ) { |
||
| 136 | |||
| 137 | |||
| 138 | public function is_used( $post_id ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Determines whether a given uri is an internal uri or not. |
||
| 183 | * |
||
| 184 | * @since 3.3.2 |
||
| 185 | * |
||
| 186 | * @param int $uri An uri. |
||
| 187 | * |
||
| 188 | * @return true if the uri internal to the current dataset otherwise false. |
||
| 189 | */ |
||
| 190 | public function is_internal_uri( $uri ) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
||
| 197 | * |
||
| 198 | * @since 3.2.0 |
||
| 199 | * |
||
| 200 | * @param string $uri The entity URI. |
||
| 201 | * |
||
| 202 | * @return WP_Post|null A WP_Post instance or null if not found. |
||
| 203 | */ |
||
| 204 | public function get_entity_post_by_uri( $uri ) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
||
| 253 | * we check that the post we're saving is the current post. |
||
| 254 | * |
||
| 255 | * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
||
| 256 | * |
||
| 257 | * @since 3.2.0 |
||
| 258 | * |
||
| 259 | * @param int $post_id Post ID. |
||
| 260 | * @param WP_Post $post Post object. |
||
| 261 | * @param bool $update Whether this is an existing post being updated or not. |
||
| 262 | */ |
||
| 263 | public function save_post( $post_id, $post, $update ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Set the alternative labels. |
||
| 285 | * |
||
| 286 | * @since 3.2.0 |
||
| 287 | * |
||
| 288 | * @param int $post_id The post id. |
||
| 289 | * @param array $alt_labels An array of labels. |
||
| 290 | */ |
||
| 291 | public function set_alternative_labels( $post_id, $alt_labels ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Retrieve the alternate labels. |
||
| 314 | * |
||
| 315 | * @since 3.2.0 |
||
| 316 | * |
||
| 317 | * @param int $post_id Post id. |
||
| 318 | * |
||
| 319 | * @return mixed An array of alternative labels. |
||
| 320 | */ |
||
| 321 | public function get_alternative_labels( $post_id ) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
||
| 328 | * |
||
| 329 | * @since 3.2.0 |
||
| 330 | * |
||
| 331 | * @param WP_Post $post Post object. |
||
| 332 | */ |
||
| 333 | public function edit_form_before_permalink( $post ) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get the URI for the entity with the specified post id. |
||
| 357 | * |
||
| 358 | * @since 3.6.0 |
||
| 359 | * |
||
| 360 | * @param int $post_id The entity post id. |
||
| 361 | * |
||
| 362 | * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
||
| 363 | */ |
||
| 364 | public function get_uri( $post_id ) { |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * Get the alternative label input HTML code. |
||
| 391 | * |
||
| 392 | * @since 3.2.0 |
||
| 393 | * |
||
| 394 | * @param string $value The input value. |
||
| 395 | * |
||
| 396 | * @return string The input HTML code. |
||
| 397 | */ |
||
| 398 | private function get_alternative_label_input( $value = '' ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get the number of entity posts published in this blog. |
||
| 405 | * |
||
| 406 | * @since 3.6.0 |
||
| 407 | * |
||
| 408 | * @return int The number of published entity posts. |
||
| 409 | */ |
||
| 410 | public function count() { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Create a new entity. |
||
| 419 | * |
||
| 420 | * @since 3.9.0 |
||
| 421 | * |
||
| 422 | * @param string $name The entity name. |
||
| 423 | * @param string $type_uri The entity's type URI. |
||
| 424 | * @param null $logo The entity logo id (or NULL if none). |
||
| 425 | * @param string $status The post status, by default 'publish'. |
||
| 426 | * |
||
| 427 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
||
| 428 | */ |
||
| 429 | public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Get the entities related to the one with the specified id. By default only |
||
| 457 | * published entities will be returned. |
||
| 458 | * |
||
| 459 | * @since 3.10.0 |
||
| 460 | * |
||
| 461 | * @param int $id The post id. |
||
| 462 | * @param string $post_status The target post status (default = publish). |
||
| 463 | * |
||
| 464 | * @return array An array of post ids. |
||
| 465 | */ |
||
| 466 | public function get_related_entities( $id, $post_status = 'publish' ) { |
||
| 470 | |||
| 471 | } |
||
| 472 |
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.