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 |
||
| 35 | class Blog extends Page implements PermissionProvider |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Permission for user management. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | const MANAGE_USERS = 'BLOG_MANAGE_USERS'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * If true, users assigned as editor, writer, or contributor will be automatically granted |
||
| 46 | * CMS_ACCESS_CMSMain permission. If false, only users with this permission already may be |
||
| 47 | * assigned. |
||
| 48 | * |
||
| 49 | * @config |
||
| 50 | * |
||
| 51 | * @var boolean |
||
| 52 | */ |
||
| 53 | private static $grant_user_access = true; |
||
|
|
|||
| 54 | |||
| 55 | /** |
||
| 56 | * Permission to either require, or grant to users assigned to work on this blog. |
||
| 57 | * |
||
| 58 | * @config |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private static $grant_user_permission = 'CMS_ACCESS_CMSMain'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Group code to assign newly granted users to. |
||
| 66 | * |
||
| 67 | * @config |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | private static $grant_user_group = 'blog-users'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritDoc} |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | private static $table_name = 'Blog'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private static $db = [ |
||
| 83 | 'PostsPerPage' => 'Int', |
||
| 84 | ]; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | private static $has_many = [ |
||
| 90 | 'Tags' => BlogTag::class, |
||
| 91 | 'Categories' => BlogCategory::class, |
||
| 92 | ]; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | private static $many_many = [ |
||
| 98 | 'Editors' => Member::class, |
||
| 99 | 'Writers' => Member::class, |
||
| 100 | 'Contributors' => Member::class, |
||
| 101 | ]; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | private static $allowed_children = [ |
||
| 107 | BlogPost::class, |
||
| 108 | ]; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var array |
||
| 112 | */ |
||
| 113 | private static $extensions = [ |
||
| 114 | BlogFilter::class, |
||
| 115 | ]; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var array |
||
| 119 | */ |
||
| 120 | private static $defaults = [ |
||
| 121 | 'ProvideComments' => false, |
||
| 122 | 'PostsPerPage' => 10 |
||
| 123 | ]; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var string |
||
| 127 | */ |
||
| 128 | private static $description = 'Adds a blog to your website.'; |
||
| 129 | |||
| 130 | private static $icon = 'blog/images/site-tree-icon.png'; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function getCMSFields() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Adds CMS related css and js overrides |
||
| 189 | */ |
||
| 190 | protected function addCMSRequirements() |
||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | public function canEdit($member = null) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param null|int|Member $member |
||
| 212 | * |
||
| 213 | * @return null|Member |
||
| 214 | */ |
||
| 215 | View Code Duplication | protected function getMember($member = null) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Check if this member is an editor of the blog. |
||
| 230 | * |
||
| 231 | * @param Member $member |
||
| 232 | * |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | public function isEditor($member) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Determine if the given member belongs to the given relation. |
||
| 245 | * |
||
| 246 | * @param Member $member |
||
| 247 | * @param DataList $relation |
||
| 248 | * |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | View Code Duplication | protected function isMemberOf($member, $relation) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Determine the role of the given member. |
||
| 266 | * |
||
| 267 | * Call be called via template to determine the current user. |
||
| 268 | * |
||
| 269 | * @example "Hello $RoleOf($CurrentMember.ID)" |
||
| 270 | * |
||
| 271 | * @param int|Member $member |
||
| 272 | * |
||
| 273 | * @return null|string |
||
| 274 | */ |
||
| 275 | public function RoleOf($member) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Check if this member is a writer of the blog. |
||
| 302 | * |
||
| 303 | * @param Member $member |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | public function isWriter($member) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Check if this member is a contributor of the blog. |
||
| 317 | * |
||
| 318 | * @param Member $member |
||
| 319 | * |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | public function isContributor($member) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * {@inheritdoc} |
||
| 332 | */ |
||
| 333 | public function canAddChildren($member = null) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * {@inheritdoc} |
||
| 346 | */ |
||
| 347 | public function getSettingsFields() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Gets the list of user candidates to be assigned to assist with this blog. |
||
| 434 | * |
||
| 435 | * @return SS_List |
||
| 436 | */ |
||
| 437 | protected function getCandidateUsers() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Determine if this user can edit the editors list. |
||
| 452 | * |
||
| 453 | * @param int|Member $member |
||
| 454 | * |
||
| 455 | * @return bool |
||
| 456 | */ |
||
| 457 | public function canEditEditors($member = null) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Determine if this user can edit writers list. |
||
| 472 | * |
||
| 473 | * @param int|Member $member |
||
| 474 | * |
||
| 475 | * @return boolean |
||
| 476 | */ |
||
| 477 | View Code Duplication | public function canEditWriters($member = null) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Determines if this user can edit the contributors list. |
||
| 496 | * |
||
| 497 | * @param int|Member $member |
||
| 498 | * |
||
| 499 | * @return boolean |
||
| 500 | */ |
||
| 501 | View Code Duplication | public function canEditContributors($member = null) |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Returns BlogPosts for a given date period. |
||
| 520 | * |
||
| 521 | * @param int $year |
||
| 522 | * @param null|int $month |
||
| 523 | * @param null|int $day |
||
| 524 | * |
||
| 525 | * @return DataList |
||
| 526 | */ |
||
| 527 | public function getArchivedBlogPosts($year, $month = null, $day = null) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Return blog posts. |
||
| 565 | * |
||
| 566 | * @return DataList of BlogPost objects |
||
| 567 | */ |
||
| 568 | public function getBlogPosts() |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Get a link to a Member profile. |
||
| 579 | * |
||
| 580 | * @param string $urlSegment |
||
| 581 | * |
||
| 582 | * @return string |
||
| 583 | */ |
||
| 584 | public function ProfileLink($urlSegment) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * This sets the title for our gridfield. |
||
| 597 | * |
||
| 598 | * @return string |
||
| 599 | */ |
||
| 600 | public function getLumberjackTitle() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * This overwrites lumberjacks default gridfield config. |
||
| 607 | * |
||
| 608 | * @return GridFieldConfig |
||
| 609 | */ |
||
| 610 | public function getLumberjackGridFieldConfig() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * {@inheritdoc} |
||
| 617 | */ |
||
| 618 | public function providePermissions() |
||
| 635 | |||
| 636 | /** |
||
| 637 | * {@inheritdoc} |
||
| 638 | */ |
||
| 639 | protected function onBeforeWrite() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Assign users as necessary to the blog group. |
||
| 647 | */ |
||
| 648 | protected function assignGroup() |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Gets or creates the group used to assign CMS access. |
||
| 670 | * |
||
| 671 | * @return Group |
||
| 672 | */ |
||
| 673 | protected function getUserGroup() |
||
| 696 | } |
||
| 697 |
This check marks private properties in classes that are never used. Those properties can be removed.