Complex classes like Api 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Api, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Api |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var ApiCore |
||
| 42 | */ |
||
| 43 | private $coreMethods = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array remote methods provided by dokuwiki plugins - will be filled lazy via |
||
| 47 | * {@see dokuwiki\Remote\RemoteAPI#getPluginMethods} |
||
| 48 | */ |
||
| 49 | private $pluginMethods = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array contains custom calls to the api. Plugins can use the XML_CALL_REGISTER event. |
||
| 53 | * The data inside is 'custom.call.something' => array('plugin name', 'remote method name') |
||
| 54 | * |
||
| 55 | * The remote method name is the same as in the remote name returned by _getMethods(). |
||
| 56 | */ |
||
| 57 | private $pluginCustomCalls = null; |
||
| 58 | |||
| 59 | private $dateTransformation; |
||
| 60 | private $fileTransformation; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * constructor |
||
| 64 | */ |
||
| 65 | public function __construct() |
||
| 66 | { |
||
| 67 | $this->dateTransformation = array($this, 'dummyTransformation'); |
||
| 68 | $this->fileTransformation = array($this, 'dummyTransformation'); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get all available methods with remote access. |
||
| 73 | * |
||
| 74 | * @return array with information to all available methods |
||
| 75 | * @throws RemoteException |
||
| 76 | */ |
||
| 77 | public function getMethods() |
||
| 78 | { |
||
| 79 | return array_merge($this->getCoreMethods(), $this->getPluginMethods()); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Call a method via remote api. |
||
| 84 | * |
||
| 85 | * @param string $method name of the method to call. |
||
| 86 | * @param array $args arguments to pass to the given method |
||
| 87 | * @return mixed result of method call, must be a primitive type. |
||
| 88 | * @throws RemoteException |
||
| 89 | */ |
||
| 90 | public function call($method, $args = array()) |
||
| 91 | { |
||
| 92 | if ($args === null) { |
||
| 93 | $args = array(); |
||
| 94 | } |
||
| 95 | // Ensure we have at least one '.' in $method |
||
| 96 | list($type, $pluginName, /* $call */) = explode('.', $method . '.', 3); |
||
| 97 | if ($type === 'plugin') { |
||
| 98 | return $this->callPlugin($pluginName, $method, $args); |
||
| 99 | } |
||
| 100 | if ($this->coreMethodExist($method)) { |
||
| 101 | return $this->callCoreMethod($method, $args); |
||
| 102 | } |
||
| 103 | return $this->callCustomCallPlugin($method, $args); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Check existance of core methods |
||
| 108 | * |
||
| 109 | * @param string $name name of the method |
||
| 110 | * @return bool if method exists |
||
| 111 | */ |
||
| 112 | private function coreMethodExist($name) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Try to call custom methods provided by plugins |
||
| 120 | * |
||
| 121 | * @param string $method name of method |
||
| 122 | * @param array $args |
||
| 123 | * @return mixed |
||
| 124 | * @throws RemoteException if method not exists |
||
| 125 | */ |
||
| 126 | private function callCustomCallPlugin($method, $args) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns plugin calls that are registered via RPC_CALL_ADD action |
||
| 139 | * |
||
| 140 | * @return array with pairs of custom plugin calls |
||
| 141 | * @triggers RPC_CALL_ADD |
||
| 142 | */ |
||
| 143 | private function getCustomCallPlugins() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Call a plugin method |
||
| 155 | * |
||
| 156 | * @param string $pluginName |
||
| 157 | * @param string $method method name |
||
| 158 | * @param array $args |
||
| 159 | * @return mixed return of custom method |
||
| 160 | * @throws RemoteException |
||
| 161 | */ |
||
| 162 | private function callPlugin($pluginName, $method, $args) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Call a core method |
||
| 183 | * |
||
| 184 | * @param string $method name of method |
||
| 185 | * @param array $args |
||
| 186 | * @return mixed |
||
| 187 | * @throws RemoteException if method not exist |
||
| 188 | */ |
||
| 189 | private function callCoreMethod($method, $args) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Check if access should be checked |
||
| 209 | * |
||
| 210 | * @param array $methodMeta data about the method |
||
| 211 | * @throws AccessDeniedException |
||
| 212 | */ |
||
| 213 | private function checkAccess($methodMeta) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Check the number of parameters |
||
| 226 | * |
||
| 227 | * @param array $methodMeta data about the method |
||
| 228 | * @param array $args |
||
| 229 | * @throws RemoteException if wrong parameter count |
||
| 230 | */ |
||
| 231 | private function checkArgumentLength($methodMeta, $args) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Determine the name of the real method |
||
| 240 | * |
||
| 241 | * @param array $methodMeta list of data of the methods |
||
| 242 | * @param string $method name of method |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | private function getMethodName($methodMeta, $method) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Perform access check for current user |
||
| 256 | * |
||
| 257 | * @return bool true if the current user has access to remote api. |
||
| 258 | * @throws AccessDeniedException If remote access disabled |
||
| 259 | */ |
||
| 260 | public function hasAccess() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Requests access |
||
| 285 | * |
||
| 286 | * @return void |
||
| 287 | * @throws AccessDeniedException On denied access. |
||
| 288 | */ |
||
| 289 | public function forceAccess() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Collects all the methods of the enabled Remote Plugins |
||
| 298 | * |
||
| 299 | * @return array all plugin methods. |
||
| 300 | * @throws RemoteException if not implemented |
||
| 301 | */ |
||
| 302 | public function getPluginMethods() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Collects all the core methods |
||
| 333 | * |
||
| 334 | * @param ApiCore $apiCore this parameter is used for testing. Here you can pass a non-default RemoteAPICore |
||
| 335 | * instance. (for mocking) |
||
| 336 | * @return array all core methods. |
||
| 337 | */ |
||
| 338 | public function getCoreMethods($apiCore = null) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Transform file to xml |
||
| 352 | * |
||
| 353 | * @param mixed $data |
||
| 354 | * @return mixed |
||
| 355 | */ |
||
| 356 | public function toFile($data) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Transform date to xml |
||
| 363 | * |
||
| 364 | * @param mixed $data |
||
| 365 | * @return mixed |
||
| 366 | */ |
||
| 367 | public function toDate($data) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * A simple transformation |
||
| 374 | * |
||
| 375 | * @param mixed $data |
||
| 376 | * @return mixed |
||
| 377 | */ |
||
| 378 | public function dummyTransformation($data) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Set the transformer function |
||
| 385 | * |
||
| 386 | * @param callback $dateTransformation |
||
| 387 | */ |
||
| 388 | public function setDateTransformation($dateTransformation) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Set the transformer function |
||
| 395 | * |
||
| 396 | * @param callback $fileTransformation |
||
| 397 | */ |
||
| 398 | public function setFileTransformation($fileTransformation) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * The error handler that catches argument-related warnings |
||
| 405 | */ |
||
| 406 | public function argumentWarningHandler($errno, $errstr) |
||
| 412 | } |
||
| 413 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..