Complex classes like SS_Object 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 SS_Object, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | abstract class SS_Object { |
||
18 | |||
19 | /** |
||
20 | * An array of extension names and parameters to be applied to this object upon construction. |
||
21 | * |
||
22 | * Example: |
||
23 | * <code> |
||
24 | * private static $extensions = array ( |
||
25 | * 'Hierarchy', |
||
26 | * "Version('Stage', 'Live')" |
||
27 | * ); |
||
28 | * </code> |
||
29 | * |
||
30 | * Use {@link Object::add_extension()} to add extensions without access to the class code, |
||
31 | * e.g. to extend core classes. |
||
32 | * |
||
33 | * Extensions are instanciated together with the object and stored in {@link $extension_instances}. |
||
34 | * |
||
35 | * @var array $extensions |
||
36 | * @config |
||
37 | */ |
||
38 | private static $extensions = null; |
||
39 | |||
40 | private static |
||
41 | $classes_constructed = array(), |
||
42 | $extra_methods = array(), |
||
43 | $built_in_methods = array(); |
||
44 | |||
45 | private static |
||
46 | $custom_classes = array(), |
||
47 | $strong_classes = array(); |
||
48 | |||
49 | /**#@-*/ |
||
50 | |||
51 | /** |
||
52 | * @var string the class name |
||
53 | */ |
||
54 | public $class; |
||
55 | |||
56 | /** |
||
57 | * Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....). |
||
58 | * @return Config_ForClass|null |
||
59 | */ |
||
60 | static public function config() { |
||
63 | |||
64 | /** |
||
65 | * @var array all current extension instances. |
||
66 | */ |
||
67 | protected $extension_instances = array(); |
||
68 | |||
69 | /** |
||
70 | * List of callbacks to call prior to extensions having extend called on them, |
||
71 | * each grouped by methodName. |
||
72 | * |
||
73 | * @var array[callable] |
||
74 | */ |
||
75 | protected $beforeExtendCallbacks = array(); |
||
76 | |||
77 | /** |
||
78 | * Allows user code to hook into Object::extend prior to control |
||
79 | * being delegated to extensions. Each callback will be reset |
||
80 | * once called. |
||
81 | * |
||
82 | * @param string $method The name of the method to hook into |
||
83 | * @param callable $callback The callback to execute |
||
84 | */ |
||
85 | protected function beforeExtending($method, $callback) { |
||
91 | |||
92 | /** |
||
93 | * List of callbacks to call after extensions having extend called on them, |
||
94 | * each grouped by methodName. |
||
95 | * |
||
96 | * @var array[callable] |
||
97 | */ |
||
98 | protected $afterExtendCallbacks = array(); |
||
99 | |||
100 | /** |
||
101 | * Allows user code to hook into Object::extend after control |
||
102 | * being delegated to extensions. Each callback will be reset |
||
103 | * once called. |
||
104 | * |
||
105 | * @param string $method The name of the method to hook into |
||
106 | * @param callable $callback The callback to execute |
||
107 | */ |
||
108 | protected function afterExtending($method, $callback) { |
||
114 | |||
115 | /** |
||
116 | * An implementation of the factory method, allows you to create an instance of a class |
||
117 | * |
||
118 | * This method first for strong class overloads (singletons & DB interaction), then custom class overloads. If an |
||
119 | * overload is found, an instance of this is returned rather than the original class. To overload a class, use |
||
120 | * {@link Object::useCustomClass()} |
||
121 | * |
||
122 | * This can be called in one of two ways - either calling via the class directly, |
||
123 | * or calling on Object and passing the class name as the first parameter. The following |
||
124 | * are equivalent: |
||
125 | * $list = DataList::create('SiteTree'); |
||
126 | * $list = SiteTree::get(); |
||
127 | * |
||
128 | * @param string $class the class name |
||
129 | * @param mixed $arguments,... arguments to pass to the constructor |
||
130 | * @return static |
||
131 | */ |
||
132 | public static function create() { |
||
133 | $args = func_get_args(); |
||
134 | |||
135 | // Class to create should be the calling class if not Object, |
||
136 | // otherwise the first parameter |
||
137 | $class = get_called_class(); |
||
138 | if($class == 'SS_Object' || $class == 'Object') $class = array_shift($args); |
||
139 | |||
140 | $class = self::getCustomClass($class); |
||
141 | |||
142 | return Injector::inst()->createWithArgs($class, $args); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Creates a class instance by the "singleton" design pattern. |
||
147 | * It will always return the same instance for this class, |
||
148 | * which can be used for performance reasons and as a simple |
||
149 | * way to access instance methods which don't rely on instance |
||
150 | * data (e.g. the custom SilverStripe static handling). |
||
151 | * |
||
152 | * @param string $className Optional classname (if called on Object directly) |
||
153 | * @return static The singleton instance |
||
154 | */ |
||
155 | public static function singleton() { |
||
165 | |||
166 | private static $_cache_inst_args = array(); |
||
167 | |||
168 | /** |
||
169 | * Create an object from a string representation. It treats it as a PHP constructor without the |
||
170 | * 'new' keyword. It also manages to construct the object without the use of eval(). |
||
171 | * |
||
172 | * Construction itself is done with Object::create(), so that Object::useCustomClass() calls |
||
173 | * are respected. |
||
174 | * |
||
175 | * `Object::create_from_string("Versioned('Stage','Live')")` will return the result of |
||
176 | * `Versioned::create('Stage', 'Live);` |
||
177 | * |
||
178 | * It is designed for simple, clonable objects. The first time this method is called for a given |
||
179 | * string it is cached, and clones of that object are returned. |
||
180 | * |
||
181 | * If you pass the $firstArg argument, this will be prepended to the constructor arguments. It's |
||
182 | * impossible to pass null as the firstArg argument. |
||
183 | * |
||
184 | * `Object::create_from_string("Varchar(50)", "MyField")` will return the result of |
||
185 | * `Vachar::create('MyField', '50');` |
||
186 | * |
||
187 | * Arguments are always strings, although this is a quirk of the current implementation rather |
||
188 | * than something that can be relied upon. |
||
189 | */ |
||
190 | public static function create_from_string($classSpec, $firstArg = null) { |
||
210 | |||
211 | /** |
||
212 | * Parses a class-spec, such as "Versioned('Stage','Live')", as passed to create_from_string(). |
||
213 | * Returns a 2-elemnent array, with classname and arguments |
||
214 | */ |
||
215 | public static function parse_class_spec($classSpec) { |
||
332 | |||
333 | /** |
||
334 | * Similar to {@link Object::create()}, except that classes are only overloaded if you set the $strong parameter to |
||
335 | * TRUE when using {@link Object::useCustomClass()} |
||
336 | * |
||
337 | * @param string $class the class name |
||
338 | * @param mixed $arguments,... arguments to pass to the constructor |
||
339 | * @return static |
||
340 | */ |
||
341 | public static function strong_create() { |
||
351 | |||
352 | /** |
||
353 | * This class allows you to overload classes with other classes when they are constructed using the factory method |
||
354 | * {@link Object::create()} |
||
355 | * |
||
356 | * @param string $oldClass the class to replace |
||
357 | * @param string $newClass the class to replace it with |
||
358 | * @param bool $strong allows you to enforce a certain class replacement under all circumstances. This is used in |
||
359 | * singletons and DB interaction classes |
||
360 | */ |
||
361 | public static function useCustomClass($oldClass, $newClass, $strong = false) { |
||
368 | |||
369 | /** |
||
370 | * If a class has been overloaded, get the class name it has been overloaded with - otherwise return the class name |
||
371 | * |
||
372 | * @param string $class the class to check |
||
373 | * @return string the class that would be created if you called {@link Object::create()} with the class |
||
374 | */ |
||
375 | public static function getCustomClass($class) { |
||
384 | |||
385 | /** |
||
386 | * Get the value of a static property of a class, even in that property is declared protected (but not private), |
||
387 | * without any inheritance, merging or parent lookup if it doesn't exist on the given class. |
||
388 | * |
||
389 | * @static |
||
390 | * @param $class - The class to get the static from |
||
391 | * @param $name - The property to get from the class |
||
392 | * @param null $default - The value to return if property doesn't exist on class |
||
393 | * @return any - The value of the static property $name on class $class, or $default if that property is not |
||
394 | * defined |
||
395 | */ |
||
396 | public static function static_lookup($class, $name, $default = null) { |
||
432 | |||
433 | /** |
||
434 | * @deprecated |
||
435 | */ |
||
436 | public static function get_static($class, $name, $uncached = false) { |
||
440 | |||
441 | /** |
||
442 | * @deprecated |
||
443 | */ |
||
444 | public static function set_static($class, $name, $value) { |
||
448 | |||
449 | /** |
||
450 | * @deprecated |
||
451 | */ |
||
452 | public static function uninherited_static($class, $name, $uncached = false) { |
||
456 | |||
457 | /** |
||
458 | * @deprecated |
||
459 | */ |
||
460 | public static function combined_static($class, $name, $ceiling = false) { |
||
466 | |||
467 | /** |
||
468 | * @deprecated |
||
469 | */ |
||
470 | public static function addStaticVars($class, $properties, $replace = false) { |
||
474 | |||
475 | /** |
||
476 | * @deprecated |
||
477 | */ |
||
478 | public static function add_static_var($class, $name, $value, $replace = false) { |
||
484 | |||
485 | /** |
||
486 | * Return TRUE if a class has a specified extension. |
||
487 | * This supports backwards-compatible format (static Object::has_extension($requiredExtension)) |
||
488 | * and new format ($object->has_extension($class, $requiredExtension)) |
||
489 | * @param string $classOrExtension if 1 argument supplied, the class name of the extension to |
||
490 | * check for; if 2 supplied, the class name to test |
||
491 | * @param string $requiredExtension used only if 2 arguments supplied |
||
492 | * @param boolean $strict if the extension has to match the required extension and not be a subclass |
||
493 | */ |
||
494 | public static function has_extension($classOrExtension, $requiredExtension = null, $strict = false) { |
||
517 | |||
518 | /** |
||
519 | * Add an extension to a specific class. |
||
520 | * |
||
521 | * The preferred method for adding extensions is through YAML config, |
||
522 | * since it avoids autoloading the class, and is easier to override in |
||
523 | * more specific configurations. |
||
524 | * |
||
525 | * As an alternative, extensions can be added to a specific class |
||
526 | * directly in the {@link Object::$extensions} array. |
||
527 | * See {@link SiteTree::$extensions} for examples. |
||
528 | * Keep in mind that the extension will only be applied to new |
||
529 | * instances, not existing ones (including all instances created through {@link singleton()}). |
||
530 | * |
||
531 | * @see http://doc.silverstripe.org/framework/en/trunk/reference/dataextension |
||
532 | * @param string $classOrExtension Class that should be extended - has to be a subclass of {@link Object} |
||
533 | * @param string $extension Subclass of {@link Extension} with optional parameters |
||
534 | * as a string, e.g. "Versioned" or "Translatable('Param')" |
||
535 | */ |
||
536 | public static function add_extension($classOrExtension, $extension = null) { |
||
583 | |||
584 | |||
585 | /** |
||
586 | * Remove an extension from a class. |
||
587 | * |
||
588 | * Keep in mind that this won't revert any datamodel additions |
||
589 | * of the extension at runtime, unless its used before the |
||
590 | * schema building kicks in (in your _config.php). |
||
591 | * Doesn't remove the extension from any {@link Object} |
||
592 | * instances which are already created, but will have an |
||
593 | * effect on new extensions. |
||
594 | * Clears any previously created singletons through {@link singleton()} |
||
595 | * to avoid side-effects from stale extension information. |
||
596 | * |
||
597 | * @todo Add support for removing extensions with parameters |
||
598 | * |
||
599 | * @param string $extension Classname of an {@link Extension} subclass, without parameters |
||
600 | */ |
||
601 | public static function remove_extension($extension) { |
||
632 | |||
633 | /** |
||
634 | * @param string $class |
||
635 | * @param bool $includeArgumentString Include the argument string in the return array, |
||
636 | * FALSE would return array("Versioned"), TRUE returns array("Versioned('Stage','Live')"). |
||
637 | * @return array Numeric array of either {@link DataExtension} classnames, |
||
638 | * or eval'ed classname strings with constructor arguments. |
||
639 | */ |
||
640 | public static function get_extensions($class, $includeArgumentString = false) { |
||
653 | |||
654 | // -------------------------------------------------------------------------------------------------------------- |
||
655 | |||
656 | private static $unextendable_classes = array('SS_Object', 'ViewableData', 'RequestHandler'); |
||
657 | |||
658 | static public function get_extra_config_sources($class = null) { |
||
696 | |||
697 | public function __construct() { |
||
717 | |||
718 | /** |
||
719 | * Attemps to locate and call a method dynamically added to a class at runtime if a default cannot be located |
||
720 | * |
||
721 | * You can add extra methods to a class using {@link Extensions}, {@link Object::createMethod()} or |
||
722 | * {@link Object::addWrapperMethod()} |
||
723 | * |
||
724 | * @param string $method |
||
725 | * @param array $arguments |
||
726 | * @return mixed |
||
727 | */ |
||
728 | public function __call($method, $arguments) { |
||
783 | |||
784 | // -------------------------------------------------------------------------------------------------------------- |
||
785 | |||
786 | /** |
||
787 | * Return TRUE if a method exists on this object |
||
788 | * |
||
789 | * This should be used rather than PHP's inbuild method_exists() as it takes into account methods added via |
||
790 | * extensions |
||
791 | * |
||
792 | * @param string $method |
||
793 | * @return bool |
||
794 | */ |
||
795 | public function hasMethod($method) { |
||
798 | |||
799 | /** |
||
800 | * Return the names of all the methods available on this object |
||
801 | * |
||
802 | * @param bool $custom include methods added dynamically at runtime |
||
803 | * @return array |
||
804 | */ |
||
805 | public function allMethodNames($custom = false) { |
||
817 | |||
818 | /** |
||
819 | * Adds any methods from {@link Extension} instances attached to this object. |
||
820 | * All these methods can then be called directly on the instance (transparently |
||
821 | * mapped through {@link __call()}), or called explicitly through {@link extend()}. |
||
822 | * |
||
823 | * @uses addMethodsFrom() |
||
824 | */ |
||
825 | protected function defineMethods() { |
||
841 | |||
842 | /** |
||
843 | * @param SS_Object $extension |
||
844 | * @return array |
||
845 | */ |
||
846 | protected function findMethodsFromExtension($extension) { |
||
860 | |||
861 | /** |
||
862 | * Add all the methods from an object property (which is an {@link Extension}) to this object. |
||
863 | * |
||
864 | * @param string $property the property name |
||
865 | * @param string|int $index an index to use if the property is an array |
||
866 | */ |
||
867 | protected function addMethodsFrom($property, $index = null) { |
||
895 | |||
896 | /** |
||
897 | * Add all the methods from an object property (which is an {@link Extension}) to this object. |
||
898 | * |
||
899 | * @param string $property the property name |
||
900 | * @param string|int $index an index to use if the property is an array |
||
901 | */ |
||
902 | protected function removeMethodsFrom($property, $index = null) { |
||
926 | |||
927 | /** |
||
928 | * Add a wrapper method - a method which points to another method with a different name. For example, Thumbnail(x) |
||
929 | * can be wrapped to generateThumbnail(x) |
||
930 | * |
||
931 | * @param string $method the method name to wrap |
||
932 | * @param string $wrap the method name to wrap to |
||
933 | */ |
||
934 | protected function addWrapperMethod($method, $wrap) { |
||
940 | |||
941 | /** |
||
942 | * Add an extra method using raw PHP code passed as a string |
||
943 | * |
||
944 | * @param string $method the method name |
||
945 | * @param string $code the PHP code - arguments will be in an array called $args, while you can access this object |
||
946 | * by using $obj. Note that you cannot call protected methods, as the method is actually an external |
||
947 | * function |
||
948 | */ |
||
949 | protected function createMethod($method, $code) { |
||
956 | |||
957 | // -------------------------------------------------------------------------------------------------------------- |
||
958 | |||
959 | /** |
||
960 | * @see SS_Object::get_static() |
||
961 | */ |
||
962 | public function stat($name, $uncached = false) { |
||
965 | |||
966 | /** |
||
967 | * @see SS_Object::set_static() |
||
968 | */ |
||
969 | public function set_stat($name, $value) { |
||
972 | |||
973 | /** |
||
974 | * @see SS_Object::uninherited_static() |
||
975 | */ |
||
976 | public function uninherited($name) { |
||
979 | |||
980 | // -------------------------------------------------------------------------------------------------------------- |
||
981 | |||
982 | /** |
||
983 | * Return true if this object "exists" i.e. has a sensible value |
||
984 | * |
||
985 | * This method should be overriden in subclasses to provide more context about the classes state. For example, a |
||
986 | * {@link DataObject} class could return false when it is deleted from the database |
||
987 | * |
||
988 | * @return bool |
||
989 | */ |
||
990 | public function exists() { |
||
993 | |||
994 | /** |
||
995 | * @return string this classes parent class |
||
996 | */ |
||
997 | public function parentClass() { |
||
1000 | |||
1001 | /** |
||
1002 | * Check if this class is an instance of a specific class, or has that class as one of its parents |
||
1003 | * |
||
1004 | * @param string $class |
||
1005 | * @return bool |
||
1006 | */ |
||
1007 | public function is_a($class) { |
||
1010 | |||
1011 | /** |
||
1012 | * @return string the class name |
||
1013 | */ |
||
1014 | public function __toString() { |
||
1017 | |||
1018 | // -------------------------------------------------------------------------------------------------------------- |
||
1019 | |||
1020 | /** |
||
1021 | * Calls a method if available on both this object and all applied {@link Extensions}, and then attempts to merge |
||
1022 | * all results into an array |
||
1023 | * |
||
1024 | * @param string $method the method name to call |
||
1025 | * @param mixed $argument a single argument to pass |
||
1026 | * @return mixed |
||
1027 | * @todo integrate inheritance rules |
||
1028 | */ |
||
1029 | public function invokeWithExtensions($method, $argument = null) { |
||
1035 | |||
1036 | /** |
||
1037 | * Run the given function on all of this object's extensions. Note that this method originally returned void, so if |
||
1038 | * you wanted to return results, you're hosed |
||
1039 | * |
||
1040 | * Currently returns an array, with an index resulting every time the function is called. Only adds returns if |
||
1041 | * they're not NULL, to avoid bogus results from methods just defined on the parent extension. This is important for |
||
1042 | * permission-checks through extend, as they use min() to determine if any of the returns is FALSE. As min() doesn't |
||
1043 | * do type checking, an included NULL return would fail the permission checks. |
||
1044 | * |
||
1045 | * The extension methods are defined during {@link __construct()} in {@link defineMethods()}. |
||
1046 | * |
||
1047 | * @param string $method the name of the method to call on each extension |
||
1048 | * @param mixed $a1,... up to 7 arguments to be passed to the method |
||
1049 | * @return array |
||
1050 | */ |
||
1051 | public function extend($method, &$a1=null, &$a2=null, &$a3=null, &$a4=null, &$a5=null, &$a6=null, &$a7=null) { |
||
1081 | |||
1082 | /** |
||
1083 | * Get an extension instance attached to this object by name. |
||
1084 | * |
||
1085 | * @uses hasExtension() |
||
1086 | * |
||
1087 | * @param string $extension |
||
1088 | * @return Extension |
||
1089 | */ |
||
1090 | public function getExtensionInstance($extension) { |
||
1093 | |||
1094 | /** |
||
1095 | * Returns TRUE if this object instance has a specific extension applied |
||
1096 | * in {@link $extension_instances}. Extension instances are initialized |
||
1097 | * at constructor time, meaning if you use {@link add_extension()} |
||
1098 | * afterwards, the added extension will just be added to new instances |
||
1099 | * of the extended class. Use the static method {@link has_extension()} |
||
1100 | * to check if a class (not an instance) has a specific extension. |
||
1101 | * Caution: Don't use singleton(<class>)->hasExtension() as it will |
||
1102 | * give you inconsistent results based on when the singleton was first |
||
1103 | * accessed. |
||
1104 | * |
||
1105 | * @param string $extension Classname of an {@link Extension} subclass without parameters |
||
1106 | * @return bool |
||
1107 | */ |
||
1108 | public function hasExtension($extension) { |
||
1111 | |||
1112 | /** |
||
1113 | * Get all extension instances for this specific object instance. |
||
1114 | * See {@link get_extensions()} to get all applied extension classes |
||
1115 | * for this class (not the instance). |
||
1116 | * |
||
1117 | * @return array Map of {@link DataExtension} instances, keyed by classname. |
||
1118 | */ |
||
1119 | public function getExtensionInstances() { |
||
1122 | |||
1123 | // -------------------------------------------------------------------------------------------------------------- |
||
1124 | |||
1125 | /** |
||
1126 | * Cache the results of an instance method in this object to a file, or if it is already cache return the cached |
||
1127 | * results |
||
1128 | * |
||
1129 | * @param string $method the method name to cache |
||
1130 | * @param int $lifetime the cache lifetime in seconds |
||
1131 | * @param string $ID custom cache ID to use |
||
1132 | * @param array $arguments an optional array of arguments |
||
1133 | * @return mixed the cached data |
||
1134 | */ |
||
1135 | public function cacheToFile($method, $lifetime = 3600, $ID = false, $arguments = array()) { |
||
1160 | |||
1161 | /** |
||
1162 | * Clears the cache for the given cacheToFile call |
||
1163 | */ |
||
1164 | public function clearCache($method, $ID = false, $arguments = array()) { |
||
1175 | |||
1176 | /** |
||
1177 | * Loads a cache from the filesystem if a valid on is present and within the specified lifetime |
||
1178 | * |
||
1179 | * @param string $cache the cache name |
||
1180 | * @param int $lifetime the lifetime (in seconds) of the cache before it is invalid |
||
1181 | * @return mixed |
||
1182 | */ |
||
1183 | protected function loadCache($cache, $lifetime = 3600) { |
||
1194 | |||
1195 | /** |
||
1196 | * Save a piece of cached data to the file system |
||
1197 | * |
||
1198 | * @param string $cache the cache name |
||
1199 | * @param mixed $data data to save (must be serializable) |
||
1200 | */ |
||
1201 | protected function saveCache($cache, $data) { |
||
1205 | |||
1206 | /** |
||
1207 | * Strip a file name of special characters so it is suitable for use as a cache file name |
||
1208 | * |
||
1209 | * @param string $name |
||
1210 | * @return string the name with all special cahracters replaced with underscores |
||
1211 | */ |
||
1212 | protected function sanitiseCachename($name) { |
||
1216 | |||
1217 | } |
||
1218 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.