Complex classes like CustomMethods 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 CustomMethods, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | trait CustomMethods { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Custom method sources |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected static $extra_methods = array(); |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Name of methods to invoke by defineMethods for this instance |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $extra_method_registers = array(); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Non-custom methods |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected static $built_in_methods = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Attempts to locate and call a method dynamically added to a class at runtime if a default cannot be located |
||
| 36 | * |
||
| 37 | * You can add extra methods to a class using {@link Extensions}, {@link Object::createMethod()} or |
||
| 38 | * {@link Object::addWrapperMethod()} |
||
| 39 | * |
||
| 40 | * @param string $method |
||
| 41 | * @param array $arguments |
||
| 42 | * @return mixed |
||
| 43 | * @throws BadMethodCallException |
||
| 44 | */ |
||
| 45 | public function __call($method, $arguments) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Adds any methods from {@link Extension} instances attached to this object. |
||
| 104 | * All these methods can then be called directly on the instance (transparently |
||
| 105 | * mapped through {@link __call()}), or called explicitly through {@link extend()}. |
||
| 106 | * |
||
| 107 | * @uses addMethodsFrom() |
||
| 108 | */ |
||
| 109 | protected function defineMethods() { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Register an callback to invoke that defines extra methods |
||
| 120 | * |
||
| 121 | * @param string $name |
||
| 122 | * @param callable $callback |
||
| 123 | */ |
||
| 124 | protected function registerExtraMethodCallback($name, $callback) { |
||
| 129 | |||
| 130 | // -------------------------------------------------------------------------------------------------------------- |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Return TRUE if a method exists on this object |
||
| 134 | * |
||
| 135 | * This should be used rather than PHP's inbuild method_exists() as it takes into account methods added via |
||
| 136 | * extensions |
||
| 137 | * |
||
| 138 | * @param string $method |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | public function hasMethod($method) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return the names of all the methods available on this object |
||
| 148 | * |
||
| 149 | * @param bool $custom include methods added dynamically at runtime |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | public function allMethodNames($custom = false) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param object $extension |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | protected function findMethodsFromExtension($extension) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Add all the methods from an object property (which is an {@link Extension}) to this object. |
||
| 186 | * |
||
| 187 | * @param string $property the property name |
||
| 188 | * @param string|int $index an index to use if the property is an array |
||
| 189 | * @throws InvalidArgumentException |
||
| 190 | */ |
||
| 191 | protected function addMethodsFrom($property, $index = null) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Add all the methods from an object property (which is an {@link Extension}) to this object. |
||
| 222 | * |
||
| 223 | * @param string $property the property name |
||
| 224 | * @param string|int $index an index to use if the property is an array |
||
| 225 | */ |
||
| 226 | protected function removeMethodsFrom($property, $index = null) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Add a wrapper method - a method which points to another method with a different name. For example, Thumbnail(x) |
||
| 253 | * can be wrapped to generateThumbnail(x) |
||
| 254 | * |
||
| 255 | * @param string $method the method name to wrap |
||
| 256 | * @param string $wrap the method name to wrap to |
||
| 257 | */ |
||
| 258 | protected function addWrapperMethod($method, $wrap) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Add an extra method using raw PHP code passed as a string |
||
| 268 | * |
||
| 269 | * @param string $method the method name |
||
| 270 | * @param string $code the PHP code - arguments will be in an array called $args, while you can access this object |
||
| 271 | * by using $obj. Note that you cannot call protected methods, as the method is actually an external |
||
| 272 | * function |
||
| 273 | */ |
||
| 274 | protected function createMethod($method, $code) { |
||
| 280 | } |
||
| 281 |
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.