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 | ); |
||
41 | |||
42 | /** |
||
43 | * @var array Define fields that are initially copied to virtual pages but left modifiable after that. |
||
44 | */ |
||
45 | private static $initially_copied_fields = array( |
||
46 | 'ShowInMenus', |
||
47 | 'ShowInSearch', |
||
48 | 'URLSegment', |
||
49 | ); |
||
50 | |||
51 | private static $has_one = array( |
||
52 | "CopyContentFrom" => "SiteTree", |
||
53 | ); |
||
54 | |||
55 | private static $owns = array( |
||
56 | "CopyContentFrom", |
||
57 | ); |
||
58 | |||
59 | private static $db = array( |
||
60 | "VersionID" => "Int", |
||
61 | ); |
||
62 | |||
63 | /** |
||
64 | * Generates the array of fields required for the page type. |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | public function getVirtualFields() { |
||
69 | // Check if copied page exists |
||
70 | $record = $this->CopyContentFrom(); |
||
71 | if(!$record || !$record->exists()) { |
||
72 | return array(); |
||
73 | } |
||
74 | |||
75 | // Diff db with non-virtual fields |
||
76 | $fields = array_keys($record->db()); |
||
77 | $nonVirtualFields = $this->getNonVirtualisedFields(); |
||
78 | return array_diff($fields, $nonVirtualFields); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * List of fields or properties to never virtualise |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | public function getNonVirtualisedFields() { |
||
87 | return array_merge($this->config()->non_virtual_fields, $this->config()->initially_copied_fields); |
||
88 | } |
||
89 | |||
90 | public function setCopyContentFromID($val) { |
||
91 | // Sanity check to prevent pages virtualising other virtual pages |
||
92 | if($val && DataObject::get_by_id('SiteTree', $val) instanceof VirtualPage) { |
||
93 | $val = 0; |
||
94 | } |
||
95 | return $this->setField("CopyContentFromID", $val); |
||
96 | } |
||
97 | |||
98 | public function ContentSource() { |
||
99 | $copied = $this->CopyContentFrom(); |
||
100 | if($copied && $copied->exists()) { |
||
101 | return $copied; |
||
102 | } |
||
103 | return $this; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * For VirtualPage, add a canonical link tag linking to the original page |
||
108 | * See TRAC #6828 & http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394 |
||
109 | * |
||
110 | * @param boolean $includeTitle Show default <title>-tag, set to false for custom templating |
||
111 | * @return string The XHTML metatags |
||
112 | */ |
||
113 | public function MetaTags($includeTitle = true) { |
||
114 | $tags = parent::MetaTags($includeTitle); |
||
115 | $copied = $this->CopyContentFrom(); |
||
116 | if ($copied && $copied->exists()) { |
||
117 | $link = Convert::raw2att($copied->Link()); |
||
118 | $tags .= "<link rel=\"canonical\" href=\"{$link}\" />\n"; |
||
119 | } |
||
120 | return $tags; |
||
121 | } |
||
122 | |||
123 | public function allowedChildren() { |
||
124 | $copy = $this->CopyContentFrom(); |
||
125 | if($copy && $copy->exists()) { |
||
126 | return $copy->allowedChildren(); |
||
127 | } |
||
128 | return array(); |
||
129 | } |
||
130 | |||
131 | public function syncLinkTracking() { |
||
138 | |||
139 | /** |
||
140 | * We can only publish the page if there is a published source page |
||
141 | * |
||
142 | * @param Member $member Member to check |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function canPublish($member = null) { |
||
148 | |||
149 | /** |
||
150 | * Returns true if is page is publishable by anyone at all |
||
151 | * Return false if the source page isn't published yet. |
||
152 | * |
||
153 | * Note that isPublishable doesn't affect ete from live, only publish. |
||
154 | */ |
||
155 | public function isPublishable() { |
||
169 | |||
170 | /** |
||
171 | * Generate the CMS fields from the fields from the original page. |
||
172 | */ |
||
173 | public function getCMSFields() { |
||
238 | |||
239 | public function onBeforeWrite() { |
||
243 | |||
244 | /** |
||
245 | * Copy any fields from the copied record to bootstrap /backup |
||
246 | */ |
||
247 | protected function refreshFromCopied() { |
||
267 | |||
268 | public function getSettingsFields() { |
||
287 | |||
288 | public function validate() { |
||
306 | |||
307 | public function updateImageTracking() { |
||
317 | |||
318 | /** |
||
319 | * @param string $numChildrenMethod |
||
320 | * @return string |
||
321 | */ |
||
322 | public function CMSTreeClasses($numChildrenMethod="numChildren") { |
||
325 | |||
326 | /** |
||
327 | * Allow attributes on the master page to pass |
||
328 | * through to the virtual page |
||
329 | * |
||
330 | * @param string $field |
||
331 | * @return mixed |
||
332 | */ |
||
333 | public function __get($field) { |
||
342 | |||
343 | public function getField($field) { |
||
349 | |||
350 | /** |
||
351 | * Check if given field is virtualised |
||
352 | * |
||
353 | * @param string $field |
||
354 | * @return bool |
||
355 | */ |
||
356 | public function isFieldVirtualised($field) { |
||
372 | |||
373 | /** |
||
374 | * Pass unrecognized method calls on to the original data object |
||
375 | * |
||
376 | * @param string $method |
||
377 | * @param string $args |
||
378 | * @return mixed |
||
379 | */ |
||
380 | public function __call($method, $args) { |
||
387 | |||
388 | /** |
||
389 | * @param string $field |
||
390 | * @return bool |
||
391 | */ |
||
392 | public function hasField($field) { |
||
399 | |||
400 | /** |
||
401 | * Overwrite to also check for method on the original data object |
||
402 | * |
||
403 | * @param string $method |
||
404 | * @return bool |
||
405 | */ |
||
406 | public function hasMethod($method) { |
||
417 | |||
418 | /** |
||
419 | * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) for a field |
||
420 | * on this object. |
||
421 | * |
||
422 | * @param string $field |
||
423 | * @return string |
||
424 | */ |
||
425 | public function castingHelper($field) { |
||
432 | |||
433 | } |
||
434 | |||
514 |
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: