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) { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Adds any methods from {@link Extension} instances attached to this object. |
||
| 105 | * All these methods can then be called directly on the instance (transparently |
||
| 106 | * mapped through {@link __call()}), or called explicitly through {@link extend()}. |
||
| 107 | * |
||
| 108 | * @uses addMethodsFrom() |
||
| 109 | */ |
||
| 110 | protected function defineMethods() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Register an callback to invoke that defines extra methods |
||
| 119 | * |
||
| 120 | * @param string $name |
||
| 121 | * @param callable $callback |
||
| 122 | */ |
||
| 123 | protected function registerExtraMethodCallback($name, $callback) { |
||
| 128 | |||
| 129 | // -------------------------------------------------------------------------------------------------------------- |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Return TRUE if a method exists on this object |
||
| 133 | * |
||
| 134 | * This should be used rather than PHP's inbuild method_exists() as it takes into account methods added via |
||
| 135 | * extensions |
||
| 136 | * |
||
| 137 | * @param string $method |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | public function hasMethod($method) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get meta-data details on a named method |
||
| 146 | * |
||
| 147 | * @param array $method |
||
| 148 | * @return array List of custom method details, if defined for this method |
||
| 149 | */ |
||
| 150 | protected function getExtraMethodConfig($method) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Return the names of all the methods available on this object |
||
| 160 | * |
||
| 161 | * @param bool $custom include methods added dynamically at runtime |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function allMethodNames($custom = false) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param object $extension |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | protected function findMethodsFromExtension($extension) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Add all the methods from an object property (which is an {@link Extension}) to this object. |
||
| 198 | * |
||
| 199 | * @param string $property the property name |
||
| 200 | * @param string|int $index an index to use if the property is an array |
||
| 201 | * @throws InvalidArgumentException |
||
| 202 | */ |
||
| 203 | protected function addMethodsFrom($property, $index = null) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Add all the methods from an object property (which is an {@link Extension}) to this object. |
||
| 234 | * |
||
| 235 | * @param string $property the property name |
||
| 236 | * @param string|int $index an index to use if the property is an array |
||
| 237 | */ |
||
| 238 | protected function removeMethodsFrom($property, $index = null) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Add a wrapper method - a method which points to another method with a different name. For example, Thumbnail(x) |
||
| 265 | * can be wrapped to generateThumbnail(x) |
||
| 266 | * |
||
| 267 | * @param string $method the method name to wrap |
||
| 268 | * @param string $wrap the method name to wrap to |
||
| 269 | */ |
||
| 270 | protected function addWrapperMethod($method, $wrap) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Add an extra method using raw PHP code passed as a string |
||
| 280 | * |
||
| 281 | * @param string $method the method name |
||
| 282 | * @param string $code the PHP code - arguments will be in an array called $args, while you can access this object |
||
| 283 | * by using $obj. Note that you cannot call protected methods, as the method is actually an external |
||
| 284 | * function |
||
| 285 | */ |
||
| 286 | protected function createMethod($method, $code) { |
||
| 292 | } |
||
| 293 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break.To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.