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 Blog 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 Blog, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Blog extends Page implements PermissionProvider |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * Permission for user management. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | const MANAGE_USERS = 'BLOG_MANAGE_USERS'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * If true, users assigned as editor, writer, or contributor will be automatically granted |
||
| 51 | * CMS_ACCESS_CMSMain permission. If false, only users with this permission already may be |
||
| 52 | * assigned. |
||
| 53 | * |
||
| 54 | * @config |
||
| 55 | * |
||
| 56 | * @var boolean |
||
| 57 | */ |
||
| 58 | private static $grant_user_access = true; |
||
|
|
|||
| 59 | |||
| 60 | /** |
||
| 61 | * Permission to either require, or grant to users assigned to work on this blog. |
||
| 62 | * |
||
| 63 | * @config |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private static $grant_user_permission = 'CMS_ACCESS_CMSMain'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Group code to assign newly granted users to. |
||
| 71 | * |
||
| 72 | * @config |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private static $grant_user_group = 'blog-users'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritDoc} |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | private static $table_name = 'Blog'; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | private static $db = array( |
||
| 88 | 'PostsPerPage' => 'Int', |
||
| 89 | ); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | private static $has_many = array( |
||
| 95 | 'Tags' => 'SilverStripe\\Blog\\Model\\BlogTag', |
||
| 96 | 'Categories' => 'SilverStripe\\Blog\\Model\\BlogCategory', |
||
| 97 | ); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | private static $many_many = array( |
||
| 103 | 'Editors' => 'SilverStripe\\Security\\Member', |
||
| 104 | 'Writers' => 'SilverStripe\\Security\\Member', |
||
| 105 | 'Contributors' => 'SilverStripe\\Security\\Member', |
||
| 106 | ); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | private static $allowed_children = array( |
||
| 112 | 'SilverStripe\\Blog\\Model\\BlogPost', |
||
| 113 | ); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | private static $extensions = array( |
||
| 119 | 'SilverStripe\\Blog\\Model\\BlogFilter', |
||
| 120 | ); |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | private static $defaults = array( |
||
| 126 | 'ProvideComments' => false, |
||
| 127 | 'PostsPerPage' => 10 |
||
| 128 | ); |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var string |
||
| 132 | */ |
||
| 133 | private static $description = 'Adds a blog to your website.'; |
||
| 134 | |||
| 135 | private static $icon = 'blog/images/site-tree-icon.png'; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | public function getCMSFields() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | public function canEdit($member = null) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param null|int|Member $member |
||
| 209 | * |
||
| 210 | * @return null|Member |
||
| 211 | */ |
||
| 212 | View Code Duplication | protected function getMember($member = null) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Check if this member is an editor of the blog. |
||
| 227 | * |
||
| 228 | * @param Member $member |
||
| 229 | * |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | public function isEditor($member) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Determine if the given member belongs to the given relation. |
||
| 242 | * |
||
| 243 | * @param Member $member |
||
| 244 | * @param DataList $relation |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | View Code Duplication | protected function isMemberOf($member, $relation) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Determine the role of the given member. |
||
| 263 | * |
||
| 264 | * Call be called via template to determine the current user. |
||
| 265 | * |
||
| 266 | * @example "Hello $RoleOf($CurrentMember.ID)" |
||
| 267 | * |
||
| 268 | * @param int|Member $member |
||
| 269 | * |
||
| 270 | * @return null|string |
||
| 271 | */ |
||
| 272 | public function RoleOf($member) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Check if this member is a writer of the blog. |
||
| 299 | * |
||
| 300 | * @param Member $member |
||
| 301 | * |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | public function isWriter($member) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Check if this member is a contributor of the blog. |
||
| 314 | * |
||
| 315 | * @param Member $member |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function isContributor($member) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * {@inheritdoc} |
||
| 329 | */ |
||
| 330 | public function canAddChildren($member = null) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * {@inheritdoc} |
||
| 343 | */ |
||
| 344 | public function getSettingsFields() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Gets the list of user candidates to be assigned to assist with this blog. |
||
| 419 | * |
||
| 420 | * @return SS_List |
||
| 421 | */ |
||
| 422 | protected function getCandidateUsers() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Determine if this user can edit the editors list. |
||
| 437 | * |
||
| 438 | * @param int|Member $member |
||
| 439 | * |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | public function canEditEditors($member = null) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Determine if this user can edit writers list. |
||
| 457 | * |
||
| 458 | * @param int|Member $member |
||
| 459 | * |
||
| 460 | * @return boolean |
||
| 461 | */ |
||
| 462 | View Code Duplication | public function canEditWriters($member = null) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Determines if this user can edit the contributors list. |
||
| 481 | * |
||
| 482 | * @param int|Member $member |
||
| 483 | * |
||
| 484 | * @return boolean |
||
| 485 | */ |
||
| 486 | View Code Duplication | public function canEditContributors($member = null) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Returns BlogPosts for a given date period. |
||
| 505 | * |
||
| 506 | * @param int $year |
||
| 507 | * @param null|int $month |
||
| 508 | * @param null|int $day |
||
| 509 | * |
||
| 510 | * @return DataList |
||
| 511 | */ |
||
| 512 | public function getArchivedBlogPosts($year, $month = null, $day = null) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Return blog posts. |
||
| 550 | * |
||
| 551 | * @return DataList of BlogPost objects |
||
| 552 | */ |
||
| 553 | public function getBlogPosts() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Get a link to a Member profile. |
||
| 564 | * |
||
| 565 | * @param string $urlSegment |
||
| 566 | * |
||
| 567 | * @return string |
||
| 568 | */ |
||
| 569 | public function ProfileLink($urlSegment) |
||
| 579 | |||
| 580 | /** |
||
| 581 | * This sets the title for our gridfield. |
||
| 582 | * |
||
| 583 | * @return string |
||
| 584 | */ |
||
| 585 | public function getLumberjackTitle() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * This overwrites lumberjacks default gridfield config. |
||
| 592 | * |
||
| 593 | * @return GridFieldConfig |
||
| 594 | */ |
||
| 595 | public function getLumberjackGridFieldConfig() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * {@inheritdoc} |
||
| 602 | */ |
||
| 603 | public function providePermissions() |
||
| 620 | |||
| 621 | /** |
||
| 622 | * {@inheritdoc} |
||
| 623 | */ |
||
| 624 | protected function onBeforeWrite() |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Assign users as necessary to the blog group. |
||
| 632 | */ |
||
| 633 | protected function assignGroup() |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Gets or creates the group used to assign CMS access. |
||
| 655 | * |
||
| 656 | * @return Group |
||
| 657 | */ |
||
| 658 | protected function getUserGroup() |
||
| 681 | } |
||
| 682 |
This check marks private properties in classes that are never used. Those properties can be removed.