| Total Complexity | 42 |
| Total Lines | 331 |
| Duplicated Lines | 0 % |
| Changes | 16 | ||
| Bugs | 3 | Features | 1 |
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 |
||
| 30 | class CallableObject |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * A reference to the callable object the user has registered |
||
| 34 | * |
||
| 35 | * @var object |
||
| 36 | */ |
||
| 37 | private $xRegisteredObject = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The reflection class of the user registered callable object |
||
| 41 | * |
||
| 42 | * @var \ReflectionClass |
||
| 43 | */ |
||
| 44 | private $xReflectionClass; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * A list of methods of the user registered callable object the library must not export to javascript |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $aProtectedMethods = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * A list of methods to call before processing the request |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | private $aBeforeMethods = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A list of methods to call after processing the request |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $aAfterMethods = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The namespace the callable class was registered from |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $sNamespace = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The character to use as separator in javascript class names |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | private $sSeparator = '.'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The class constructor |
||
| 83 | * |
||
| 84 | * @param string $sCallable The callable object instance or class name |
||
| 85 | * |
||
| 86 | */ |
||
| 87 | public function __construct($sCallable) |
||
| 88 | { |
||
| 89 | $this->xReflectionClass = new \ReflectionClass($sCallable); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Return the class name of this callable object, without the namespace if any |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getClassName() |
||
| 98 | { |
||
| 99 | // Get the class name without the namespace. |
||
| 100 | return $this->xReflectionClass->getShortName(); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Return the name of this callable object |
||
| 105 | * |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | public function getName() |
||
| 109 | { |
||
| 110 | // Get the class name with the namespace. |
||
| 111 | return $this->xReflectionClass->getName(); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Return the name of the corresponding javascript class |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function getJsName() |
||
| 120 | { |
||
| 121 | return str_replace('\\', $this->sSeparator, $this->getName()); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Return the namespace of this callable object |
||
| 126 | * |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function getNamespace() |
||
| 130 | { |
||
| 131 | // The namespace of the registered class. |
||
| 132 | return $this->xReflectionClass->getNamespaceName(); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Return the namespace the callable class was registered from |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function getRootNamespace() |
||
| 141 | { |
||
| 142 | // The namespace the callable class was registered from. |
||
| 143 | return $this->sNamespace; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Set hook methods |
||
| 148 | * |
||
| 149 | * @param array $aHookMethods The array of hook methods |
||
| 150 | * @param string|array $xValue The value of the configuration option |
||
| 151 | * |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | public function setHookMethods(&$aHookMethods, $xValue) |
||
| 155 | { |
||
| 156 | foreach($xValue as $sCalledMethods => $xMethodToCall) |
||
| 157 | { |
||
| 158 | $aCalledMethods = explode(',', $sCalledMethods); |
||
| 159 | if(is_array($xMethodToCall)) |
||
| 160 | { |
||
| 161 | foreach($aCalledMethods as $sCalledMethod) |
||
| 162 | { |
||
| 163 | $aHookMethods[$sCalledMethod] = $xMethodToCall; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | elseif(is_string($xMethodToCall)) |
||
| 167 | { |
||
| 168 | foreach($aCalledMethods as $sCalledMethod) |
||
| 169 | { |
||
| 170 | $aHookMethods[$sCalledMethod] = [$xMethodToCall]; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Set configuration options / call options for each method |
||
| 178 | * |
||
| 179 | * @param string $sName The name of the configuration option |
||
| 180 | * @param string|array $xValue The value of the configuration option |
||
| 181 | * |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | public function configure($sName, $xValue) |
||
| 185 | { |
||
| 186 | switch($sName) |
||
| 187 | { |
||
| 188 | // Set the separator |
||
| 189 | case 'separator': |
||
| 190 | if($xValue == '_' || $xValue == '.') |
||
| 191 | { |
||
| 192 | $this->sSeparator = $xValue; |
||
| 193 | } |
||
| 194 | break; |
||
| 195 | // Set the namespace |
||
| 196 | case 'namespace': |
||
| 197 | if(is_string($xValue)) |
||
| 198 | { |
||
| 199 | $this->sNamespace = $xValue; |
||
| 200 | } |
||
| 201 | break; |
||
| 202 | // Set the protected methods |
||
| 203 | case 'protected': |
||
| 204 | if(is_array($xValue)) |
||
| 205 | { |
||
| 206 | $this->aProtectedMethods = array_merge($this->aProtectedMethods, $xValue); |
||
| 207 | } |
||
| 208 | elseif(is_string($xValue)) |
||
| 209 | { |
||
| 210 | $this->aProtectedMethods[] = $xValue; |
||
| 211 | } |
||
| 212 | break; |
||
| 213 | // Set the methods to call before processing the request |
||
| 214 | case '__before': |
||
| 215 | $this->setHookMethods($this->aBeforeMethods, $xValue); |
||
| 216 | break; |
||
| 217 | // Set the methods to call after processing the request |
||
| 218 | case '__after': |
||
| 219 | $this->setHookMethods($this->aAfterMethods, $xValue); |
||
| 220 | break; |
||
| 221 | default: |
||
| 222 | break; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Return a list of methods of the callable object to export to javascript |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | public function getMethods() |
||
| 232 | { |
||
| 233 | $aMethods = []; |
||
| 234 | foreach($this->xReflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $xMethod) |
||
| 235 | { |
||
| 236 | $sMethodName = $xMethod->getShortName(); |
||
| 237 | // Don't take magic __call, __construct, __destruct methods |
||
| 238 | if(strlen($sMethodName) > 2 && substr($sMethodName, 0, 2) == '__') |
||
| 239 | { |
||
| 240 | continue; |
||
| 241 | } |
||
| 242 | // Don't take excluded methods |
||
| 243 | if(in_array($sMethodName, $this->aProtectedMethods)) |
||
| 244 | { |
||
| 245 | continue; |
||
| 246 | } |
||
| 247 | $aMethods[] = $sMethodName; |
||
| 248 | } |
||
| 249 | return $aMethods; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Return the registered callable object |
||
| 254 | * |
||
| 255 | * @return null|object |
||
| 256 | */ |
||
| 257 | public function getRegisteredObject() |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Check if the specified method name is one of the methods of the registered callable object |
||
| 287 | * |
||
| 288 | * @param string $sMethod The name of the method to check |
||
| 289 | * |
||
| 290 | * @return boolean |
||
| 291 | */ |
||
| 292 | public function hasMethod($sMethod) |
||
| 293 | { |
||
| 294 | return $this->xReflectionClass->hasMethod($sMethod)/* || $this->xReflectionClass->hasMethod('__call')*/; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Call the specified method of the registered callable object using the specified array of arguments |
||
| 299 | * |
||
| 300 | * @param array $aClassMethods The methods config options |
||
| 301 | * @param string $sMethod The method called by the request |
||
| 302 | * @param Response $xResponse The response returned by the method |
||
| 303 | * |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | private function callHookMethods($aClassMethods, $sMethod) |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Call the specified method of the registered callable object using the specified array of arguments |
||
| 338 | * |
||
| 339 | * @param string $sMethod The name of the method to call |
||
| 340 | * @param array $aArgs The arguments to pass to the method |
||
| 341 | * |
||
| 342 | * @return null|Response |
||
| 343 | */ |
||
| 344 | public function call($sMethod, $aArgs) |
||
| 361 | } |
||
| 362 | } |
||
| 363 |