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 |
||
| 24 | class VirtualPage extends Page |
||
| 25 | { |
||
| 26 | |||
| 27 | private static $description = 'Displays the content of another page'; |
||
| 28 | |||
| 29 | public static $virtualFields; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array Define fields that are not virtual - the virtual page must define these fields themselves. |
||
| 33 | * Note that anything in {@link self::config()->initially_copied_fields} is implicitly included in this list. |
||
| 34 | */ |
||
| 35 | private static $non_virtual_fields = array( |
||
| 36 | "ID", |
||
| 37 | "ClassName", |
||
| 38 | "ObsoleteClassName", |
||
| 39 | "SecurityTypeID", |
||
| 40 | "OwnerID", |
||
| 41 | "ParentID", |
||
| 42 | "URLSegment", |
||
| 43 | "Sort", |
||
| 44 | "Status", |
||
| 45 | 'ShowInMenus', |
||
| 46 | // 'Locale' |
||
| 47 | 'ShowInSearch', |
||
| 48 | 'Version', |
||
| 49 | "Embargo", |
||
| 50 | "Expiry", |
||
| 51 | "CanViewType", |
||
| 52 | "CanEditType", |
||
| 53 | "CopyContentFromID", |
||
| 54 | "HasBrokenLink", |
||
| 55 | ); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array Define fields that are initially copied to virtual pages but left modifiable after that. |
||
| 59 | */ |
||
| 60 | private static $initially_copied_fields = array( |
||
| 61 | 'ShowInMenus', |
||
| 62 | 'ShowInSearch', |
||
| 63 | 'URLSegment', |
||
| 64 | ); |
||
| 65 | |||
| 66 | private static $has_one = array( |
||
| 67 | "CopyContentFrom" => "SilverStripe\\CMS\\Model\\SiteTree", |
||
| 68 | ); |
||
| 69 | |||
| 70 | private static $owns = array( |
||
| 71 | "CopyContentFrom", |
||
| 72 | ); |
||
| 73 | |||
| 74 | private static $db = array( |
||
| 75 | "VersionID" => "Int", |
||
| 76 | ); |
||
| 77 | |||
| 78 | private static $table_name = 'VirtualPage'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Generates the array of fields required for the page type. |
||
| 82 | * |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | public function getVirtualFields() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * List of fields or properties to never virtualise |
||
| 101 | * |
||
| 102 | * @return array |
||
| 103 | */ |
||
| 104 | public function getNonVirtualisedFields() |
||
| 111 | |||
| 112 | public function setCopyContentFromID($val) |
||
| 120 | |||
| 121 | public function ContentSource() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * For VirtualPage, add a canonical link tag linking to the original page |
||
| 132 | * See TRAC #6828 & http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394 |
||
| 133 | * |
||
| 134 | * @param boolean $includeTitle Show default <title>-tag, set to false for custom templating |
||
| 135 | * @return string The XHTML metatags |
||
| 136 | */ |
||
| 137 | public function MetaTags($includeTitle = true) |
||
| 147 | |||
| 148 | public function allowedChildren() |
||
| 156 | |||
| 157 | public function syncLinkTracking() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * We can only publish the page if there is a published source page |
||
| 168 | * |
||
| 169 | * @param Member $member Member to check |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | public function canPublish($member = null) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Returns true if is page is publishable by anyone at all |
||
| 179 | * Return false if the source page isn't published yet. |
||
| 180 | * |
||
| 181 | * Note that isPublishable doesn't affect ete from live, only publish. |
||
| 182 | */ |
||
| 183 | public function isPublishable() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Generate the CMS fields from the fields from the original page. |
||
| 201 | */ |
||
| 202 | public function getCMSFields() |
||
| 273 | |||
| 274 | public function onBeforeWrite() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Copy any fields from the copied record to bootstrap /backup |
||
| 282 | */ |
||
| 283 | protected function refreshFromCopied() |
||
| 304 | |||
| 305 | public function getSettingsFields() |
||
| 326 | |||
| 327 | public function validate() |
||
| 347 | |||
| 348 | public function updateImageTracking() |
||
| 361 | |||
| 362 | public function CMSTreeClasses() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Allow attributes on the master page to pass |
||
| 369 | * through to the virtual page |
||
| 370 | * |
||
| 371 | * @param string $field |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | public function __get($field) |
||
| 387 | |||
| 388 | public function getField($field) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Check if given field is virtualised |
||
| 398 | * |
||
| 399 | * @param string $field |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | public function isFieldVirtualised($field) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Pass unrecognized method calls on to the original data object |
||
| 422 | * |
||
| 423 | * @param string $method |
||
| 424 | * @param array $args |
||
| 425 | * @return mixed |
||
| 426 | */ |
||
| 427 | public function __call($method, $args) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param string $field |
||
| 438 | * @return bool |
||
| 439 | */ |
||
| 440 | public function hasField($field) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) for a field |
||
| 451 | * on this object. |
||
| 452 | * |
||
| 453 | * @param string $field |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | public function castingHelper($field) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * {@inheritdoc} |
||
| 467 | */ |
||
| 468 | public function allMethodNames($custom = false) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * {@inheritdoc} |
||
| 481 | */ |
||
| 482 | public function getControllerName() |
||
| 490 | } |
||
| 491 |
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: