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 Give_Comment 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 Give_Comment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Give_Comment { |
||
|
|
|||
| 13 | /** |
||
| 14 | * Instance. |
||
| 15 | * |
||
| 16 | * @since 2.2.0 |
||
| 17 | * @access private |
||
| 18 | * @var |
||
| 19 | */ |
||
| 20 | static private $instance; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Comment Types. |
||
| 24 | * |
||
| 25 | * @since 2.2.0 |
||
| 26 | * @access private |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $comment_types; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Comment Table. |
||
| 33 | * |
||
| 34 | * @since 2.3.0 |
||
| 35 | * @access public |
||
| 36 | * @var Give_DB_Comments |
||
| 37 | */ |
||
| 38 | public $db; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Comment Meta Table. |
||
| 42 | * |
||
| 43 | * @since 2.3.0 |
||
| 44 | * @access public |
||
| 45 | * @var Give_DB_Comment_Meta |
||
| 46 | */ |
||
| 47 | public $db_meta; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Singleton pattern. |
||
| 51 | * |
||
| 52 | * @since 2.2.0 |
||
| 53 | * @access private |
||
| 54 | */ |
||
| 55 | private function __construct() { |
||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * Get instance. |
||
| 61 | * |
||
| 62 | * @since 2.2.0 |
||
| 63 | * @access pu |
||
| 64 | * @return Give_Comment |
||
| 65 | */ |
||
| 66 | public static function get_instance() { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Initialize |
||
| 77 | * |
||
| 78 | * @since 2.2.0 |
||
| 79 | * @access private |
||
| 80 | */ |
||
| 81 | private function init() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Insert/Update comment |
||
| 104 | * |
||
| 105 | * @since 2.2.0 |
||
| 106 | * @access public |
||
| 107 | * |
||
| 108 | * @param int $id Payment|Donor ID. |
||
| 109 | * @param string $note Comment Text |
||
| 110 | * @param string $comment_type Value can ve donor|payment |
||
| 111 | * @param array $comment_args Comment arguments |
||
| 112 | * |
||
| 113 | * @return int|WP_Error |
||
| 114 | */ |
||
| 115 | public static function add( $id, $note, $comment_type, $comment_args = array() ) { |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * Delete comment |
||
| 201 | * |
||
| 202 | * @since 2.2.0 |
||
| 203 | * @access public |
||
| 204 | * |
||
| 205 | * @param int $comment_id The comment ID to delete. |
||
| 206 | * @param int $id The payment|Donor ID the note is connected to. |
||
| 207 | * @param string $comment_type Value can ve donor|payment. |
||
| 208 | * |
||
| 209 | * @since 1.0 |
||
| 210 | * |
||
| 211 | * @return bool True on success, false otherwise. |
||
| 212 | */ |
||
| 213 | public static function delete( $comment_id, $id, $comment_type ) { |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * Get comments |
||
| 254 | * |
||
| 255 | * @since 2.2.0 |
||
| 256 | * @access public |
||
| 257 | * |
||
| 258 | * @param int $id |
||
| 259 | * @param string $comment_type |
||
| 260 | * @param array $comment_args |
||
| 261 | * @param string $search |
||
| 262 | * |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | public static function get( $id, $comment_type, $comment_args = array(), $search = '' ) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Exclude comments from showing in Recent |
||
| 336 | * Comments widgets |
||
| 337 | * |
||
| 338 | * @since 2.2.0 |
||
| 339 | * @access public |
||
| 340 | * |
||
| 341 | * @param object $query WordPress Comment Query Object. |
||
| 342 | * |
||
| 343 | * @return void |
||
| 344 | */ |
||
| 345 | public function hide_comments( $query ) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Exclude notes (comments) from showing in Recent Comments widgets |
||
| 360 | * |
||
| 361 | * @since 2.2.0 |
||
| 362 | * @access public |
||
| 363 | * |
||
| 364 | * @param array $clauses Comment clauses for comment query. |
||
| 365 | * |
||
| 366 | * @return array $clauses Updated comment clauses. |
||
| 367 | */ |
||
| 368 | public function hide_comments_pre_wp_41( $clauses ) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Exclude notes (comments) from showing in comment feeds |
||
| 380 | * |
||
| 381 | * @since 2.2.0 |
||
| 382 | * @access public |
||
| 383 | * |
||
| 384 | * @param string $where |
||
| 385 | * |
||
| 386 | * @return string $where |
||
| 387 | */ |
||
| 388 | public function hide_comments_from_feeds( $where ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Remove Give Comments from the wp_count_comments function |
||
| 400 | * |
||
| 401 | * @since 2.2.0 |
||
| 402 | * @access public |
||
| 403 | * |
||
| 404 | * @param array $stats (empty from core filter). |
||
| 405 | * @param int $post_id Post ID. |
||
| 406 | * |
||
| 407 | * @return array|object Array of comment counts. |
||
| 408 | */ |
||
| 409 | public function remove_comments_from_comment_counts( $stats, $post_id ) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get donor name |
||
| 479 | * |
||
| 480 | * @since 2.2.0 |
||
| 481 | * @access public |
||
| 482 | * |
||
| 483 | * @param string $author |
||
| 484 | * @param int $comment_id |
||
| 485 | * @param WP_Comment $comment |
||
| 486 | * |
||
| 487 | * @return mixed |
||
| 488 | */ |
||
| 489 | public function __get_comment_author( $author, $comment_id, $comment ) { |
||
| 501 | |||
| 502 | |||
| 503 | /** |
||
| 504 | * Get comment types |
||
| 505 | * |
||
| 506 | * @since 2.2.0 |
||
| 507 | * @access public |
||
| 508 | * |
||
| 509 | * @param array @comment_types |
||
| 510 | * |
||
| 511 | * @return array |
||
| 512 | */ |
||
| 513 | public static function get_comment_types( $comment_types ) { |
||
| 521 | } |
||
| 522 |