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() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Merge some arbitrary data in with this object. This method returns a {@link ViewableData_Customised} instance |
||
| 204 | * with references to both this and the new custom data. |
||
| 205 | * |
||
| 206 | * Note that any fields you specify will take precedence over the fields on this object. |
||
| 207 | * |
||
| 208 | * @param array|ViewableData $data |
||
| 209 | * @return ViewableData_Customised |
||
| 210 | */ |
||
| 211 | public function customise($data) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return ViewableData |
||
| 227 | */ |
||
| 228 | public function getCustomisedObj() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param ViewableData $object |
||
| 234 | */ |
||
| 235 | public function setCustomisedObj(ViewableData $object) { |
||
| 238 | |||
| 239 | // CASTING --------------------------------------------------------------------------------------------------------- |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get the class a field on this object would be casted to, as well as the casting helper for casting a field to |
||
| 243 | * an object (see {@link ViewableData::castingHelper()} for information on casting helpers). |
||
| 244 | * |
||
| 245 | * The returned array contains two keys: |
||
| 246 | * - className: the class the field would be casted to (e.g. "Varchar") |
||
| 247 | * - castingHelper: the casting helper for casting the field (e.g. "return new Varchar($fieldName)") |
||
| 248 | * |
||
| 249 | * @param string $field |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | public function castingHelperPair($field) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) for a field |
||
| 259 | * on this object. |
||
| 260 | * |
||
| 261 | * @param string $field |
||
| 262 | * @return string Casting helper |
||
| 263 | */ |
||
| 264 | public function castingHelper($field) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get the class name a field on this object will be casted to |
||
| 275 | * |
||
| 276 | * @param string $field |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public function castingClass($field) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Return the string-format type for the given field. |
||
| 290 | * |
||
| 291 | * @param string $field |
||
| 292 | * @return string 'xml'|'raw' |
||
| 293 | */ |
||
| 294 | public function escapeTypeForField($field) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Save the casting cache for this object (including data from any failovers) into a variable |
||
| 304 | * |
||
| 305 | * @param reference $cache |
||
| 306 | */ |
||
| 307 | public function buildCastingCache(&$cache) { |
||
| 330 | |||
| 331 | // TEMPLATE ACCESS LAYER ------------------------------------------------------------------------------------------- |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Render this object into the template, and get the result as a string. You can pass one of the following as the |
||
| 335 | * $template parameter: |
||
| 336 | * - a template name (e.g. Page) |
||
| 337 | * - an array of possible template names - the first valid one will be used |
||
| 338 | * - an SSViewer instance |
||
| 339 | * |
||
| 340 | * @param string|array|SSViewer $template the template to render into |
||
| 341 | * @param array $customFields fields to customise() the object with before rendering |
||
| 342 | * @return HTMLText |
||
| 343 | */ |
||
| 344 | public function renderWith($template, $customFields = null) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Generate the cache name for a field |
||
| 365 | * |
||
| 366 | * @param string $fieldName Name of field |
||
| 367 | * @param array $arguments List of optional arguments given |
||
| 368 | */ |
||
| 369 | protected function objCacheName($fieldName, $arguments) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get a cached value from the field cache |
||
| 377 | * |
||
| 378 | * @param string $key Cache key |
||
| 379 | * @return mixed |
||
| 380 | */ |
||
| 381 | protected function objCacheGet($key) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Store a value in the field cache |
||
| 387 | * |
||
| 388 | * @param string $key Cache key |
||
| 389 | * @param mixed $value |
||
| 390 | */ |
||
| 391 | protected function objCacheSet($key, $value) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get the value of a field on this object, automatically inserting the value into any available casting objects |
||
| 397 | * that have been specified. |
||
| 398 | * |
||
| 399 | * @param string $fieldName |
||
| 400 | * @param array $arguments |
||
| 401 | * @param bool $forceReturnedObject if TRUE, the value will ALWAYS be casted to an object before being returned, |
||
| 402 | * even if there is no explicit casting information |
||
| 403 | * @param bool $cache Cache this object |
||
| 404 | * @param string $cacheName a custom cache name |
||
| 405 | */ |
||
| 406 | public function obj($fieldName, $arguments = null, $forceReturnedObject = true, $cache = false, $cacheName = null) { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * A simple wrapper around {@link ViewableData::obj()} that automatically caches the result so it can be used again |
||
| 447 | * without re-running the method. |
||
| 448 | * |
||
| 449 | * @param string $field |
||
| 450 | * @param array $arguments |
||
| 451 | * @param string $identifier an optional custom cache identifier |
||
| 452 | */ |
||
| 453 | public function cachedCall($field, $arguments = null, $identifier = null) { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Checks if a given method/field has a valid value. If the result is an object, this will return the result of the |
||
| 459 | * exists method, otherwise will check if the result is not just an empty paragraph tag. |
||
| 460 | * |
||
| 461 | * @param string $field |
||
| 462 | * @param array $arguments |
||
| 463 | * @param bool $cache |
||
| 464 | * @return bool |
||
| 465 | */ |
||
| 466 | public function hasValue($field, $arguments = null, $cache = true) { |
||
| 476 | |||
| 477 | /**#@+ |
||
| 478 | * @param string $field |
||
| 479 | * @param array $arguments |
||
| 480 | * @param bool $cache |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get the string value of a field on this object that has been suitable escaped to be inserted directly into a |
||
| 486 | * template. |
||
| 487 | */ |
||
| 488 | public function XML_val($field, $arguments = null, $cache = false) { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Return the value of the field without any escaping being applied. |
||
| 495 | */ |
||
| 496 | public function RAW_val($field, $arguments = null, $cache = true) { |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Return the value of a field in an SQL-safe format. |
||
| 502 | */ |
||
| 503 | public function SQL_val($field, $arguments = null, $cache = true) { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Return the value of a field in a JavaScript-save format. |
||
| 509 | */ |
||
| 510 | public function JS_val($field, $arguments = null, $cache = true) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Return the value of a field escaped suitable to be inserted into an XML node attribute. |
||
| 516 | */ |
||
| 517 | public function ATT_val($field, $arguments = null, $cache = true) { |
||
| 520 | |||
| 521 | /**#@-*/ |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Get an array of XML-escaped values by field name |
||
| 525 | * |
||
| 526 | * @param array $elements an array of field names |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | public function getXMLValues($fields) { |
||
| 538 | |||
| 539 | // ITERATOR SUPPORT ------------------------------------------------------------------------------------------------ |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Return a single-item iterator so you can iterate over the fields of a single record. |
||
| 543 | * |
||
| 544 | * This is useful so you can use a single record inside a <% control %> block in a template - and then use |
||
| 545 | * to access individual fields on this object. |
||
| 546 | * |
||
| 547 | * @return ArrayIterator |
||
| 548 | */ |
||
| 549 | public function getIterator() { |
||
| 552 | |||
| 553 | // UTILITY METHODS ------------------------------------------------------------------------------------------------- |
||
| 554 | |||
| 555 | /** |
||
| 556 | * When rendering some objects it is necessary to iterate over the object being rendered, to do this, you need |
||
| 557 | * access to itself. |
||
| 558 | * |
||
| 559 | * @return ViewableData |
||
| 560 | */ |
||
| 561 | public function Me() { |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Return the directory if the current active theme (relative to the site root). |
||
| 567 | * |
||
| 568 | * This method is useful for things such as accessing theme images from your template without hardcoding the theme |
||
| 569 | * page - e.g. <img src="$ThemeDir/images/something.gif">. |
||
| 570 | * |
||
| 571 | * This method should only be used when a theme is currently active. However, it will fall over to the current |
||
| 572 | * project directory. |
||
| 573 | * |
||
| 574 | * @param string $subtheme the subtheme path to get |
||
| 575 | * @return string |
||
| 576 | */ |
||
| 577 | public function ThemeDir($subtheme = false) { |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Get part of the current classes ancestry to be used as a CSS class. |
||
| 590 | * |
||
| 591 | * This method returns an escaped string of CSS classes representing the current classes ancestry until it hits a |
||
| 592 | * stop point - e.g. "Page DataObject ViewableData". |
||
| 593 | * |
||
| 594 | * @param string $stopAtClass the class to stop at (default: ViewableData) |
||
| 595 | * @return string |
||
| 596 | * @uses ClassInfo |
||
| 597 | */ |
||
| 598 | public function CSSClasses($stopAtClass = 'ViewableData') { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Return debug information about this object that can be rendered into a template |
||
| 618 | * |
||
| 619 | * @return ViewableData_Debugger |
||
| 620 | */ |
||
| 621 | public function Debug() { |
||
| 624 | |||
| 625 | } |
||
| 626 | |||
| 782 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.