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 | * Singleton pattern. |
||
| 33 | * |
||
| 34 | * @since 2.2.0 |
||
| 35 | * @access private |
||
| 36 | */ |
||
| 37 | private function __construct() { |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * Get instance. |
||
| 43 | * |
||
| 44 | * @since 2.2.0 |
||
| 45 | * @access pu |
||
| 46 | * @return Give_Comment |
||
| 47 | */ |
||
| 48 | public static function get_instance() { |
||
| 49 | if ( null === static::$instance ) { |
||
| 50 | self::$instance = new static(); |
||
| 51 | self::$instance->init(); |
||
| 52 | } |
||
| 53 | |||
| 54 | return self::$instance; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Initialize |
||
| 59 | * |
||
| 60 | * @since 2.2.0 |
||
| 61 | * @access private |
||
| 62 | */ |
||
| 63 | private function init() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Insert/Update comment |
||
| 83 | * |
||
| 84 | * @since 2.2.0 |
||
| 85 | * @access public |
||
| 86 | * |
||
| 87 | * @param int $id Payment|Donor ID. |
||
| 88 | * @param string $note Comment Text |
||
| 89 | * @param string $comment_type Value can ve donor|payment |
||
| 90 | * @param array $comment_args Comment arguments |
||
| 91 | * |
||
| 92 | * @return int|WP_Error |
||
| 93 | */ |
||
| 94 | public static function add( $id, $note, $comment_type, $comment_args = array() ) { |
||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * Delete comment |
||
| 176 | * |
||
| 177 | * @since 2.2.0 |
||
| 178 | * @access public |
||
| 179 | * |
||
| 180 | * @param int $comment_id The comment ID to delete. |
||
| 181 | * @param int $id The payment|Donor ID the note is connected to. |
||
| 182 | * @param string $comment_type Value can ve donor|payment. |
||
| 183 | * |
||
| 184 | * @since 1.0 |
||
| 185 | * |
||
| 186 | * @return bool True on success, false otherwise. |
||
| 187 | */ |
||
| 188 | public static function delete( $comment_id, $id, $comment_type ) { |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Get comments |
||
| 225 | * |
||
| 226 | * @since 2.2.0 |
||
| 227 | * @access public |
||
| 228 | * |
||
| 229 | * @param int $id |
||
| 230 | * @param string $comment_type |
||
| 231 | * @param array $comment_args |
||
| 232 | * @param string $search |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | public static function get( $id, $comment_type, $comment_args = array(), $search = '' ) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Exclude comments from showing in Recent |
||
| 303 | * Comments widgets |
||
| 304 | * |
||
| 305 | * @since 2.2.0 |
||
| 306 | * @access public |
||
| 307 | * |
||
| 308 | * @param object $query WordPress Comment Query Object. |
||
| 309 | * |
||
| 310 | * @return void |
||
| 311 | */ |
||
| 312 | public function hide_comments( $query ) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Exclude notes (comments) from showing in Recent Comments widgets |
||
| 327 | * |
||
| 328 | * @since 2.2.0 |
||
| 329 | * @access public |
||
| 330 | * |
||
| 331 | * @param array $clauses Comment clauses for comment query. |
||
| 332 | * |
||
| 333 | * @return array $clauses Updated comment clauses. |
||
| 334 | */ |
||
| 335 | public function hide_comments_pre_wp_41( $clauses ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Exclude notes (comments) from showing in comment feeds |
||
| 347 | * |
||
| 348 | * @since 2.2.0 |
||
| 349 | * @access public |
||
| 350 | * |
||
| 351 | * @param string $where |
||
| 352 | * |
||
| 353 | * @return string $where |
||
| 354 | */ |
||
| 355 | public function hide_comments_from_feeds( $where ) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Remove Give Comments from the wp_count_comments function |
||
| 367 | * |
||
| 368 | * @since 2.2.0 |
||
| 369 | * @access public |
||
| 370 | * |
||
| 371 | * @param array $stats (empty from core filter). |
||
| 372 | * @param int $post_id Post ID. |
||
| 373 | * |
||
| 374 | * @return array|object Array of comment counts. |
||
| 375 | */ |
||
| 376 | public function remove_comments_from_comment_counts( $stats, $post_id ) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Get donor name |
||
| 446 | * |
||
| 447 | * @since 2.2.0 |
||
| 448 | * @access public |
||
| 449 | * |
||
| 450 | * @param string $author |
||
| 451 | * @param int $comment_id |
||
| 452 | * @param WP_Comment $comment |
||
| 453 | * |
||
| 454 | * @return mixed |
||
| 455 | */ |
||
| 456 | public function __get_comment_author( $author, $comment_id, $comment ) { |
||
| 468 | |||
| 469 | |||
| 470 | /** |
||
| 471 | * Get comment types |
||
| 472 | * |
||
| 473 | * @since 2.2.0 |
||
| 474 | * @access public |
||
| 475 | * |
||
| 476 | * @param array @comment_types |
||
| 477 | * |
||
| 478 | * @return array |
||
| 479 | */ |
||
| 480 | public static function get_comment_types( $comment_types ) { |
||
| 488 | } |
||
| 489 |