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 |
||
| 23 | class VirtualPage extends Page { |
||
| 24 | |||
| 25 | private static $description = 'Displays the content of another page'; |
||
| 26 | |||
| 27 | public static $virtualFields; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array Define fields that are not virtual - the virtual page must define these fields themselves. |
||
| 31 | * Note that anything in {@link self::config()->initially_copied_fields} is implicitly included in this list. |
||
| 32 | */ |
||
| 33 | private static $non_virtual_fields = array( |
||
| 34 | "ID", |
||
| 35 | "ClassName", |
||
| 36 | "ObsoleteClassName", |
||
| 37 | "SecurityTypeID", |
||
| 38 | "OwnerID", |
||
| 39 | "ParentID", |
||
| 40 | "URLSegment", |
||
| 41 | "Sort", |
||
| 42 | "Status", |
||
| 43 | 'ShowInMenus', |
||
| 44 | // 'Locale' |
||
| 45 | 'ShowInSearch', |
||
| 46 | 'Version', |
||
| 47 | "Embargo", |
||
| 48 | "Expiry", |
||
| 49 | "CanViewType", |
||
| 50 | "CanEditType", |
||
| 51 | "CopyContentFromID", |
||
| 52 | "HasBrokenLink", |
||
| 53 | ); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array Define fields that are initially copied to virtual pages but left modifiable after that. |
||
| 57 | */ |
||
| 58 | private static $initially_copied_fields = array( |
||
| 59 | 'ShowInMenus', |
||
| 60 | 'ShowInSearch', |
||
| 61 | 'URLSegment', |
||
| 62 | ); |
||
| 63 | |||
| 64 | private static $has_one = array( |
||
| 65 | "CopyContentFrom" => "SilverStripe\\CMS\\Model\\SiteTree", |
||
| 66 | ); |
||
| 67 | |||
| 68 | private static $owns = array( |
||
| 69 | "CopyContentFrom", |
||
| 70 | ); |
||
| 71 | |||
| 72 | private static $db = array( |
||
| 73 | "VersionID" => "Int", |
||
| 74 | ); |
||
| 75 | |||
| 76 | private static $table_name = 'VirtualPage'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Generates the array of fields required for the page type. |
||
| 80 | * |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | public function getVirtualFields() { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * List of fields or properties to never virtualise |
||
| 98 | * |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | public function getNonVirtualisedFields() { |
||
| 104 | |||
| 105 | public function setCopyContentFromID($val) { |
||
| 112 | |||
| 113 | public function ContentSource() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * For VirtualPage, add a canonical link tag linking to the original page |
||
| 123 | * See TRAC #6828 & http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394 |
||
| 124 | * |
||
| 125 | * @param boolean $includeTitle Show default <title>-tag, set to false for custom templating |
||
| 126 | * @return string The XHTML metatags |
||
| 127 | */ |
||
| 128 | public function MetaTags($includeTitle = true) { |
||
| 137 | |||
| 138 | public function allowedChildren() { |
||
| 145 | |||
| 146 | public function syncLinkTracking() { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * We can only publish the page if there is a published source page |
||
| 156 | * |
||
| 157 | * @param Member $member Member to check |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | public function canPublish($member = null) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns true if is page is publishable by anyone at all |
||
| 166 | * Return false if the source page isn't published yet. |
||
| 167 | * |
||
| 168 | * Note that isPublishable doesn't affect ete from live, only publish. |
||
| 169 | */ |
||
| 170 | public function isPublishable() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Generate the CMS fields from the fields from the original page. |
||
| 187 | */ |
||
| 188 | public function getCMSFields() { |
||
| 253 | |||
| 254 | public function onBeforeWrite() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Copy any fields from the copied record to bootstrap /backup |
||
| 261 | */ |
||
| 262 | protected function refreshFromCopied() { |
||
| 282 | |||
| 283 | public function getSettingsFields() { |
||
| 302 | |||
| 303 | public function validate() { |
||
| 322 | |||
| 323 | public function updateImageTracking() { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $numChildrenMethod |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public function CMSTreeClasses($numChildrenMethod="numChildren") { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Allow attributes on the master page to pass |
||
| 344 | * through to the virtual page |
||
| 345 | * |
||
| 346 | * @param string $field |
||
| 347 | * @return mixed |
||
| 348 | */ |
||
| 349 | public function __get($field) { |
||
| 361 | |||
| 362 | public function getField($field) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Check if given field is virtualised |
||
| 371 | * |
||
| 372 | * @param string $field |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | public function isFieldVirtualised($field) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Pass unrecognized method calls on to the original data object |
||
| 394 | * |
||
| 395 | * @param string $method |
||
| 396 | * @param string $args |
||
| 397 | * @return mixed |
||
| 398 | */ |
||
| 399 | public function __call($method, $args) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param string $field |
||
| 409 | * @return bool |
||
| 410 | */ |
||
| 411 | public function hasField($field) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) for a field |
||
| 421 | * on this object. |
||
| 422 | * |
||
| 423 | * @param string $field |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function castingHelper($field) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * {@inheritdoc} |
||
| 436 | */ |
||
| 437 | public function allMethodNames($custom = false) { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * {@inheritdoc} |
||
| 449 | */ |
||
| 450 | public function getControllerName() { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * {@inheritDoc} |
||
| 460 | */ |
||
| 461 | public function getFrontendControllerName() |
||
| 469 | |||
| 470 | } |
||
| 471 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: