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 |
||
| 27 | class CallableObject extends RequestPlugin |
||
| 28 | { |
||
| 29 | use \Jaxon\Utils\Traits\Config; |
||
| 30 | use \Jaxon\Utils\Traits\Manager; |
||
| 31 | use \Jaxon\Utils\Traits\Validator; |
||
| 32 | use \Jaxon\Utils\Traits\Translator; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The registered callable objects |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $aCallableObjects; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The classpaths of the registered callable objects |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $aClassPaths; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The value of the class parameter of the incoming Jaxon request |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $sRequestedClass; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The value of the method parameter of the incoming Jaxon request |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $sRequestedMethod; |
||
| 61 | |||
| 62 | public function __construct() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Return the name of this plugin |
||
| 90 | * |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | public function getName() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Register a user defined callable object |
||
| 100 | * |
||
| 101 | * @param array $aArgs An array containing the callable object specification |
||
| 102 | * |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | public function register($aArgs) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Generate a hash for the registered callable objects |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function generateHash() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Generate client side javascript code for the registered callable objects |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getScript() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Check if this plugin can process the incoming Jaxon request |
||
| 203 | * |
||
| 204 | * @return boolean |
||
| 205 | */ |
||
| 206 | public function canProcessRequest() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Process the incoming Jaxon request |
||
| 225 | * |
||
| 226 | * @return boolean |
||
| 227 | */ |
||
| 228 | public function processRequest() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Find a callable object by class name |
||
| 257 | * |
||
| 258 | * @param string $sClassName The class name of the callable object |
||
| 259 | * |
||
| 260 | * @return object |
||
| 261 | */ |
||
| 262 | public function getCallableObject($sClassName) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Find a user registered callable object by class name |
||
| 273 | * |
||
| 274 | * @param string $sClassName The class name of the callable object |
||
| 275 | * |
||
| 276 | * @return object |
||
| 277 | */ |
||
| 278 | public function getRegisteredObject($sClassName) |
||
| 284 | } |
||
| 285 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: