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 | 'getAuthorName' => 'Submitted By', |
||
129 | 'getAuthorEmail' => '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 | * Return the comment authors email address |
||
455 | * |
||
456 | * @return string |
||
457 | */ |
||
458 | public function getAuthorEmail() |
||
466 | |||
467 | /** |
||
468 | * Generate a secure admin-action link authorised for the specified member |
||
469 | * |
||
470 | * @param string $action An action on CommentingController to link to |
||
471 | * @param Member $member The member authorised to invoke this action |
||
472 | * |
||
473 | * @return string |
||
474 | */ |
||
475 | protected function actionLink($action, $member = null) |
||
503 | |||
504 | /** |
||
505 | * Link to delete this comment |
||
506 | * |
||
507 | * @param Member $member |
||
508 | * |
||
509 | * @return string |
||
510 | */ |
||
511 | public function DeleteLink($member = null) |
||
517 | |||
518 | /** |
||
519 | * Link to mark as spam |
||
520 | * |
||
521 | * @param Member $member |
||
522 | * |
||
523 | * @return string |
||
524 | */ |
||
525 | public function SpamLink($member = null) |
||
531 | |||
532 | /** |
||
533 | * Link to mark as not-spam (ham) |
||
534 | * |
||
535 | * @param Member $member |
||
536 | * |
||
537 | * @return string |
||
538 | */ |
||
539 | public function HamLink($member = null) |
||
545 | |||
546 | /** |
||
547 | * Link to approve this comment |
||
548 | * |
||
549 | * @param Member $member |
||
550 | * |
||
551 | * @return string |
||
552 | */ |
||
553 | public function ApproveLink($member = null) |
||
559 | |||
560 | /** |
||
561 | * Mark this comment as spam |
||
562 | */ |
||
563 | public function markSpam() |
||
570 | |||
571 | /** |
||
572 | * Mark this comment as approved |
||
573 | */ |
||
574 | public function markApproved() |
||
581 | |||
582 | /** |
||
583 | * Mark this comment as unapproved |
||
584 | */ |
||
585 | public function markUnapproved() |
||
591 | |||
592 | /** |
||
593 | * @return string |
||
594 | */ |
||
595 | public function SpamClass() |
||
605 | |||
606 | /** |
||
607 | * @return string |
||
608 | */ |
||
609 | public function getTitle() |
||
621 | |||
622 | /* |
||
623 | * Modify the default fields shown to the user |
||
624 | */ |
||
625 | public function getCMSFields() |
||
698 | |||
699 | /** |
||
700 | * @param string $dirtyHtml |
||
701 | * |
||
702 | * @return string |
||
703 | */ |
||
704 | public function purifyHtml($dirtyHtml) |
||
712 | |||
713 | /** |
||
714 | * @return HTMLPurifier (or anything with a "purify()" method) |
||
715 | */ |
||
716 | public function getHtmlPurifierService() |
||
738 | |||
739 | /** |
||
740 | * Calculate the Gravatar link from the email address |
||
741 | * |
||
742 | * @return string |
||
743 | */ |
||
744 | public function Gravatar() |
||
763 | |||
764 | /** |
||
765 | * Determine if replies are enabled for this instance |
||
766 | * |
||
767 | * @return boolean |
||
768 | */ |
||
769 | public function getRepliesEnabled() |
||
781 | |||
782 | /** |
||
783 | * Proxy for checking whether the has permission to comment on the comment parent. |
||
784 | * |
||
785 | * @param Member $member Member to check |
||
786 | * |
||
787 | * @return boolean |
||
788 | */ |
||
789 | public function canPostComment($member = null) |
||
795 | |||
796 | /** |
||
797 | * Returns the list of all replies |
||
798 | * |
||
799 | * @return SS_List |
||
800 | */ |
||
801 | public function AllReplies() |
||
818 | |||
819 | /** |
||
820 | * Returns the list of replies, with spam and unmoderated items excluded, for use in the frontend |
||
821 | * |
||
822 | * @return SS_List |
||
823 | */ |
||
824 | public function Replies() |
||
851 | |||
852 | /** |
||
853 | * Returns the list of replies paged, with spam and unmoderated items excluded, for use in the frontend |
||
854 | * |
||
855 | * @return PaginatedList |
||
856 | */ |
||
857 | public function PagedReplies() |
||
869 | |||
870 | /** |
||
871 | * Generate a reply form for this comment |
||
872 | * |
||
873 | * @return Form |
||
874 | */ |
||
875 | public function ReplyForm() |
||
896 | |||
897 | /** |
||
898 | * @return string |
||
899 | */ |
||
900 | public function getDate() |
||
904 | |||
905 | /** |
||
906 | * Refresh of this comment in the hierarchy |
||
907 | */ |
||
908 | public function updateDepth() |
||
918 | } |
||
919 |