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 TimberComment 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 TimberComment, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 22 | class TimberComment extends TimberCore implements TimberCoreInterface { | 
            ||
| 23 | |||
| 24 | public $PostClass = 'TimberPost';  | 
            ||
| 25 | public $object_type = 'comment';  | 
            ||
| 26 | |||
| 27 | public static $representation = 'comment';  | 
            ||
| 28 | |||
| 29 | public $ID;  | 
            ||
| 30 | public $id;  | 
            ||
| 31 | public $comment_author_email;  | 
            ||
| 32 | public $comment_content;  | 
            ||
| 33 | public $comment_date;  | 
            ||
| 34 | public $comment_ID;  | 
            ||
| 35 | public $user_id;  | 
            ||
| 36 | public $comment_author;  | 
            ||
| 37 | |||
| 38 | public $children = array();  | 
            ||
| 39 | |||
| 40 | /**  | 
            ||
| 41 | * @param int $cid  | 
            ||
| 42 | */  | 
            ||
| 43 | 	function __construct($cid) { | 
            ||
| 46 | |||
| 47 | 	function __toString(){ | 
            ||
| 50 | |||
| 51 | /**  | 
            ||
| 52 | * @internal  | 
            ||
| 53 | * @param integer $cid  | 
            ||
| 54 | */  | 
            ||
| 55 | 	function init($cid) { | 
            ||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * @api  | 
            ||
| 69 | * @example  | 
            ||
| 70 | * ```twig  | 
            ||
| 71 | * <h3>Comments by...</h3>  | 
            ||
| 72 | * <ol>  | 
            ||
| 73 | 	 * {% for comment in post.comments %} | 
            ||
| 74 | 	 * 	<li>{{comment.author.name}}, who is a {{comment.author.role}}</li> | 
            ||
| 75 | 	 * {% endfor %} | 
            ||
| 76 | * </ol>  | 
            ||
| 77 | * ```  | 
            ||
| 78 | * ```html  | 
            ||
| 79 | * <h3>Comments by...</h3>  | 
            ||
| 80 | * <ol>  | 
            ||
| 81 | * <li>Jared Novack, who is a contributor</li>  | 
            ||
| 82 | * <li>Katie Ricci, who is a subscriber</li>  | 
            ||
| 83 | * <li>Rebecca Pearl, who is a author</li>  | 
            ||
| 84 | * </ol>  | 
            ||
| 85 | * ```  | 
            ||
| 86 | * @return TimberUser  | 
            ||
| 87 | */  | 
            ||
| 88 | 	public function author() { | 
            ||
| 101 | |||
| 102 | /**  | 
            ||
| 103 | * Fetches the Gravatar  | 
            ||
| 104 | * @api  | 
            ||
| 105 | * @example  | 
            ||
| 106 | * ```twig  | 
            ||
| 107 | 	 * <img src="{{comment.avatar(36,template_uri~"/img/dude.jpg")}}" alt="Image of {{comment.author.name}}" /> | 
            ||
| 108 | * ```  | 
            ||
| 109 | * ```html  | 
            ||
| 110 | * <img src="http://gravatar.com/i/sfsfsdfasdfsfa.jpg" alt="Image of Katherine Rich" />  | 
            ||
| 111 | * ```  | 
            ||
| 112 | * @param int $size  | 
            ||
| 113 | * @param string $default  | 
            ||
| 114 | * @return bool|mixed|string  | 
            ||
| 115 | */  | 
            ||
| 116 | 	public function avatar($size = 92, $default = '') { | 
            ||
| 138 | |||
| 139 | /**  | 
            ||
| 140 | * @api  | 
            ||
| 141 | * @return string  | 
            ||
| 142 | */  | 
            ||
| 143 | 	public function content() { | 
            ||
| 146 | |||
| 147 | /**  | 
            ||
| 148 | * @api  | 
            ||
| 149 | * @example  | 
            ||
| 150 | * ```twig  | 
            ||
| 151 | 	 * {% if comment.approved %} | 
            ||
| 152 | * Your comment is good  | 
            ||
| 153 | 	 * {% else %} | 
            ||
| 154 | * Do you kiss your mother with that mouth?  | 
            ||
| 155 | 	 * {% endif %} | 
            ||
| 156 | * ```  | 
            ||
| 157 | * @return boolean  | 
            ||
| 158 | */  | 
            ||
| 159 | 	public function approved() { | 
            ||
| 162 | |||
| 163 | /**  | 
            ||
| 164 | * @api  | 
            ||
| 165 | * @example  | 
            ||
| 166 | * ```twig  | 
            ||
| 167 | 	 * {% for comment in post.comments %} | 
            ||
| 168 | * <article class="comment">  | 
            ||
| 169 | 	 *   <p class="date">Posted on {{ comment.date }}:</p> | 
            ||
| 170 | 	 *   <p class="comment">{{ comment.content }}</p> | 
            ||
| 171 | * </article>  | 
            ||
| 172 | 	 * {% endfor %} | 
            ||
| 173 | * ```  | 
            ||
| 174 | * ```html  | 
            ||
| 175 | * <article class="comment">  | 
            ||
| 176 | * <p class="date">Posted on September 28, 2015:</p>  | 
            ||
| 177 | * <p class="comment">Happy Birthday!</p>  | 
            ||
| 178 | * </article>  | 
            ||
| 179 | * ```  | 
            ||
| 180 | * @return string  | 
            ||
| 181 | */  | 
            ||
| 182 | 	public function date( $date_format = '' ) { | 
            ||
| 187 | |||
| 188 | /**  | 
            ||
| 189 | * @api  | 
            ||
| 190 | * @example  | 
            ||
| 191 | * ```twig  | 
            ||
| 192 | 	 * {% for comment in post.comments %} | 
            ||
| 193 | * <article class="comment">  | 
            ||
| 194 | 	 *   <p class="date">Posted on {{ comment.date }} at {{comment.time}}:</p> | 
            ||
| 195 | 	 *   <p class="comment">{{ comment.content }}</p> | 
            ||
| 196 | * </article>  | 
            ||
| 197 | 	 * {% endfor %} | 
            ||
| 198 | * ```  | 
            ||
| 199 | * ```html  | 
            ||
| 200 | * <article class="comment">  | 
            ||
| 201 | * <p class="date">Posted on September 28, 2015 at 12:45 am:</p>  | 
            ||
| 202 | * <p class="comment">Happy Birthday!</p>  | 
            ||
| 203 | * </article>  | 
            ||
| 204 | * ```  | 
            ||
| 205 | * @return string  | 
            ||
| 206 | */  | 
            ||
| 207 | 	public function time( $time_format = '' ) { | 
            ||
| 212 | |||
| 213 | /**  | 
            ||
| 214 | * @param string $field_name  | 
            ||
| 215 | * @return mixed  | 
            ||
| 216 | */  | 
            ||
| 217 | 	public function meta($field_name) { | 
            ||
| 220 | |||
| 221 | /**  | 
            ||
| 222 | * @api  | 
            ||
| 223 | * @return bool  | 
            ||
| 224 | */  | 
            ||
| 225 | 	public function is_child() { | 
            ||
| 228 | |||
| 229 | /**  | 
            ||
| 230 | * @internal  | 
            ||
| 231 | * @param int $comment_id  | 
            ||
| 232 | * @return mixed  | 
            ||
| 233 | */  | 
            ||
| 234 | 	protected function get_meta_fields($comment_id = null) { | 
            ||
| 249 | |||
| 250 | /**  | 
            ||
| 251 | * @internal  | 
            ||
| 252 | * @param string $field_name  | 
            ||
| 253 | * @return mixed  | 
            ||
| 254 | */  | 
            ||
| 255 | View Code Duplication | 	protected function get_meta_field($field_name) { | 
            |
| 263 | |||
| 264 | /**  | 
            ||
| 265 | * Enqueue the WP threaded comments javascript,  | 
            ||
| 266 | * and fetch the reply link for various comments.  | 
            ||
| 267 | * @api  | 
            ||
| 268 | * @param int $comment_id  | 
            ||
| 269 | * @param int $post_id  | 
            ||
| 270 | * @return string  | 
            ||
| 271 | */  | 
            ||
| 272 | 	public function reply_link( $reply_text = 'Reply' ) { | 
            ||
| 291 | |||
| 292 | /* AVATAR Stuff  | 
            ||
| 293 | ======================= */  | 
            ||
| 294 | |||
| 295 | /**  | 
            ||
| 296 | * @internal  | 
            ||
| 297 | * @return string  | 
            ||
| 298 | */  | 
            ||
| 299 | 	protected function avatar_email() { | 
            ||
| 309 | |||
| 310 | /**  | 
            ||
| 311 | * @internal  | 
            ||
| 312 | * @param string $email_hash  | 
            ||
| 313 | * @return string  | 
            ||
| 314 | */  | 
            ||
| 315 | 	protected function avatar_host($email_hash) { | 
            ||
| 327 | |||
| 328 | /**  | 
            ||
| 329 | * @internal  | 
            ||
| 330 | * @todo what if it's relative?  | 
            ||
| 331 | * @param string $default  | 
            ||
| 332 | * @param string $email  | 
            ||
| 333 | * @param string $size  | 
            ||
| 334 | * @param string $host  | 
            ||
| 335 | * @return string  | 
            ||
| 336 | */  | 
            ||
| 337 | 	protected function avatar_default($default, $email, $size, $host) { | 
            ||
| 364 | |||
| 365 | /**  | 
            ||
| 366 | * @internal  | 
            ||
| 367 | * @param string $default  | 
            ||
| 368 | * @param string $host  | 
            ||
| 369 | * @param string $email_hash  | 
            ||
| 370 | * @param string $size  | 
            ||
| 371 | * @return mixed  | 
            ||
| 372 | */  | 
            ||
| 373 | 	protected function avatar_out($default, $host, $email_hash, $size) { | 
            ||
| 381 | |||
| 382 | }  | 
            ||
| 383 | 
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.