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 |
||
51 | class Comment extends DataObject |
||
52 | { |
||
53 | /** |
||
54 | * {@inheritDoc} |
||
55 | */ |
||
56 | private static $db = array( |
||
57 | 'Name' => 'Varchar(200)', |
||
58 | 'Comment' => 'Text', |
||
59 | 'Email' => 'Varchar(200)', |
||
60 | 'URL' => 'Varchar(255)', |
||
61 | 'Moderated' => 'Boolean(0)', |
||
62 | 'IsSpam' => 'Boolean(0)', |
||
63 | 'AllowHtml' => 'Boolean', |
||
64 | 'SecretToken' => 'Varchar(255)', |
||
65 | 'Depth' => 'Int' |
||
66 | ); |
||
67 | |||
68 | /** |
||
69 | * {@inheritDoc} |
||
70 | */ |
||
71 | private static $has_one = array( |
||
72 | 'Author' => Member::class, |
||
73 | 'ParentComment' => self::class, |
||
74 | 'Parent' => DataObject::class |
||
75 | ); |
||
76 | |||
77 | /** |
||
78 | * {@inheritDoc} |
||
79 | */ |
||
80 | private static $has_many = array( |
||
81 | 'ChildComments' => self::class |
||
82 | ); |
||
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | private static $default_sort = '"Created" DESC'; |
||
88 | |||
89 | /** |
||
90 | * {@inheritDoc} |
||
91 | */ |
||
92 | private static $defaults = array( |
||
93 | 'Moderated' => 0, |
||
94 | 'IsSpam' => 0, |
||
95 | ); |
||
96 | |||
97 | /** |
||
98 | * {@inheritDoc} |
||
99 | */ |
||
100 | private static $casting = array( |
||
101 | 'Title' => 'Varchar', |
||
102 | 'ParentTitle' => 'Varchar', |
||
103 | 'ParentClassName' => 'Varchar', |
||
104 | 'AuthorName' => 'Varchar', |
||
105 | 'RSSName' => 'Varchar', |
||
106 | 'DeleteLink' => 'Varchar', |
||
107 | 'Date' => 'Datetime', |
||
108 | 'SpamLink' => 'Varchar', |
||
109 | 'HamLink' => 'Varchar', |
||
110 | 'ApproveLink' => 'Varchar', |
||
111 | 'Permalink' => 'Varchar' |
||
112 | ); |
||
113 | |||
114 | /** |
||
115 | * {@inheritDoc} |
||
116 | */ |
||
117 | private static $searchable_fields = array( |
||
118 | 'Name', |
||
119 | 'Email', |
||
120 | 'Comment', |
||
121 | 'Created' |
||
122 | ); |
||
123 | |||
124 | /** |
||
125 | * {@inheritDoc} |
||
126 | */ |
||
127 | private static $summary_fields = array( |
||
128 | 'Name' => 'Submitted By', |
||
129 | 'Email' => 'Email', |
||
130 | 'Comment.LimitWordCount' => 'Comment', |
||
131 | 'Created' => 'Date Posted', |
||
132 | 'Parent.Title' => 'Post', |
||
133 | 'IsSpam' => 'Is Spam' |
||
134 | ); |
||
135 | |||
136 | /** |
||
137 | * {@inheritDoc} |
||
138 | */ |
||
139 | private static $field_labels = array( |
||
140 | 'Author' => 'Author Member' |
||
141 | ); |
||
142 | |||
143 | /** |
||
144 | * {@inheritDoc} |
||
145 | */ |
||
146 | private static $table_name = 'Comment'; |
||
147 | |||
148 | /** |
||
149 | * {@inheritDoc} |
||
150 | */ |
||
151 | public function onBeforeWrite() |
||
163 | |||
164 | /** |
||
165 | * {@inheritDoc} |
||
166 | */ |
||
167 | public function onBeforeDelete() |
||
176 | |||
177 | /** |
||
178 | * @return Comment_SecurityToken |
||
179 | */ |
||
180 | public function getSecurityToken() |
||
184 | |||
185 | /** |
||
186 | * Return a link to this comment |
||
187 | * |
||
188 | * @param string $action |
||
189 | * |
||
190 | * @return string link to this comment. |
||
191 | */ |
||
192 | public function Link($action = '') |
||
198 | |||
199 | /** |
||
200 | * Returns the permalink for this {@link Comment}. Inserted into |
||
201 | * the ID tag of the comment |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | public function Permalink() |
||
210 | |||
211 | /** |
||
212 | * Translate the form field labels for the CMS administration |
||
213 | * |
||
214 | * @param boolean $includerelations |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | public function fieldLabels($includerelations = true) |
||
233 | |||
234 | /** |
||
235 | * Get the commenting option |
||
236 | * |
||
237 | * @param string $key |
||
238 | * |
||
239 | * @return mixed Result if the setting is available, or null otherwise |
||
240 | */ |
||
241 | public function getOption($key) |
||
258 | |||
259 | /** |
||
260 | * Returns the parent {@link DataObject} this comment is attached too |
||
261 | * |
||
262 | * @deprecated 4.0.0 Use $this->Parent() instead |
||
263 | * @return DataObject |
||
264 | */ |
||
265 | public function getParent() |
||
271 | |||
272 | |||
273 | /** |
||
274 | * Returns a string to help identify the parent of the comment |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | public function getParentTitle() |
||
284 | |||
285 | /** |
||
286 | * Comment-parent classnames obviously vary, return the parent classname |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | public function getParentClassName() |
||
294 | |||
295 | /** |
||
296 | * {@inheritDoc} |
||
297 | */ |
||
298 | public function castingHelper($field) |
||
306 | |||
307 | /** |
||
308 | * Content to be safely escaped on the frontend |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | public function getEscapedComment() |
||
316 | |||
317 | /** |
||
318 | * Return whether this comment is a preview (has not been written to the db) |
||
319 | * |
||
320 | * @return boolean |
||
321 | */ |
||
322 | public function isPreview() |
||
326 | |||
327 | /** |
||
328 | * @todo needs to compare to the new {@link Commenting} configuration API |
||
329 | * |
||
330 | * @param Member $member |
||
331 | * @param array $context |
||
332 | * @return bool |
||
333 | */ |
||
334 | public function canCreate($member = null, $context = []) |
||
338 | |||
339 | /** |
||
340 | * Checks for association with a page, and {@link SiteTree->ProvidePermission} |
||
341 | * flag being set to true. |
||
342 | * |
||
343 | * @param Member $member |
||
344 | * @return Boolean |
||
345 | */ |
||
346 | public function canView($member = null) |
||
367 | |||
368 | /** |
||
369 | * Checks if the comment can be edited. |
||
370 | * |
||
371 | * @param null|int|Member $member |
||
372 | * @return Boolean |
||
373 | */ |
||
374 | public function canEdit($member = null) |
||
397 | |||
398 | /** |
||
399 | * Checks if the comment can be deleted. |
||
400 | * |
||
401 | * @param null|int|Member $member |
||
402 | * @return Boolean |
||
403 | */ |
||
404 | public function canDelete($member = null) |
||
419 | |||
420 | /** |
||
421 | * Resolves Member object. |
||
422 | * |
||
423 | * @param Member|int|null $member |
||
424 | * @return Member|null |
||
425 | */ |
||
426 | protected function getMember($member = null) |
||
438 | |||
439 | /** |
||
440 | * Return the authors name for the comment |
||
441 | * |
||
442 | * @return string |
||
443 | */ |
||
444 | public function getAuthorName() |
||
452 | |||
453 | /** |
||
454 | * Generate a secure admin-action link authorised for the specified member |
||
455 | * |
||
456 | * @param string $action An action on CommentingController to link to |
||
457 | * @param Member $member The member authorised to invoke this action |
||
458 | * |
||
459 | * @return string |
||
460 | */ |
||
461 | protected function actionLink($action, $member = null) |
||
489 | |||
490 | /** |
||
491 | * Link to delete this comment |
||
492 | * |
||
493 | * @param Member $member |
||
494 | * |
||
495 | * @return string |
||
496 | */ |
||
497 | public function DeleteLink($member = null) |
||
503 | |||
504 | /** |
||
505 | * Link to mark as spam |
||
506 | * |
||
507 | * @param Member $member |
||
508 | * |
||
509 | * @return string |
||
510 | */ |
||
511 | public function SpamLink($member = null) |
||
517 | |||
518 | /** |
||
519 | * Link to mark as not-spam (ham) |
||
520 | * |
||
521 | * @param Member $member |
||
522 | * |
||
523 | * @return string |
||
524 | */ |
||
525 | public function HamLink($member = null) |
||
531 | |||
532 | /** |
||
533 | * Link to approve this comment |
||
534 | * |
||
535 | * @param Member $member |
||
536 | * |
||
537 | * @return string |
||
538 | */ |
||
539 | public function ApproveLink($member = null) |
||
545 | |||
546 | /** |
||
547 | * Mark this comment as spam |
||
548 | */ |
||
549 | public function markSpam() |
||
556 | |||
557 | /** |
||
558 | * Mark this comment as approved |
||
559 | */ |
||
560 | public function markApproved() |
||
567 | |||
568 | /** |
||
569 | * Mark this comment as unapproved |
||
570 | */ |
||
571 | public function markUnapproved() |
||
577 | |||
578 | /** |
||
579 | * @return string |
||
580 | */ |
||
581 | public function SpamClass() |
||
591 | |||
592 | /** |
||
593 | * @return string |
||
594 | */ |
||
595 | public function getTitle() |
||
607 | |||
608 | /* |
||
609 | * Modify the default fields shown to the user |
||
610 | */ |
||
611 | public function getCMSFields() |
||
684 | |||
685 | /** |
||
686 | * @param string $dirtyHtml |
||
687 | * |
||
688 | * @return string |
||
689 | */ |
||
690 | public function purifyHtml($dirtyHtml) |
||
698 | |||
699 | /** |
||
700 | * @return HTMLPurifier (or anything with a "purify()" method) |
||
701 | */ |
||
702 | public function getHtmlPurifierService() |
||
724 | |||
725 | /** |
||
726 | * Calculate the Gravatar link from the email address |
||
727 | * |
||
728 | * @return string |
||
729 | */ |
||
730 | public function Gravatar() |
||
749 | |||
750 | /** |
||
751 | * Determine if replies are enabled for this instance |
||
752 | * |
||
753 | * @return boolean |
||
754 | */ |
||
755 | public function getRepliesEnabled() |
||
767 | |||
768 | /** |
||
769 | * Returns the list of all replies |
||
770 | * |
||
771 | * @return SS_List |
||
772 | */ |
||
773 | public function AllReplies() |
||
790 | |||
791 | /** |
||
792 | * Returns the list of replies, with spam and unmoderated items excluded, for use in the frontend |
||
793 | * |
||
794 | * @return SS_List |
||
795 | */ |
||
796 | public function Replies() |
||
823 | |||
824 | /** |
||
825 | * Returns the list of replies paged, with spam and unmoderated items excluded, for use in the frontend |
||
826 | * |
||
827 | * @return PaginatedList |
||
828 | */ |
||
829 | View Code Duplication | public function PagedReplies() |
|
841 | |||
842 | /** |
||
843 | * Generate a reply form for this comment |
||
844 | * |
||
845 | * @return Form |
||
846 | */ |
||
847 | public function ReplyForm() |
||
868 | |||
869 | /** |
||
870 | * @return string |
||
871 | */ |
||
872 | public function getDate() |
||
876 | |||
877 | /** |
||
878 | * Refresh of this comment in the hierarchy |
||
879 | */ |
||
880 | public function updateDepth() |
||
890 | } |
||
891 |