| Total Complexity | 43 |
| Total Lines | 349 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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 |
||
| 52 | class CallableClass extends RequestPlugin |
||
| 53 | { |
||
| 54 | /** |
||
| 55 | * @var Config |
||
| 56 | */ |
||
| 57 | protected $xConfig; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The request handler |
||
| 61 | * |
||
| 62 | * @var RequestHandler |
||
| 63 | */ |
||
| 64 | protected $xRequestHandler; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The response manager |
||
| 68 | * |
||
| 69 | * @var ResponseManager |
||
| 70 | */ |
||
| 71 | protected $xResponseManager; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The callable registry |
||
| 75 | * |
||
| 76 | * @var CallableRegistry |
||
| 77 | */ |
||
| 78 | protected $xRegistry; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The callable repository |
||
| 82 | * |
||
| 83 | * @var CallableRepository |
||
| 84 | */ |
||
| 85 | protected $xRepository; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The request data validator |
||
| 89 | * |
||
| 90 | * @var Validator |
||
| 91 | */ |
||
| 92 | protected $xValidator; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var TemplateEngine |
||
| 96 | */ |
||
| 97 | protected $xTemplateEngine; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var Translator |
||
| 101 | */ |
||
| 102 | protected $xTranslator; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * The value of the class parameter of the incoming Jaxon request |
||
| 106 | * |
||
| 107 | * @var string |
||
| 108 | */ |
||
| 109 | protected $sRequestedClass = ''; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * The value of the method parameter of the incoming Jaxon request |
||
| 113 | * |
||
| 114 | * @var string |
||
| 115 | */ |
||
| 116 | protected $sRequestedMethod = ''; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * The class constructor |
||
| 120 | * |
||
| 121 | * @param Config $xConfig |
||
| 122 | * @param RequestHandler $xRequestHandler |
||
| 123 | * @param ResponseManager $xResponseManager |
||
| 124 | * @param CallableRegistry $xRegistry The callable class registry |
||
| 125 | * @param CallableRepository $xRepository The callable object repository |
||
| 126 | * @param TemplateEngine $xTemplateEngine |
||
| 127 | * @param Translator $xTranslator |
||
| 128 | * @param Validator $xValidator |
||
| 129 | */ |
||
| 130 | public function __construct(Config $xConfig, RequestHandler $xRequestHandler, |
||
| 131 | ResponseManager $xResponseManager, CallableRegistry $xRegistry, CallableRepository $xRepository, |
||
| 132 | TemplateEngine $xTemplateEngine, Translator $xTranslator, Validator $xValidator) |
||
| 133 | { |
||
| 134 | $this->xConfig = $xConfig; |
||
| 135 | $this->xRequestHandler = $xRequestHandler; |
||
| 136 | $this->xResponseManager = $xResponseManager; |
||
| 137 | $this->xRegistry = $xRegistry; |
||
| 138 | $this->xRepository = $xRepository; |
||
| 139 | $this->xTemplateEngine = $xTemplateEngine; |
||
| 140 | $this->xTranslator = $xTranslator; |
||
| 141 | $this->xValidator = $xValidator; |
||
| 142 | |||
| 143 | if(isset($_GET['jxncls'])) |
||
| 144 | { |
||
| 145 | $this->sRequestedClass = trim($_GET['jxncls']); |
||
| 146 | } |
||
| 147 | if(isset($_GET['jxnmthd'])) |
||
| 148 | { |
||
| 149 | $this->sRequestedMethod = trim($_GET['jxnmthd']); |
||
| 150 | } |
||
| 151 | if(isset($_POST['jxncls'])) |
||
| 152 | { |
||
| 153 | $this->sRequestedClass = trim($_POST['jxncls']); |
||
| 154 | } |
||
| 155 | if(isset($_POST['jxnmthd'])) |
||
| 156 | { |
||
| 157 | $this->sRequestedMethod = trim($_POST['jxnmthd']); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @inheritDoc |
||
| 163 | */ |
||
| 164 | public function getName(): string |
||
| 165 | { |
||
| 166 | return Jaxon::CALLABLE_CLASS; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @inheritDoc |
||
| 171 | */ |
||
| 172 | public function getTarget(): ?Target |
||
| 173 | { |
||
| 174 | if(!$this->sRequestedClass || !$this->sRequestedMethod) |
||
| 175 | { |
||
| 176 | return null; |
||
| 177 | } |
||
| 178 | return Target::makeClass($this->sRequestedClass, $this->sRequestedMethod); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @inheritDoc |
||
| 183 | * @throws SetupException |
||
| 184 | */ |
||
| 185 | public function checkOptions(string $sCallable, $xOptions): array |
||
| 186 | { |
||
| 187 | if(!$this->xValidator->validateClass(trim($sCallable))) |
||
| 188 | { |
||
| 189 | throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
||
| 190 | } |
||
| 191 | if(is_string($xOptions)) |
||
| 192 | { |
||
| 193 | $xOptions = ['include' => $xOptions]; |
||
| 194 | } |
||
| 195 | elseif(!is_array($xOptions)) |
||
| 196 | { |
||
| 197 | throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
||
| 198 | } |
||
| 199 | return $xOptions; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Register a callable class |
||
| 204 | * |
||
| 205 | * @param string $sType The type of request handler being registered |
||
| 206 | * @param string $sCallable The name of the class being registered |
||
| 207 | * @param array $aOptions The associated options |
||
| 208 | * |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | public function register(string $sType, string $sCallable, array $aOptions): bool |
||
| 212 | { |
||
| 213 | $sClassName = trim($sCallable); |
||
| 214 | $this->xRepository->addClass($sClassName, $aOptions); |
||
| 215 | return true; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @inheritDoc |
||
| 220 | */ |
||
| 221 | public function getHash(): string |
||
| 222 | { |
||
| 223 | $this->xRegistry->parseCallableClasses(); |
||
| 224 | $aNamespaces = $this->xRepository->getNamespaces(); |
||
| 225 | $aClasses = $this->xRepository->getClasses(); |
||
| 226 | $sHash = ''; |
||
| 227 | |||
| 228 | foreach($aNamespaces as $sNamespace => $aOptions) |
||
| 229 | { |
||
| 230 | $sHash .= $sNamespace . $aOptions['separator']; |
||
| 231 | } |
||
| 232 | foreach($aClasses as $sClassName => $aOptions) |
||
| 233 | { |
||
| 234 | $sHash .= $sClassName . $aOptions['timestamp']; |
||
| 235 | } |
||
| 236 | |||
| 237 | return md5($sHash); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Generate client side javascript code for namespaces |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | private function getNamespacesScript(): string |
||
| 246 | { |
||
| 247 | $sCode = ''; |
||
| 248 | $sPrefix = $this->xConfig->getOption('core.prefix.class'); |
||
| 249 | $aJsClasses = []; |
||
| 250 | $aNamespaces = array_keys($this->xRepository->getNamespaces()); |
||
| 251 | foreach($aNamespaces as $sNamespace) |
||
| 252 | { |
||
| 253 | $offset = 0; |
||
| 254 | $sJsNamespace = str_replace('\\', '.', $sNamespace); |
||
| 255 | $sJsNamespace .= '.Null'; // This is a sentinel. The last token is not processed in the while loop. |
||
| 256 | while(($dotPosition = strpos($sJsNamespace, '.', $offset)) !== false) |
||
| 257 | { |
||
| 258 | $sJsClass = substr($sJsNamespace, 0, $dotPosition); |
||
| 259 | // Generate code for this object |
||
| 260 | if(!isset($aJsClasses[$sJsClass])) |
||
| 261 | { |
||
| 262 | $sCode .= "$sPrefix$sJsClass = {};\n"; |
||
| 263 | $aJsClasses[$sJsClass] = $sJsClass; |
||
| 264 | } |
||
| 265 | $offset = $dotPosition + 1; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | return $sCode; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Generate client side javascript code for a callable class |
||
| 273 | * |
||
| 274 | * @param CallableObject $xCallableObject The corresponding callable object |
||
| 275 | * @param string $sClassName The class name |
||
| 276 | * @param array $aProtectedMethods The protected methods |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | private function getCallableScript(CallableObject $xCallableObject, string $sClassName, array $aProtectedMethods): string |
||
| 281 | { |
||
| 282 | $aConfig = $xCallableObject->getOptions(); |
||
| 283 | |||
| 284 | // Convert an option to string, to be displayed in the js script template. |
||
| 285 | $fConvertOption = function($xOption) { |
||
| 286 | return is_array($xOption) ? json_encode($xOption) : $xOption; |
||
| 287 | }; |
||
| 288 | $aCommonConfig = isset($aConfig['*']) ? array_map($fConvertOption, $aConfig['*']) : []; |
||
| 289 | |||
| 290 | $_aProtectedMethods = is_subclass_of($sClassName, UserCallableClass::class) ? $aProtectedMethods : []; |
||
| 291 | $aMethods = []; |
||
| 292 | foreach($xCallableObject->getMethods() as $sMethodName) |
||
| 293 | { |
||
| 294 | // Don't export methods of the CallableClass class |
||
| 295 | if(in_array($sMethodName, $_aProtectedMethods)) |
||
| 296 | { |
||
| 297 | continue; |
||
| 298 | } |
||
| 299 | // Specific options for this method |
||
| 300 | $aMethodConfig = isset($aConfig[$sMethodName]) ? |
||
| 301 | array_map($fConvertOption, $aConfig[$sMethodName]) : []; |
||
| 302 | $aMethods[] = [ |
||
| 303 | 'name' => $sMethodName, |
||
| 304 | 'config' => array_merge($aCommonConfig, $aMethodConfig), |
||
| 305 | ]; |
||
| 306 | } |
||
| 307 | |||
| 308 | $sPrefix = $this->xConfig->getOption('core.prefix.class'); |
||
| 309 | return $this->xTemplateEngine->render('jaxon::support/object.js', [ |
||
| 310 | 'sPrefix' => $sPrefix, |
||
| 311 | 'sClass' => $xCallableObject->getJsName(), |
||
| 312 | 'aMethods' => $aMethods, |
||
| 313 | ]); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Generate client side javascript code for the registered callable objects |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public function getScript(): string |
||
| 322 | { |
||
| 323 | $this->xRegistry->registerCallableObjects(); |
||
| 324 | |||
| 325 | // The methods of the \Jaxon\CallableClass class must not be exported |
||
| 326 | $xCallableClass = new \ReflectionClass(UserCallableClass::class); |
||
| 327 | $aProtectedMethods = []; |
||
| 328 | foreach($xCallableClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $xMethod) |
||
| 329 | { |
||
| 330 | $aProtectedMethods[] = $xMethod->getName(); |
||
| 331 | } |
||
| 332 | |||
| 333 | $sCode = $this->getNamespacesScript(); |
||
| 334 | |||
| 335 | $aClassNames = $this->xRepository->getClassNames(); |
||
| 336 | // Sort the options by key length asc |
||
| 337 | uksort($aClassNames, function($name1, $name2) { |
||
| 338 | return strlen($name1) - strlen($name2); |
||
| 339 | }); |
||
| 340 | foreach($aClassNames as $sClassName) |
||
| 341 | { |
||
| 342 | $xCallableObject = $this->xRegistry->getCallableObject($sClassName); |
||
| 343 | $sCode .= $this->getCallableScript($xCallableObject, $sClassName, $aProtectedMethods); |
||
| 344 | } |
||
| 345 | |||
| 346 | return $sCode; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @inheritDoc |
||
| 351 | */ |
||
| 352 | public function canProcessRequest(): bool |
||
| 353 | { |
||
| 354 | // Check the validity of the class name |
||
| 355 | if(($this->sRequestedClass !== null && !$this->xValidator->validateClass($this->sRequestedClass)) || |
||
| 356 | ($this->sRequestedMethod !== null && !$this->xValidator->validateMethod($this->sRequestedMethod))) |
||
| 357 | { |
||
| 358 | $this->sRequestedClass = null; |
||
| 359 | $this->sRequestedMethod = null; |
||
| 360 | } |
||
| 361 | return ($this->sRequestedClass !== null && $this->sRequestedMethod !== null); |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @inheritDoc |
||
| 366 | * @throws RequestException |
||
| 367 | */ |
||
| 368 | public function processRequest(): bool |
||
| 401 | } |
||
| 402 | } |
||
| 403 |