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) { |
||
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() { |
||
144 | |||
145 | public function syncLinkTracking() { |
||
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() { |
||
183 | |||
184 | /** |
||
185 | * Generate the CMS fields from the fields from the original page. |
||
186 | */ |
||
187 | public function getCMSFields() { |
||
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($action = null) { |
||
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: