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 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 Comment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class Comment extends Basis { |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Comment ID |
||
| 8 | * @var int |
||
| 9 | */ |
||
| 10 | public $comment_ID; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Comment post ID |
||
| 14 | * @var int |
||
| 15 | */ |
||
| 16 | public $comment_post_ID; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Comment Author name |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | public $comment_author; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Comment author email |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | public $comment_author_email; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Comment author link |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | public $comment_author_url; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Comment |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | public $comment_content; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Comment approved |
||
| 44 | * @var boolean |
||
| 45 | */ |
||
| 46 | public $comment_approved; |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * Comment date |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | public $comment_date; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * User Id |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | public $user_id; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Comment nested Level |
||
| 63 | * @var int |
||
| 64 | */ |
||
| 65 | public $level; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Comment Parent Id |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | public $comment_parent; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Child comments |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $children = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Checks if provided arg is instance of WP_Comment and inits it |
||
| 81 | * |
||
| 82 | * @param \WP_Comment $item |
||
| 83 | */ |
||
| 84 | public function __construct( $item ) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Returns User object of comment author |
||
| 96 | * |
||
| 97 | * @return object User |
||
| 98 | */ |
||
| 99 | public function author() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns comment content |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function content() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Return true if comment is approved |
||
| 131 | * |
||
| 132 | * @return boolean |
||
| 133 | */ |
||
| 134 | public function approved() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns comment date |
||
| 140 | * |
||
| 141 | * @param string $date_format |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function date( $date_format = '' ) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns true if comment has parent |
||
| 153 | * |
||
| 154 | * @return boolean |
||
| 155 | */ |
||
| 156 | public function has_parent() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Shows gravatar |
||
| 162 | * |
||
| 163 | * @param integer $size avatar image size |
||
| 164 | * @param string $default |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function avatar( $size = 92, $default = '' ) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns email address that will be used for generating avatar |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | protected function get_avatar_email() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Returns default avatar url |
||
| 217 | * |
||
| 218 | * @param string $default |
||
| 219 | * @param string $email |
||
| 220 | * @param int $size |
||
| 221 | * @param string $host |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | protected function get_default_avatar( $default, $email, $size, $host ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns gravatar host |
||
| 258 | * |
||
| 259 | * @param string $email_hash |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | protected function get_avatar_host( $email_hash ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Returns avatar url |
||
| 280 | * |
||
| 281 | * @param string $default |
||
| 282 | * @param string $host |
||
| 283 | * @param string $email_hash |
||
| 284 | * @param int $size |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | protected function get_avatar_url( $default, $host, $email_hash, $size ) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Adds child to current Comment |
||
| 302 | * |
||
| 303 | * @param Comment $comment |
||
| 304 | */ |
||
| 305 | public function add_child( $comment ) { |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Updates children nesting level param |
||
| 319 | * |
||
| 320 | * @return boolean |
||
| 321 | */ |
||
| 322 | View Code Duplication | protected function update_child_levels() { |
|
| 338 | } |
||
| 339 |