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 |
||
| 22 | class VirtualPage extends Page { |
||
| 23 | |||
| 24 | private static $description = 'Displays the content of another page'; |
||
| 25 | |||
| 26 | public static $virtualFields; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array Define fields that are not virtual - the virtual page must define these fields themselves. |
||
| 30 | * Note that anything in {@link self::config()->initially_copied_fields} is implicitly included in this list. |
||
| 31 | */ |
||
| 32 | private static $non_virtual_fields = array( |
||
| 33 | "ID", |
||
| 34 | "ClassName", |
||
| 35 | "ObsoleteClassName", |
||
| 36 | "SecurityTypeID", |
||
| 37 | "OwnerID", |
||
| 38 | "ParentID", |
||
| 39 | "URLSegment", |
||
| 40 | "Sort", |
||
| 41 | "Status", |
||
| 42 | 'ShowInMenus', |
||
| 43 | // 'Locale' |
||
| 44 | 'ShowInSearch', |
||
| 45 | 'Version', |
||
| 46 | "Embargo", |
||
| 47 | "Expiry", |
||
| 48 | "CanViewType", |
||
| 49 | "CanEditType", |
||
| 50 | "CopyContentFromID", |
||
| 51 | "HasBrokenLink", |
||
| 52 | ); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array Define fields that are initially copied to virtual pages but left modifiable after that. |
||
| 56 | */ |
||
| 57 | private static $initially_copied_fields = array( |
||
| 58 | 'ShowInMenus', |
||
| 59 | 'ShowInSearch', |
||
| 60 | 'URLSegment', |
||
| 61 | ); |
||
| 62 | |||
| 63 | private static $has_one = array( |
||
| 64 | "CopyContentFrom" => "SilverStripe\\CMS\\Model\\SiteTree", |
||
| 65 | ); |
||
| 66 | |||
| 67 | private static $owns = array( |
||
| 68 | "CopyContentFrom", |
||
| 69 | ); |
||
| 70 | |||
| 71 | private static $db = array( |
||
| 72 | "VersionID" => "Int", |
||
| 73 | ); |
||
| 74 | |||
| 75 | private static $table_name = 'VirtualPage'; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Generates the array of fields required for the page type. |
||
| 79 | * |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | public function getVirtualFields() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * List of fields or properties to never virtualise |
||
| 97 | * |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | public function getNonVirtualisedFields() { |
||
| 103 | |||
| 104 | public function setCopyContentFromID($val) { |
||
| 105 | // Sanity check to prevent pages virtualising other virtual pages |
||
| 106 | if($val && DataObject::get_by_id('SilverStripe\\CMS\\Model\\SiteTree', $val) instanceof VirtualPage) { |
||
| 107 | $val = 0; |
||
| 108 | } |
||
| 109 | return $this->setField("CopyContentFromID", $val); |
||
| 110 | } |
||
| 111 | |||
| 112 | public function ContentSource() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * For VirtualPage, add a canonical link tag linking to the original page |
||
| 122 | * See TRAC #6828 & http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394 |
||
| 123 | * |
||
| 124 | * @param boolean $includeTitle Show default <title>-tag, set to false for custom templating |
||
| 125 | * @return string The XHTML metatags |
||
| 126 | */ |
||
| 127 | public function MetaTags($includeTitle = true) { |
||
| 136 | |||
| 137 | public function allowedChildren() { |
||
| 138 | $copy = $this->CopyContentFrom(); |
||
| 139 | if($copy && $copy->exists()) { |
||
| 140 | return $copy->allowedChildren(); |
||
| 141 | } |
||
| 142 | return array(); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function syncLinkTracking() { |
||
| 146 | if($this->CopyContentFromID) { |
||
| 147 | $this->HasBrokenLink = !(bool) DataObject::get_by_id('SilverStripe\\CMS\\Model\\SiteTree', $this->CopyContentFromID); |
||
| 148 | } else { |
||
| 149 | $this->HasBrokenLink = true; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * We can only publish the page if there is a published source page |
||
| 155 | * |
||
| 156 | * @param Member $member Member to check |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | public function canPublish($member = null) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns true if is page is publishable by anyone at all |
||
| 165 | * Return false if the source page isn't published yet. |
||
| 166 | * |
||
| 167 | * Note that isPublishable doesn't affect ete from live, only publish. |
||
| 168 | */ |
||
| 169 | public function isPublishable() { |
||
| 170 | // No source |
||
| 171 | if(!$this->CopyContentFrom() || !$this->CopyContentFrom()->ID) { |
||
| 172 | return false; |
||
| 173 | } |
||
| 174 | |||
| 175 | // Unpublished source |
||
| 176 | if(!Versioned::get_versionnumber_by_stage('SilverStripe\\CMS\\Model\\SiteTree', 'Live', $this->CopyContentFromID)) { |
||
| 177 | return false; |
||
| 178 | } |
||
| 179 | |||
| 180 | // Default - publishable |
||
| 181 | return true; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Generate the CMS fields from the fields from the original page. |
||
| 186 | */ |
||
| 187 | public function getCMSFields() { |
||
| 188 | $fields = parent::getCMSFields(); |
||
| 189 | |||
| 190 | // Setup the linking to the original page. |
||
| 191 | $copyContentFromField = new TreeDropdownField( |
||
| 192 | "CopyContentFromID", |
||
| 193 | _t('VirtualPage.CHOOSE', "Linked Page"), |
||
| 194 | "SilverStripe\\CMS\\Model\\SiteTree" |
||
| 195 | ); |
||
| 196 | // filter doesn't let you select children of virtual pages as as source page |
||
| 197 | //$copyContentFromField->setFilterFunction(create_function('$item', 'return !($item instanceof VirtualPage);')); |
||
| 198 | |||
| 199 | // Setup virtual fields |
||
| 200 | if($virtualFields = $this->getVirtualFields()) { |
||
| 201 | $roTransformation = new ReadonlyTransformation(); |
||
| 202 | foreach($virtualFields as $virtualField) { |
||
| 203 | if($fields->dataFieldByName($virtualField)) |
||
| 204 | $fields->replaceField($virtualField, $fields->dataFieldByName($virtualField)->transform($roTransformation)); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | $msgs = array(); |
||
| 209 | |||
| 210 | $fields->addFieldToTab("Root.Main", $copyContentFromField, "Title"); |
||
| 211 | |||
| 212 | // Create links back to the original object in the CMS |
||
| 213 | if($this->CopyContentFrom()->exists()) { |
||
| 214 | $link = "<a class=\"cmsEditlink\" href=\"admin/pages/edit/show/$this->CopyContentFromID\">" |
||
| 215 | . _t('VirtualPage.EditLink', 'edit') |
||
| 216 | . "</a>"; |
||
| 217 | $msgs[] = _t( |
||
| 218 | 'VirtualPage.HEADERWITHLINK', |
||
| 219 | "This is a virtual page copying content from \"{title}\" ({link})", |
||
| 220 | array( |
||
|
|
|||
| 221 | 'title' => $this->CopyContentFrom()->obj('Title'), |
||
| 222 | 'link' => $link |
||
| 223 | ) |
||
| 224 | ); |
||
| 225 | } else { |
||
| 226 | $msgs[] = _t('VirtualPage.HEADER', "This is a virtual page"); |
||
| 227 | $msgs[] = _t( |
||
| 228 | 'SITETREE.VIRTUALPAGEWARNING', |
||
| 229 | 'Please choose a linked page and save first in order to publish this page' |
||
| 230 | ); |
||
| 231 | } |
||
| 232 | if( |
||
| 233 | $this->CopyContentFromID |
||
| 234 | && !Versioned::get_versionnumber_by_stage('SilverStripe\\CMS\\Model\\SiteTree', 'Live', $this->CopyContentFromID) |
||
| 235 | ) { |
||
| 236 | $msgs[] = _t( |
||
| 237 | 'SITETREE.VIRTUALPAGEDRAFTWARNING', |
||
| 238 | 'Please publish the linked page in order to publish the virtual page' |
||
| 239 | ); |
||
| 240 | } |
||
| 241 | |||
| 242 | $fields->addFieldToTab("Root.Main", |
||
| 243 | new LiteralField( |
||
| 244 | 'VirtualPageMessage', |
||
| 245 | '<div class="message notice">' . implode('. ', $msgs) . '.</div>' |
||
| 246 | ), |
||
| 247 | 'CopyContentFromID' |
||
| 248 | ); |
||
| 249 | |||
| 250 | return $fields; |
||
| 251 | } |
||
| 252 | |||
| 253 | public function onBeforeWrite() { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Copy any fields from the copied record to bootstrap /backup |
||
| 260 | */ |
||
| 261 | protected function refreshFromCopied() { |
||
| 281 | |||
| 282 | public function getSettingsFields() { |
||
| 301 | |||
| 302 | public function validate() { |
||
| 320 | |||
| 321 | public function updateImageTracking() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $numChildrenMethod |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | public function CMSTreeClasses($numChildrenMethod="numChildren") { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Allow attributes on the master page to pass |
||
| 342 | * through to the virtual page |
||
| 343 | * |
||
| 344 | * @param string $field |
||
| 345 | * @return mixed |
||
| 346 | */ |
||
| 347 | public function __get($field) { |
||
| 359 | |||
| 360 | public function getField($field) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Check if given field is virtualised |
||
| 369 | * |
||
| 370 | * @param string $field |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | public function isFieldVirtualised($field) { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Pass unrecognized method calls on to the original data object |
||
| 392 | * |
||
| 393 | * @param string $method |
||
| 394 | * @param string $args |
||
| 395 | * @return mixed |
||
| 396 | */ |
||
| 397 | public function __call($method, $args) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param string $field |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | public function hasField($field) { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) for a field |
||
| 419 | * on this object. |
||
| 420 | * |
||
| 421 | * @param string $field |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | public function castingHelper($field) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * {@inheritdoc} |
||
| 434 | */ |
||
| 435 | public function allMethodNames($custom = false) { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * {@inheritdoc} |
||
| 447 | */ |
||
| 448 | public function getControllerName() { |
||
| 455 | |||
| 456 | } |
||
| 457 |
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: