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() ) { |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * Delete comment |
||
| 162 | * |
||
| 163 | * @since 2.2.0 |
||
| 164 | * @access public |
||
| 165 | * |
||
| 166 | * @param int $comment_id The comment ID to delete. |
||
| 167 | * @param int $id The payment|Donor ID the note is connected to. |
||
| 168 | * @param string $comment_type Value can ve donor|payment. |
||
| 169 | * |
||
| 170 | * @since 1.0 |
||
| 171 | * |
||
| 172 | * @return bool True on success, false otherwise. |
||
| 173 | */ |
||
| 174 | public static function delete( $comment_id, $id, $comment_type ) { |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Get comments |
||
| 211 | * |
||
| 212 | * @since 2.2.0 |
||
| 213 | * @access public |
||
| 214 | * |
||
| 215 | * @param int $id |
||
| 216 | * @param string $comment_type |
||
| 217 | * @param array $comment_args |
||
| 218 | * @param string $search |
||
| 219 | * |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | public static function get( $id, $comment_type, $comment_args = array(), $search = '' ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Exclude comments from showing in Recent |
||
| 289 | * Comments widgets |
||
| 290 | * |
||
| 291 | * @since 2.2.0 |
||
| 292 | * @access public |
||
| 293 | * |
||
| 294 | * @param object $query WordPress Comment Query Object. |
||
| 295 | * |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | public function hide_comments( $query ) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Exclude notes (comments) from showing in Recent Comments widgets |
||
| 313 | * |
||
| 314 | * @since 2.2.0 |
||
| 315 | * @access public |
||
| 316 | * |
||
| 317 | * @param array $clauses Comment clauses for comment query. |
||
| 318 | * |
||
| 319 | * @return array $clauses Updated comment clauses. |
||
| 320 | */ |
||
| 321 | public function hide_comments_pre_wp_41( $clauses ) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Exclude notes (comments) from showing in comment feeds |
||
| 333 | * |
||
| 334 | * @since 2.2.0 |
||
| 335 | * @access public |
||
| 336 | * |
||
| 337 | * @param string $where |
||
| 338 | * |
||
| 339 | * @return string $where |
||
| 340 | */ |
||
| 341 | public function hide_comments_from_feeds( $where ) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Remove Give Comments from the wp_count_comments function |
||
| 353 | * |
||
| 354 | * @since 2.2.0 |
||
| 355 | * @access public |
||
| 356 | * |
||
| 357 | * @param array $stats (empty from core filter). |
||
| 358 | * @param int $post_id Post ID. |
||
| 359 | * |
||
| 360 | * @return array|object Array of comment counts. |
||
| 361 | */ |
||
| 362 | public function remove_comments_from_comment_counts( $stats, $post_id ) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Get donor name |
||
| 432 | * |
||
| 433 | * @since 2.2.0 |
||
| 434 | * @access public |
||
| 435 | * |
||
| 436 | * @param string $author |
||
| 437 | * @param int $comment_id |
||
| 438 | * @param WP_Comment $comment |
||
| 439 | * |
||
| 440 | * @return mixed |
||
| 441 | */ |
||
| 442 | public function __get_comment_author( $author, $comment_id, $comment ) { |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * Get comment types |
||
| 458 | * |
||
| 459 | * @since 2.2.0 |
||
| 460 | * @access public |
||
| 461 | * |
||
| 462 | * @param array @comment_types |
||
| 463 | * |
||
| 464 | * @return array |
||
| 465 | */ |
||
| 466 | public static function get_comment_types( $comment_types ) { |
||
| 474 | } |
||
| 475 |