Complex classes like ViewableData 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 ViewableData, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class ViewableData extends Object implements IteratorAggregate { |
||
13 | |||
14 | /** |
||
15 | * An array of objects to cast certain fields to. This is set up as an array in the format: |
||
16 | * |
||
17 | * <code> |
||
18 | * public static $casting = array ( |
||
19 | * 'FieldName' => 'ClassToCastTo(Arguments)' |
||
20 | * ); |
||
21 | * </code> |
||
22 | * |
||
23 | * @var array |
||
24 | * @config |
||
25 | */ |
||
26 | private static $casting = array( |
||
27 | 'CSSClasses' => 'Varchar' |
||
28 | ); |
||
29 | |||
30 | /** |
||
31 | * The default object to cast scalar fields to if casting information is not specified, and casting to an object |
||
32 | * is required. |
||
33 | * |
||
34 | * @var string |
||
35 | * @config |
||
36 | */ |
||
37 | private static $default_cast = 'Text'; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | private static $casting_cache = array(); |
||
43 | |||
44 | // ----------------------------------------------------------------------------------------------------------------- |
||
45 | |||
46 | /** |
||
47 | * A failover object to attempt to get data from if it is not present on this object. |
||
48 | * |
||
49 | * @var ViewableData |
||
50 | */ |
||
51 | protected $failover; |
||
52 | |||
53 | /** |
||
54 | * @var ViewableData |
||
55 | */ |
||
56 | protected $customisedObject; |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | private $objCache = array(); |
||
62 | |||
63 | // ----------------------------------------------------------------------------------------------------------------- |
||
64 | |||
65 | /** |
||
66 | * Converts a field spec into an object creator. For example: "Int" becomes "new Int($fieldName);" and "Varchar(50)" |
||
67 | * becomes "new Varchar($fieldName, 50);". |
||
68 | * |
||
69 | * @param string $fieldSchema The field spec |
||
70 | * @return string |
||
71 | */ |
||
72 | public static function castingObjectCreator($fieldSchema) { |
||
75 | |||
76 | /** |
||
77 | * Convert a field schema (e.g. "Varchar(50)") into a casting object creator array that contains both a className |
||
78 | * and castingHelper constructor code. See {@link castingObjectCreator} for more information about the constructor. |
||
79 | * |
||
80 | * @param string $fieldSchema |
||
81 | * @return array |
||
82 | */ |
||
83 | public static function castingObjectCreatorPair($fieldSchema) { |
||
86 | |||
87 | // FIELD GETTERS & SETTERS ----------------------------------------------------------------------------------------- |
||
88 | |||
89 | /** |
||
90 | * Check if a field exists on this object or its failover. |
||
91 | * |
||
92 | * @param string $property |
||
93 | * @return bool |
||
94 | */ |
||
95 | public function __isset($property) { |
||
98 | |||
99 | /** |
||
100 | * Get the value of a property/field on this object. This will check if a method called get{$property} exists, then |
||
101 | * check if a field is available using {@link ViewableData::getField()}, then fall back on a failover object. |
||
102 | * |
||
103 | * @param string $property |
||
104 | * @return mixed |
||
105 | */ |
||
106 | public function __get($property) { |
||
115 | |||
116 | /** |
||
117 | * Set a property/field on this object. This will check for the existence of a method called set{$property}, then |
||
118 | * use the {@link ViewableData::setField()} method. |
||
119 | * |
||
120 | * @param string $property |
||
121 | * @param mixed $value |
||
122 | */ |
||
123 | public function __set($property, $value) { |
||
130 | |||
131 | /** |
||
132 | * Set a failover object to attempt to get data from if it is not present on this object. |
||
133 | * |
||
134 | * @param ViewableData $failover |
||
135 | */ |
||
136 | public function setFailover(ViewableData $failover) { |
||
145 | |||
146 | /** |
||
147 | * Get the current failover object if set |
||
148 | * |
||
149 | * @return ViewableData|null |
||
150 | */ |
||
151 | public function getFailover() { |
||
154 | |||
155 | /** |
||
156 | * Check if a field exists on this object. This should be overloaded in child classes. |
||
157 | * |
||
158 | * @param string $field |
||
159 | * @return bool |
||
160 | */ |
||
161 | public function hasField($field) { |
||
164 | |||
165 | /** |
||
166 | * Get the value of a field on this object. This should be overloaded in child classes. |
||
167 | * |
||
168 | * @param string $field |
||
169 | * @return mixed |
||
170 | */ |
||
171 | public function getField($field) { |
||
174 | |||
175 | /** |
||
176 | * Set a field on this object. This should be overloaded in child classes. |
||
177 | * |
||
178 | * @param string $field |
||
179 | * @param mixed $value |
||
180 | */ |
||
181 | public function setField($field, $value) { |
||
184 | |||
185 | // ----------------------------------------------------------------------------------------------------------------- |
||
186 | |||
187 | /** |
||
188 | * Add methods from the {@link ViewableData::$failover} object, as well as wrapping any methods prefixed with an |
||
189 | * underscore into a {@link ViewableData::cachedCall()}. |
||
190 | */ |
||
191 | public function defineMethods() { |
||
212 | |||
213 | /** |
||
214 | * Method to facilitate deprecation of underscore-prefixed methods automatically being cached. |
||
215 | * |
||
216 | * @param string $field |
||
217 | * @param array $arguments |
||
218 | * @param string $identifier an optional custom cache identifier |
||
219 | * @return unknown |
||
220 | */ |
||
221 | public function deprecatedCachedCall($method, $args = null, $identifier = null) { |
||
230 | |||
231 | /** |
||
232 | * Merge some arbitrary data in with this object. This method returns a {@link ViewableData_Customised} instance |
||
233 | * with references to both this and the new custom data. |
||
234 | * |
||
235 | * Note that any fields you specify will take precedence over the fields on this object. |
||
236 | * |
||
237 | * @param array|ViewableData $data |
||
238 | * @return ViewableData_Customised |
||
239 | */ |
||
240 | public function customise($data) { |
||
253 | |||
254 | /** |
||
255 | * @return ViewableData |
||
256 | */ |
||
257 | public function getCustomisedObj() { |
||
260 | |||
261 | /** |
||
262 | * @param ViewableData $object |
||
263 | */ |
||
264 | public function setCustomisedObj(ViewableData $object) { |
||
267 | |||
268 | // CASTING --------------------------------------------------------------------------------------------------------- |
||
269 | |||
270 | /** |
||
271 | * Get the class a field on this object would be casted to, as well as the casting helper for casting a field to |
||
272 | * an object (see {@link ViewableData::castingHelper()} for information on casting helpers). |
||
273 | * |
||
274 | * The returned array contains two keys: |
||
275 | * - className: the class the field would be casted to (e.g. "Varchar") |
||
276 | * - castingHelper: the casting helper for casting the field (e.g. "return new Varchar($fieldName)") |
||
277 | * |
||
278 | * @param string $field |
||
279 | * @return array |
||
280 | */ |
||
281 | public function castingHelperPair($field) { |
||
285 | |||
286 | /** |
||
287 | * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) for a field |
||
288 | * on this object. |
||
289 | * |
||
290 | * @param string $field |
||
291 | * @return string |
||
292 | */ |
||
293 | public function castingHelper($field) { |
||
309 | |||
310 | /** |
||
311 | * Get the class name a field on this object will be casted to |
||
312 | * |
||
313 | * @param string $field |
||
314 | * @return string |
||
315 | */ |
||
316 | public function castingClass($field) { |
||
324 | |||
325 | /** |
||
326 | * Return the string-format type for the given field. |
||
327 | * |
||
328 | * @param string $field |
||
329 | * @return string 'xml'|'raw' |
||
330 | */ |
||
331 | public function escapeTypeForField($field) { |
||
336 | |||
337 | /** |
||
338 | * Save the casting cache for this object (including data from any failovers) into a variable |
||
339 | * |
||
340 | * @param reference $cache |
||
341 | */ |
||
342 | public function buildCastingCache(&$cache) { |
||
365 | |||
366 | // TEMPLATE ACCESS LAYER ------------------------------------------------------------------------------------------- |
||
367 | |||
368 | /** |
||
369 | * Render this object into the template, and get the result as a string. You can pass one of the following as the |
||
370 | * $template parameter: |
||
371 | * - a template name (e.g. Page) |
||
372 | * - an array of possible template names - the first valid one will be used |
||
373 | * - an SSViewer instance |
||
374 | * |
||
375 | * @param string|array|SSViewer $template the template to render into |
||
376 | * @param array $customFields fields to customise() the object with before rendering |
||
377 | * @return HTMLText |
||
378 | */ |
||
379 | public function renderWith($template, $customFields = null) { |
||
397 | |||
398 | /** |
||
399 | * Generate the cache name for a field |
||
400 | * |
||
401 | * @param string $fieldName Name of field |
||
402 | * @param array $arguments List of optional arguments given |
||
403 | */ |
||
404 | protected function objCacheName($fieldName, $arguments) { |
||
409 | |||
410 | /** |
||
411 | * Get a cached value from the field cache |
||
412 | * |
||
413 | * @param string $key Cache key |
||
414 | * @return mixed |
||
415 | */ |
||
416 | protected function objCacheGet($key) { |
||
419 | |||
420 | /** |
||
421 | * Store a value in the field cache |
||
422 | * |
||
423 | * @param string $key Cache key |
||
424 | * @param mixed $value |
||
425 | */ |
||
426 | protected function objCacheSet($key, $value) { |
||
429 | |||
430 | /** |
||
431 | * Get the value of a field on this object, automatically inserting the value into any available casting objects |
||
432 | * that have been specified. |
||
433 | * |
||
434 | * @param string $fieldName |
||
435 | * @param array $arguments |
||
436 | * @param bool $forceReturnedObject if TRUE, the value will ALWAYS be casted to an object before being returned, |
||
437 | * even if there is no explicit casting information |
||
438 | * @param bool $cache Cache this object |
||
439 | * @param string $cacheName a custom cache name |
||
440 | */ |
||
441 | public function obj($fieldName, $arguments = null, $forceReturnedObject = true, $cache = false, $cacheName = null) { |
||
479 | |||
480 | /** |
||
481 | * A simple wrapper around {@link ViewableData::obj()} that automatically caches the result so it can be used again |
||
482 | * without re-running the method. |
||
483 | * |
||
484 | * @param string $field |
||
485 | * @param array $arguments |
||
486 | * @param string $identifier an optional custom cache identifier |
||
487 | */ |
||
488 | public function cachedCall($field, $arguments = null, $identifier = null) { |
||
491 | |||
492 | /** |
||
493 | * Checks if a given method/field has a valid value. If the result is an object, this will return the result of the |
||
494 | * exists method, otherwise will check if the result is not just an empty paragraph tag. |
||
495 | * |
||
496 | * @param string $field |
||
497 | * @param array $arguments |
||
498 | * @param bool $cache |
||
499 | * @return bool |
||
500 | */ |
||
501 | public function hasValue($field, $arguments = null, $cache = true) { |
||
511 | |||
512 | /**#@+ |
||
513 | * @param string $field |
||
514 | * @param array $arguments |
||
515 | * @param bool $cache |
||
516 | * @return string |
||
517 | */ |
||
518 | |||
519 | /** |
||
520 | * Get the string value of a field on this object that has been suitable escaped to be inserted directly into a |
||
521 | * template. |
||
522 | */ |
||
523 | public function XML_val($field, $arguments = null, $cache = false) { |
||
527 | |||
528 | /** |
||
529 | * Return the value of the field without any escaping being applied. |
||
530 | */ |
||
531 | public function RAW_val($field, $arguments = null, $cache = true) { |
||
534 | |||
535 | /** |
||
536 | * Return the value of a field in an SQL-safe format. |
||
537 | */ |
||
538 | public function SQL_val($field, $arguments = null, $cache = true) { |
||
541 | |||
542 | /** |
||
543 | * Return the value of a field in a JavaScript-save format. |
||
544 | */ |
||
545 | public function JS_val($field, $arguments = null, $cache = true) { |
||
548 | |||
549 | /** |
||
550 | * Return the value of a field escaped suitable to be inserted into an XML node attribute. |
||
551 | */ |
||
552 | public function ATT_val($field, $arguments = null, $cache = true) { |
||
555 | |||
556 | /**#@-*/ |
||
557 | |||
558 | /** |
||
559 | * Get an array of XML-escaped values by field name |
||
560 | * |
||
561 | * @param array $elements an array of field names |
||
562 | * @return array |
||
563 | */ |
||
564 | public function getXMLValues($fields) { |
||
573 | |||
574 | // ITERATOR SUPPORT ------------------------------------------------------------------------------------------------ |
||
575 | |||
576 | /** |
||
577 | * Return a single-item iterator so you can iterate over the fields of a single record. |
||
578 | * |
||
579 | * This is useful so you can use a single record inside a <% control %> block in a template - and then use |
||
580 | * to access individual fields on this object. |
||
581 | * |
||
582 | * @return ArrayIterator |
||
583 | */ |
||
584 | public function getIterator() { |
||
587 | |||
588 | // UTILITY METHODS ------------------------------------------------------------------------------------------------- |
||
589 | |||
590 | /** |
||
591 | * When rendering some objects it is necessary to iterate over the object being rendered, to do this, you need |
||
592 | * access to itself. |
||
593 | * |
||
594 | * @return ViewableData |
||
595 | */ |
||
596 | public function Me() { |
||
599 | |||
600 | /** |
||
601 | * Return the directory if the current active theme (relative to the site root). |
||
602 | * |
||
603 | * This method is useful for things such as accessing theme images from your template without hardcoding the theme |
||
604 | * page - e.g. <img src="$ThemeDir/images/something.gif">. |
||
605 | * |
||
606 | * This method should only be used when a theme is currently active. However, it will fall over to the current |
||
607 | * project directory. |
||
608 | * |
||
609 | * @param string $subtheme the subtheme path to get |
||
610 | * @return string |
||
611 | */ |
||
612 | public function ThemeDir($subtheme = false) { |
||
622 | |||
623 | /** |
||
624 | * Get part of the current classes ancestry to be used as a CSS class. |
||
625 | * |
||
626 | * This method returns an escaped string of CSS classes representing the current classes ancestry until it hits a |
||
627 | * stop point - e.g. "Page DataObject ViewableData". |
||
628 | * |
||
629 | * @param string $stopAtClass the class to stop at (default: ViewableData) |
||
630 | * @return string |
||
631 | * @uses ClassInfo |
||
632 | */ |
||
633 | public function CSSClasses($stopAtClass = 'ViewableData') { |
||
650 | |||
651 | /** |
||
652 | * Return debug information about this object that can be rendered into a template |
||
653 | * |
||
654 | * @return ViewableData_Debugger |
||
655 | */ |
||
656 | public function Debug() { |
||
659 | |||
660 | } |
||
661 | |||
817 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.