Complex classes like CallableClass 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 CallableClass, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class CallableClass extends RequestPlugin |
||
| 33 | { |
||
| 34 | use \Jaxon\Features\Config; |
||
| 35 | use \Jaxon\Features\Template; |
||
| 36 | use \Jaxon\Features\Validator; |
||
| 37 | use \Jaxon\Features\Translator; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The callable registrar |
||
| 41 | * |
||
| 42 | * @var CallableRegistry |
||
| 43 | */ |
||
| 44 | protected $xRegistry; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The callable repository |
||
| 48 | * |
||
| 49 | * @var CallableRepository |
||
| 50 | */ |
||
| 51 | protected $xRepository; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The value of the class parameter of the incoming Jaxon request |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $sRequestedClass = ''; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The value of the method parameter of the incoming Jaxon request |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $sRequestedMethod = ''; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The class constructor |
||
| 69 | * |
||
| 70 | * @param CallableRegistry $xRegistry |
||
| 71 | * @param CallableRepository $xRepository |
||
| 72 | */ |
||
| 73 | public function __construct(CallableRegistry $xRegistry, CallableRepository $xRepository) |
||
| 74 | { |
||
| 75 | $this->xRegistry = $xRegistry; |
||
| 76 | $this->xRepository = $xRepository; |
||
| 77 | |||
| 78 | if(!empty($_GET['jxncls'])) |
||
| 79 | { |
||
| 80 | $this->sRequestedClass = trim($_GET['jxncls']); |
||
| 81 | } |
||
| 82 | if(!empty($_GET['jxnmthd'])) |
||
| 83 | { |
||
| 84 | $this->sRequestedMethod = trim($_GET['jxnmthd']); |
||
| 85 | } |
||
| 86 | if(!empty($_POST['jxncls'])) |
||
| 87 | { |
||
| 88 | $this->sRequestedClass = trim($_POST['jxncls']); |
||
| 89 | } |
||
| 90 | if(!empty($_POST['jxnmthd'])) |
||
| 91 | { |
||
| 92 | $this->sRequestedMethod = trim($_POST['jxnmthd']); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Return the name of this plugin |
||
| 98 | * |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | public function getName() |
||
| 102 | { |
||
| 103 | return Jaxon::CALLABLE_CLASS; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Return the target class and method |
||
| 108 | * |
||
| 109 | * @return Target|null |
||
| 110 | */ |
||
| 111 | public function getTarget() |
||
| 112 | { |
||
| 113 | if(!$this->sRequestedClass || !$this->sRequestedMethod) |
||
| 114 | { |
||
| 115 | return null; |
||
| 116 | } |
||
| 117 | return Target::makeClass($this->sRequestedClass, $this->sRequestedMethod); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Register a callable class |
||
| 122 | * |
||
| 123 | * @param string $sType The type of request handler being registered |
||
| 124 | * @param string $sClassName The name of the class being registered |
||
| 125 | * @param array|string $aOptions The associated options |
||
| 126 | * |
||
| 127 | * @return boolean |
||
| 128 | */ |
||
| 129 | public function register($sType, $sClassName, $aOptions) |
||
| 130 | { |
||
| 131 | $sType = trim($sType); |
||
| 132 | if($sType != $this->getName()) |
||
| 133 | { |
||
| 134 | return false; |
||
| 135 | } |
||
| 136 | |||
| 137 | if(!is_string($sClassName)) |
||
| 138 | { |
||
| 139 | throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration')); |
||
| 140 | } |
||
| 141 | if(is_string($aOptions)) |
||
| 142 | { |
||
| 143 | $aOptions = ['include' => $aOptions]; |
||
| 144 | } |
||
| 145 | if(!is_array($aOptions)) |
||
| 146 | { |
||
| 147 | throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration')); |
||
| 148 | } |
||
| 149 | |||
| 150 | $this->xRepository->addClass(trim($sClassName), $aOptions); |
||
| 151 | |||
| 152 | return true; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Create callable objects for all registered namespaces |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | protected function createCallableObjects() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Generate a hash for the registered callable objects |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function generateHash() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Generate client side javascript code for namespaces |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | private function getNamespacesScript() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Generate client side javascript code for a callable class |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | private function getCallableScript($sClassName, CallableObject $xCallableObject, array $aProtectedMethods) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Generate client side javascript code for the registered callable objects |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getScript() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Check if this plugin can process the incoming Jaxon request |
||
| 293 | * |
||
| 294 | * @return boolean |
||
| 295 | */ |
||
| 296 | public function canProcessRequest() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Process the incoming Jaxon request |
||
| 310 | * |
||
| 311 | * @return boolean |
||
| 312 | */ |
||
| 313 | public function processRequest() |
||
| 339 | } |
||
| 340 |