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 BlogPost 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 BlogPost, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class BlogPost extends Page {
|
||
|
|
|||
| 19 | |||
| 20 | /** |
||
| 21 | * Same as above, but for list of users that can be |
||
| 22 | * given credit in the author field for blog posts |
||
| 23 | * @var string|bool false or group code |
||
| 24 | */ |
||
| 25 | private static $restrict_authors_to_group = false; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private static $db = array( |
||
| 31 | 'PublishDate' => 'SS_Datetime', |
||
| 32 | 'AuthorNames' => 'Varchar(1024)', |
||
| 33 | 'Summary' => 'HTMLText', |
||
| 34 | ); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private static $has_one = array( |
||
| 40 | 'FeaturedImage' => 'Image', |
||
| 41 | ); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private static $many_many = array( |
||
| 47 | 'Categories' => 'BlogCategory', |
||
| 48 | 'Tags' => 'BlogTag', |
||
| 49 | 'Authors' => 'Member', |
||
| 50 | ); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private static $defaults = array( |
||
| 56 | 'ShowInMenus' => false, |
||
| 57 | 'InheritSideBar' => true, |
||
| 58 | 'ProvideComments' => true, |
||
| 59 | ); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private static $extensions = array( |
||
| 65 | 'BlogPostFilter', |
||
| 66 | ); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | private static $searchable_fields = array( |
||
| 72 | 'Title', |
||
| 73 | ); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | private static $summary_fields = array( |
||
| 79 | 'Title', |
||
| 80 | ); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private static $casting = array( |
||
| 86 | 'Excerpt' => 'Text', |
||
| 87 | ); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | private static $allowed_children = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The default sorting lists BlogPosts with an empty PublishDate at the top. |
||
| 96 | * |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC'; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | private static $can_be_root = false; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * This will display or hide the current class from the SiteTree. This variable can be |
||
| 108 | * configured using YAML. |
||
| 109 | * |
||
| 110 | * @var bool |
||
| 111 | */ |
||
| 112 | private static $show_in_sitetree = false; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Determine the role of the given member. |
||
| 116 | * |
||
| 117 | * Call be called via template to determine the current user. |
||
| 118 | * |
||
| 119 | * @example "Hello $RoleOf($CurrentMember.ID)" |
||
| 120 | * |
||
| 121 | * @param null|int|Member $member |
||
| 122 | * |
||
| 123 | * @return null|string |
||
| 124 | */ |
||
| 125 | public function RoleOf($member = null) {
|
||
| 144 | |||
| 145 | /** |
||
| 146 | * Determine if the given member is an author of this post. |
||
| 147 | * |
||
| 148 | * @param null|Member $member |
||
| 149 | * |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function isAuthor($member = null) {
|
|
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc}
|
||
| 168 | */ |
||
| 169 | public function getCMSFields() {
|
||
| 170 | Requirements::css(BLOGGER_DIR . '/css/cms.css'); |
||
| 171 | Requirements::javascript(BLOGGER_DIR . '/js/cms.js'); |
||
| 172 | |||
| 173 | $self =& $this; |
||
| 174 | |||
| 175 | $this->beforeUpdateCMSFields(function ($fields) use ($self) {
|
||
| 176 | $uploadField = UploadField::create('FeaturedImage', _t('BlogPost.FeaturedImage', 'Banner Image'));
|
||
| 177 | $uploadField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
|
||
| 178 | |||
| 179 | /** |
||
| 180 | * @var FieldList $fields |
||
| 181 | */ |
||
| 182 | $fields->insertAfter($uploadField, 'Content'); |
||
| 183 | |||
| 184 | $summary = HtmlEditorField::create('Summary', false);
|
||
| 185 | $summary->setRows(5); |
||
| 186 | $summary->setDescription(_t( |
||
| 187 | 'BlogPost.SUMMARY_DESCRIPTION', |
||
| 188 | 'If no summary is specified the first 30 words will be used.' |
||
| 189 | )); |
||
| 190 | |||
| 191 | $summaryHolder = ToggleCompositeField::create( |
||
| 192 | 'CustomSummary', |
||
| 193 | _t('BlogPost.CUSTOMSUMMARY', 'Add A Custom Summary'),
|
||
| 194 | array( |
||
| 195 | $summary, |
||
| 196 | ) |
||
| 197 | ); |
||
| 198 | $summaryHolder->setHeadingLevel(4); |
||
| 199 | $summaryHolder->addExtraClass('custom-summary');
|
||
| 200 | |||
| 201 | $fields->insertAfter($summaryHolder, 'FeaturedImage'); |
||
| 202 | |||
| 203 | $fields->push(HiddenField::create('MenuTitle'));
|
||
| 204 | |||
| 205 | $urlSegment = $fields->dataFieldByName('URLSegment');
|
||
| 206 | $urlSegment->setURLPrefix($self->Parent()->RelativeLink()); |
||
| 207 | |||
| 208 | $fields->removeFieldsFromTab('Root.Main', array(
|
||
| 209 | 'MenuTitle', |
||
| 210 | 'URLSegment', |
||
| 211 | )); |
||
| 212 | |||
| 213 | $authorField = ListboxField::create( |
||
| 214 | 'Authors', |
||
| 215 | _t('BlogPost.Authors', 'Authors'),
|
||
| 216 | $this->getCandidateAuthors()->map()->toArray() |
||
| 217 | )->setMultiple(true); |
||
| 218 | |||
| 219 | $authorNames = TextField::create( |
||
| 220 | 'AuthorNames', |
||
| 221 | _t('BlogPost.AdditionalCredits', 'Additional Credits'),
|
||
| 222 | null, |
||
| 223 | 1024 |
||
| 224 | )->setDescription(_t( |
||
| 225 | 'BlogPost.AdditionalCredits_Description', |
||
| 226 | 'If some authors of this post don\'t have CMS access, enter their name(s) here. You can separate multiple names with a comma.') |
||
| 227 | ); |
||
| 228 | |||
| 229 | if(!$self->canEditAuthors()) {
|
||
| 230 | $authorField = $authorField->performDisabledTransformation(); |
||
| 231 | $authorNames = $authorNames->performDisabledTransformation(); |
||
| 232 | } |
||
| 233 | |||
| 234 | $publishDate = DatetimeField::create('PublishDate', _t('BlogPost.PublishDate', 'Publish Date'));
|
||
| 235 | $publishDate->getDateField()->setConfig('showcalendar', true);
|
||
| 236 | if(!$self->PublishDate) {
|
||
| 237 | $publishDate->setDescription(_t( |
||
| 238 | 'BlogPost.PublishDate_Description', |
||
| 239 | 'Will be set to "now" if published without a value.') |
||
| 240 | ); |
||
| 241 | } |
||
| 242 | |||
| 243 | // Get categories and tags |
||
| 244 | $parent = $self->Parent(); |
||
| 245 | $categories = $parent instanceof Blog |
||
| 246 | ? $parent->Categories() |
||
| 247 | : BlogCategory::get(); |
||
| 248 | $tags = $parent instanceof Blog |
||
| 249 | ? $parent->Tags() |
||
| 250 | : BlogTag::get(); |
||
| 251 | |||
| 252 | $options = BlogAdminSidebar::create( |
||
| 253 | $publishDate, |
||
| 254 | $urlSegment, |
||
| 255 | TagField::create( |
||
| 256 | 'Categories', |
||
| 257 | _t('BlogPost.Categories', 'Categories'),
|
||
| 258 | $categories, |
||
| 259 | $self->Categories() |
||
| 260 | ) |
||
| 261 | ->setCanCreate($self->canCreateCategories()) |
||
| 262 | ->setShouldLazyLoad(true), |
||
| 263 | TagField::create( |
||
| 264 | 'Tags', |
||
| 265 | _t('BlogPost.Tags', 'Tags'),
|
||
| 266 | $tags, |
||
| 267 | $self->Tags() |
||
| 268 | ) |
||
| 269 | ->setCanCreate($self->canCreateTags()) |
||
| 270 | ->setShouldLazyLoad(true), |
||
| 271 | $authorField, |
||
| 272 | $authorNames |
||
| 273 | )->setTitle('Post Options');
|
||
| 274 | |||
| 275 | $options->setName('blog-admin-sidebar');
|
||
| 276 | |||
| 277 | $fields->insertBefore($options, 'Root'); |
||
| 278 | }); |
||
| 279 | |||
| 280 | $fields = parent::getCMSFields(); |
||
| 281 | |||
| 282 | $fields->fieldByName('Root')->setTemplate('TabSet_holder');
|
||
| 283 | |||
| 284 | return $fields; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Gets the list of author candidates to be assigned as authors of this blog post. |
||
| 289 | * |
||
| 290 | * @return SS_List |
||
| 291 | */ |
||
| 292 | public function getCandidateAuthors() {
|
||
| 293 | if($this->config()->restrict_authors_to_group) {
|
||
| 294 | return Group::get()->filter('Code', $this->config()->restrict_authors_to_group)->first()->Members();
|
||
| 295 | } else {
|
||
| 296 | $list = Member::get(); |
||
| 297 | $this->extend('updateCandidateAuthors', $list);
|
||
| 298 | return $list; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Determine if this user can edit the authors list. |
||
| 304 | * |
||
| 305 | * @param null|int|Member $member |
||
| 306 | * |
||
| 307 | * @return bool |
||
| 308 | */ |
||
| 309 | public function canEditAuthors($member = null) {
|
||
| 310 | $member = $this->getMember($member); |
||
| 311 | |||
| 312 | $extended = $this->extendedCan('canEditAuthors', $member);
|
||
| 313 | |||
| 314 | if($extended !== null) {
|
||
| 315 | return $extended; |
||
| 316 | } |
||
| 317 | |||
| 318 | $parent = $this->Parent(); |
||
| 319 | |||
| 320 | if($parent instanceof Blog && $parent->exists()) {
|
||
| 321 | if($parent->isEditor($member)) {
|
||
| 322 | return true; |
||
| 323 | } |
||
| 324 | |||
| 325 | if($parent->isWriter($member) && $this->isAuthor($member)) {
|
||
| 326 | return true; |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | return Permission::checkMember($member, Blog::MANAGE_USERS); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param null|int|Member $member |
||
| 335 | * |
||
| 336 | * @return null|Member |
||
| 337 | */ |
||
| 338 | View Code Duplication | protected function getMember($member = null) {
|
|
| 339 | if(!$member) {
|
||
| 340 | $member = Member::currentUser(); |
||
| 341 | } |
||
| 342 | |||
| 343 | if(is_numeric($member)) {
|
||
| 344 | $member = Member::get()->byID($member); |
||
| 345 | } |
||
| 346 | |||
| 347 | return $member; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Determine whether user can create new categories. |
||
| 352 | * |
||
| 353 | * @param null|int|Member $member |
||
| 354 | * |
||
| 355 | * @return bool |
||
| 356 | */ |
||
| 357 | View Code Duplication | public function canCreateCategories($member = null) {
|
|
| 358 | $member = $this->getMember($member); |
||
| 359 | |||
| 360 | $parent = $this->Parent(); |
||
| 361 | |||
| 362 | if(!$parent || !$parent->exists() || !($parent instanceof Blog)) {
|
||
| 363 | return false; |
||
| 364 | } |
||
| 365 | |||
| 366 | if($parent->isEditor($member)) {
|
||
| 367 | return true; |
||
| 368 | } |
||
| 369 | |||
| 370 | return Permission::checkMember($member, 'ADMIN'); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Determine whether user can create new tags. |
||
| 375 | * |
||
| 376 | * @param null|int|Member $member |
||
| 377 | * |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | View Code Duplication | public function canCreateTags($member = null) {
|
|
| 381 | $member = $this->getMember($member); |
||
| 382 | |||
| 383 | $parent = $this->Parent(); |
||
| 384 | |||
| 385 | if(!$parent || !$parent->exists() || !($parent instanceof Blog)) {
|
||
| 386 | return false; |
||
| 387 | } |
||
| 388 | |||
| 389 | if($parent->isEditor($member)) {
|
||
| 390 | return true; |
||
| 391 | } |
||
| 392 | |||
| 393 | if($parent->isWriter($member)) {
|
||
| 394 | return true; |
||
| 395 | } |
||
| 396 | |||
| 397 | return Permission::checkMember($member, 'ADMIN'); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * {@inheritdoc}
|
||
| 402 | * |
||
| 403 | * Update the PublishDate to now if the BlogPost would otherwise be published without a date. |
||
| 404 | */ |
||
| 405 | public function onBeforePublish() {
|
||
| 406 | /** |
||
| 407 | * @var SS_Datetime $publishDate |
||
| 408 | */ |
||
| 409 | $publishDate = $this->dbObject('PublishDate');
|
||
| 410 | |||
| 411 | if(!$publishDate->getValue()) {
|
||
| 412 | $this->PublishDate = SS_Datetime::now()->getValue(); |
||
| 413 | $this->write(); |
||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * {@inheritdoc}
|
||
| 419 | * |
||
| 420 | * Sets blog relationship on all categories and tags assigned to this post. |
||
| 421 | */ |
||
| 422 | public function onAfterWrite() {
|
||
| 423 | parent::onAfterWrite(); |
||
| 424 | |||
| 425 | foreach($this->Categories() as $category) {
|
||
| 426 | /** |
||
| 427 | * @var BlogCategory $category |
||
| 428 | */ |
||
| 429 | $category->BlogID = $this->ParentID; |
||
| 430 | $category->write(); |
||
| 431 | } |
||
| 432 | |||
| 433 | foreach($this->Tags() as $tag) {
|
||
| 434 | /** |
||
| 435 | * @var BlogTag $tag |
||
| 436 | */ |
||
| 437 | $tag->BlogID = $this->ParentID; |
||
| 438 | $tag->write(); |
||
| 439 | } |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * {@inheritdoc}
|
||
| 444 | */ |
||
| 445 | View Code Duplication | public function canView($member = null) {
|
|
| 465 | |||
| 466 | /** |
||
| 467 | * {@inheritdoc}
|
||
| 468 | */ |
||
| 469 | public function canPublish($member = null) {
|
||
| 470 | $member = $this->getMember($member); |
||
| 471 | |||
| 472 | if(Permission::checkMember($member, 'ADMIN')) {
|
||
| 473 | return true; |
||
| 500 | |||
| 501 | /** |
||
| 502 | * {@inheritdoc}
|
||
| 503 | */ |
||
| 504 | public function canEdit($member = null) {
|
||
| 527 | |||
| 528 | /** |
||
| 529 | * Returns the post excerpt. |
||
| 530 | * |
||
| 531 | * @param int $wordsToDisplay |
||
| 532 | * |
||
| 533 | * @return string |
||
| 534 | */ |
||
| 535 | public function Excerpt($wordsToDisplay = 30) {
|
||
| 543 | |||
| 544 | /** |
||
| 545 | * Returns a monthly archive link for the current blog post. |
||
| 546 | * |
||
| 547 | * @param string $type |
||
| 548 | * |
||
| 549 | * @return string |
||
| 550 | */ |
||
| 551 | public function getMonthlyArchiveLink($type = 'day') {
|
||
| 572 | |||
| 573 | /** |
||
| 574 | * Returns a yearly archive link for the current blog post. |
||
| 575 | * |
||
| 576 | * @return string |
||
| 577 | */ |
||
| 578 | public function getYearlyArchiveLink() {
|
||
| 586 | |||
| 587 | /** |
||
| 588 | * Resolves static and dynamic authors linked to this post. |
||
| 589 | * |
||
| 590 | * @return ArrayList |
||
| 591 | */ |
||
| 592 | public function getCredits() {
|
||
| 600 | |||
| 601 | /** |
||
| 602 | * Resolves dynamic authors linked to this post. |
||
| 603 | * |
||
| 604 | * @return ArrayList |
||
| 605 | */ |
||
| 606 | protected function getDynamicCredits() {
|
||
| 631 | |||
| 632 | /** |
||
| 633 | * Resolves static authors linked to this post. |
||
| 634 | * |
||
| 635 | * @return ArrayList |
||
| 636 | */ |
||
| 637 | protected function getStaticCredits() {
|
||
| 652 | |||
| 653 | /** |
||
| 654 | * Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name'). |
||
| 655 | * |
||
| 656 | * @param bool $includeRelations |
||
| 657 | * |
||
| 658 | * @return array |
||
| 659 | */ |
||
| 660 | public function fieldLabels($includeRelations = true) {
|
||
| 667 | |||
| 668 | /** |
||
| 669 | * {@inheritdoc}
|
||
| 670 | */ |
||
| 671 | protected function onBeforeWrite() {
|
||
| 678 | } |
||
| 679 | |||
| 687 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.