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 |
||
| 44 | class Blog extends Page implements PermissionProvider |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * Permission for user management. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | const MANAGE_USERS = 'BLOG_MANAGE_USERS'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * If true, users assigned as editor, writer, or contributor will be automatically granted |
||
| 55 | * CMS_ACCESS_CMSMain permission. If false, only users with this permission already may be |
||
| 56 | * assigned. |
||
| 57 | * |
||
| 58 | * @config |
||
| 59 | * |
||
| 60 | * @var boolean |
||
| 61 | */ |
||
| 62 | private static $grant_user_access = true; |
||
|
|
|||
| 63 | |||
| 64 | /** |
||
| 65 | * Permission to either require, or grant to users assigned to work on this blog. |
||
| 66 | * |
||
| 67 | * @config |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | private static $grant_user_permission = 'CMS_ACCESS_CMSMain'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Group code to assign newly granted users to. |
||
| 75 | * |
||
| 76 | * @config |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | private static $grant_user_group = 'blog-users'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritDoc} |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private static $table_name = 'Blog'; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | private static $db = array( |
||
| 92 | 'PostsPerPage' => 'Int', |
||
| 93 | ); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | private static $has_many = array( |
||
| 99 | 'Tags' => BlogTag::class, |
||
| 100 | 'Categories' => BlogCategory::class, |
||
| 101 | ); |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | private static $many_many = array( |
||
| 107 | 'Editors' => Member::class, |
||
| 108 | 'Writers' => Member::class, |
||
| 109 | 'Contributors' => Member::class, |
||
| 110 | ); |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | private static $allowed_children = array( |
||
| 116 | BlogPost::class, |
||
| 117 | ); |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | private static $extensions = array( |
||
| 123 | BlogFilter::class, |
||
| 124 | ); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | private static $defaults = array( |
||
| 130 | 'ProvideComments' => false, |
||
| 131 | 'PostsPerPage' => 10 |
||
| 132 | ); |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var string |
||
| 136 | */ |
||
| 137 | private static $description = 'Adds a blog to your website.'; |
||
| 138 | |||
| 139 | private static $icon = 'blog/images/site-tree-icon.png'; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | public function getCMSFields() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | public function canEdit($member = null) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param null|int|Member $member |
||
| 213 | * |
||
| 214 | * @return null|Member |
||
| 215 | */ |
||
| 216 | View Code Duplication | protected function getMember($member = null) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Check if this member is an editor of the blog. |
||
| 231 | * |
||
| 232 | * @param Member $member |
||
| 233 | * |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | public function isEditor($member) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Determine if the given member belongs to the given relation. |
||
| 246 | * |
||
| 247 | * @param Member $member |
||
| 248 | * @param DataList $relation |
||
| 249 | * |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | View Code Duplication | protected function isMemberOf($member, $relation) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Determine the role of the given member. |
||
| 267 | * |
||
| 268 | * Call be called via template to determine the current user. |
||
| 269 | * |
||
| 270 | * @example "Hello $RoleOf($CurrentMember.ID)" |
||
| 271 | * |
||
| 272 | * @param int|Member $member |
||
| 273 | * |
||
| 274 | * @return null|string |
||
| 275 | */ |
||
| 276 | public function RoleOf($member) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Check if this member is a writer of the blog. |
||
| 303 | * |
||
| 304 | * @param Member $member |
||
| 305 | * |
||
| 306 | * @return bool |
||
| 307 | */ |
||
| 308 | public function isWriter($member) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Check if this member is a contributor of the blog. |
||
| 318 | * |
||
| 319 | * @param Member $member |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | public function isContributor($member) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * {@inheritdoc} |
||
| 333 | */ |
||
| 334 | public function canAddChildren($member = null) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * {@inheritdoc} |
||
| 347 | */ |
||
| 348 | public function getSettingsFields() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Gets the list of user candidates to be assigned to assist with this blog. |
||
| 423 | * |
||
| 424 | * @return SS_List |
||
| 425 | */ |
||
| 426 | protected function getCandidateUsers() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Determine if this user can edit the editors list. |
||
| 441 | * |
||
| 442 | * @param int|Member $member |
||
| 443 | * |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | public function canEditEditors($member = null) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Determine if this user can edit writers list. |
||
| 461 | * |
||
| 462 | * @param int|Member $member |
||
| 463 | * |
||
| 464 | * @return boolean |
||
| 465 | */ |
||
| 466 | View Code Duplication | public function canEditWriters($member = null) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Determines if this user can edit the contributors list. |
||
| 485 | * |
||
| 486 | * @param int|Member $member |
||
| 487 | * |
||
| 488 | * @return boolean |
||
| 489 | */ |
||
| 490 | View Code Duplication | public function canEditContributors($member = null) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Returns BlogPosts for a given date period. |
||
| 509 | * |
||
| 510 | * @param int $year |
||
| 511 | * @param null|int $month |
||
| 512 | * @param null|int $day |
||
| 513 | * |
||
| 514 | * @return DataList |
||
| 515 | */ |
||
| 516 | public function getArchivedBlogPosts($year, $month = null, $day = null) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Return blog posts. |
||
| 554 | * |
||
| 555 | * @return DataList of BlogPost objects |
||
| 556 | */ |
||
| 557 | public function getBlogPosts() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Get a link to a Member profile. |
||
| 568 | * |
||
| 569 | * @param string $urlSegment |
||
| 570 | * |
||
| 571 | * @return string |
||
| 572 | */ |
||
| 573 | public function ProfileLink($urlSegment) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * This sets the title for our gridfield. |
||
| 586 | * |
||
| 587 | * @return string |
||
| 588 | */ |
||
| 589 | public function getLumberjackTitle() |
||
| 593 | |||
| 594 | /** |
||
| 595 | * This overwrites lumberjacks default gridfield config. |
||
| 596 | * |
||
| 597 | * @return GridFieldConfig |
||
| 598 | */ |
||
| 599 | public function getLumberjackGridFieldConfig() |
||
| 603 | |||
| 604 | /** |
||
| 605 | * {@inheritdoc} |
||
| 606 | */ |
||
| 607 | public function providePermissions() |
||
| 624 | |||
| 625 | /** |
||
| 626 | * {@inheritdoc} |
||
| 627 | */ |
||
| 628 | protected function onBeforeWrite() |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Assign users as necessary to the blog group. |
||
| 636 | */ |
||
| 637 | protected function assignGroup() |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Gets or creates the group used to assign CMS access. |
||
| 659 | * |
||
| 660 | * @return Group |
||
| 661 | */ |
||
| 662 | protected function getUserGroup() |
||
| 685 | } |
||
| 686 |
This check marks private properties in classes that are never used. Those properties can be removed.