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 $callableObject; |
||
| 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) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Set a user registered callable object. |
||
| 104 | * |
||
| 105 | * @return void |
||
| 106 | */ |
||
| 107 | private function makeCallable() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Return the registered callable object |
||
| 134 | * |
||
| 135 | * @return object |
||
| 136 | */ |
||
| 137 | public function getRegisteredObject() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return the class name of this callable object, without the namespace if any |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getClassName() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Return the name of this callable object |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | public function getName() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Return the javascript name of this callable object |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function getJsName() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Return the namespace of this callable object |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function getNamespace() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Return the class path of this callable object |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function getPath() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Return a list of methods of the callable object to export to javascript |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function getMethods() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Set configuration options / call options for each method |
||
| 234 | * |
||
| 235 | * @param string $sMethod The name of the method |
||
| 236 | * @param string $sName The name of the configuration option |
||
| 237 | * @param string $sValue The value of the configuration option |
||
| 238 | * |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | public function configure($sMethod, $sName, $sValue) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Generate client side javascript code for calls to all methods exposed by this callable object |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function getScript() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Check if the specified class name matches the class name of the registered callable object |
||
| 324 | * |
||
| 325 | * @return boolean |
||
| 326 | */ |
||
| 327 | public function isClass($sClass) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Check if the specified method name is one of the methods of the registered callable object |
||
| 334 | * |
||
| 335 | * @param string $sMethod The name of the method to check |
||
| 336 | * |
||
| 337 | * @return boolean |
||
| 338 | */ |
||
| 339 | public function hasMethod($sMethod) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Call the specified method of the registered callable object using the specified array of arguments |
||
| 346 | * |
||
| 347 | * @param string $sMethod The name of the method to call |
||
| 348 | * @param array $aArgs The arguments to pass to the method |
||
| 349 | * |
||
| 350 | * @return void |
||
| 351 | */ |
||
| 352 | public function call($sMethod, $aArgs) |
||
| 366 | } |
||
| 367 |