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 |
||
11 | class VirtualPage extends Page { |
||
12 | |||
13 | private static $description = 'Displays the content of another page'; |
||
14 | |||
15 | public static $virtualFields; |
||
16 | |||
17 | /** |
||
18 | * @var array Define fields that are not virtual - the virtual page must define these fields themselves. |
||
19 | * Note that anything in {@link self::config()->initially_copied_fields} is implicitly included in this list. |
||
20 | */ |
||
21 | private static $non_virtual_fields = array( |
||
22 | "ID", |
||
23 | "ClassName", |
||
24 | "ObsoleteClassName", |
||
25 | "SecurityTypeID", |
||
26 | "OwnerID", |
||
27 | "ParentID", |
||
28 | "URLSegment", |
||
29 | "Sort", |
||
30 | "Status", |
||
31 | 'ShowInMenus', |
||
32 | // 'Locale' |
||
33 | 'ShowInSearch', |
||
34 | 'Version', |
||
35 | "Embargo", |
||
36 | "Expiry", |
||
37 | "CanViewType", |
||
38 | "CanEditType", |
||
39 | "CopyContentFromID", |
||
40 | "HasBrokenLink", |
||
41 | ); |
||
42 | |||
43 | /** |
||
44 | * @var array Define fields that are initially copied to virtual pages but left modifiable after that. |
||
45 | */ |
||
46 | private static $initially_copied_fields = array( |
||
47 | 'ShowInMenus', |
||
48 | 'ShowInSearch', |
||
49 | 'URLSegment', |
||
50 | ); |
||
51 | |||
52 | private static $has_one = array( |
||
53 | "CopyContentFrom" => "SiteTree", |
||
54 | ); |
||
55 | |||
56 | private static $owns = array( |
||
57 | "CopyContentFrom", |
||
58 | ); |
||
59 | |||
60 | private static $db = array( |
||
61 | "VersionID" => "Int", |
||
62 | ); |
||
63 | |||
64 | /** |
||
65 | * Generates the array of fields required for the page type. |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | public function getVirtualFields() { |
||
70 | // Check if copied page exists |
||
71 | $record = $this->CopyContentFrom(); |
||
72 | if(!$record || !$record->exists()) { |
||
73 | return array(); |
||
74 | } |
||
75 | |||
76 | // Diff db with non-virtual fields |
||
77 | $fields = array_keys($record->db()); |
||
78 | $nonVirtualFields = $this->getNonVirtualisedFields(); |
||
79 | return array_diff($fields, $nonVirtualFields); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * List of fields or properties to never virtualise |
||
84 | * |
||
85 | * @return array |
||
86 | */ |
||
87 | public function getNonVirtualisedFields() { |
||
88 | return array_merge($this->config()->non_virtual_fields, $this->config()->initially_copied_fields); |
||
89 | } |
||
90 | |||
91 | public function setCopyContentFromID($val) { |
||
92 | // Sanity check to prevent pages virtualising other virtual pages |
||
93 | if($val && DataObject::get_by_id('SiteTree', $val) instanceof VirtualPage) { |
||
94 | $val = 0; |
||
95 | } |
||
96 | return $this->setField("CopyContentFromID", $val); |
||
97 | } |
||
98 | |||
99 | public function ContentSource() { |
||
100 | $copied = $this->CopyContentFrom(); |
||
101 | if($copied && $copied->exists()) { |
||
102 | return $copied; |
||
103 | } |
||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * For VirtualPage, add a canonical link tag linking to the original page |
||
109 | * See TRAC #6828 & http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394 |
||
110 | * |
||
111 | * @param boolean $includeTitle Show default <title>-tag, set to false for custom templating |
||
112 | * @return string The XHTML metatags |
||
113 | */ |
||
114 | public function MetaTags($includeTitle = true) { |
||
115 | $tags = parent::MetaTags($includeTitle); |
||
116 | $copied = $this->CopyContentFrom(); |
||
117 | if ($copied && $copied->exists()) { |
||
118 | $link = Convert::raw2att($copied->Link()); |
||
119 | $tags .= "<link rel=\"canonical\" href=\"{$link}\" />\n"; |
||
120 | } |
||
121 | return $tags; |
||
122 | } |
||
123 | |||
124 | public function allowedChildren() { |
||
125 | $copy = $this->CopyContentFrom(); |
||
126 | if($copy && $copy->exists()) { |
||
127 | return $copy->allowedChildren(); |
||
128 | } |
||
129 | return array(); |
||
130 | } |
||
131 | |||
132 | public function syncLinkTracking() { |
||
139 | |||
140 | /** |
||
141 | * We can only publish the page if there is a published source page |
||
142 | * |
||
143 | * @param Member $member Member to check |
||
144 | * @return bool |
||
145 | */ |
||
146 | public function canPublish($member = null) { |
||
149 | |||
150 | /** |
||
151 | * Returns true if is page is publishable by anyone at all |
||
152 | * Return false if the source page isn't published yet. |
||
153 | * |
||
154 | * Note that isPublishable doesn't affect ete from live, only publish. |
||
155 | */ |
||
156 | public function isPublishable() { |
||
170 | |||
171 | /** |
||
172 | * Generate the CMS fields from the fields from the original page. |
||
173 | */ |
||
174 | public function getCMSFields() { |
||
239 | |||
240 | public function onBeforeWrite() { |
||
241 | parent::onBeforeWrite(); |
||
242 | $this->refreshFromCopied(); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Copy any fields from the copied record to bootstrap /backup |
||
247 | */ |
||
248 | protected function refreshFromCopied() { |
||
268 | |||
269 | public function getSettingsFields() { |
||
288 | |||
289 | public function validate() { |
||
290 | $result = parent::validate(); |
||
291 | |||
292 | // "Can be root" validation |
||
293 | $orig = $this->CopyContentFrom(); |
||
294 | if($orig && $orig->exists() && !$orig->stat('can_be_root') && !$this->ParentID) { |
||
295 | $result->error( |
||
296 | _t( |
||
297 | 'VirtualPage.PageTypNotAllowedOnRoot', |
||
298 | 'Original page type "{type}" is not allowed on the root level for this virtual page', |
||
299 | array('type' => $orig->i18n_singular_name()) |
||
300 | ), |
||
301 | 'CAN_BE_ROOT_VIRTUAL' |
||
302 | ); |
||
303 | } |
||
304 | |||
305 | return $result; |
||
306 | } |
||
307 | |||
308 | public function updateImageTracking() { |
||
318 | |||
319 | /** |
||
320 | * @param string $numChildrenMethod |
||
321 | * @return string |
||
322 | */ |
||
323 | public function CMSTreeClasses($numChildrenMethod="numChildren") { |
||
326 | |||
327 | /** |
||
328 | * Allow attributes on the master page to pass |
||
329 | * through to the virtual page |
||
330 | * |
||
331 | * @param string $field |
||
332 | * @return mixed |
||
333 | */ |
||
334 | public function __get($field) { |
||
343 | |||
344 | public function getField($field) { |
||
350 | |||
351 | /** |
||
352 | * Check if given field is virtualised |
||
353 | * |
||
354 | * @param string $field |
||
355 | * @return bool |
||
356 | */ |
||
357 | public function isFieldVirtualised($field) { |
||
373 | |||
374 | /** |
||
375 | * Pass unrecognized method calls on to the original data object |
||
376 | * |
||
377 | * @param string $method |
||
378 | * @param string $args |
||
379 | * @return mixed |
||
380 | */ |
||
381 | public function __call($method, $args) { |
||
388 | |||
389 | /** |
||
390 | * @param string $field |
||
391 | * @return bool |
||
392 | */ |
||
393 | public function hasField($field) { |
||
394 | if(parent::hasField($field)) { |
||
395 | return true; |
||
396 | } |
||
397 | $copy = $this->CopyContentFrom(); |
||
398 | return $copy && $copy->exists() && $copy->hasField($field); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Overwrite to also check for method on the original data object |
||
403 | * |
||
404 | * @param string $method |
||
405 | * @return bool |
||
406 | */ |
||
407 | public function hasMethod($method) { |
||
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 | |||
436 | /** |
||
515 |
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: