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 |
||
| 33 | class CommentsExtension extends DataExtension |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Default configuration values |
||
| 37 | * |
||
| 38 | * enabled: Allows commenting to be disabled even if the extension is present |
||
| 39 | * enabled_cms: Allows commenting to be enabled or disabled via the CMS |
||
| 40 | * require_login: Boolean, whether a user needs to login (required for required_permission) |
||
| 41 | * require_login_cms: Allows require_login to be set via the CMS |
||
| 42 | * required_permission: Permission (or array of permissions) required to comment |
||
| 43 | * include_js: Enhance operation by ajax behaviour on moderation links (required for use_preview) |
||
| 44 | * use_gravatar: Set to true to show gravatar icons |
||
| 45 | * gravatar_default: Theme for 'not found' gravatar {@see http://gravatar.com/site/implement/images} |
||
| 46 | * gravatar_rating: Gravatar rating (same as the standard default) |
||
| 47 | * show_comments_when_disabled: Show older comments when commenting has been disabled. |
||
| 48 | * order_comments_by: Default sort order. |
||
| 49 | * order_replies_by: Sort order for replies. |
||
| 50 | * comments_holder_id: ID for the comments holder |
||
| 51 | * comment_permalink_prefix: ID prefix for each comment |
||
| 52 | * require_moderation: Require moderation for all comments |
||
| 53 | * require_moderation_cms: Ignore other comment moderation config settings and set via CMS |
||
| 54 | * frontend_moderation: Display unmoderated comments in the frontend, if the user can moderate them. |
||
| 55 | * frontend_spam: Display spam comments in the frontend, if the user can moderate them. |
||
| 56 | * html_allowed: Allow for sanitized HTML in comments |
||
| 57 | * use_preview: Preview formatted comment (when allowing HTML) |
||
| 58 | * nested_comments: Enable nested comments |
||
| 59 | * nested_depth: Max depth of nested comments in levels (where root is 1 depth) 0 means no limit. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | * |
||
| 63 | * @config |
||
| 64 | */ |
||
| 65 | private static $comments = [ |
||
|
|
|||
| 66 | 'enabled' => true, |
||
| 67 | 'enabled_cms' => false, |
||
| 68 | 'require_login' => false, |
||
| 69 | 'require_login_cms' => false, |
||
| 70 | 'required_permission' => false, |
||
| 71 | 'include_js' => true, |
||
| 72 | 'use_gravatar' => false, |
||
| 73 | 'gravatar_size' => 80, |
||
| 74 | 'gravatar_default' => 'identicon', |
||
| 75 | 'gravatar_rating' => 'g', |
||
| 76 | 'show_comments_when_disabled' => false, |
||
| 77 | 'order_comments_by' => '"Created" DESC', |
||
| 78 | 'order_replies_by' => false, |
||
| 79 | 'comments_per_page' => 10, |
||
| 80 | 'comments_holder_id' => 'comments-holder', |
||
| 81 | 'comment_permalink_prefix' => 'comment-', |
||
| 82 | 'require_moderation' => false, |
||
| 83 | 'require_moderation_nonmembers' => false, |
||
| 84 | 'require_moderation_cms' => false, |
||
| 85 | 'frontend_moderation' => false, |
||
| 86 | 'frontend_spam' => false, |
||
| 87 | 'html_allowed' => false, |
||
| 88 | 'html_allowed_elements' => ['a', 'img', 'i', 'b'], |
||
| 89 | 'use_preview' => false, |
||
| 90 | 'nested_comments' => false, |
||
| 91 | 'nested_depth' => 2, |
||
| 92 | ]; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | private static $db = [ |
||
| 98 | 'ProvideComments' => 'Boolean', |
||
| 99 | 'ModerationRequired' => 'Enum(\'None,Required,NonMembersOnly\',\'None\')', |
||
| 100 | 'CommentsRequireLogin' => 'Boolean', |
||
| 101 | ]; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritDoc} |
||
| 105 | */ |
||
| 106 | private static $has_many = [ |
||
| 107 | 'Commments' => Comment::class . '.Parent' |
||
| 108 | ]; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * CMS configurable options should default to the config values, but respect |
||
| 112 | * default values specified by the object |
||
| 113 | */ |
||
| 114 | public function populateDefaults() |
||
| 115 | { |
||
| 116 | $defaults = $this->owner->config()->get('defaults'); |
||
| 117 | |||
| 118 | // Set if comments should be enabled by default |
||
| 119 | View Code Duplication | if (isset($defaults['ProvideComments'])) { |
|
| 120 | $this->owner->ProvideComments = $defaults['ProvideComments']; |
||
| 121 | } else { |
||
| 122 | $this->owner->ProvideComments = $this->owner->getCommentsOption('enabled') ? 1 : 0; |
||
| 123 | } |
||
| 124 | |||
| 125 | // If moderation options should be configurable via the CMS then |
||
| 126 | if (isset($defaults['ModerationRequired'])) { |
||
| 127 | $this->owner->ModerationRequired = $defaults['ModerationRequired']; |
||
| 128 | } elseif ($this->owner->getCommentsOption('require_moderation')) { |
||
| 129 | $this->owner->ModerationRequired = 'Required'; |
||
| 130 | } elseif ($this->owner->getCommentsOption('require_moderation_nonmembers')) { |
||
| 131 | $this->owner->ModerationRequired = 'NonMembersOnly'; |
||
| 132 | } else { |
||
| 133 | $this->owner->ModerationRequired = 'None'; |
||
| 134 | } |
||
| 135 | |||
| 136 | // Set login required |
||
| 137 | View Code Duplication | if (isset($defaults['CommentsRequireLogin'])) { |
|
| 138 | $this->owner->CommentsRequireLogin = $defaults['CommentsRequireLogin']; |
||
| 139 | } else { |
||
| 140 | $this->owner->CommentsRequireLogin = $this->owner->getCommentsOption('require_login') ? 1 : 0; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * If this extension is applied to a {@link SiteTree} record then |
||
| 147 | * append a Provide Comments checkbox to allow authors to trigger |
||
| 148 | * whether or not to display comments |
||
| 149 | * |
||
| 150 | * @todo Allow customization of other {@link Commenting} configuration |
||
| 151 | * |
||
| 152 | * @param FieldList $fields |
||
| 153 | */ |
||
| 154 | public function updateSettingsFields(FieldList $fields) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get comment moderation rules for this parent |
||
| 201 | * |
||
| 202 | * None: No moderation required |
||
| 203 | * Required: All comments |
||
| 204 | * NonMembersOnly: Only anonymous users |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getModerationRequired() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Determine if users must be logged in to post comments |
||
| 223 | * |
||
| 224 | * @return boolean |
||
| 225 | */ |
||
| 226 | public function getCommentsRequireLogin() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Returns the RelationList of all comments against this object. Can be used as a data source |
||
| 237 | * for a gridfield with write access. |
||
| 238 | * |
||
| 239 | * @return DataList |
||
| 240 | */ |
||
| 241 | View Code Duplication | public function AllComments() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Returns all comments against this object, with with spam and unmoderated items excluded, for use in the frontend |
||
| 253 | * |
||
| 254 | * @return DataList |
||
| 255 | */ |
||
| 256 | public function AllVisibleComments() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Returns the root level comments, with spam and unmoderated items excluded, for use in the frontend |
||
| 280 | * |
||
| 281 | * @return DataList |
||
| 282 | */ |
||
| 283 | View Code Duplication | public function Comments() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Returns a paged list of the root level comments, with spam and unmoderated items excluded, |
||
| 298 | * for use in the frontend |
||
| 299 | * |
||
| 300 | * @return PaginatedList |
||
| 301 | */ |
||
| 302 | View Code Duplication | public function PagedComments() |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Determine if comments are enabled for this instance |
||
| 317 | * |
||
| 318 | * @return boolean |
||
| 319 | */ |
||
| 320 | public function getCommentsEnabled() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get the HTML ID for the comment holder in the template |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function getCommentHolderID() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Permission codes required in order to post (or empty if none required) |
||
| 347 | * |
||
| 348 | * @return string|array Permission or list of permissions, if required |
||
| 349 | */ |
||
| 350 | public function getPostingRequiredPermission() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Determine if a user can post comments on this item |
||
| 357 | * |
||
| 358 | * @param Member $member Member to check |
||
| 359 | * |
||
| 360 | * @return boolean |
||
| 361 | */ |
||
| 362 | public function canPostComment($member = null) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Determine if this member can moderate comments in the CMS |
||
| 397 | * |
||
| 398 | * @param Member $member |
||
| 399 | * |
||
| 400 | * @return boolean |
||
| 401 | */ |
||
| 402 | public function canModerateComments($member = null) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Gets the RSS link to all comments |
||
| 415 | * |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public function getCommentRSSLink() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Get the RSS link to all comments on this page |
||
| 425 | * |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | public function getCommentRSSLinkPage() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Comments interface for the front end. Includes the CommentAddForm and the composition |
||
| 439 | * of the comments display. |
||
| 440 | * |
||
| 441 | * To customize the html see templates/CommentInterface.ss or extend this function with |
||
| 442 | * your own extension. |
||
| 443 | * |
||
| 444 | * @todo Cleanup the passing of all this configuration based functionality |
||
| 445 | * |
||
| 446 | * @see docs/en/Extending |
||
| 447 | */ |
||
| 448 | public function CommentsForm() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Returns whether this extension instance is attached to a {@link SiteTree} object |
||
| 486 | * |
||
| 487 | * @return bool |
||
| 488 | */ |
||
| 489 | public function attachedToSiteTree() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get the commenting option for this object. |
||
| 498 | * |
||
| 499 | * This can be overridden in any instance or extension to customise the |
||
| 500 | * option available. |
||
| 501 | * |
||
| 502 | * @param string $key |
||
| 503 | * |
||
| 504 | * @return mixed Result if the setting is available, or null otherwise |
||
| 505 | */ |
||
| 506 | public function getCommentsOption($key) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | public function getCommentsOptions() |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Add moderation functions to the current fieldlist |
||
| 541 | * |
||
| 542 | * @param FieldList $fields |
||
| 543 | */ |
||
| 544 | protected function updateModerationFields(FieldList $fields) |
||
| 607 | |||
| 608 | public function updateCMSFields(FieldList $fields) |
||
| 620 | } |
||
| 621 |
This check marks private properties in classes that are never used. Those properties can be removed.