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 CommentsExtension 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 CommentsExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class CommentsExtension extends DataExtension |
||
32 | { |
||
33 | /** |
||
34 | * Default configuration values |
||
35 | * |
||
36 | * enabled: Allows commenting to be disabled even if the extension is present |
||
37 | * enabled_cms: Allows commenting to be enabled or disabled via the CMS |
||
38 | * require_login: Boolean, whether a user needs to login (required for required_permission) |
||
39 | * require_login_cms: Allows require_login to be set via the CMS |
||
40 | * required_permission: Permission (or array of permissions) required to comment |
||
41 | * include_js: Enhance operation by ajax behaviour on moderation links (required for use_preview) |
||
42 | * use_gravatar: Set to true to show gravatar icons |
||
43 | * gravatar_default: Theme for 'not found' gravatar {@see http://gravatar.com/site/implement/images} |
||
44 | * gravatar_rating: Gravatar rating (same as the standard default) |
||
45 | * show_comments_when_disabled: Show older comments when commenting has been disabled. |
||
46 | * order_comments_by: Default sort order. |
||
47 | * order_replies_by: Sort order for replies. |
||
48 | * comments_holder_id: ID for the comments holder |
||
49 | * comment_permalink_prefix: ID prefix for each comment |
||
50 | * require_moderation: Require moderation for all comments |
||
51 | * require_moderation_cms: Ignore other comment moderation config settings and set via CMS |
||
52 | * frontend_moderation: Display unmoderated comments in the frontend, if the user can moderate them. |
||
53 | * frontend_spam: Display spam comments in the frontend, if the user can moderate them. |
||
54 | * html_allowed: Allow for sanitized HTML in comments |
||
55 | * use_preview: Preview formatted comment (when allowing HTML) |
||
56 | * nested_comments: Enable nested comments |
||
57 | * nested_depth: Max depth of nested comments in levels (where root is 1 depth) 0 means no limit. |
||
58 | * |
||
59 | * @var array |
||
60 | * |
||
61 | * @config |
||
62 | */ |
||
63 | private static $comments = array( |
||
|
|||
64 | 'enabled' => true, |
||
65 | 'enabled_cms' => false, |
||
66 | 'require_login' => false, |
||
67 | 'require_login_cms' => false, |
||
68 | 'required_permission' => false, |
||
69 | 'include_js' => true, |
||
70 | 'use_gravatar' => false, |
||
71 | 'gravatar_size' => 80, |
||
72 | 'gravatar_default' => 'identicon', |
||
73 | 'gravatar_rating' => 'g', |
||
74 | 'show_comments_when_disabled' => false, |
||
75 | 'order_comments_by' => '"Created" DESC', |
||
76 | 'order_replies_by' => false, |
||
77 | 'comments_per_page' => 10, |
||
78 | 'comments_holder_id' => 'comments-holder', |
||
79 | 'comment_permalink_prefix' => 'comment-', |
||
80 | 'require_moderation' => false, |
||
81 | 'require_moderation_nonmembers' => false, |
||
82 | 'require_moderation_cms' => false, |
||
83 | 'frontend_moderation' => false, |
||
84 | 'frontend_spam' => false, |
||
85 | 'html_allowed' => false, |
||
86 | 'html_allowed_elements' => array('a', 'img', 'i', 'b'), |
||
87 | 'use_preview' => false, |
||
88 | 'nested_comments' => false, |
||
89 | 'nested_depth' => 2, |
||
90 | ); |
||
91 | |||
92 | /** |
||
93 | * @var array |
||
94 | */ |
||
95 | private static $db = array( |
||
96 | 'ProvideComments' => 'Boolean', |
||
97 | 'ModerationRequired' => 'Enum(\'None,Required,NonMembersOnly\',\'None\')', |
||
98 | 'CommentsRequireLogin' => 'Boolean', |
||
99 | ); |
||
100 | |||
101 | /** |
||
102 | * {@inheritDoc} |
||
103 | */ |
||
104 | private static $has_many = [ |
||
105 | 'Commments' => 'SilverStripe\\Comments\\Model\\Comment.Parent' |
||
106 | ]; |
||
107 | |||
108 | /** |
||
109 | * CMS configurable options should default to the config values, but respect |
||
110 | * default values specified by the object |
||
111 | */ |
||
112 | public function populateDefaults() |
||
141 | |||
142 | |||
143 | /** |
||
144 | * If this extension is applied to a {@link SiteTree} record then |
||
145 | * append a Provide Comments checkbox to allow authors to trigger |
||
146 | * whether or not to display comments |
||
147 | * |
||
148 | * @todo Allow customization of other {@link Commenting} configuration |
||
149 | * |
||
150 | * @param FieldList $fields |
||
151 | */ |
||
152 | public function updateSettingsFields(FieldList $fields) |
||
196 | |||
197 | /** |
||
198 | * Get comment moderation rules for this parent |
||
199 | * |
||
200 | * None: No moderation required |
||
201 | * Required: All comments |
||
202 | * NonMembersOnly: Only anonymous users |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | public function getModerationRequired() |
||
218 | |||
219 | /** |
||
220 | * Determine if users must be logged in to post comments |
||
221 | * |
||
222 | * @return boolean |
||
223 | */ |
||
224 | public function getCommentsRequireLogin() |
||
232 | |||
233 | /** |
||
234 | * Returns the RelationList of all comments against this object. Can be used as a data source |
||
235 | * for a gridfield with write access. |
||
236 | * |
||
237 | * @return DataList |
||
238 | */ |
||
239 | View Code Duplication | public function AllComments() |
|
248 | |||
249 | /** |
||
250 | * Returns all comments against this object, with with spam and unmoderated items excluded, for use in the frontend |
||
251 | * |
||
252 | * @return DataList |
||
253 | */ |
||
254 | public function AllVisibleComments() |
||
274 | |||
275 | /** |
||
276 | * Returns the root level comments, with spam and unmoderated items excluded, for use in the frontend |
||
277 | * |
||
278 | * @return DataList |
||
279 | */ |
||
280 | View Code Duplication | public function Comments() |
|
292 | |||
293 | /** |
||
294 | * Returns a paged list of the root level comments, with spam and unmoderated items excluded, |
||
295 | * for use in the frontend |
||
296 | * |
||
297 | * @return PaginatedList |
||
298 | */ |
||
299 | View Code Duplication | public function PagedComments() |
|
311 | |||
312 | /** |
||
313 | * Determine if comments are enabled for this instance |
||
314 | * |
||
315 | * @return boolean |
||
316 | */ |
||
317 | public function getCommentsEnabled() |
||
331 | |||
332 | /** |
||
333 | * Get the HTML ID for the comment holder in the template |
||
334 | * |
||
335 | * @return string |
||
336 | */ |
||
337 | public function getCommentHolderID() |
||
341 | |||
342 | /** |
||
343 | * Permission codes required in order to post (or empty if none required) |
||
344 | * |
||
345 | * @return string|array Permission or list of permissions, if required |
||
346 | */ |
||
347 | public function getPostingRequiredPermission() |
||
351 | |||
352 | /** |
||
353 | * Determine if a user can post comments on this item |
||
354 | * |
||
355 | * @param Member $member Member to check |
||
356 | * |
||
357 | * @return boolean |
||
358 | */ |
||
359 | public function canPostComment($member = null) |
||
386 | |||
387 | /** |
||
388 | * Determine if this member can moderate comments in the CMS |
||
389 | * |
||
390 | * @param Member $member |
||
391 | * |
||
392 | * @return boolean |
||
393 | */ |
||
394 | public function canModerateComments($member = null) |
||
404 | |||
405 | /** |
||
406 | * Gets the RSS link to all comments |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | public function getCommentRSSLink() |
||
414 | |||
415 | /** |
||
416 | * Get the RSS link to all comments on this page |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | public function getCommentRSSLinkPage() |
||
428 | |||
429 | /** |
||
430 | * Comments interface for the front end. Includes the CommentAddForm and the composition |
||
431 | * of the comments display. |
||
432 | * |
||
433 | * To customize the html see templates/CommentInterface.ss or extend this function with |
||
434 | * your own extension. |
||
435 | * |
||
436 | * @todo Cleanup the passing of all this configuration based functionality |
||
437 | * |
||
438 | * @see docs/en/Extending |
||
439 | */ |
||
440 | public function CommentsForm() |
||
473 | |||
474 | /** |
||
475 | * Returns whether this extension instance is attached to a {@link SiteTree} object |
||
476 | * |
||
477 | * @return bool |
||
478 | */ |
||
479 | public function attachedToSiteTree() |
||
485 | |||
486 | /** |
||
487 | * Get the commenting option for this object |
||
488 | * |
||
489 | * This can be overridden in any instance or extension to customise the option available |
||
490 | * |
||
491 | * @param string $key |
||
492 | * |
||
493 | * @return mixed Result if the setting is available, or null otherwise |
||
494 | */ |
||
495 | public function getCommentsOption($key) |
||
511 | |||
512 | /** |
||
513 | * Add moderation functions to the current fieldlist |
||
514 | * |
||
515 | * @param FieldList $fields |
||
516 | */ |
||
517 | protected function updateModerationFields(FieldList $fields) |
||
572 | |||
573 | public function updateCMSFields(FieldList $fields) |
||
585 | } |
||
586 |
This check marks private properties in classes that are never used. Those properties can be removed.