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) { |
||
56 | $comment_data = $cid; |
||
57 | if (is_integer($cid)) { |
||
58 | $comment_data = get_comment($cid); |
||
59 | } |
||
60 | $this->import($comment_data); |
||
61 | $this->ID = $this->comment_ID; |
||
62 | $this->id = $this->comment_ID; |
||
63 | $comment_meta_data = $this->get_meta_fields($this->ID); |
||
64 | $this->import($comment_meta_data); |
||
65 | } |
||
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 = '') { |
||
117 | if (!get_option('show_avatars')) { |
||
118 | return false; |
||
119 | } |
||
120 | if (!is_numeric($size)) { |
||
121 | $size = '92'; |
||
122 | } |
||
123 | |||
124 | $email = $this->avatar_email(); |
||
125 | $email_hash = ''; |
||
126 | if (!empty($email)) { |
||
127 | $email_hash = md5(strtolower(trim($email))); |
||
128 | } |
||
129 | $host = $this->avatar_host($email_hash); |
||
130 | $default = $this->avatar_default($default, $email, $size, $host); |
||
131 | if (!empty($email)) { |
||
132 | $avatar = $this->avatar_out($default, $host, $email_hash, $size); |
||
133 | } else { |
||
134 | $avatar = $default; |
||
135 | } |
||
136 | return $avatar; |
||
137 | } |
||
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 = '' ) { |
||
183 | $df = $date_format ? $date_format : get_option('date_format'); |
||
184 | $the_date = (string)mysql2date($df, $this->comment_date); |
||
185 | return apply_filters('get_comment_date ', $the_date, $df); |
||
186 | } |
||
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 = '' ) { |
||
208 | $tf = $time_format ? $time_format : get_option('time_format'); |
||
209 | $the_time = (string)mysql2date($tf, $this->comment_date); |
||
210 | return apply_filters('get_comment_time', $the_time, $tf); |
||
211 | } |
||
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) { |
||
235 | if ($comment_id === null) { |
||
236 | $comment_id = $this->ID; |
||
237 | } |
||
238 | //Could not find a WP function to fetch all comment meta data, so I made one. |
||
239 | apply_filters('timber_comment_get_meta_pre', array(), $comment_id); |
||
240 | $comment_metas = get_comment_meta($comment_id); |
||
241 | foreach ($comment_metas as &$cm) { |
||
242 | if (is_array($cm) && count($cm) == 1) { |
||
243 | $cm = $cm[0]; |
||
244 | } |
||
245 | } |
||
246 | $comment_metas = apply_filters('timber_comment_get_meta', $comment_metas, $comment_id); |
||
247 | return $comment_metas; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @internal |
||
252 | * @param string $field_name |
||
253 | * @return mixed |
||
254 | */ |
||
255 | View Code Duplication | protected function get_meta_field($field_name) { |
|
256 | $value = apply_filters('timber_comment_get_meta_field_pre', null, $this->ID, $field_name, $this); |
||
257 | if ($value === null) { |
||
258 | $value = get_comment_meta($this->ID, $field_name, true); |
||
259 | } |
||
260 | $value = apply_filters('timber_comment_get_meta_field', $value, $this->ID, $field_name, $this); |
||
261 | return $value; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Enqueue the WP threaded comments javascript, |
||
266 | * and fetch the reply link for various comments. |
||
267 | * @api |
||
268 | * @return string |
||
269 | */ |
||
270 | public function reply_link( $reply_text = 'Reply' ) { |
||
289 | |||
290 | /* AVATAR Stuff |
||
291 | ======================= */ |
||
292 | |||
293 | /** |
||
294 | * @internal |
||
295 | * @return string |
||
296 | */ |
||
297 | protected function avatar_email() { |
||
307 | |||
308 | /** |
||
309 | * @internal |
||
310 | * @param string $email_hash |
||
311 | * @return string |
||
312 | */ |
||
313 | protected function avatar_host($email_hash) { |
||
325 | |||
326 | /** |
||
327 | * @internal |
||
328 | * @todo what if it's relative? |
||
329 | * @param string $default |
||
330 | * @param string $email |
||
331 | * @param string $size |
||
332 | * @param string $host |
||
333 | * @return string |
||
334 | */ |
||
335 | protected function avatar_default($default, $email, $size, $host) { |
||
362 | |||
363 | /** |
||
364 | * @internal |
||
365 | * @param string $default |
||
366 | * @param string $host |
||
367 | * @param string $email_hash |
||
368 | * @param string $size |
||
369 | * @return mixed |
||
370 | */ |
||
371 | protected function avatar_out($default, $host, $email_hash, $size) { |
||
379 | |||
380 | } |
||
381 |
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.