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 ClassyComment 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 ClassyComment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class ClassyComment extends ClassyBasis { |
||
|
|
|||
| 4 | |||
| 5 | /** |
||
| 6 | * Comment ID |
||
| 7 | * @var int |
||
| 8 | */ |
||
| 9 | public $comment_ID; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Comment post ID |
||
| 13 | * @var int |
||
| 14 | */ |
||
| 15 | public $comment_post_ID; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Comment Author name |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | public $comment_author; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Comment author email |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $comment_author_email; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Comment author link |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | public $comment_author_url; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Comment |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | public $comment_content; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Child comments |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $children = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Checks if provided arg is instance of WP_Comment and inits it |
||
| 49 | * |
||
| 50 | * @param WP_Comment $item |
||
| 51 | */ |
||
| 52 | public function __construct($item) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Returns ClassyUser object of comment author |
||
| 64 | * |
||
| 65 | * @return object ClassyUser |
||
| 66 | */ |
||
| 67 | public function author() { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns comment content |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public function content() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Return true if comment is approved |
||
| 100 | * |
||
| 101 | * @return boolean |
||
| 102 | */ |
||
| 103 | public function approved() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Returns comment date |
||
| 109 | * |
||
| 110 | * @param string $date_format |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function date( $date_format = '' ) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns true if comment has parent |
||
| 122 | * |
||
| 123 | * @return boolean |
||
| 124 | */ |
||
| 125 | public function has_parent() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Shows gravatar |
||
| 131 | * |
||
| 132 | * @param integer $size avatar image size |
||
| 133 | * @param string $default |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function avatar($size = 92, $default = '') { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns email address that will be used for generating avatar |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | protected function get_avatar_email() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns default avatar url |
||
| 187 | * |
||
| 188 | * @param string $default |
||
| 189 | * @param string $email |
||
| 190 | * @param int $size |
||
| 191 | * @param string $host |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | protected function get_default_avatar($default, $email, $size, $host) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns gravatar host |
||
| 228 | * |
||
| 229 | * @param string $email_hash |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | protected function get_avatar_host($email_hash) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Returns avatar url |
||
| 250 | * |
||
| 251 | * @param string $default |
||
| 252 | * @param string $host |
||
| 253 | * @param string $email_hash |
||
| 254 | * @param int $size |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | protected function get_avatar_url($default, $host, $email_hash, $size) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Adds child to current ClassyComment |
||
| 272 | * |
||
| 273 | * @param ClassyComment $comment |
||
| 274 | */ |
||
| 275 | public function add_child($comment) { |
||
| 285 | |||
| 286 | |||
| 287 | /** |
||
| 288 | * Updates children nesting level param |
||
| 289 | * |
||
| 290 | * @return boolean |
||
| 291 | */ |
||
| 292 | View Code Duplication | protected function update_child_levels() { |
|
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.