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 | View Code Duplication | public static function get_instance() { |
|
| 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() ) { |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Delete comment |
||
| 170 | * |
||
| 171 | * @since 2.2.0 |
||
| 172 | * @access public |
||
| 173 | * |
||
| 174 | * @param int $comment_id The comment ID to delete. |
||
| 175 | * @param int $id The payment|Donor ID the note is connected to. |
||
| 176 | * @param string $comment_type Value can ve donor|payment. |
||
| 177 | * |
||
| 178 | * @since 1.0 |
||
| 179 | * |
||
| 180 | * @return bool True on success, false otherwise. |
||
| 181 | */ |
||
| 182 | public static function delete( $comment_id, $id, $comment_type ) { |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * Get comments |
||
| 219 | * |
||
| 220 | * @since 2.2.0 |
||
| 221 | * @access public |
||
| 222 | * |
||
| 223 | * @param int $id |
||
| 224 | * @param string $comment_type |
||
| 225 | * @param array $comment_args |
||
| 226 | * @param string $search |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public static function get( $id, $comment_type, $comment_args = array(), $search = '' ) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Exclude comments from showing in Recent |
||
| 297 | * Comments widgets |
||
| 298 | * |
||
| 299 | * @since 2.2.0 |
||
| 300 | * @access public |
||
| 301 | * |
||
| 302 | * @param object $query WordPress Comment Query Object. |
||
| 303 | * |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | public function hide_comments( $query ) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Exclude notes (comments) from showing in Recent Comments widgets |
||
| 321 | * |
||
| 322 | * @since 2.2.0 |
||
| 323 | * @access public |
||
| 324 | * |
||
| 325 | * @param array $clauses Comment clauses for comment query. |
||
| 326 | * |
||
| 327 | * @return array $clauses Updated comment clauses. |
||
| 328 | */ |
||
| 329 | public function hide_comments_pre_wp_41( $clauses ) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Exclude notes (comments) from showing in comment feeds |
||
| 341 | * |
||
| 342 | * @since 2.2.0 |
||
| 343 | * @access public |
||
| 344 | * |
||
| 345 | * @param string $where |
||
| 346 | * |
||
| 347 | * @return string $where |
||
| 348 | */ |
||
| 349 | public function hide_comments_from_feeds( $where ) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Remove Give Comments from the wp_count_comments function |
||
| 361 | * |
||
| 362 | * @since 2.2.0 |
||
| 363 | * @access public |
||
| 364 | * |
||
| 365 | * @param array $stats (empty from core filter). |
||
| 366 | * @param int $post_id Post ID. |
||
| 367 | * |
||
| 368 | * @return array|object Array of comment counts. |
||
| 369 | */ |
||
| 370 | public function remove_comments_from_comment_counts( $stats, $post_id ) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Get donor name |
||
| 440 | * |
||
| 441 | * @since 2.2.0 |
||
| 442 | * @access public |
||
| 443 | * |
||
| 444 | * @param string $author |
||
| 445 | * @param int $comment_id |
||
| 446 | * @param WP_Comment $comment |
||
| 447 | * |
||
| 448 | * @return mixed |
||
| 449 | */ |
||
| 450 | public function __get_comment_author( $author, $comment_id, $comment ) { |
||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * Get comment types |
||
| 466 | * |
||
| 467 | * @since 2.2.0 |
||
| 468 | * @access public |
||
| 469 | * |
||
| 470 | * @param array @comment_types |
||
| 471 | * |
||
| 472 | * @return array |
||
| 473 | */ |
||
| 474 | public static function get_comment_types( $comment_types ) { |
||
| 482 | } |
||
| 483 |