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