| Total Complexity | 46 |
| Total Lines | 439 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
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.
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 |
||
| 52 | class CallableObject |
||
| 53 | { |
||
| 54 | /** |
||
| 55 | * The DI container |
||
| 56 | * |
||
| 57 | * @var Container |
||
| 58 | */ |
||
| 59 | protected $di; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The reflection class of the user registered callable object |
||
| 63 | * |
||
| 64 | * @var ReflectionClass |
||
| 65 | */ |
||
| 66 | private $xReflectionClass; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * A list of methods of the user registered callable object the library must not export to javascript |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $aProtectedMethods = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * A list of methods to call before processing the request |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | private $aBeforeMethods = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * A list of methods to call after processing the request |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | private $aAfterMethods = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The callable object options |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | private $aOptions = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The character to use as separator in javascript class names |
||
| 98 | * |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | private $sSeparator = '.'; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Check if the js code for this object must be generated |
||
| 105 | * |
||
| 106 | * @var bool |
||
| 107 | */ |
||
| 108 | private $bExcluded = false; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The user registered callable object |
||
| 112 | * |
||
| 113 | * @var object |
||
| 114 | */ |
||
| 115 | private $xRegisteredObject = null; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The attributes to inject in the user registered callable object |
||
| 119 | * |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | private $aAttributes = []; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The target of the Jaxon call |
||
| 126 | * |
||
| 127 | * @var Target |
||
| 128 | */ |
||
| 129 | private $xTarget; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * The class constructor |
||
| 133 | * |
||
| 134 | * @param Container $di |
||
| 135 | * @param ReflectionClass $xReflectionClass The reflection class |
||
| 136 | * |
||
| 137 | */ |
||
| 138 | public function __construct(Container $di, ReflectionClass $xReflectionClass) |
||
| 139 | { |
||
| 140 | $this->di = $di; |
||
| 141 | $this->xReflectionClass = $xReflectionClass; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get callable object options |
||
| 146 | * |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | public function getOptions(): array |
||
| 150 | { |
||
| 151 | return $this->aOptions; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Check if the js code for this object must be generated |
||
| 156 | * |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | public function excluded(): bool |
||
| 160 | { |
||
| 161 | return $this->bExcluded; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Set callable object options |
||
| 166 | * |
||
| 167 | * @param array $aOptions |
||
| 168 | * |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | public function setOptions(array $aOptions) |
||
| 172 | { |
||
| 173 | $this->aOptions = $aOptions; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get the name of the corresponding javascript class |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function getJsName(): string |
||
| 182 | { |
||
| 183 | return str_replace('\\', $this->sSeparator, $this->xReflectionClass->getName()); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set hook methods |
||
| 188 | * |
||
| 189 | * @param array $aHookMethods The array of hook methods |
||
| 190 | * @param string|array $xValue The value of the configuration option |
||
| 191 | * |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | private function setHookMethods(array &$aHookMethods, $xValue) |
||
| 195 | { |
||
| 196 | foreach($xValue as $sCalledMethod => $xMethodToCall) |
||
| 197 | { |
||
| 198 | if(is_array($xMethodToCall)) |
||
| 199 | { |
||
| 200 | $aHookMethods[$sCalledMethod] = $xMethodToCall; |
||
| 201 | } |
||
| 202 | elseif(is_string($xMethodToCall)) |
||
| 203 | { |
||
| 204 | $aHookMethods[$sCalledMethod] = [$xMethodToCall]; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Set configuration options / call options for each method |
||
| 211 | * |
||
| 212 | * @param string $sName The name of the configuration option |
||
| 213 | * @param string|array $xValue The value of the configuration option |
||
| 214 | * |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | public function configure(string $sName, $xValue) |
||
| 218 | { |
||
| 219 | switch($sName) |
||
| 220 | { |
||
| 221 | // Set the separator |
||
| 222 | case 'separator': |
||
| 223 | if($xValue === '_' || $xValue === '.') |
||
| 224 | { |
||
| 225 | $this->sSeparator = $xValue; |
||
| 226 | } |
||
| 227 | break; |
||
| 228 | // Set the protected methods |
||
| 229 | case 'protected': |
||
| 230 | if(is_array($xValue)) |
||
| 231 | { |
||
| 232 | $this->aProtectedMethods = array_merge($this->aProtectedMethods, $xValue); |
||
| 233 | } |
||
| 234 | break; |
||
| 235 | // Set the methods to call before processing the request |
||
| 236 | case '__before': |
||
| 237 | $this->setHookMethods($this->aBeforeMethods, $xValue); |
||
| 238 | break; |
||
| 239 | // Set the methods to call after processing the request |
||
| 240 | case '__after': |
||
| 241 | $this->setHookMethods($this->aAfterMethods, $xValue); |
||
| 242 | break; |
||
| 243 | // Set the attributes to inject in the callable object |
||
| 244 | case '__di': |
||
| 245 | $this->aAttributes = array_merge($this->aAttributes, $xValue); |
||
| 246 | break; |
||
| 247 | case 'excluded': |
||
| 248 | $this->bExcluded = (bool)$xValue; |
||
| 249 | break; |
||
| 250 | default: |
||
| 251 | break; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get the public and protected attributes of the callable object |
||
| 257 | * |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public function getProperties(): array |
||
| 261 | { |
||
| 262 | return array_map(function($xProperty) { |
||
| 263 | return $xProperty->getName(); |
||
| 264 | }, $this->xReflectionClass->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED)); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get the public methods of the callable object |
||
| 269 | * |
||
| 270 | * @param array $aProtectedMethods The protected methods |
||
| 271 | * |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | public function getPublicMethods(array $aProtectedMethods = []): array |
||
| 275 | { |
||
| 276 | $aMethods = array_map(function($xMethod) { |
||
| 277 | return $xMethod->getShortName(); |
||
| 278 | }, $this->xReflectionClass->getMethods(ReflectionMethod::IS_PUBLIC)); |
||
| 279 | |||
| 280 | return array_filter($aMethods, function($sMethodName) use($aProtectedMethods) { |
||
| 281 | // Don't take magic __call, __construct, __destruct methods |
||
| 282 | // Don't take protected methods |
||
| 283 | return substr($sMethodName, 0, 2) !== '__' && |
||
| 284 | !in_array($sMethodName, $aProtectedMethods) && |
||
| 285 | !in_array($sMethodName, $this->aProtectedMethods); |
||
| 286 | }); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param array $aCommonOptions |
||
| 291 | * @param array $aMethodOptions |
||
| 292 | * @param string $sMethodName |
||
| 293 | * |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | private function getOptionValue(array $aCommonOptions, array $aMethodOptions, string $sOptionName) |
||
| 297 | { |
||
| 298 | if(!isset($aCommonOptions[$sOptionName])) |
||
| 299 | { |
||
| 300 | return $aMethodOptions[$sOptionName]; |
||
| 301 | } |
||
| 302 | if(!isset($aMethodOptions[$sOptionName])) |
||
| 303 | { |
||
| 304 | return $aCommonOptions[$sOptionName]; |
||
| 305 | } |
||
| 306 | // If both are not arrays, return the latest. |
||
| 307 | if(!is_array($aCommonOptions[$sOptionName]) && !is_array($aMethodOptions[$sOptionName])) |
||
| 308 | { |
||
| 309 | return $aMethodOptions[$sOptionName]; |
||
| 310 | } |
||
| 311 | |||
| 312 | // Merge the options. |
||
| 313 | $_aCommonOptions = is_array($aCommonOptions[$sOptionName]) ? |
||
| 314 | $aCommonOptions[$sOptionName] : [$aCommonOptions[$sOptionName]]; |
||
| 315 | $_aMethodOptions = is_array($aMethodOptions[$sOptionName]) ? |
||
| 316 | $aMethodOptions[$sOptionName] : [$aMethodOptions[$sOptionName]]; |
||
| 317 | return array_merge($_aCommonOptions, $_aMethodOptions); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $sMethodName |
||
| 322 | * |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | private function getMethodOptions(string $sMethodName): array |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Return a list of methods of the callable object to export to javascript |
||
| 340 | * |
||
| 341 | * @param array $aProtectedMethods The protected methods |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | public function getCallableMethods(array $aProtectedMethods): array |
||
| 346 | { |
||
| 347 | // Convert an option to a string to be displayed in the js script template. |
||
| 348 | $fConvertOption = function($xOption) { |
||
| 349 | return is_array($xOption) ? json_encode($xOption) : $xOption; |
||
| 350 | }; |
||
| 351 | |||
| 352 | return array_map(function($sMethodName) use($fConvertOption) { |
||
| 353 | return [ |
||
| 354 | 'name' => $sMethodName, |
||
| 355 | 'config' => array_map($fConvertOption, $this->getMethodOptions($sMethodName)), |
||
| 356 | ]; |
||
| 357 | }, $this->getPublicMethods($aProtectedMethods)); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get the registered callable object |
||
| 362 | * |
||
| 363 | * @return null|object |
||
| 364 | */ |
||
| 365 | public function getRegisteredObject() |
||
| 366 | { |
||
| 367 | return $this->di->g($this->xReflectionClass->getName()); |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Check if the specified method name is one of the methods of the registered callable object |
||
| 372 | * |
||
| 373 | * @param string $sMethod The name of the method to check |
||
| 374 | * |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | public function hasMethod(string $sMethod): bool |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Call the specified method of the registered callable object using the specified array of arguments |
||
| 384 | * |
||
| 385 | * @param string $sMethod The method name |
||
| 386 | * @param array $aArgs The method arguments |
||
| 387 | * @param bool $bAccessible If false, only calls to public method are allowed |
||
| 388 | * |
||
| 389 | * @return mixed |
||
| 390 | * @throws ReflectionException |
||
| 391 | */ |
||
| 392 | private function callMethod(string $sMethod, array $aArgs, bool $bAccessible) |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Call the specified method of the registered callable object using the specified array of arguments |
||
| 401 | * |
||
| 402 | * @param array $aHookMethods The method config options |
||
| 403 | * |
||
| 404 | * @return void |
||
| 405 | * @throws ReflectionException |
||
| 406 | */ |
||
| 407 | private function callHookMethods(array $aHookMethods) |
||
| 408 | { |
||
| 409 | $sMethod = $this->xTarget->getMethodName(); |
||
| 410 | $aArgs = $this->xTarget->getMethodArgs(); |
||
| 411 | // The hooks defined at method level override those defined at class level. |
||
| 412 | // $aMethods = $aHookMethods[$sMethod] ?? $aHookMethods['*'] ?? []; |
||
| 413 | // The hooks defined at method level are merged with those defined at class level. |
||
| 414 | $aMethods = array_merge($aHookMethods['*'] ?? [], $aHookMethods[$sMethod] ?? []); |
||
| 415 | foreach($aMethods as $xKey => $xValue) |
||
| 416 | { |
||
| 417 | $sHookName = $xValue; |
||
| 418 | $aHookArgs = []; |
||
| 419 | if(is_string($xKey)) |
||
| 420 | { |
||
| 421 | $sHookName = $xKey; |
||
| 422 | $aHookArgs = is_array($xValue) ? $xValue : [$xValue]; |
||
| 423 | // The name and arguments of the method can be passed to the hooks. |
||
| 424 | /*$aHookArgs = array_map(function($xHookArg) use($sMethod, $aArgs) { |
||
| 425 | switch($xHookArg) |
||
| 426 | { |
||
| 427 | case '__method__': |
||
| 428 | return $sMethod; |
||
| 429 | case '__args__': |
||
| 430 | return $aArgs; |
||
| 431 | default: |
||
| 432 | return $xHookArg; |
||
| 433 | } |
||
| 434 | }, $aHookArgs);*/ |
||
| 435 | } |
||
| 436 | $this->callMethod($sHookName, $aHookArgs, true); |
||
| 437 | } |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Call the specified method of the registered callable object using the specified array of arguments |
||
| 442 | * |
||
| 443 | * @param Target $xTarget The target of the Jaxon call |
||
| 444 | * |
||
| 445 | * @return null|ResponseInterface |
||
| 446 | * @throws ReflectionException |
||
| 447 | * @throws SetupException |
||
| 448 | */ |
||
| 449 | public function call(Target $xTarget): ?ResponseInterface |
||
| 491 | } |
||
| 492 | } |
||
| 493 |