Complex classes like 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 Object, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | abstract class 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() { |
||
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('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) { |
||
816 | |||
817 | /** |
||
818 | * Adds any methods from {@link Extension} instances attached to this object. |
||
819 | * All these methods can then be called directly on the instance (transparently |
||
820 | * mapped through {@link __call()}), or called explicitly through {@link extend()}. |
||
821 | * |
||
822 | * @uses addMethodsFrom() |
||
823 | */ |
||
824 | protected function defineMethods() { |
||
839 | |||
840 | /** |
||
841 | * @param object $extension |
||
842 | * @return array |
||
843 | */ |
||
844 | protected function findMethodsFromExtension($extension) { |
||
858 | |||
859 | /** |
||
860 | * Add all the methods from an object property (which is an {@link Extension}) to this object. |
||
861 | * |
||
862 | * @param string $property the property name |
||
863 | * @param string|int $index an index to use if the property is an array |
||
864 | */ |
||
865 | protected function addMethodsFrom($property, $index = null) { |
||
892 | |||
893 | /** |
||
894 | * Add all the methods from an object property (which is an {@link Extension}) to this object. |
||
895 | * |
||
896 | * @param string $property the property name |
||
897 | * @param string|int $index an index to use if the property is an array |
||
898 | */ |
||
899 | protected function removeMethodsFrom($property, $index = null) { |
||
923 | |||
924 | /** |
||
925 | * Add a wrapper method - a method which points to another method with a different name. For example, Thumbnail(x) |
||
926 | * can be wrapped to generateThumbnail(x) |
||
927 | * |
||
928 | * @param string $method the method name to wrap |
||
929 | * @param string $wrap the method name to wrap to |
||
930 | */ |
||
931 | protected function addWrapperMethod($method, $wrap) { |
||
937 | |||
938 | /** |
||
939 | * Add an extra method using raw PHP code passed as a string |
||
940 | * |
||
941 | * @param string $method the method name |
||
942 | * @param string $code the PHP code - arguments will be in an array called $args, while you can access this object |
||
943 | * by using $obj. Note that you cannot call protected methods, as the method is actually an external |
||
944 | * function |
||
945 | */ |
||
946 | protected function createMethod($method, $code) { |
||
951 | |||
952 | // -------------------------------------------------------------------------------------------------------------- |
||
953 | |||
954 | /** |
||
955 | * @see Object::get_static() |
||
956 | */ |
||
957 | public function stat($name, $uncached = false) { |
||
960 | |||
961 | /** |
||
962 | * @see Object::set_static() |
||
963 | */ |
||
964 | public function set_stat($name, $value) { |
||
967 | |||
968 | /** |
||
969 | * @see Object::uninherited_static() |
||
970 | */ |
||
971 | public function uninherited($name) { |
||
974 | |||
975 | // -------------------------------------------------------------------------------------------------------------- |
||
976 | |||
977 | /** |
||
978 | * Return true if this object "exists" i.e. has a sensible value |
||
979 | * |
||
980 | * This method should be overriden in subclasses to provide more context about the classes state. For example, a |
||
981 | * {@link DataObject} class could return false when it is deleted from the database |
||
982 | * |
||
983 | * @return bool |
||
984 | */ |
||
985 | public function exists() { |
||
988 | |||
989 | /** |
||
990 | * @return string this classes parent class |
||
991 | */ |
||
992 | public function parentClass() { |
||
995 | |||
996 | /** |
||
997 | * Check if this class is an instance of a specific class, or has that class as one of its parents |
||
998 | * |
||
999 | * @param string $class |
||
1000 | * @return bool |
||
1001 | */ |
||
1002 | public function is_a($class) { |
||
1005 | |||
1006 | /** |
||
1007 | * @return string the class name |
||
1008 | */ |
||
1009 | public function __toString() { |
||
1012 | |||
1013 | // -------------------------------------------------------------------------------------------------------------- |
||
1014 | |||
1015 | /** |
||
1016 | * Calls a method if available on both this object and all applied {@link Extensions}, and then attempts to merge |
||
1017 | * all results into an array |
||
1018 | * |
||
1019 | * @param string $method the method name to call |
||
1020 | * @param mixed $argument a single argument to pass |
||
1021 | * @return mixed |
||
1022 | * @todo integrate inheritance rules |
||
1023 | */ |
||
1024 | public function invokeWithExtensions($method, $argument = null) { |
||
1030 | |||
1031 | /** |
||
1032 | * Run the given function on all of this object's extensions. Note that this method originally returned void, so if |
||
1033 | * you wanted to return results, you're hosed |
||
1034 | * |
||
1035 | * Currently returns an array, with an index resulting every time the function is called. Only adds returns if |
||
1036 | * they're not NULL, to avoid bogus results from methods just defined on the parent extension. This is important for |
||
1037 | * permission-checks through extend, as they use min() to determine if any of the returns is FALSE. As min() doesn't |
||
1038 | * do type checking, an included NULL return would fail the permission checks. |
||
1039 | * |
||
1040 | * The extension methods are defined during {@link __construct()} in {@link defineMethods()}. |
||
1041 | * |
||
1042 | * @param string $method the name of the method to call on each extension |
||
1043 | * @param mixed $a1,... up to 7 arguments to be passed to the method |
||
1044 | * @return array |
||
1045 | */ |
||
1046 | public function extend($method, &$a1=null, &$a2=null, &$a3=null, &$a4=null, &$a5=null, &$a6=null, &$a7=null) { |
||
1076 | |||
1077 | /** |
||
1078 | * Get an extension instance attached to this object by name. |
||
1079 | * |
||
1080 | * @uses hasExtension() |
||
1081 | * |
||
1082 | * @param string $extension |
||
1083 | * @return Extension |
||
1084 | */ |
||
1085 | public function getExtensionInstance($extension) { |
||
1088 | |||
1089 | /** |
||
1090 | * Returns TRUE if this object instance has a specific extension applied |
||
1091 | * in {@link $extension_instances}. Extension instances are initialized |
||
1092 | * at constructor time, meaning if you use {@link add_extension()} |
||
1093 | * afterwards, the added extension will just be added to new instances |
||
1094 | * of the extended class. Use the static method {@link has_extension()} |
||
1095 | * to check if a class (not an instance) has a specific extension. |
||
1096 | * Caution: Don't use singleton(<class>)->hasExtension() as it will |
||
1097 | * give you inconsistent results based on when the singleton was first |
||
1098 | * accessed. |
||
1099 | * |
||
1100 | * @param string $extension Classname of an {@link Extension} subclass without parameters |
||
1101 | * @return bool |
||
1102 | */ |
||
1103 | public function hasExtension($extension) { |
||
1106 | |||
1107 | /** |
||
1108 | * Get all extension instances for this specific object instance. |
||
1109 | * See {@link get_extensions()} to get all applied extension classes |
||
1110 | * for this class (not the instance). |
||
1111 | * |
||
1112 | * @return array Map of {@link DataExtension} instances, keyed by classname. |
||
1113 | */ |
||
1114 | public function getExtensionInstances() { |
||
1117 | |||
1118 | // -------------------------------------------------------------------------------------------------------------- |
||
1119 | |||
1120 | /** |
||
1121 | * Cache the results of an instance method in this object to a file, or if it is already cache return the cached |
||
1122 | * results |
||
1123 | * |
||
1124 | * @param string $method the method name to cache |
||
1125 | * @param int $lifetime the cache lifetime in seconds |
||
1126 | * @param string $ID custom cache ID to use |
||
1127 | * @param array $arguments an optional array of arguments |
||
1128 | * @return mixed the cached data |
||
1129 | */ |
||
1130 | public function cacheToFile($method, $lifetime = 3600, $ID = false, $arguments = array()) { |
||
1155 | |||
1156 | /** |
||
1157 | * Clears the cache for the given cacheToFile call |
||
1158 | */ |
||
1159 | public function clearCache($method, $ID = false, $arguments = array()) { |
||
1170 | |||
1171 | /** |
||
1172 | * Loads a cache from the filesystem if a valid on is present and within the specified lifetime |
||
1173 | * |
||
1174 | * @param string $cache the cache name |
||
1175 | * @param int $lifetime the lifetime (in seconds) of the cache before it is invalid |
||
1176 | * @return mixed |
||
1177 | */ |
||
1178 | protected function loadCache($cache, $lifetime = 3600) { |
||
1189 | |||
1190 | /** |
||
1191 | * Save a piece of cached data to the file system |
||
1192 | * |
||
1193 | * @param string $cache the cache name |
||
1194 | * @param mixed $data data to save (must be serializable) |
||
1195 | */ |
||
1196 | protected function saveCache($cache, $data) { |
||
1200 | |||
1201 | /** |
||
1202 | * Strip a file name of special characters so it is suitable for use as a cache file name |
||
1203 | * |
||
1204 | * @param string $name |
||
1205 | * @return string the name with all special cahracters replaced with underscores |
||
1206 | */ |
||
1207 | protected function sanitiseCachename($name) { |
||
1211 | |||
1212 | } |
||
1213 |
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.