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 |
||
| 29 | class ViewableData implements IteratorAggregate |
||
| 30 | { |
||
| 31 | use Extensible { |
||
| 32 | defineMethods as extensibleDefineMethods; |
||
| 33 | } |
||
| 34 | use Injectable; |
||
| 35 | use Configurable; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * An array of objects to cast certain fields to. This is set up as an array in the format: |
||
| 39 | * |
||
| 40 | * <code> |
||
| 41 | * public static $casting = array ( |
||
| 42 | * 'FieldName' => 'ClassToCastTo(Arguments)' |
||
| 43 | * ); |
||
| 44 | * </code> |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | * @config |
||
| 48 | */ |
||
| 49 | private static $casting = array( |
||
| 50 | 'CSSClasses' => 'Varchar' |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The default object to cast scalar fields to if casting information is not specified, and casting to an object |
||
| 55 | * is required. |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | * @config |
||
| 59 | */ |
||
| 60 | private static $default_cast = 'Text'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private static $casting_cache = array(); |
||
| 66 | |||
| 67 | // ----------------------------------------------------------------------------------------------------------------- |
||
| 68 | |||
| 69 | /** |
||
| 70 | * A failover object to attempt to get data from if it is not present on this object. |
||
| 71 | * |
||
| 72 | * @var ViewableData |
||
| 73 | */ |
||
| 74 | protected $failover; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var ViewableData |
||
| 78 | */ |
||
| 79 | protected $customisedObject; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | private $objCache = array(); |
||
| 85 | |||
| 86 | public function __construct() |
||
| 90 | |||
| 91 | // ----------------------------------------------------------------------------------------------------------------- |
||
| 92 | |||
| 93 | // FIELD GETTERS & SETTERS ----------------------------------------------------------------------------------------- |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Check if a field exists on this object or its failover. |
||
| 97 | * Note that, unlike the core isset() implementation, this will return true if the property is defined |
||
| 98 | * and set to null. |
||
| 99 | * |
||
| 100 | * @param string $property |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | public function __isset($property) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get the value of a property/field on this object. This will check if a method called get{$property} exists, then |
||
| 119 | * check if a field is available using {@link ViewableData::getField()}, then fall back on a failover object. |
||
| 120 | * |
||
| 121 | * @param string $property |
||
| 122 | * @return mixed |
||
| 123 | */ |
||
| 124 | public function __get($property) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Set a property/field on this object. This will check for the existence of a method called set{$property}, then |
||
| 140 | * use the {@link ViewableData::setField()} method. |
||
| 141 | * |
||
| 142 | * @param string $property |
||
| 143 | * @param mixed $value |
||
| 144 | */ |
||
| 145 | public function __set($property, $value) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Set a failover object to attempt to get data from if it is not present on this object. |
||
| 157 | * |
||
| 158 | * @param ViewableData $failover |
||
| 159 | */ |
||
| 160 | public function setFailover(ViewableData $failover) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get the current failover object if set |
||
| 173 | * |
||
| 174 | * @return ViewableData|null |
||
| 175 | */ |
||
| 176 | public function getFailover() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Check if a field exists on this object. This should be overloaded in child classes. |
||
| 183 | * |
||
| 184 | * @param string $field |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | public function hasField($field) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get the value of a field on this object. This should be overloaded in child classes. |
||
| 194 | * |
||
| 195 | * @param string $field |
||
| 196 | * @return mixed |
||
| 197 | */ |
||
| 198 | public function getField($field) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Set a field on this object. This should be overloaded in child classes. |
||
| 205 | * |
||
| 206 | * @param string $field |
||
| 207 | * @param mixed $value |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function setField($field, $value) |
||
| 216 | |||
| 217 | // ----------------------------------------------------------------------------------------------------------------- |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Add methods from the {@link ViewableData::$failover} object, as well as wrapping any methods prefixed with an |
||
| 221 | * underscore into a {@link ViewableData::cachedCall()}. |
||
| 222 | * |
||
| 223 | * @throws LogicException |
||
| 224 | */ |
||
| 225 | public function defineMethods() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Merge some arbitrary data in with this object. This method returns a {@link ViewableData_Customised} instance |
||
| 244 | * with references to both this and the new custom data. |
||
| 245 | * |
||
| 246 | * Note that any fields you specify will take precedence over the fields on this object. |
||
| 247 | * |
||
| 248 | * @param array|ViewableData $data |
||
| 249 | * @return ViewableData_Customised |
||
| 250 | */ |
||
| 251 | public function customise($data) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Return true if this object "exists" i.e. has a sensible value |
||
| 268 | * |
||
| 269 | * This method should be overriden in subclasses to provide more context about the classes state. For example, a |
||
| 270 | * {@link DataObject} class could return false when it is deleted from the database |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | public function exists() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return string the class name |
||
| 281 | */ |
||
| 282 | public function __toString() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return ViewableData |
||
| 289 | */ |
||
| 290 | public function getCustomisedObj() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param ViewableData $object |
||
| 297 | */ |
||
| 298 | public function setCustomisedObj(ViewableData $object) |
||
| 302 | |||
| 303 | // CASTING --------------------------------------------------------------------------------------------------------- |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) |
||
| 307 | * for a field on this object. This helper will be a subclass of DBField. |
||
| 308 | * |
||
| 309 | * @param string $field |
||
| 310 | * @return string Casting helper As a constructor pattern, and may include arguments. |
||
| 311 | * @throws Exception |
||
| 312 | */ |
||
| 313 | public function castingHelper($field) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Get the class name a field on this object will be casted to. |
||
| 341 | * |
||
| 342 | * @param string $field |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public function castingClass($field) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Return the string-format type for the given field. |
||
| 354 | * |
||
| 355 | * @param string $field |
||
| 356 | * @return string 'xml'|'raw' |
||
| 357 | */ |
||
| 358 | public function escapeTypeForField($field) |
||
| 368 | |||
| 369 | // TEMPLATE ACCESS LAYER ------------------------------------------------------------------------------------------- |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Render this object into the template, and get the result as a string. You can pass one of the following as the |
||
| 373 | * $template parameter: |
||
| 374 | * - a template name (e.g. Page) |
||
| 375 | * - an array of possible template names - the first valid one will be used |
||
| 376 | * - an SSViewer instance |
||
| 377 | * |
||
| 378 | * @param string|array|SSViewer $template the template to render into |
||
| 379 | * @param array $customFields fields to customise() the object with before rendering |
||
| 380 | * @return DBHTMLText |
||
| 381 | */ |
||
| 382 | public function renderWith($template, $customFields = null) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Generate the cache name for a field |
||
| 404 | * |
||
| 405 | * @param string $fieldName Name of field |
||
| 406 | * @param array $arguments List of optional arguments given |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | protected function objCacheName($fieldName, $arguments) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get a cached value from the field cache |
||
| 418 | * |
||
| 419 | * @param string $key Cache key |
||
| 420 | * @return mixed |
||
| 421 | */ |
||
| 422 | protected function objCacheGet($key) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Store a value in the field cache |
||
| 432 | * |
||
| 433 | * @param string $key Cache key |
||
| 434 | * @param mixed $value |
||
| 435 | * @return $this |
||
| 436 | */ |
||
| 437 | protected function objCacheSet($key, $value) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Clear object cache |
||
| 445 | * |
||
| 446 | * @return $this |
||
| 447 | */ |
||
| 448 | protected function objCacheClear() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get the value of a field on this object, automatically inserting the value into any available casting objects |
||
| 456 | * that have been specified. |
||
| 457 | * |
||
| 458 | * @param string $fieldName |
||
| 459 | * @param array $arguments |
||
| 460 | * @param bool $cache Cache this object |
||
| 461 | * @param string $cacheName a custom cache name |
||
| 462 | * @return Object|DBField |
||
| 463 | */ |
||
| 464 | public function obj($fieldName, $arguments = [], $cache = false, $cacheName = null) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * A simple wrapper around {@link ViewableData::obj()} that automatically caches the result so it can be used again |
||
| 502 | * without re-running the method. |
||
| 503 | * |
||
| 504 | * @param string $field |
||
| 505 | * @param array $arguments |
||
| 506 | * @param string $identifier an optional custom cache identifier |
||
| 507 | * @return Object|DBField |
||
| 508 | */ |
||
| 509 | public function cachedCall($field, $arguments = [], $identifier = null) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Checks if a given method/field has a valid value. If the result is an object, this will return the result of the |
||
| 516 | * exists method, otherwise will check if the result is not just an empty paragraph tag. |
||
| 517 | * |
||
| 518 | * @param string $field |
||
| 519 | * @param array $arguments |
||
| 520 | * @param bool $cache |
||
| 521 | * @return bool |
||
| 522 | */ |
||
| 523 | public function hasValue($field, $arguments = [], $cache = true) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Get the string value of a field on this object that has been suitable escaped to be inserted directly into a |
||
| 531 | * template. |
||
| 532 | * |
||
| 533 | * @param string $field |
||
| 534 | * @param array $arguments |
||
| 535 | * @param bool $cache |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | public function XML_val($field, $arguments = [], $cache = false) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Get an array of XML-escaped values by field name |
||
| 547 | * |
||
| 548 | * @param array $fields an array of field names |
||
| 549 | * @return array |
||
| 550 | */ |
||
| 551 | public function getXMLValues($fields) |
||
| 561 | |||
| 562 | // ITERATOR SUPPORT ------------------------------------------------------------------------------------------------ |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Return a single-item iterator so you can iterate over the fields of a single record. |
||
| 566 | * |
||
| 567 | * This is useful so you can use a single record inside a <% control %> block in a template - and then use |
||
| 568 | * to access individual fields on this object. |
||
| 569 | * |
||
| 570 | * @return ArrayIterator |
||
| 571 | */ |
||
| 572 | public function getIterator() |
||
| 576 | |||
| 577 | // UTILITY METHODS ------------------------------------------------------------------------------------------------- |
||
| 578 | |||
| 579 | /** |
||
| 580 | * When rendering some objects it is necessary to iterate over the object being rendered, to do this, you need |
||
| 581 | * access to itself. |
||
| 582 | * |
||
| 583 | * @return ViewableData |
||
| 584 | */ |
||
| 585 | public function Me() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Get part of the current classes ancestry to be used as a CSS class. |
||
| 592 | * |
||
| 593 | * This method returns an escaped string of CSS classes representing the current classes ancestry until it hits a |
||
| 594 | * stop point - e.g. "Page DataObject ViewableData". |
||
| 595 | * |
||
| 596 | * @param string $stopAtClass the class to stop at (default: ViewableData) |
||
| 597 | * @return string |
||
| 598 | * @uses ClassInfo |
||
| 599 | */ |
||
| 600 | public function CSSClasses($stopAtClass = self::class) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Return debug information about this object that can be rendered into a template |
||
| 626 | * |
||
| 627 | * @return ViewableData_Debugger |
||
| 628 | */ |
||
| 629 | public function Debug() |
||
| 633 | } |
||
| 634 |
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: