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 |
||
| 17 | trait Extensible { |
||
| 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('Object', 'ViewableData', 'RequestHandler'); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array all current extension instances. |
||
| 52 | */ |
||
| 53 | protected $extension_instances = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * List of callbacks to call prior to extensions having extend called on them, |
||
| 57 | * each grouped by methodName. |
||
| 58 | * |
||
| 59 | * @var array[callable] |
||
| 60 | */ |
||
| 61 | protected $beforeExtendCallbacks = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * List of callbacks to call after extensions having extend called on them, |
||
| 65 | * each grouped by methodName. |
||
| 66 | * |
||
| 67 | * @var array[callable] |
||
| 68 | */ |
||
| 69 | protected $afterExtendCallbacks = array(); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Allows user code to hook into Object::extend prior to control |
||
| 73 | * being delegated to extensions. Each callback will be reset |
||
| 74 | * once called. |
||
| 75 | * |
||
| 76 | * @param string $method The name of the method to hook into |
||
| 77 | * @param callable $callback The callback to execute |
||
| 78 | */ |
||
| 79 | protected function beforeExtending($method, $callback) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Allows user code to hook into Object::extend after control |
||
| 88 | * being delegated to extensions. Each callback will be reset |
||
| 89 | * once called. |
||
| 90 | * |
||
| 91 | * @param string $method The name of the method to hook into |
||
| 92 | * @param callable $callback The callback to execute |
||
| 93 | */ |
||
| 94 | protected function afterExtending($method, $callback) { |
||
| 100 | |||
| 101 | protected function constructExtensions() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Adds any methods from {@link Extension} instances attached to this object. |
||
| 130 | * All these methods can then be called directly on the instance (transparently |
||
| 131 | * mapped through {@link __call()}), or called explicitly through {@link extend()}. |
||
| 132 | * |
||
| 133 | * @uses addMethodsFrom() |
||
| 134 | */ |
||
| 135 | protected function defineExtensionMethods() { |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Add an extension to a specific class. |
||
| 146 | * |
||
| 147 | * The preferred method for adding extensions is through YAML config, |
||
| 148 | * since it avoids autoloading the class, and is easier to override in |
||
| 149 | * more specific configurations. |
||
| 150 | * |
||
| 151 | * As an alternative, extensions can be added to a specific class |
||
| 152 | * directly in the {@link Object::$extensions} array. |
||
| 153 | * See {@link SiteTree::$extensions} for examples. |
||
| 154 | * Keep in mind that the extension will only be applied to new |
||
| 155 | * instances, not existing ones (including all instances created through {@link singleton()}). |
||
| 156 | * |
||
| 157 | * @see http://doc.silverstripe.org/framework/en/trunk/reference/dataextension |
||
| 158 | * @param string $classOrExtension Class that should be extended - has to be a subclass of {@link Object} |
||
| 159 | * @param string $extension Subclass of {@link Extension} with optional parameters |
||
| 160 | * as a string, e.g. "Versioned" or "Translatable('Param')" |
||
| 161 | * @return bool Flag if the extension was added |
||
| 162 | */ |
||
| 163 | public static function add_extension($classOrExtension, $extension = null) { |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * Remove an extension from a class. |
||
| 215 | * |
||
| 216 | * Keep in mind that this won't revert any datamodel additions |
||
| 217 | * of the extension at runtime, unless its used before the |
||
| 218 | * schema building kicks in (in your _config.php). |
||
| 219 | * Doesn't remove the extension from any {@link Object} |
||
| 220 | * instances which are already created, but will have an |
||
| 221 | * effect on new extensions. |
||
| 222 | * Clears any previously created singletons through {@link singleton()} |
||
| 223 | * to avoid side-effects from stale extension information. |
||
| 224 | * |
||
| 225 | * @todo Add support for removing extensions with parameters |
||
| 226 | * |
||
| 227 | * @param string $extension class name of an {@link Extension} subclass, without parameters |
||
| 228 | */ |
||
| 229 | public static function remove_extension($extension) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $class |
||
| 263 | * @param bool $includeArgumentString Include the argument string in the return array, |
||
| 264 | * FALSE would return array("Versioned"), TRUE returns array("Versioned('Stage','Live')"). |
||
| 265 | * @return array Numeric array of either {@link DataExtension} class names, |
||
| 266 | * or eval'ed class name strings with constructor arguments. |
||
| 267 | */ |
||
| 268 | public static function get_extensions($class, $includeArgumentString = false) { |
||
| 287 | |||
| 288 | |||
| 289 | public static function get_extra_config_sources($class = null) { |
||
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * Return TRUE if a class has a specified extension. |
||
| 330 | * This supports backwards-compatible format (static Object::has_extension($requiredExtension)) |
||
| 331 | * and new format ($object->has_extension($class, $requiredExtension)) |
||
| 332 | * @param string $classOrExtension if 1 argument supplied, the class name of the extension to |
||
| 333 | * check for; if 2 supplied, the class name to test |
||
| 334 | * @param string $requiredExtension used only if 2 arguments supplied |
||
| 335 | * @param boolean $strict if the extension has to match the required extension and not be a subclass |
||
| 336 | * @return bool Flag if the extension exists |
||
| 337 | */ |
||
| 338 | public static function has_extension($classOrExtension, $requiredExtension = null, $strict = false) { |
||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * Calls a method if available on both this object and all applied {@link Extensions}, and then attempts to merge |
||
| 364 | * all results into an array |
||
| 365 | * |
||
| 366 | * @param string $method the method name to call |
||
| 367 | * @param mixed $argument a single argument to pass |
||
| 368 | * @return mixed |
||
| 369 | * @todo integrate inheritance rules |
||
| 370 | */ |
||
| 371 | public function invokeWithExtensions($method, $argument = null) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Run the given function on all of this object's extensions. Note that this method originally returned void, so if |
||
| 380 | * you wanted to return results, you're hosed |
||
| 381 | * |
||
| 382 | * Currently returns an array, with an index resulting every time the function is called. Only adds returns if |
||
| 383 | * they're not NULL, to avoid bogus results from methods just defined on the parent extension. This is important for |
||
| 384 | * permission-checks through extend, as they use min() to determine if any of the returns is FALSE. As min() doesn't |
||
| 385 | * do type checking, an included NULL return would fail the permission checks. |
||
| 386 | * |
||
| 387 | * The extension methods are defined during {@link __construct()} in {@link defineMethods()}. |
||
| 388 | * |
||
| 389 | * @param string $method the name of the method to call on each extension |
||
| 390 | * @param mixed $a1,... up to 7 arguments to be passed to the method |
||
| 391 | * @return array |
||
| 392 | */ |
||
| 393 | public function extend($method, &$a1=null, &$a2=null, &$a3=null, &$a4=null, &$a5=null, &$a6=null, &$a7=null) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get an extension instance attached to this object by name. |
||
| 426 | * |
||
| 427 | * @uses hasExtension() |
||
| 428 | * |
||
| 429 | * @param string $extension |
||
| 430 | * @return Extension |
||
| 431 | */ |
||
| 432 | public function getExtensionInstance($extension) { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Returns TRUE if this object instance has a specific extension applied |
||
| 438 | * in {@link $extension_instances}. Extension instances are initialized |
||
| 439 | * at constructor time, meaning if you use {@link add_extension()} |
||
| 440 | * afterwards, the added extension will just be added to new instances |
||
| 441 | * of the extended class. Use the static method {@link has_extension()} |
||
| 442 | * to check if a class (not an instance) has a specific extension. |
||
| 443 | * Caution: Don't use singleton(<class>)->hasExtension() as it will |
||
| 444 | * give you inconsistent results based on when the singleton was first |
||
| 445 | * accessed. |
||
| 446 | * |
||
| 447 | * @param string $extension Classname of an {@link Extension} subclass without parameters |
||
| 448 | * @return bool |
||
| 449 | */ |
||
| 450 | public function hasExtension($extension) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get all extension instances for this specific object instance. |
||
| 456 | * See {@link get_extensions()} to get all applied extension classes |
||
| 457 | * for this class (not the instance). |
||
| 458 | * |
||
| 459 | * @return array Map of {@link DataExtension} instances, keyed by classname. |
||
| 460 | */ |
||
| 461 | public function getExtensionInstances() { |
||
| 464 | |||
| 465 | } |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.