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 | // FIELD GETTERS & SETTERS ----------------------------------------------------------------------------------------- |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Check if a field exists on this object or its failover. |
||
| 72 | * |
||
| 73 | * @param string $property |
||
| 74 | * @return bool |
||
| 75 | */ |
||
| 76 | public function __isset($property) { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get the value of a property/field on this object. This will check if a method called get{$property} exists, then |
||
| 82 | * check if a field is available using {@link ViewableData::getField()}, then fall back on a failover object. |
||
| 83 | * |
||
| 84 | * @param string $property |
||
| 85 | * @return mixed |
||
| 86 | */ |
||
| 87 | public function __get($property) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Set a property/field on this object. This will check for the existence of a method called set{$property}, then |
||
| 100 | * use the {@link ViewableData::setField()} method. |
||
| 101 | * |
||
| 102 | * @param string $property |
||
| 103 | * @param mixed $value |
||
| 104 | */ |
||
| 105 | public function __set($property, $value) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Set a failover object to attempt to get data from if it is not present on this object. |
||
| 116 | * |
||
| 117 | * @param ViewableData $failover |
||
| 118 | */ |
||
| 119 | public function setFailover(ViewableData $failover) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the current failover object if set |
||
| 131 | * |
||
| 132 | * @return ViewableData|null |
||
| 133 | */ |
||
| 134 | public function getFailover() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Check if a field exists on this object. This should be overloaded in child classes. |
||
| 140 | * |
||
| 141 | * @param string $field |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | public function hasField($field) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the value of a field on this object. This should be overloaded in child classes. |
||
| 150 | * |
||
| 151 | * @param string $field |
||
| 152 | * @return mixed |
||
| 153 | */ |
||
| 154 | public function getField($field) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Set a field on this object. This should be overloaded in child classes. |
||
| 160 | * |
||
| 161 | * @param string $field |
||
| 162 | * @param mixed $value |
||
| 163 | * @return $this |
||
| 164 | */ |
||
| 165 | public function setField($field, $value) { |
||
| 170 | |||
| 171 | // ----------------------------------------------------------------------------------------------------------------- |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Add methods from the {@link ViewableData::$failover} object, as well as wrapping any methods prefixed with an |
||
| 175 | * underscore into a {@link ViewableData::cachedCall()}. |
||
| 176 | * |
||
| 177 | * @throws LogicException |
||
| 178 | */ |
||
| 179 | public function defineMethods() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Merge some arbitrary data in with this object. This method returns a {@link ViewableData_Customised} instance |
||
| 196 | * with references to both this and the new custom data. |
||
| 197 | * |
||
| 198 | * Note that any fields you specify will take precedence over the fields on this object. |
||
| 199 | * |
||
| 200 | * @param array|ViewableData $data |
||
| 201 | * @return ViewableData_Customised |
||
| 202 | */ |
||
| 203 | public function customise($data) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return ViewableData |
||
| 219 | */ |
||
| 220 | public function getCustomisedObj() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param ViewableData $object |
||
| 226 | */ |
||
| 227 | public function setCustomisedObj(ViewableData $object) { |
||
| 230 | |||
| 231 | // CASTING --------------------------------------------------------------------------------------------------------- |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) |
||
| 235 | * for a field on this object. This helper will be a subclass of DBField. |
||
| 236 | * |
||
| 237 | * @param string $field |
||
| 238 | * @return string Casting helper As a constructor pattern, and may include arguments. |
||
| 239 | */ |
||
| 240 | public function castingHelper($field) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get the class name a field on this object will be casted to. |
||
| 263 | * |
||
| 264 | * @param string $field |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function castingClass($field) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Return the string-format type for the given field. |
||
| 275 | * |
||
| 276 | * @param string $field |
||
| 277 | * @return string 'xml'|'raw' |
||
| 278 | */ |
||
| 279 | public function escapeTypeForField($field) { |
||
| 286 | |||
| 287 | // TEMPLATE ACCESS LAYER ------------------------------------------------------------------------------------------- |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Render this object into the template, and get the result as a string. You can pass one of the following as the |
||
| 291 | * $template parameter: |
||
| 292 | * - a template name (e.g. Page) |
||
| 293 | * - an array of possible template names - the first valid one will be used |
||
| 294 | * - an SSViewer instance |
||
| 295 | * |
||
| 296 | * @param string|array|SSViewer $template the template to render into |
||
| 297 | * @param array $customFields fields to customise() the object with before rendering |
||
| 298 | * @return DBHTMLText |
||
| 299 | */ |
||
| 300 | public function renderWith($template, $customFields = null) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Generate the cache name for a field |
||
| 321 | * |
||
| 322 | * @param string $fieldName Name of field |
||
| 323 | * @param array $arguments List of optional arguments given |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | protected function objCacheName($fieldName, $arguments) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get a cached value from the field cache |
||
| 334 | * |
||
| 335 | * @param string $key Cache key |
||
| 336 | * @return mixed |
||
| 337 | */ |
||
| 338 | protected function objCacheGet($key) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Store a value in the field cache |
||
| 347 | * |
||
| 348 | * @param string $key Cache key |
||
| 349 | * @param mixed $value |
||
| 350 | * @return $this |
||
| 351 | */ |
||
| 352 | protected function objCacheSet($key, $value) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Clear object cache |
||
| 359 | * |
||
| 360 | * @return $this |
||
| 361 | */ |
||
| 362 | protected function objCacheClear() { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get the value of a field on this object, automatically inserting the value into any available casting objects |
||
| 369 | * that have been specified. |
||
| 370 | * |
||
| 371 | * @param string $fieldName |
||
| 372 | * @param array $arguments |
||
| 373 | * @param bool $cache Cache this object |
||
| 374 | * @param string $cacheName a custom cache name |
||
| 375 | * @return Object|DBField |
||
| 376 | */ |
||
| 377 | public function obj($fieldName, $arguments = [], $cache = false, $cacheName = null) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * A simple wrapper around {@link ViewableData::obj()} that automatically caches the result so it can be used again |
||
| 414 | * without re-running the method. |
||
| 415 | * |
||
| 416 | * @param string $field |
||
| 417 | * @param array $arguments |
||
| 418 | * @param string $identifier an optional custom cache identifier |
||
| 419 | * @return Object|DBField |
||
| 420 | */ |
||
| 421 | public function cachedCall($field, $arguments = [], $identifier = null) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Checks if a given method/field has a valid value. If the result is an object, this will return the result of the |
||
| 427 | * exists method, otherwise will check if the result is not just an empty paragraph tag. |
||
| 428 | * |
||
| 429 | * @param string $field |
||
| 430 | * @param array $arguments |
||
| 431 | * @param bool $cache |
||
| 432 | * @return bool |
||
| 433 | */ |
||
| 434 | public function hasValue($field, $arguments = [], $cache = true) { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get the string value of a field on this object that has been suitable escaped to be inserted directly into a |
||
| 441 | * template. |
||
| 442 | * |
||
| 443 | * @param string $field |
||
| 444 | * @param array $arguments |
||
| 445 | * @param bool $cache |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | public function XML_val($field, $arguments = [], $cache = false) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get an array of XML-escaped values by field name |
||
| 456 | * |
||
| 457 | * @param array $fields an array of field names |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | public function getXMLValues($fields) { |
||
| 469 | |||
| 470 | // ITERATOR SUPPORT ------------------------------------------------------------------------------------------------ |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Return a single-item iterator so you can iterate over the fields of a single record. |
||
| 474 | * |
||
| 475 | * This is useful so you can use a single record inside a <% control %> block in a template - and then use |
||
| 476 | * to access individual fields on this object. |
||
| 477 | * |
||
| 478 | * @return ArrayIterator |
||
| 479 | */ |
||
| 480 | public function getIterator() { |
||
| 483 | |||
| 484 | // UTILITY METHODS ------------------------------------------------------------------------------------------------- |
||
| 485 | |||
| 486 | /** |
||
| 487 | * When rendering some objects it is necessary to iterate over the object being rendered, to do this, you need |
||
| 488 | * access to itself. |
||
| 489 | * |
||
| 490 | * @return ViewableData |
||
| 491 | */ |
||
| 492 | public function Me() { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Return the directory if the current active theme (relative to the site root). |
||
| 498 | * |
||
| 499 | * This method is useful for things such as accessing theme images from your template without hardcoding the theme |
||
| 500 | * page - e.g. <img src="$ThemeDir/images/something.gif">. |
||
| 501 | * |
||
| 502 | * This method should only be used when a theme is currently active. However, it will fall over to the current |
||
| 503 | * project directory. |
||
| 504 | * |
||
| 505 | * @param string $subtheme the subtheme path to get |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | public function ThemeDir($subtheme = null) { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Get part of the current classes ancestry to be used as a CSS class. |
||
| 521 | * |
||
| 522 | * This method returns an escaped string of CSS classes representing the current classes ancestry until it hits a |
||
| 523 | * stop point - e.g. "Page DataObject ViewableData". |
||
| 524 | * |
||
| 525 | * @param string $stopAtClass the class to stop at (default: ViewableData) |
||
| 526 | * @return string |
||
| 527 | * @uses ClassInfo |
||
| 528 | */ |
||
| 529 | public function CSSClasses($stopAtClass = 'ViewableData') { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Return debug information about this object that can be rendered into a template |
||
| 549 | * |
||
| 550 | * @return ViewableData_Debugger |
||
| 551 | */ |
||
| 552 | public function Debug() { |
||
| 555 | |||
| 556 | } |
||
| 557 | |||
| 709 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: