| Total Complexity | 42 |
| Total Lines | 293 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
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.
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 registry |
||
| 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 The callable class registry |
||
| 71 | * @param CallableRepository $xRepository The callable object repository |
||
| 72 | */ |
||
| 73 | public function __construct(CallableRegistry $xRegistry, CallableRepository $xRepository) |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @inheritDoc |
||
| 98 | */ |
||
| 99 | public function getName() |
||
| 100 | { |
||
| 101 | return Jaxon::CALLABLE_CLASS; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @inheritDoc |
||
| 106 | */ |
||
| 107 | public function getTarget() |
||
| 108 | { |
||
| 109 | if(!$this->sRequestedClass || !$this->sRequestedMethod) |
||
| 110 | { |
||
| 111 | return null; |
||
| 112 | } |
||
| 113 | return Target::makeClass($this->sRequestedClass, $this->sRequestedMethod); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Register a callable class |
||
| 118 | * |
||
| 119 | * @param string $sType The type of request handler being registered |
||
| 120 | * @param string $sClassName The name of the class being registered |
||
| 121 | * @param array|string $aOptions The associated options |
||
| 122 | * |
||
| 123 | * @return boolean |
||
| 124 | */ |
||
| 125 | public function register($sType, $sClassName, $aOptions) |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @inheritDoc |
||
| 153 | */ |
||
| 154 | public function getHash() |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Generate client side javascript code for namespaces |
||
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | private function getNamespacesScript() |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Generate client side javascript code for a callable class |
||
| 206 | * |
||
| 207 | * @param string $sClassName The class name |
||
| 208 | * @param CallableObject $xCallableObject The corresponding callable object |
||
| 209 | * @param array $aProtectedMethods The protected methods |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | private function getCallableScript($sClassName, CallableObject $xCallableObject, array $aProtectedMethods) |
||
| 214 | { |
||
| 215 | $aCallableOptions = $this->xRepository->getCallableOptions(); |
||
| 216 | $aConfig = $aCallableOptions[$sClassName]; |
||
| 217 | |||
| 218 | // Convert an option to string, to be displayed in the js script template. |
||
| 219 | $fConvertOption = function($xOption) { |
||
| 220 | return is_array($xOption) ? json_encode($xOption) : $xOption; |
||
| 221 | }; |
||
| 222 | $aCommonConfig = isset($aConfig['*']) ? array_map($fConvertOption, $aConfig['*']) : []; |
||
| 223 | |||
| 224 | $_aProtectedMethods = is_subclass_of($sClassName, UserCallableClass::class) ? $aProtectedMethods : []; |
||
| 225 | $aMethods = []; |
||
| 226 | foreach($xCallableObject->getMethods() as $sMethodName) |
||
| 227 | { |
||
| 228 | // Don't export methods of the CallableClass class |
||
| 229 | if(in_array($sMethodName, $_aProtectedMethods)) |
||
| 230 | { |
||
| 231 | continue; |
||
| 232 | } |
||
| 233 | // Specific options for this method |
||
| 234 | $aMethodConfig = isset($aConfig[$sMethodName]) ? |
||
| 235 | array_map($fConvertOption, $aConfig[$sMethodName]) : []; |
||
| 236 | $aMethods[] = [ |
||
| 237 | 'name' => $sMethodName, |
||
| 238 | 'config' => array_merge($aCommonConfig, $aMethodConfig), |
||
| 239 | ]; |
||
| 240 | } |
||
| 241 | |||
| 242 | $sPrefix = $this->getOption('core.prefix.class'); |
||
| 243 | return $this->render('jaxon::support/object.js', [ |
||
| 244 | 'sPrefix' => $sPrefix, |
||
| 245 | 'sClass' => $xCallableObject->getJsName(), |
||
| 246 | 'aMethods' => $aMethods, |
||
| 247 | ]); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Generate client side javascript code for the registered callable objects |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | public function getScript() |
||
| 256 | { |
||
| 257 | $this->xRegistry->createCallableObjects(); |
||
| 258 | |||
| 259 | // The methods of the \Jaxon\CallableClass class must not be exported |
||
| 260 | $xCallableClass = new \ReflectionClass(UserCallableClass::class); |
||
| 261 | $aProtectedMethods = []; |
||
| 262 | foreach($xCallableClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $xMethod) |
||
| 263 | { |
||
| 264 | $aProtectedMethods[] = $xMethod->getName(); |
||
| 265 | } |
||
| 266 | |||
| 267 | $sCode = $this->getNamespacesScript(); |
||
| 268 | |||
| 269 | $aCallableObjects = $this->xRepository->getCallableObjects(); |
||
| 270 | // Sort the options by key length asc |
||
| 271 | uksort($aCallableObjects, function($name1, $name2) { |
||
| 272 | return strlen($name1) - strlen($name2); |
||
| 273 | }); |
||
| 274 | foreach($aCallableObjects as $sClassName => $xCallableObject) |
||
| 275 | { |
||
| 276 | $sCode .= $this->getCallableScript($sClassName, $xCallableObject, $aProtectedMethods); |
||
| 277 | } |
||
| 278 | |||
| 279 | return $sCode; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @inheritDoc |
||
| 284 | */ |
||
| 285 | public function canProcessRequest() |
||
| 286 | { |
||
| 287 | // Check the validity of the class name |
||
| 288 | if(($this->sRequestedClass !== null && !$this->validateClass($this->sRequestedClass)) || |
||
| 289 | ($this->sRequestedMethod !== null && !$this->validateMethod($this->sRequestedMethod))) |
||
| 290 | { |
||
| 291 | $this->sRequestedClass = null; |
||
| 292 | $this->sRequestedMethod = null; |
||
| 293 | } |
||
| 294 | return ($this->sRequestedClass !== null && $this->sRequestedMethod !== null); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @inheritDoc |
||
| 299 | */ |
||
| 300 | public function processRequest() |
||
| 325 | } |
||
| 326 | } |
||
| 327 |