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 |
||
| 15 | class ViewableData extends Object implements IteratorAggregate { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * An array of objects to cast certain fields to. This is set up as an array in the format: |
||
| 19 | * |
||
| 20 | * <code> |
||
| 21 | * public static $casting = array ( |
||
| 22 | * 'FieldName' => 'ClassToCastTo(Arguments)' |
||
| 23 | * ); |
||
| 24 | * </code> |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | * @config |
||
| 28 | */ |
||
| 29 | private static $casting = array( |
||
| 30 | 'CSSClasses' => 'Varchar' |
||
| 31 | ); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The default object to cast scalar fields to if casting information is not specified, and casting to an object |
||
| 35 | * is required. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | * @config |
||
| 39 | */ |
||
| 40 | private static $default_cast = 'Text'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private static $casting_cache = array(); |
||
| 46 | |||
| 47 | // ----------------------------------------------------------------------------------------------------------------- |
||
| 48 | |||
| 49 | /** |
||
| 50 | * A failover object to attempt to get data from if it is not present on this object. |
||
| 51 | * |
||
| 52 | * @var ViewableData |
||
| 53 | */ |
||
| 54 | protected $failover; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var ViewableData |
||
| 58 | */ |
||
| 59 | protected $customisedObject; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $objCache = array(); |
||
| 65 | |||
| 66 | // ----------------------------------------------------------------------------------------------------------------- |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Converts a field spec into an object creator. For example: "Int" becomes "new Int($fieldName);" and "Varchar(50)" |
||
| 70 | * becomes "new DBVarchar($fieldName, 50);". |
||
| 71 | * |
||
| 72 | * @param string $fieldSchema The field spec |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public static function castingObjectCreator($fieldSchema) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Convert a field schema (e.g. "Varchar(50)") into a casting object creator array that contains both a className |
||
| 81 | * and castingHelper constructor code. See {@link castingObjectCreator} for more information about the constructor. |
||
| 82 | * |
||
| 83 | * @param string $fieldSchema |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | public static function castingObjectCreatorPair($fieldSchema) { |
||
| 89 | |||
| 90 | // FIELD GETTERS & SETTERS ----------------------------------------------------------------------------------------- |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Check if a field exists on this object or its failover. |
||
| 94 | * |
||
| 95 | * @param string $property |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function __isset($property) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the value of a property/field on this object. This will check if a method called get{$property} exists, then |
||
| 104 | * check if a field is available using {@link ViewableData::getField()}, then fall back on a failover object. |
||
| 105 | * |
||
| 106 | * @param string $property |
||
| 107 | * @return mixed |
||
| 108 | */ |
||
| 109 | public function __get($property) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Set a property/field on this object. This will check for the existence of a method called set{$property}, then |
||
| 121 | * use the {@link ViewableData::setField()} method. |
||
| 122 | * |
||
| 123 | * @param string $property |
||
| 124 | * @param mixed $value |
||
| 125 | */ |
||
| 126 | public function __set($property, $value) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Set a failover object to attempt to get data from if it is not present on this object. |
||
| 136 | * |
||
| 137 | * @param ViewableData $failover |
||
| 138 | */ |
||
| 139 | public function setFailover(ViewableData $failover) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get the current failover object if set |
||
| 146 | * |
||
| 147 | * @return ViewableData|null |
||
| 148 | */ |
||
| 149 | public function getFailover() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Check if a field exists on this object. This should be overloaded in child classes. |
||
| 155 | * |
||
| 156 | * @param string $field |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | public function hasField($field) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get the value of a field on this object. This should be overloaded in child classes. |
||
| 165 | * |
||
| 166 | * @param string $field |
||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | public function getField($field) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Set a field on this object. This should be overloaded in child classes. |
||
| 175 | * |
||
| 176 | * @param string $field |
||
| 177 | * @param mixed $value |
||
| 178 | */ |
||
| 179 | public function setField($field, $value) { |
||
| 182 | |||
| 183 | // ----------------------------------------------------------------------------------------------------------------- |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Add methods from the {@link ViewableData::$failover} object, as well as wrapping any methods prefixed with an |
||
| 187 | * underscore into a {@link ViewableData::cachedCall()}. |
||
| 188 | */ |
||
| 189 | public function defineMethods() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Method to facilitate deprecation of underscore-prefixed methods automatically being cached. |
||
| 213 | * |
||
| 214 | * @param string $field |
||
| 215 | * @param array $arguments |
||
| 216 | * @param string $identifier an optional custom cache identifier |
||
| 217 | * @return unknown |
||
| 218 | */ |
||
| 219 | public function deprecatedCachedCall($method, $args = null, $identifier = null) { |
||
| 228 | /** |
||
| 229 | * Merge some arbitrary data in with this object. This method returns a {@link ViewableData_Customised} instance |
||
| 230 | * with references to both this and the new custom data. |
||
| 231 | * |
||
| 232 | * Note that any fields you specify will take precedence over the fields on this object. |
||
| 233 | * |
||
| 234 | * @param array|ViewableData $data |
||
| 235 | * @return ViewableData_Customised |
||
| 236 | */ |
||
| 237 | public function customise($data) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return ViewableData |
||
| 253 | */ |
||
| 254 | public function getCustomisedObj() { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param ViewableData $object |
||
| 260 | */ |
||
| 261 | public function setCustomisedObj(ViewableData $object) { |
||
| 264 | |||
| 265 | // CASTING --------------------------------------------------------------------------------------------------------- |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get the class a field on this object would be casted to, as well as the casting helper for casting a field to |
||
| 269 | * an object (see {@link ViewableData::castingHelper()} for information on casting helpers). |
||
| 270 | * |
||
| 271 | * The returned array contains two keys: |
||
| 272 | * - className: the class the field would be casted to (e.g. "Varchar") |
||
| 273 | * - castingHelper: the casting helper for casting the field (e.g. "return new Varchar($fieldName)") |
||
| 274 | * |
||
| 275 | * @param string $field |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | public function castingHelperPair($field) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) for a field |
||
| 285 | * on this object. |
||
| 286 | * |
||
| 287 | * @param string $field |
||
| 288 | * @return string Casting helper |
||
| 289 | */ |
||
| 290 | public function castingHelper($field) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get the class name a field on this object will be casted to |
||
| 301 | * |
||
| 302 | * @param string $field |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function castingClass($field) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Return the string-format type for the given field. |
||
| 316 | * |
||
| 317 | * @param string $field |
||
| 318 | * @return string 'xml'|'raw' |
||
| 319 | */ |
||
| 320 | public function escapeTypeForField($field) { |
||
| 321 | $class = $this->castingClass($field) ?: $this->config()->default_cast; |
||
| 322 | |||
| 323 | // TODO: It would be quicker not to instantiate the object, but to merely |
||
| 324 | // get its class from the Injector |
||
| 325 | return Injector::inst()->get($class, true)->config()->escape_type; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Save the casting cache for this object (including data from any failovers) into a variable |
||
| 330 | * |
||
| 331 | * @param reference $cache |
||
| 332 | */ |
||
| 333 | public function buildCastingCache(&$cache) { |
||
| 356 | |||
| 357 | // TEMPLATE ACCESS LAYER ------------------------------------------------------------------------------------------- |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Render this object into the template, and get the result as a string. You can pass one of the following as the |
||
| 361 | * $template parameter: |
||
| 362 | * - a template name (e.g. Page) |
||
| 363 | * - an array of possible template names - the first valid one will be used |
||
| 364 | * - an SSViewer instance |
||
| 365 | * |
||
| 366 | * @param string|array|SSViewer $template the template to render into |
||
| 367 | * @param array $customFields fields to customise() the object with before rendering |
||
| 368 | * @return HTMLText |
||
| 369 | */ |
||
| 370 | public function renderWith($template, $customFields = null) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Generate the cache name for a field |
||
| 391 | * |
||
| 392 | * @param string $fieldName Name of field |
||
| 393 | * @param array $arguments List of optional arguments given |
||
| 394 | */ |
||
| 395 | protected function objCacheName($fieldName, $arguments) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get a cached value from the field cache |
||
| 403 | * |
||
| 404 | * @param string $key Cache key |
||
| 405 | * @return mixed |
||
| 406 | */ |
||
| 407 | protected function objCacheGet($key) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Store a value in the field cache |
||
| 413 | * |
||
| 414 | * @param string $key Cache key |
||
| 415 | * @param mixed $value |
||
| 416 | */ |
||
| 417 | protected function objCacheSet($key, $value) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get the value of a field on this object, automatically inserting the value into any available casting objects |
||
| 423 | * that have been specified. |
||
| 424 | * |
||
| 425 | * @param string $fieldName |
||
| 426 | * @param array $arguments |
||
| 427 | * @param bool $forceReturnedObject if TRUE, the value will ALWAYS be casted to an object before being returned, |
||
| 428 | * even if there is no explicit casting information |
||
| 429 | * @param bool $cache Cache this object |
||
| 430 | * @param string $cacheName a custom cache name |
||
| 431 | */ |
||
| 432 | public function obj($fieldName, $arguments = null, $forceReturnedObject = true, $cache = false, $cacheName = null) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * A simple wrapper around {@link ViewableData::obj()} that automatically caches the result so it can be used again |
||
| 473 | * without re-running the method. |
||
| 474 | * |
||
| 475 | * @param string $field |
||
| 476 | * @param array $arguments |
||
| 477 | * @param string $identifier an optional custom cache identifier |
||
| 478 | */ |
||
| 479 | public function cachedCall($field, $arguments = null, $identifier = null) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Checks if a given method/field has a valid value. If the result is an object, this will return the result of the |
||
| 485 | * exists method, otherwise will check if the result is not just an empty paragraph tag. |
||
| 486 | * |
||
| 487 | * @param string $field |
||
| 488 | * @param array $arguments |
||
| 489 | * @param bool $cache |
||
| 490 | * @return bool |
||
| 491 | */ |
||
| 492 | public function hasValue($field, $arguments = null, $cache = true) { |
||
| 502 | |||
| 503 | /**#@+ |
||
| 504 | * @param string $field |
||
| 505 | * @param array $arguments |
||
| 506 | * @param bool $cache |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Get the string value of a field on this object that has been suitable escaped to be inserted directly into a |
||
| 512 | * template. |
||
| 513 | */ |
||
| 514 | public function XML_val($field, $arguments = null, $cache = false) { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Return the value of the field without any escaping being applied. |
||
| 521 | */ |
||
| 522 | public function RAW_val($field, $arguments = null, $cache = true) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Return the value of a field in an SQL-safe format. |
||
| 528 | */ |
||
| 529 | public function SQL_val($field, $arguments = null, $cache = true) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Return the value of a field in a JavaScript-save format. |
||
| 535 | */ |
||
| 536 | public function JS_val($field, $arguments = null, $cache = true) { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Return the value of a field escaped suitable to be inserted into an XML node attribute. |
||
| 542 | */ |
||
| 543 | public function ATT_val($field, $arguments = null, $cache = true) { |
||
| 546 | |||
| 547 | /**#@-*/ |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Get an array of XML-escaped values by field name |
||
| 551 | * |
||
| 552 | * @param array $elements an array of field names |
||
| 553 | * @return array |
||
| 554 | */ |
||
| 555 | public function getXMLValues($fields) { |
||
| 564 | |||
| 565 | // ITERATOR SUPPORT ------------------------------------------------------------------------------------------------ |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Return a single-item iterator so you can iterate over the fields of a single record. |
||
| 569 | * |
||
| 570 | * This is useful so you can use a single record inside a <% control %> block in a template - and then use |
||
| 571 | * to access individual fields on this object. |
||
| 572 | * |
||
| 573 | * @return ArrayIterator |
||
| 574 | */ |
||
| 575 | public function getIterator() { |
||
| 578 | |||
| 579 | // UTILITY METHODS ------------------------------------------------------------------------------------------------- |
||
| 580 | |||
| 581 | /** |
||
| 582 | * When rendering some objects it is necessary to iterate over the object being rendered, to do this, you need |
||
| 583 | * access to itself. |
||
| 584 | * |
||
| 585 | * @return ViewableData |
||
| 586 | */ |
||
| 587 | public function Me() { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Return the directory if the current active theme (relative to the site root). |
||
| 593 | * |
||
| 594 | * This method is useful for things such as accessing theme images from your template without hardcoding the theme |
||
| 595 | * page - e.g. <img src="$ThemeDir/images/something.gif">. |
||
| 596 | * |
||
| 597 | * This method should only be used when a theme is currently active. However, it will fall over to the current |
||
| 598 | * project directory. |
||
| 599 | * |
||
| 600 | * @param string $subtheme the subtheme path to get |
||
| 601 | * @return string |
||
| 602 | */ |
||
| 603 | public function ThemeDir($subtheme = false) { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Get part of the current classes ancestry to be used as a CSS class. |
||
| 616 | * |
||
| 617 | * This method returns an escaped string of CSS classes representing the current classes ancestry until it hits a |
||
| 618 | * stop point - e.g. "Page DataObject ViewableData". |
||
| 619 | * |
||
| 620 | * @param string $stopAtClass the class to stop at (default: ViewableData) |
||
| 621 | * @return string |
||
| 622 | * @uses ClassInfo |
||
| 623 | */ |
||
| 624 | public function CSSClasses($stopAtClass = 'ViewableData') { |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Return debug information about this object that can be rendered into a template |
||
| 644 | * |
||
| 645 | * @return ViewableData_Debugger |
||
| 646 | */ |
||
| 647 | public function Debug() { |
||
| 650 | |||
| 651 | } |
||
| 652 | |||
| 808 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.