| Total Complexity | 46 |
| Total Lines | 376 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 40 | class CallableObject |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * The DI container |
||
| 44 | * |
||
| 45 | * @var Container |
||
| 46 | */ |
||
| 47 | protected $di; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The reflection class of the user registered callable object |
||
| 51 | * |
||
| 52 | * @var ReflectionClass |
||
| 53 | */ |
||
| 54 | private $xReflectionClass; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * A list of methods of the user registered callable object the library must not export to javascript |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | private $aProtectedMethods = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * A list of methods to call before processing the request |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $aBeforeMethods = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * A list of methods to call after processing the request |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | private $aAfterMethods = []; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The callable object options |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $aOptions = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The namespace the callable class was registered from |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | private $sNamespace = ''; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The character to use as separator in javascript class names |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | private $sSeparator = '.'; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The class constructor |
||
| 100 | * |
||
| 101 | * @param Container $di |
||
| 102 | * @param ReflectionClass $xReflectionClass The reflection class |
||
| 103 | * |
||
| 104 | */ |
||
| 105 | public function __construct(Container $di, ReflectionClass $xReflectionClass) |
||
| 106 | { |
||
| 107 | $this->di = $di; |
||
| 108 | $this->xReflectionClass = $xReflectionClass; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set callable object options |
||
| 113 | * |
||
| 114 | * @param array $aOptions |
||
| 115 | * |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | public function setOptions(array $aOptions) |
||
| 119 | { |
||
| 120 | $this->aOptions = $aOptions; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get callable object options |
||
| 125 | * |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | public function getOptions(): array |
||
| 129 | { |
||
| 130 | return $this->aOptions; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get the reflection class |
||
| 135 | * |
||
| 136 | * @return ReflectionClass |
||
| 137 | */ |
||
| 138 | public function getReflectionClass(): ReflectionClass |
||
| 139 | { |
||
| 140 | return $this->xReflectionClass; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get the class name of this callable object, without the namespace if any |
||
| 145 | * |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function getClassName(): string |
||
| 149 | { |
||
| 150 | // Get the class name without the namespace. |
||
| 151 | return $this->xReflectionClass->getShortName(); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get the name of this callable object |
||
| 156 | * |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function getName(): string |
||
| 160 | { |
||
| 161 | // Get the class name with the namespace. |
||
| 162 | return $this->xReflectionClass->getName(); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get the name of the corresponding javascript class |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function getJsName(): string |
||
| 171 | { |
||
| 172 | return str_replace('\\', $this->sSeparator, $this->getName()); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get the namespace of this callable object |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getNamespace(): string |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get the namespace the callable class was registered from |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function getRootNamespace(): string |
||
| 192 | { |
||
| 193 | // The namespace the callable class was registered from. |
||
| 194 | return $this->sNamespace; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Set hook methods |
||
| 199 | * |
||
| 200 | * @param array $aHookMethods The array of hook methods |
||
| 201 | * @param string|array $xValue The value of the configuration option |
||
| 202 | * |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | public function setHookMethods(array &$aHookMethods, $xValue) |
||
| 206 | { |
||
| 207 | foreach($xValue as $sCalledMethod => $xMethodToCall) |
||
| 208 | { |
||
| 209 | if(is_array($xMethodToCall)) |
||
| 210 | { |
||
| 211 | $aHookMethods[$sCalledMethod] = $xMethodToCall; |
||
| 212 | } |
||
| 213 | elseif(is_string($xMethodToCall)) |
||
| 214 | { |
||
| 215 | $aHookMethods[$sCalledMethod] = [$xMethodToCall]; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set configuration options / call options for each method |
||
| 222 | * |
||
| 223 | * @param string $sName The name of the configuration option |
||
| 224 | * @param string|array $xValue The value of the configuration option |
||
| 225 | * |
||
| 226 | * @return void |
||
| 227 | */ |
||
| 228 | public function configure(string $sName, $xValue) |
||
| 229 | { |
||
| 230 | switch($sName) |
||
| 231 | { |
||
| 232 | // Set the separator |
||
| 233 | case 'separator': |
||
| 234 | if($xValue == '_' || $xValue == '.') |
||
| 235 | { |
||
| 236 | $this->sSeparator = $xValue; |
||
| 237 | } |
||
| 238 | break; |
||
| 239 | // Set the namespace |
||
| 240 | case 'namespace': |
||
| 241 | if(is_string($xValue)) |
||
| 242 | { |
||
| 243 | $this->sNamespace = $xValue; |
||
| 244 | } |
||
| 245 | break; |
||
| 246 | // Set the protected methods |
||
| 247 | case 'protected': |
||
| 248 | if(is_array($xValue)) |
||
| 249 | { |
||
| 250 | $this->aProtectedMethods = array_merge($this->aProtectedMethods, $xValue); |
||
| 251 | } |
||
| 252 | elseif(is_string($xValue)) |
||
| 253 | { |
||
| 254 | $this->aProtectedMethods[] = $xValue; |
||
| 255 | } |
||
| 256 | break; |
||
| 257 | // Set the methods to call before processing the request |
||
| 258 | case '__before': |
||
| 259 | $this->setHookMethods($this->aBeforeMethods, $xValue); |
||
| 260 | break; |
||
| 261 | // Set the methods to call after processing the request |
||
| 262 | case '__after': |
||
| 263 | $this->setHookMethods($this->aAfterMethods, $xValue); |
||
| 264 | break; |
||
| 265 | default: |
||
| 266 | break; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Return a list of methods of the callable object |
||
| 272 | * |
||
| 273 | * @param array $aProtectedMethods The protected methods |
||
| 274 | * |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | private function _getMethods(array $aProtectedMethods): array |
||
| 278 | { |
||
| 279 | $aMethods = []; |
||
| 280 | foreach($this->xReflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $xMethod) |
||
| 281 | { |
||
| 282 | $sMethodName = $xMethod->getShortName(); |
||
| 283 | // Don't take magic __call, __construct, __destruct methods |
||
| 284 | if(strlen($sMethodName) > 2 && substr($sMethodName, 0, 2) == '__') |
||
| 285 | { |
||
| 286 | continue; |
||
| 287 | } |
||
| 288 | // Don't take protected methods |
||
| 289 | if(in_array($sMethodName, $aProtectedMethods) || in_array($sMethodName, $this->aProtectedMethods)) |
||
| 290 | { |
||
| 291 | continue; |
||
| 292 | } |
||
| 293 | $aMethods[] = $sMethodName; |
||
| 294 | } |
||
| 295 | return $aMethods; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Return a list of methods of the callable object to export to javascript |
||
| 300 | * |
||
| 301 | * @param array $aProtectedMethods The protected methods |
||
| 302 | * |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | public function getMethods(array $aProtectedMethods): array |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get the registered callable object |
||
| 331 | * |
||
| 332 | * @return null|object |
||
| 333 | */ |
||
| 334 | public function getRegisteredObject() |
||
| 335 | { |
||
| 336 | return $this->di->get($this->xReflectionClass->getName()); |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Check if the specified method name is one of the methods of the registered callable object |
||
| 341 | * |
||
| 342 | * @param string $sMethod The name of the method to check |
||
| 343 | * |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | public function hasMethod(string $sMethod): bool |
||
| 347 | { |
||
| 348 | return $this->xReflectionClass->hasMethod($sMethod); |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Call the specified method of the registered callable object using the specified array of arguments |
||
| 353 | * |
||
| 354 | * @param array $aClassMethods The method config options |
||
| 355 | * @param string $sMethod The method called by the request |
||
| 356 | * |
||
| 357 | * @return void |
||
| 358 | * @throws ReflectionException |
||
| 359 | */ |
||
| 360 | private function callHookMethods(array $aClassMethods, string $sMethod) |
||
| 361 | { |
||
| 362 | $aMethods = []; |
||
| 363 | if(isset($aClassMethods[$sMethod])) |
||
| 364 | { |
||
| 365 | $aMethods = $aClassMethods[$sMethod]; |
||
| 366 | } |
||
| 367 | elseif(isset($aClassMethods['*'])) |
||
| 368 | { |
||
| 369 | $aMethods = $aClassMethods['*']; |
||
| 370 | } |
||
| 371 | foreach($aMethods as $xKey => $xValue) |
||
| 372 | { |
||
| 373 | $sMethodName = $xValue; |
||
| 374 | $aMethodArgs = []; |
||
| 375 | if(is_string($xKey)) |
||
| 376 | { |
||
| 377 | $sMethodName = $xKey; |
||
| 378 | $aMethodArgs = is_array($xValue) ? $xValue : [$xValue]; |
||
| 379 | } |
||
| 380 | if(!$this->xReflectionClass->hasMethod($sMethodName)) |
||
| 381 | { |
||
| 382 | continue; |
||
| 383 | } |
||
| 384 | $reflectionMethod = $this->xReflectionClass->getMethod($sMethodName); |
||
| 385 | $reflectionMethod->setAccessible(true); // Make it possible to call protected methods |
||
| 386 | $reflectionMethod->invokeArgs($this->getRegisteredObject(), $aMethodArgs); |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Call the specified method of the registered callable object using the specified array of arguments |
||
| 392 | * |
||
| 393 | * @param string $sMethod The name of the method to call |
||
| 394 | * @param array $aArgs The arguments to pass to the method |
||
| 395 | * |
||
| 396 | * @return null|Response |
||
| 397 | * @throws ReflectionException |
||
| 398 | */ |
||
| 399 | public function call(string $sMethod, array $aArgs): ?Response |
||
| 416 | } |
||
| 417 | } |
||
| 418 |