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 |
||
50 | class Comment extends DataObject |
||
51 | { |
||
52 | /** |
||
53 | * {@inheritDoc} |
||
54 | */ |
||
55 | private static $db = array( |
||
56 | 'Name' => 'Varchar(200)', |
||
57 | 'Comment' => 'Text', |
||
58 | 'Email' => 'Varchar(200)', |
||
59 | 'URL' => 'Varchar(255)', |
||
60 | 'Moderated' => 'Boolean(0)', |
||
61 | 'IsSpam' => 'Boolean(0)', |
||
62 | 'AllowHtml' => 'Boolean', |
||
63 | 'SecretToken' => 'Varchar(255)', |
||
64 | 'Depth' => 'Int' |
||
65 | ); |
||
66 | |||
67 | /** |
||
68 | * {@inheritDoc} |
||
69 | */ |
||
70 | private static $has_one = array( |
||
71 | 'Author' => Member::class, |
||
72 | 'ParentComment' => self::class, |
||
73 | 'Parent' => DataObject::class |
||
74 | ); |
||
75 | |||
76 | /** |
||
77 | * {@inheritDoc} |
||
78 | */ |
||
79 | private static $has_many = array( |
||
80 | 'ChildComments' => self::class |
||
81 | ); |
||
82 | |||
83 | /** |
||
84 | * {@inheritDoc} |
||
85 | */ |
||
86 | private static $default_sort = '"Created" DESC'; |
||
87 | |||
88 | /** |
||
89 | * {@inheritDoc} |
||
90 | */ |
||
91 | private static $defaults = array( |
||
92 | 'Moderated' => 0, |
||
93 | 'IsSpam' => 0, |
||
94 | ); |
||
95 | |||
96 | /** |
||
97 | * {@inheritDoc} |
||
98 | */ |
||
99 | private static $casting = array( |
||
100 | 'Title' => 'Varchar', |
||
101 | 'ParentTitle' => 'Varchar', |
||
102 | 'ParentClassName' => 'Varchar', |
||
103 | 'AuthorName' => 'Varchar', |
||
104 | 'RSSName' => 'Varchar', |
||
105 | 'DeleteLink' => 'Varchar', |
||
106 | 'SpamLink' => 'Varchar', |
||
107 | 'HamLink' => 'Varchar', |
||
108 | 'ApproveLink' => 'Varchar', |
||
109 | 'Permalink' => 'Varchar' |
||
110 | ); |
||
111 | |||
112 | /** |
||
113 | * {@inheritDoc} |
||
114 | */ |
||
115 | private static $searchable_fields = array( |
||
116 | 'Name', |
||
117 | 'Email', |
||
118 | 'Comment', |
||
119 | 'Created' |
||
120 | ); |
||
121 | |||
122 | /** |
||
123 | * {@inheritDoc} |
||
124 | */ |
||
125 | private static $summary_fields = array( |
||
126 | 'Name' => 'Submitted By', |
||
127 | 'Email' => 'Email', |
||
128 | 'Comment.LimitWordCount' => 'Comment', |
||
129 | 'Created' => 'Date Posted', |
||
130 | 'Parent.Title' => 'Post', |
||
131 | 'IsSpam' => 'Is Spam' |
||
132 | ); |
||
133 | |||
134 | /** |
||
135 | * {@inheritDoc} |
||
136 | */ |
||
137 | private static $field_labels = array( |
||
138 | 'Author' => 'Author Member' |
||
139 | ); |
||
140 | |||
141 | /** |
||
142 | * {@inheritDoc} |
||
143 | */ |
||
144 | private static $table_name = 'Comment'; |
||
145 | |||
146 | /** |
||
147 | * {@inheritDoc} |
||
148 | */ |
||
149 | public function onBeforeWrite() |
||
161 | |||
162 | /** |
||
163 | * {@inheritDoc} |
||
164 | */ |
||
165 | public function onBeforeDelete() |
||
174 | |||
175 | /** |
||
176 | * @return Comment_SecurityToken |
||
177 | */ |
||
178 | public function getSecurityToken() |
||
182 | |||
183 | /** |
||
184 | * Migrates the old {@link PageComment} objects to {@link Comment} |
||
185 | */ |
||
186 | public function requireDefaultRecords() |
||
214 | |||
215 | /** |
||
216 | * Return a link to this comment |
||
217 | * |
||
218 | * @param string $action |
||
219 | * |
||
220 | * @return string link to this comment. |
||
221 | */ |
||
222 | public function Link($action = '') |
||
228 | |||
229 | /** |
||
230 | * Returns the permalink for this {@link Comment}. Inserted into |
||
231 | * the ID tag of the comment |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | public function Permalink() |
||
240 | |||
241 | /** |
||
242 | * Translate the form field labels for the CMS administration |
||
243 | * |
||
244 | * @param boolean $includerelations |
||
245 | * |
||
246 | * @return array |
||
247 | */ |
||
248 | public function fieldLabels($includerelations = true) |
||
263 | |||
264 | /** |
||
265 | * Get the commenting option |
||
266 | * |
||
267 | * @param string $key |
||
268 | * |
||
269 | * @return mixed Result if the setting is available, or null otherwise |
||
270 | */ |
||
271 | public function getOption($key) |
||
288 | |||
289 | /** |
||
290 | * Returns the parent {@link DataObject} this comment is attached too |
||
291 | * |
||
292 | * @deprecated 4.0.0 Use $this->Parent() instead |
||
293 | * @return DataObject |
||
294 | */ |
||
295 | public function getParent() |
||
301 | |||
302 | |||
303 | /** |
||
304 | * Returns a string to help identify the parent of the comment |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | public function getParentTitle() |
||
314 | |||
315 | /** |
||
316 | * Comment-parent classnames obviously vary, return the parent classname |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | public function getParentClassName() |
||
324 | |||
325 | /** |
||
326 | * {@inheritDoc} |
||
327 | */ |
||
328 | public function castingHelper($field) |
||
336 | |||
337 | /** |
||
338 | * Content to be safely escaped on the frontend |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | public function getEscapedComment() |
||
346 | |||
347 | /** |
||
348 | * Return whether this comment is a preview (has not been written to the db) |
||
349 | * |
||
350 | * @return boolean |
||
351 | */ |
||
352 | public function isPreview() |
||
356 | |||
357 | /** |
||
358 | * @todo needs to compare to the new {@link Commenting} configuration API |
||
359 | * |
||
360 | * @param Member $member |
||
361 | * @param array $context |
||
362 | * @return bool |
||
363 | */ |
||
364 | public function canCreate($member = null, $context = []) |
||
368 | |||
369 | /** |
||
370 | * Checks for association with a page, and {@link SiteTree->ProvidePermission} |
||
371 | * flag being set to true. |
||
372 | * |
||
373 | * @param Member $member |
||
374 | * @return Boolean |
||
375 | */ |
||
376 | public function canView($member = null) |
||
397 | |||
398 | /** |
||
399 | * Checks if the comment can be edited. |
||
400 | * |
||
401 | * @param null|int|Member $member |
||
402 | * @return Boolean |
||
403 | */ |
||
404 | public function canEdit($member = null) |
||
427 | |||
428 | /** |
||
429 | * Checks if the comment can be deleted. |
||
430 | * |
||
431 | * @param null|int|Member $member |
||
432 | * @return Boolean |
||
433 | */ |
||
434 | public function canDelete($member = null) |
||
449 | |||
450 | /** |
||
451 | * Resolves Member object. |
||
452 | * |
||
453 | * @param Member|int|null $member |
||
454 | * @return Member|null |
||
455 | */ |
||
456 | protected function getMember($member = null) |
||
468 | |||
469 | /** |
||
470 | * Return the authors name for the comment |
||
471 | * |
||
472 | * @return string |
||
473 | */ |
||
474 | public function getAuthorName() |
||
482 | |||
483 | /** |
||
484 | * Generate a secure admin-action link authorised for the specified member |
||
485 | * |
||
486 | * @param string $action An action on CommentingController to link to |
||
487 | * @param Member $member The member authorised to invoke this action |
||
488 | * |
||
489 | * @return string |
||
490 | */ |
||
491 | protected function actionLink($action, $member = null) |
||
519 | |||
520 | /** |
||
521 | * Link to delete this comment |
||
522 | * |
||
523 | * @param Member $member |
||
524 | * |
||
525 | * @return string |
||
526 | */ |
||
527 | public function DeleteLink($member = null) |
||
533 | |||
534 | /** |
||
535 | * Link to mark as spam |
||
536 | * |
||
537 | * @param Member $member |
||
538 | * |
||
539 | * @return string |
||
540 | */ |
||
541 | public function SpamLink($member = null) |
||
547 | |||
548 | /** |
||
549 | * Link to mark as not-spam (ham) |
||
550 | * |
||
551 | * @param Member $member |
||
552 | * |
||
553 | * @return string |
||
554 | */ |
||
555 | public function HamLink($member = null) |
||
561 | |||
562 | /** |
||
563 | * Link to approve this comment |
||
564 | * |
||
565 | * @param Member $member |
||
566 | * |
||
567 | * @return string |
||
568 | */ |
||
569 | public function ApproveLink($member = null) |
||
575 | |||
576 | /** |
||
577 | * Mark this comment as spam |
||
578 | */ |
||
579 | public function markSpam() |
||
586 | |||
587 | /** |
||
588 | * Mark this comment as approved |
||
589 | */ |
||
590 | public function markApproved() |
||
597 | |||
598 | /** |
||
599 | * Mark this comment as unapproved |
||
600 | */ |
||
601 | public function markUnapproved() |
||
607 | |||
608 | /** |
||
609 | * @return string |
||
610 | */ |
||
611 | public function SpamClass() |
||
621 | |||
622 | /** |
||
623 | * @return string |
||
624 | */ |
||
625 | public function getTitle() |
||
637 | |||
638 | /* |
||
639 | * Modify the default fields shown to the user |
||
640 | */ |
||
641 | public function getCMSFields() |
||
714 | |||
715 | /** |
||
716 | * @param string $dirtyHtml |
||
717 | * |
||
718 | * @return string |
||
719 | */ |
||
720 | public function purifyHtml($dirtyHtml) |
||
728 | |||
729 | /** |
||
730 | * @return HTMLPurifier (or anything with a "purify()" method) |
||
731 | */ |
||
732 | public function getHtmlPurifierService() |
||
754 | |||
755 | /** |
||
756 | * Calculate the Gravatar link from the email address |
||
757 | * |
||
758 | * @return string |
||
759 | */ |
||
760 | public function Gravatar() |
||
775 | |||
776 | /** |
||
777 | * Determine if replies are enabled for this instance |
||
778 | * |
||
779 | * @return boolean |
||
780 | */ |
||
781 | public function getRepliesEnabled() |
||
793 | |||
794 | /** |
||
795 | * Returns the list of all replies |
||
796 | * |
||
797 | * @return SS_List |
||
798 | */ |
||
799 | public function AllReplies() |
||
816 | |||
817 | /** |
||
818 | * Returns the list of replies, with spam and unmoderated items excluded, for use in the frontend |
||
819 | * |
||
820 | * @return SS_List |
||
821 | */ |
||
822 | public function Replies() |
||
849 | |||
850 | /** |
||
851 | * Returns the list of replies paged, with spam and unmoderated items excluded, for use in the frontend |
||
852 | * |
||
853 | * @return PaginatedList |
||
854 | */ |
||
855 | View Code Duplication | public function PagedReplies() |
|
867 | |||
868 | /** |
||
869 | * Generate a reply form for this comment |
||
870 | * |
||
871 | * @return Form |
||
872 | */ |
||
873 | public function ReplyForm() |
||
894 | |||
895 | /** |
||
896 | * Refresh of this comment in the hierarchy |
||
897 | */ |
||
898 | public function updateDepth() |
||
908 | } |
||
909 |