Complex classes like VirtualPage 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 VirtualPage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class VirtualPage extends Page |
||
| 27 | { |
||
| 28 | |||
| 29 | private static $description = 'Displays the content of another page'; |
||
| 30 | |||
| 31 | public static $virtualFields; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array Define fields that are not virtual - the virtual page must define these fields themselves. |
||
| 35 | * Note that anything in {@link self::config()->initially_copied_fields} is implicitly included in this list. |
||
| 36 | */ |
||
| 37 | private static $non_virtual_fields = array( |
||
| 38 | "ID", |
||
| 39 | "ClassName", |
||
| 40 | "ObsoleteClassName", |
||
| 41 | "SecurityTypeID", |
||
| 42 | "OwnerID", |
||
| 43 | "ParentID", |
||
| 44 | "URLSegment", |
||
| 45 | "Sort", |
||
| 46 | "Status", |
||
| 47 | 'ShowInMenus', |
||
| 48 | // 'Locale' |
||
| 49 | 'ShowInSearch', |
||
| 50 | 'Version', |
||
| 51 | "Embargo", |
||
| 52 | "Expiry", |
||
| 53 | "CanViewType", |
||
| 54 | "CanEditType", |
||
| 55 | "CopyContentFromID", |
||
| 56 | "HasBrokenLink", |
||
| 57 | ); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array Define fields that are initially copied to virtual pages but left modifiable after that. |
||
| 61 | */ |
||
| 62 | private static $initially_copied_fields = array( |
||
| 63 | 'ShowInMenus', |
||
| 64 | 'ShowInSearch', |
||
| 65 | 'URLSegment', |
||
| 66 | ); |
||
| 67 | |||
| 68 | private static $has_one = array( |
||
| 69 | "CopyContentFrom" => "SilverStripe\\CMS\\Model\\SiteTree", |
||
| 70 | ); |
||
| 71 | |||
| 72 | private static $owns = array( |
||
| 73 | "CopyContentFrom", |
||
| 74 | ); |
||
| 75 | |||
| 76 | private static $db = array( |
||
| 77 | "VersionID" => "Int", |
||
| 78 | ); |
||
| 79 | |||
| 80 | private static $table_name = 'VirtualPage'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Generates the array of fields required for the page type. |
||
| 84 | * |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | public function getVirtualFields() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * List of fields or properties to never virtualise |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function getNonVirtualisedFields() |
||
| 113 | |||
| 114 | public function setCopyContentFromID($val) |
||
| 122 | |||
| 123 | public function ContentSource() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * For VirtualPage, add a canonical link tag linking to the original page |
||
| 134 | * See TRAC #6828 & http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394 |
||
| 135 | * |
||
| 136 | * @param boolean $includeTitle Show default <title>-tag, set to false for custom templating |
||
| 137 | * @return string The XHTML metatags |
||
| 138 | */ |
||
| 139 | public function MetaTags($includeTitle = true) |
||
| 149 | |||
| 150 | public function allowedChildren() |
||
| 158 | |||
| 159 | public function syncLinkTracking() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * We can only publish the page if there is a published source page |
||
| 171 | * |
||
| 172 | * @param Member $member Member to check |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | public function canPublish($member = null) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Returns true if is page is publishable by anyone at all |
||
| 182 | * Return false if the source page isn't published yet. |
||
| 183 | * |
||
| 184 | * Note that isPublishable doesn't affect ete from live, only publish. |
||
| 185 | */ |
||
| 186 | public function isPublishable() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Generate the CMS fields from the fields from the original page. |
||
| 208 | */ |
||
| 209 | public function getCMSFields() |
||
| 277 | |||
| 278 | public function onBeforeWrite() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Copy any fields from the copied record to bootstrap /backup |
||
| 286 | */ |
||
| 287 | protected function refreshFromCopied() |
||
| 308 | |||
| 309 | public function getSettingsFields() |
||
| 330 | |||
| 331 | public function validate() |
||
| 351 | |||
| 352 | public function updateImageTracking() |
||
| 365 | |||
| 366 | public function CMSTreeClasses() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Allow attributes on the master page to pass |
||
| 373 | * through to the virtual page |
||
| 374 | * |
||
| 375 | * @param string $field |
||
| 376 | * @return mixed |
||
| 377 | */ |
||
| 378 | public function __get($field) |
||
| 391 | |||
| 392 | public function getField($field) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Check if given field is virtualised |
||
| 402 | * |
||
| 403 | * @param string $field |
||
| 404 | * @return bool |
||
| 405 | */ |
||
| 406 | public function isFieldVirtualised($field) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Pass unrecognized method calls on to the original data object |
||
| 426 | * |
||
| 427 | * @param string $method |
||
| 428 | * @param array $args |
||
| 429 | * @return mixed |
||
| 430 | */ |
||
| 431 | public function __call($method, $args) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @param string $field |
||
| 442 | * @return bool |
||
| 443 | */ |
||
| 444 | public function hasField($field) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) for a field |
||
| 455 | * on this object. |
||
| 456 | * |
||
| 457 | * @param string $field |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | public function castingHelper($field) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * {@inheritdoc} |
||
| 471 | */ |
||
| 472 | public function allMethodNames($custom = false) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * {@inheritdoc} |
||
| 485 | */ |
||
| 486 | public function getControllerName() |
||
| 494 | } |
||
| 495 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: