Complex classes like CallableObject 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 CallableObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class CallableObject |
||
| 30 | { |
||
| 31 | use \Jaxon\Utils\Traits\Config; |
||
| 32 | use \Jaxon\Utils\Traits\Manager; |
||
| 33 | use \Jaxon\Utils\Traits\Template; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * A reference to the callable object the user has registered |
||
| 37 | * |
||
| 38 | * @var object |
||
| 39 | */ |
||
| 40 | private $registeredObject = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The reflection class of the user registered callable object |
||
| 44 | * |
||
| 45 | * @var \ReflectionClass |
||
| 46 | */ |
||
| 47 | private $reflectionClass; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * A list of methods of the user registered callable object the library must not export to javascript |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $aProtectedMethods = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The namespace where the callable object class is defined |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private $namespace = ''; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The path to the directory where the callable object class is defined, starting from the namespace root |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $classpath = ''; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The character to use as separator in javascript class names |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | private $separator = '.'; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * An associative array that will contain configuration options for zero or more of the objects methods |
||
| 79 | * |
||
| 80 | * These configuration options will define the call options for each request. |
||
| 81 | * The call options will be passed to the client browser when the function stubs are generated. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private $aConfiguration = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The class constructor |
||
| 89 | * |
||
| 90 | * @param string $sCallable The callable object instance or class name |
||
| 91 | * |
||
| 92 | */ |
||
| 93 | public function __construct($sCallable) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Return the registered callable object |
||
| 100 | * |
||
| 101 | * @return object |
||
| 102 | */ |
||
| 103 | public function getRegisteredObject() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Return the class name of this callable object, without the namespace if any |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function getClassName() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Return the name of this callable object |
||
| 141 | * |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function getName() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Return the javascript name of this callable object |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getJsName() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Return the namespace of this callable object |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getNamespace() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Return the class path of this callable object |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function getPath() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Return a list of methods of the callable object to export to javascript |
||
| 190 | * |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function getMethods() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Set configuration options / call options for each method |
||
| 216 | * |
||
| 217 | * @param string $sMethod The name of the method |
||
| 218 | * @param string $sName The name of the configuration option |
||
| 219 | * @param string $sValue The value of the configuration option |
||
| 220 | * |
||
| 221 | * @return void |
||
| 222 | */ |
||
| 223 | public function configure($sMethod, $sName, $sValue) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Generate client side javascript code for calls to all methods exposed by this callable object |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getScript() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Check if the specified class name matches the class name of the registered callable object |
||
| 306 | * |
||
| 307 | * @return boolean |
||
| 308 | */ |
||
| 309 | public function isClass($sClass) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Check if the specified method name is one of the methods of the registered callable object |
||
| 316 | * |
||
| 317 | * @param string $sMethod The name of the method to check |
||
| 318 | * |
||
| 319 | * @return boolean |
||
| 320 | */ |
||
| 321 | public function hasMethod($sMethod) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Call the specified method of the registered callable object using the specified array of arguments |
||
| 328 | * |
||
| 329 | * @param string $sMethod The name of the method to call |
||
| 330 | * @param array $aArgs The arguments to pass to the method |
||
| 331 | * |
||
| 332 | * @return void |
||
| 333 | */ |
||
| 334 | public function call($sMethod, $aArgs) |
||
| 348 | } |
||
| 349 |