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.1.0 |
||
| 17 | * @access private |
||
| 18 | * @var |
||
| 19 | */ |
||
| 20 | static private $instance; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Comment Types. |
||
| 24 | * |
||
| 25 | * @since 2.1.0 |
||
| 26 | * @access private |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $comment_types; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Singleton pattern. |
||
| 33 | * |
||
| 34 | * @since 2.1.0 |
||
| 35 | * @access private |
||
| 36 | */ |
||
| 37 | private function __construct() { |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * Get instance. |
||
| 43 | * |
||
| 44 | * @since 2.1.0 |
||
| 45 | * @access pu |
||
| 46 | * @return Give_Comment |
||
| 47 | */ |
||
| 48 | public static function get_instance() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Initialize |
||
| 59 | * |
||
| 60 | * @since 2.1.0 |
||
| 61 | * @access private |
||
| 62 | */ |
||
| 63 | private function init() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Insert comment |
||
| 83 | * |
||
| 84 | * @since 2.1.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 |
||
| 93 | */ |
||
| 94 | public static function add( $id, $note, $comment_type, $comment_args = array() ) { |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * Delete comment |
||
| 150 | * |
||
| 151 | * @since 2.1.0 |
||
| 152 | * @access public |
||
| 153 | * |
||
| 154 | * @param int $comment_id The comment ID to delete. |
||
| 155 | * @param int $id The payment|Donor ID the note is connected to. |
||
| 156 | * @param string $comment_type Value can ve donor|payment. |
||
| 157 | * |
||
| 158 | * @since 1.0 |
||
| 159 | * |
||
| 160 | * @return bool True on success, false otherwise. |
||
| 161 | */ |
||
| 162 | public static function delete( $comment_id, $id, $comment_type ) { |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * Get comments |
||
| 199 | * |
||
| 200 | * @since 2.1.0 |
||
| 201 | * @access public |
||
| 202 | * |
||
| 203 | * @param int $id |
||
| 204 | * @param string $search |
||
| 205 | * @param string $comment_type |
||
| 206 | * @param array $comment_args |
||
| 207 | * |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | public static function get( $id, $search = '', $comment_type, $comment_args = array() ) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Exclude comments from showing in Recent |
||
| 275 | * Comments widgets |
||
| 276 | * |
||
| 277 | * @since 2.1.0 |
||
| 278 | * @access public |
||
| 279 | * |
||
| 280 | * @param object $query WordPress Comment Query Object. |
||
| 281 | * |
||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | public function hide_comments( $query ) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Exclude notes (comments) from showing in Recent Comments widgets |
||
| 299 | * |
||
| 300 | * @since 2.1.0 |
||
| 301 | * @access public |
||
| 302 | * |
||
| 303 | * @param array $clauses Comment clauses for comment query. |
||
| 304 | * |
||
| 305 | * @return array $clauses Updated comment clauses. |
||
| 306 | */ |
||
| 307 | public function hide_comments_pre_wp_41( $clauses ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Exclude notes (comments) from showing in comment feeds |
||
| 319 | * |
||
| 320 | * @since 2.1.0 |
||
| 321 | * @access public |
||
| 322 | * |
||
| 323 | * @param string $where |
||
| 324 | * |
||
| 325 | * @return string $where |
||
| 326 | */ |
||
| 327 | public function hide_comments_from_feeds( $where ) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Remove Give Comments from the wp_count_comments function |
||
| 337 | * |
||
| 338 | * @since 2.1.0 |
||
| 339 | * @access public |
||
| 340 | * |
||
| 341 | * @param array $stats (empty from core filter). |
||
| 342 | * @param int $post_id Post ID. |
||
| 343 | * |
||
| 344 | * @return array|object Array of comment counts. |
||
| 345 | */ |
||
| 346 | public function remove_comments_from_comment_counts( $stats, $post_id ) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get donor name |
||
| 416 | * |
||
| 417 | * @since 2.1.0 |
||
| 418 | * @access public |
||
| 419 | * |
||
| 420 | * @param string $author |
||
| 421 | * @param int $comment_id |
||
| 422 | * @param WP_Comment $comment |
||
| 423 | * |
||
| 424 | * @return mixed |
||
| 425 | */ |
||
| 426 | public function __get_comment_author( $author, $comment_id, $comment ) { |
||
| 438 | |||
| 439 | |||
| 440 | /** |
||
| 441 | * Get comment types |
||
| 442 | * |
||
| 443 | * @since 2.1.0 |
||
| 444 | * @access public |
||
| 445 | * |
||
| 446 | * @param array @comment_types |
||
| 447 | * |
||
| 448 | * @return array |
||
| 449 | */ |
||
| 450 | public static function get_comment_types( $comment_types ) { |
||
| 458 | } |
||
| 459 |