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 |
||
27 | class ViewableData extends Object implements IteratorAggregate { |
||
28 | |||
29 | /** |
||
30 | * An array of objects to cast certain fields to. This is set up as an array in the format: |
||
31 | * |
||
32 | * <code> |
||
33 | * public static $casting = array ( |
||
34 | * 'FieldName' => 'ClassToCastTo(Arguments)' |
||
35 | * ); |
||
36 | * </code> |
||
37 | * |
||
38 | * @var array |
||
39 | * @config |
||
40 | */ |
||
41 | private static $casting = array( |
||
42 | 'CSSClasses' => 'Varchar' |
||
43 | ); |
||
44 | |||
45 | /** |
||
46 | * The default object to cast scalar fields to if casting information is not specified, and casting to an object |
||
47 | * is required. |
||
48 | * |
||
49 | * @var string |
||
50 | * @config |
||
51 | */ |
||
52 | private static $default_cast = 'Text'; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | private static $casting_cache = array(); |
||
58 | |||
59 | // ----------------------------------------------------------------------------------------------------------------- |
||
60 | |||
61 | /** |
||
62 | * A failover object to attempt to get data from if it is not present on this object. |
||
63 | * |
||
64 | * @var ViewableData |
||
65 | */ |
||
66 | protected $failover; |
||
67 | |||
68 | /** |
||
69 | * @var ViewableData |
||
70 | */ |
||
71 | protected $customisedObject; |
||
72 | |||
73 | /** |
||
74 | * @var array |
||
75 | */ |
||
76 | private $objCache = array(); |
||
77 | |||
78 | // ----------------------------------------------------------------------------------------------------------------- |
||
79 | |||
80 | // FIELD GETTERS & SETTERS ----------------------------------------------------------------------------------------- |
||
81 | |||
82 | /** |
||
83 | * Check if a field exists on this object or its failover. |
||
84 | * Note that, unlike the core isset() implementation, this will return true if the property is defined |
||
85 | * and set to null. |
||
86 | * |
||
87 | * @param string $property |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function __isset($property) { |
||
104 | |||
105 | /** |
||
106 | * Get the value of a property/field on this object. This will check if a method called get{$property} exists, then |
||
107 | * check if a field is available using {@link ViewableData::getField()}, then fall back on a failover object. |
||
108 | * |
||
109 | * @param string $property |
||
110 | * @return mixed |
||
111 | */ |
||
112 | public function __get($property) { |
||
126 | |||
127 | /** |
||
128 | * Set a property/field on this object. This will check for the existence of a method called set{$property}, then |
||
129 | * use the {@link ViewableData::setField()} method. |
||
130 | * |
||
131 | * @param string $property |
||
132 | * @param mixed $value |
||
133 | */ |
||
134 | public function __set($property, $value) { |
||
142 | |||
143 | /** |
||
144 | * Set a failover object to attempt to get data from if it is not present on this object. |
||
145 | * |
||
146 | * @param ViewableData $failover |
||
147 | */ |
||
148 | public function setFailover(ViewableData $failover) { |
||
157 | |||
158 | /** |
||
159 | * Get the current failover object if set |
||
160 | * |
||
161 | * @return ViewableData|null |
||
162 | */ |
||
163 | public function getFailover() { |
||
166 | |||
167 | /** |
||
168 | * Check if a field exists on this object. This should be overloaded in child classes. |
||
169 | * |
||
170 | * @param string $field |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function hasField($field) { |
||
176 | |||
177 | /** |
||
178 | * Get the value of a field on this object. This should be overloaded in child classes. |
||
179 | * |
||
180 | * @param string $field |
||
181 | * @return mixed |
||
182 | */ |
||
183 | public function getField($field) { |
||
186 | |||
187 | /** |
||
188 | * Set a field on this object. This should be overloaded in child classes. |
||
189 | * |
||
190 | * @param string $field |
||
191 | * @param mixed $value |
||
192 | * @return $this |
||
193 | */ |
||
194 | public function setField($field, $value) { |
||
199 | |||
200 | // ----------------------------------------------------------------------------------------------------------------- |
||
201 | |||
202 | /** |
||
203 | * Add methods from the {@link ViewableData::$failover} object, as well as wrapping any methods prefixed with an |
||
204 | * underscore into a {@link ViewableData::cachedCall()}. |
||
205 | * |
||
206 | * @throws LogicException |
||
207 | */ |
||
208 | public function defineMethods() { |
||
222 | |||
223 | /** |
||
224 | * Merge some arbitrary data in with this object. This method returns a {@link ViewableData_Customised} instance |
||
225 | * with references to both this and the new custom data. |
||
226 | * |
||
227 | * Note that any fields you specify will take precedence over the fields on this object. |
||
228 | * |
||
229 | * @param array|ViewableData $data |
||
230 | * @return ViewableData_Customised |
||
231 | */ |
||
232 | public function customise($data) { |
||
245 | |||
246 | /** |
||
247 | * @return ViewableData |
||
248 | */ |
||
249 | public function getCustomisedObj() { |
||
252 | |||
253 | /** |
||
254 | * @param ViewableData $object |
||
255 | */ |
||
256 | public function setCustomisedObj(ViewableData $object) { |
||
259 | |||
260 | // CASTING --------------------------------------------------------------------------------------------------------- |
||
261 | |||
262 | /** |
||
263 | * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) |
||
264 | * for a field on this object. This helper will be a subclass of DBField. |
||
265 | * |
||
266 | * @param string $field |
||
267 | * @return string Casting helper As a constructor pattern, and may include arguments. |
||
268 | */ |
||
269 | public function castingHelper($field) { |
||
289 | |||
290 | /** |
||
291 | * Get the class name a field on this object will be casted to. |
||
292 | * |
||
293 | * @param string $field |
||
294 | * @return string |
||
295 | */ |
||
296 | public function castingClass($field) { |
||
301 | |||
302 | /** |
||
303 | * Return the string-format type for the given field. |
||
304 | * |
||
305 | * @param string $field |
||
306 | * @return string 'xml'|'raw' |
||
307 | */ |
||
308 | public function escapeTypeForField($field) { |
||
315 | |||
316 | // TEMPLATE ACCESS LAYER ------------------------------------------------------------------------------------------- |
||
317 | |||
318 | /** |
||
319 | * Render this object into the template, and get the result as a string. You can pass one of the following as the |
||
320 | * $template parameter: |
||
321 | * - a template name (e.g. Page) |
||
322 | * - an array of possible template names - the first valid one will be used |
||
323 | * - an SSViewer instance |
||
324 | * |
||
325 | * @param string|array|SSViewer $template the template to render into |
||
326 | * @param array $customFields fields to customise() the object with before rendering |
||
327 | * @return DBHTMLText |
||
328 | */ |
||
329 | public function renderWith($template, $customFields = null) { |
||
347 | |||
348 | /** |
||
349 | * Generate the cache name for a field |
||
350 | * |
||
351 | * @param string $fieldName Name of field |
||
352 | * @param array $arguments List of optional arguments given |
||
353 | * @return string |
||
354 | */ |
||
355 | protected function objCacheName($fieldName, $arguments) { |
||
360 | |||
361 | /** |
||
362 | * Get a cached value from the field cache |
||
363 | * |
||
364 | * @param string $key Cache key |
||
365 | * @return mixed |
||
366 | */ |
||
367 | protected function objCacheGet($key) { |
||
373 | |||
374 | /** |
||
375 | * Store a value in the field cache |
||
376 | * |
||
377 | * @param string $key Cache key |
||
378 | * @param mixed $value |
||
379 | * @return $this |
||
380 | */ |
||
381 | protected function objCacheSet($key, $value) { |
||
385 | |||
386 | /** |
||
387 | * Clear object cache |
||
388 | * |
||
389 | * @return $this |
||
390 | */ |
||
391 | protected function objCacheClear() { |
||
395 | |||
396 | /** |
||
397 | * Get the value of a field on this object, automatically inserting the value into any available casting objects |
||
398 | * that have been specified. |
||
399 | * |
||
400 | * @param string $fieldName |
||
401 | * @param array $arguments |
||
402 | * @param bool $cache Cache this object |
||
403 | * @param string $cacheName a custom cache name |
||
404 | * @return Object|DBField |
||
405 | */ |
||
406 | public function obj($fieldName, $arguments = [], $cache = false, $cacheName = null) { |
||
440 | |||
441 | /** |
||
442 | * A simple wrapper around {@link ViewableData::obj()} that automatically caches the result so it can be used again |
||
443 | * without re-running the method. |
||
444 | * |
||
445 | * @param string $field |
||
446 | * @param array $arguments |
||
447 | * @param string $identifier an optional custom cache identifier |
||
448 | * @return Object|DBField |
||
449 | */ |
||
450 | public function cachedCall($field, $arguments = [], $identifier = null) { |
||
453 | |||
454 | /** |
||
455 | * Checks if a given method/field has a valid value. If the result is an object, this will return the result of the |
||
456 | * exists method, otherwise will check if the result is not just an empty paragraph tag. |
||
457 | * |
||
458 | * @param string $field |
||
459 | * @param array $arguments |
||
460 | * @param bool $cache |
||
461 | * @return bool |
||
462 | */ |
||
463 | public function hasValue($field, $arguments = [], $cache = true) { |
||
467 | |||
468 | /** |
||
469 | * Get the string value of a field on this object that has been suitable escaped to be inserted directly into a |
||
470 | * template. |
||
471 | * |
||
472 | * @param string $field |
||
473 | * @param array $arguments |
||
474 | * @param bool $cache |
||
475 | * @return string |
||
476 | */ |
||
477 | public function XML_val($field, $arguments = [], $cache = false) { |
||
482 | |||
483 | /** |
||
484 | * Get an array of XML-escaped values by field name |
||
485 | * |
||
486 | * @param array $fields an array of field names |
||
487 | * @return array |
||
488 | */ |
||
489 | public function getXMLValues($fields) { |
||
498 | |||
499 | // ITERATOR SUPPORT ------------------------------------------------------------------------------------------------ |
||
500 | |||
501 | /** |
||
502 | * Return a single-item iterator so you can iterate over the fields of a single record. |
||
503 | * |
||
504 | * This is useful so you can use a single record inside a <% control %> block in a template - and then use |
||
505 | * to access individual fields on this object. |
||
506 | * |
||
507 | * @return ArrayIterator |
||
508 | */ |
||
509 | public function getIterator() { |
||
512 | |||
513 | // UTILITY METHODS ------------------------------------------------------------------------------------------------- |
||
514 | |||
515 | /** |
||
516 | * When rendering some objects it is necessary to iterate over the object being rendered, to do this, you need |
||
517 | * access to itself. |
||
518 | * |
||
519 | * @return ViewableData |
||
520 | */ |
||
521 | public function Me() { |
||
524 | |||
525 | /** |
||
526 | * Return the directory if the current active theme (relative to the site root). |
||
527 | * |
||
528 | * This method is useful for things such as accessing theme images from your template without hardcoding the theme |
||
529 | * page - e.g. <img src="$ThemeDir/images/something.gif">. |
||
530 | * |
||
531 | * This method should only be used when a theme is currently active. However, it will fall over to the current |
||
532 | * project directory. |
||
533 | * |
||
534 | * @param string $subtheme the subtheme path to get |
||
535 | * @return string |
||
536 | */ |
||
537 | public function ThemeDir($subtheme = null) { |
||
547 | |||
548 | /** |
||
549 | * Get part of the current classes ancestry to be used as a CSS class. |
||
550 | * |
||
551 | * This method returns an escaped string of CSS classes representing the current classes ancestry until it hits a |
||
552 | * stop point - e.g. "Page DataObject ViewableData". |
||
553 | * |
||
554 | * @param string $stopAtClass the class to stop at (default: ViewableData) |
||
555 | * @return string |
||
556 | * @uses ClassInfo |
||
557 | */ |
||
558 | public function CSSClasses($stopAtClass = 'SilverStripe\\View\\ViewableData') { |
||
578 | |||
579 | /** |
||
580 | * Return debug information about this object that can be rendered into a template |
||
581 | * |
||
582 | * @return ViewableData_Debugger |
||
583 | */ |
||
584 | public function Debug() { |
||
587 | |||
588 | } |
||
589 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.