Complex classes like Extensible 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 Extensible, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | trait Extensible |
||
| 17 | { |
||
| 18 | use CustomMethods; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * An array of extension names and parameters to be applied to this object upon construction. |
||
| 22 | * |
||
| 23 | * Example: |
||
| 24 | * <code> |
||
| 25 | * private static $extensions = array ( |
||
| 26 | * 'Hierarchy', |
||
| 27 | * "Version('Stage', 'Live')" |
||
| 28 | * ); |
||
| 29 | * </code> |
||
| 30 | * |
||
| 31 | * Use {@link Object::add_extension()} to add extensions without access to the class code, |
||
| 32 | * e.g. to extend core classes. |
||
| 33 | * |
||
| 34 | * Extensions are instantiated together with the object and stored in {@link $extension_instances}. |
||
| 35 | * |
||
| 36 | * @var array $extensions |
||
| 37 | * @config |
||
| 38 | */ |
||
| 39 | private static $extensions = null; |
||
| 40 | |||
| 41 | private static $classes_constructed = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Classes that cannot be extended |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | private static $unextendable_classes = array( |
||
| 49 | 'SilverStripe\\Core\\Object', |
||
| 50 | 'SilverStripe\\View\\ViewableData', |
||
| 51 | 'SilverStripe\\Control\\RequestHandler' |
||
| 52 | ); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Extension[] all current extension instances. |
||
| 56 | */ |
||
| 57 | protected $extension_instances = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * List of callbacks to call prior to extensions having extend called on them, |
||
| 61 | * each grouped by methodName. |
||
| 62 | * |
||
| 63 | * Top level array is method names, each of which is an array of callbacks for that name. |
||
| 64 | * |
||
| 65 | * @var callable[][] |
||
| 66 | */ |
||
| 67 | protected $beforeExtendCallbacks = array(); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * List of callbacks to call after extensions having extend called on them, |
||
| 71 | * each grouped by methodName. |
||
| 72 | * |
||
| 73 | * Top level array is method names, each of which is an array of callbacks for that name. |
||
| 74 | * |
||
| 75 | * @var callable[][] |
||
| 76 | */ |
||
| 77 | protected $afterExtendCallbacks = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Allows user code to hook into Object::extend prior to control |
||
| 81 | * being delegated to extensions. Each callback will be reset |
||
| 82 | * once called. |
||
| 83 | * |
||
| 84 | * @param string $method The name of the method to hook into |
||
| 85 | * @param callable $callback The callback to execute |
||
| 86 | */ |
||
| 87 | protected function beforeExtending($method, $callback) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Allows user code to hook into Object::extend after control |
||
| 97 | * being delegated to extensions. Each callback will be reset |
||
| 98 | * once called. |
||
| 99 | * |
||
| 100 | * @param string $method The name of the method to hook into |
||
| 101 | * @param callable $callback The callback to execute |
||
| 102 | */ |
||
| 103 | protected function afterExtending($method, $callback) |
||
| 110 | |||
| 111 | protected function constructExtensions() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Adds any methods from {@link Extension} instances attached to this object. |
||
| 144 | * All these methods can then be called directly on the instance (transparently |
||
| 145 | * mapped through {@link __call()}), or called explicitly through {@link extend()}. |
||
| 146 | * |
||
| 147 | * @uses addMethodsFrom() |
||
| 148 | */ |
||
| 149 | protected function defineExtensionMethods() |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * Add an extension to a specific class. |
||
| 161 | * |
||
| 162 | * The preferred method for adding extensions is through YAML config, |
||
| 163 | * since it avoids autoloading the class, and is easier to override in |
||
| 164 | * more specific configurations. |
||
| 165 | * |
||
| 166 | * As an alternative, extensions can be added to a specific class |
||
| 167 | * directly in the {@link Object::$extensions} array. |
||
| 168 | * See {@link SiteTree::$extensions} for examples. |
||
| 169 | * Keep in mind that the extension will only be applied to new |
||
| 170 | * instances, not existing ones (including all instances created through {@link singleton()}). |
||
| 171 | * |
||
| 172 | * @see http://doc.silverstripe.org/framework/en/trunk/reference/dataextension |
||
| 173 | * @param string $classOrExtension Class that should be extended - has to be a subclass of {@link Object} |
||
| 174 | * @param string $extension Subclass of {@link Extension} with optional parameters |
||
| 175 | * as a string, e.g. "Versioned" or "Translatable('Param')" |
||
| 176 | * @return bool Flag if the extension was added |
||
| 177 | */ |
||
| 178 | public static function add_extension($classOrExtension, $extension = null) |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * Remove an extension from a class. |
||
| 235 | * Note: This will not remove extensions from parent classes, and must be called |
||
| 236 | * directly on the class assigned the extension. |
||
| 237 | * |
||
| 238 | * Keep in mind that this won't revert any datamodel additions |
||
| 239 | * of the extension at runtime, unless its used before the |
||
| 240 | * schema building kicks in (in your _config.php). |
||
| 241 | * Doesn't remove the extension from any {@link Object} |
||
| 242 | * instances which are already created, but will have an |
||
| 243 | * effect on new extensions. |
||
| 244 | * Clears any previously created singletons through {@link singleton()} |
||
| 245 | * to avoid side-effects from stale extension information. |
||
| 246 | * |
||
| 247 | * @todo Add support for removing extensions with parameters |
||
| 248 | * |
||
| 249 | * @param string $extension class name of an {@link Extension} subclass, without parameters |
||
| 250 | */ |
||
| 251 | public static function remove_extension($extension) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $class |
||
| 289 | * @param bool $includeArgumentString Include the argument string in the return array, |
||
| 290 | * FALSE would return array("Versioned"), TRUE returns array("Versioned('Stage','Live')"). |
||
| 291 | * @return array Numeric array of either {@link DataExtension} class names, |
||
| 292 | * or eval'ed class name strings with constructor arguments. |
||
| 293 | */ |
||
| 294 | public static function get_extensions($class, $includeArgumentString = false) |
||
| 316 | |||
| 317 | |||
| 318 | public static function get_extra_config_sources($class = null) |
||
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * Return TRUE if a class has a specified extension. |
||
| 368 | * This supports backwards-compatible format (static Object::has_extension($requiredExtension)) |
||
| 369 | * and new format ($object->has_extension($class, $requiredExtension)) |
||
| 370 | * @param string $classOrExtension if 1 argument supplied, the class name of the extension to |
||
| 371 | * check for; if 2 supplied, the class name to test |
||
| 372 | * @param string $requiredExtension used only if 2 arguments supplied |
||
| 373 | * @param boolean $strict if the extension has to match the required extension and not be a subclass |
||
| 374 | * @return bool Flag if the extension exists |
||
| 375 | */ |
||
| 376 | public static function has_extension($classOrExtension, $requiredExtension = null, $strict = false) |
||
| 399 | |||
| 400 | |||
| 401 | /** |
||
| 402 | * Calls a method if available on both this object and all applied {@link Extensions}, and then attempts to merge |
||
| 403 | * all results into an array |
||
| 404 | * |
||
| 405 | * @param string $method the method name to call |
||
| 406 | * @param mixed $a1 |
||
| 407 | * @param mixed $a2 |
||
| 408 | * @param mixed $a3 |
||
| 409 | * @param mixed $a4 |
||
| 410 | * @param mixed $a5 |
||
| 411 | * @param mixed $a6 |
||
| 412 | * @param mixed $a7 |
||
| 413 | * @return array List of results with nulls filtered out |
||
| 414 | */ |
||
| 415 | public function invokeWithExtensions($method, &$a1 = null, &$a2 = null, &$a3 = null, &$a4 = null, &$a5 = null, &$a6 = null, &$a7 = null) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Run the given function on all of this object's extensions. Note that this method originally returned void, so if |
||
| 431 | * you wanted to return results, you're hosed |
||
| 432 | * |
||
| 433 | * Currently returns an array, with an index resulting every time the function is called. Only adds returns if |
||
| 434 | * they're not NULL, to avoid bogus results from methods just defined on the parent extension. This is important for |
||
| 435 | * permission-checks through extend, as they use min() to determine if any of the returns is FALSE. As min() doesn't |
||
| 436 | * do type checking, an included NULL return would fail the permission checks. |
||
| 437 | * |
||
| 438 | * The extension methods are defined during {@link __construct()} in {@link defineMethods()}. |
||
| 439 | * |
||
| 440 | * @param string $method the name of the method to call on each extension |
||
| 441 | * @param mixed $a1 |
||
| 442 | * @param mixed $a2 |
||
| 443 | * @param mixed $a3 |
||
| 444 | * @param mixed $a4 |
||
| 445 | * @param mixed $a5 |
||
| 446 | * @param mixed $a6 |
||
| 447 | * @param mixed $a7 |
||
| 448 | * @return array |
||
| 449 | */ |
||
| 450 | public function extend($method, &$a1 = null, &$a2 = null, &$a3 = null, &$a4 = null, &$a5 = null, &$a6 = null, &$a7 = null) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Get an extension instance attached to this object by name. |
||
| 492 | * |
||
| 493 | * @uses hasExtension() |
||
| 494 | * |
||
| 495 | * @param string $extension |
||
| 496 | * @return Extension |
||
| 497 | */ |
||
| 498 | public function getExtensionInstance($extension) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Returns TRUE if this object instance has a specific extension applied |
||
| 507 | * in {@link $extension_instances}. Extension instances are initialized |
||
| 508 | * at constructor time, meaning if you use {@link add_extension()} |
||
| 509 | * afterwards, the added extension will just be added to new instances |
||
| 510 | * of the extended class. Use the static method {@link has_extension()} |
||
| 511 | * to check if a class (not an instance) has a specific extension. |
||
| 512 | * Caution: Don't use singleton(<class>)->hasExtension() as it will |
||
| 513 | * give you inconsistent results based on when the singleton was first |
||
| 514 | * accessed. |
||
| 515 | * |
||
| 516 | * @param string $extension Classname of an {@link Extension} subclass without parameters |
||
| 517 | * @return bool |
||
| 518 | */ |
||
| 519 | public function hasExtension($extension) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Get all extension instances for this specific object instance. |
||
| 526 | * See {@link get_extensions()} to get all applied extension classes |
||
| 527 | * for this class (not the instance). |
||
| 528 | * |
||
| 529 | * @return array Map of {@link DataExtension} instances, keyed by classname. |
||
| 530 | */ |
||
| 531 | public function getExtensionInstances() |
||
| 535 | } |
||
| 536 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.