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() |
||
| 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() |
||
| 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()) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Check existance of core methods |
||
| 107 | * |
||
| 108 | * @param string $name name of the method |
||
| 109 | * @return bool if method exists |
||
| 110 | */ |
||
| 111 | private function coreMethodExist($name) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Try to call custom methods provided by plugins |
||
| 119 | * |
||
| 120 | * @param string $method name of method |
||
| 121 | * @param array $args |
||
| 122 | * @return mixed |
||
| 123 | * @throws RemoteException if method not exists |
||
| 124 | */ |
||
| 125 | private function callCustomCallPlugin($method, $args) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns plugin calls that are registered via RPC_CALL_ADD action |
||
| 137 | * |
||
| 138 | * @return array with pairs of custom plugin calls |
||
| 139 | * @triggers RPC_CALL_ADD |
||
| 140 | */ |
||
| 141 | private function getCustomCallPlugins() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Call a plugin method |
||
| 153 | * |
||
| 154 | * @param string $pluginName |
||
| 155 | * @param string $method method name |
||
| 156 | * @param array $args |
||
| 157 | * @return mixed return of custom method |
||
| 158 | * @throws RemoteException |
||
| 159 | */ |
||
| 160 | private function callPlugin($pluginName, $method, $args) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Call a core method |
||
| 181 | * |
||
| 182 | * @param string $method name of method |
||
| 183 | * @param array $args |
||
| 184 | * @return mixed |
||
| 185 | * @throws RemoteException if method not exist |
||
| 186 | */ |
||
| 187 | private function callCoreMethod($method, $args) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Check if access should be checked |
||
| 207 | * |
||
| 208 | * @param array $methodMeta data about the method |
||
| 209 | * @throws AccessDeniedException |
||
| 210 | */ |
||
| 211 | private function checkAccess($methodMeta) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Check the number of parameters |
||
| 224 | * |
||
| 225 | * @param array $methodMeta data about the method |
||
| 226 | * @param array $args |
||
| 227 | * @throws RemoteException if wrong parameter count |
||
| 228 | */ |
||
| 229 | private function checkArgumentLength($methodMeta, $args) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Determine the name of the real method |
||
| 238 | * |
||
| 239 | * @param array $methodMeta list of data of the methods |
||
| 240 | * @param string $method name of method |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | private function getMethodName($methodMeta, $method) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Perform access check for current user |
||
| 254 | * |
||
| 255 | * @return bool true if the current user has access to remote api. |
||
| 256 | * @throws AccessDeniedException If remote access disabled |
||
| 257 | */ |
||
| 258 | public function hasAccess() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Requests access |
||
| 283 | * |
||
| 284 | * @return void |
||
| 285 | * @throws AccessDeniedException On denied access. |
||
| 286 | */ |
||
| 287 | public function forceAccess() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Collects all the methods of the enabled Remote Plugins |
||
| 296 | * |
||
| 297 | * @return array all plugin methods. |
||
| 298 | * @throws RemoteException if not implemented |
||
| 299 | */ |
||
| 300 | public function getPluginMethods() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Collects all the core methods |
||
| 331 | * |
||
| 332 | * @param ApiCore $apiCore this parameter is used for testing. Here you can pass a non-default RemoteAPICore |
||
| 333 | * instance. (for mocking) |
||
| 334 | * @return array all core methods. |
||
| 335 | */ |
||
| 336 | public function getCoreMethods($apiCore = null) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Transform file to xml |
||
| 350 | * |
||
| 351 | * @param mixed $data |
||
| 352 | * @return mixed |
||
| 353 | */ |
||
| 354 | public function toFile($data) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Transform date to xml |
||
| 361 | * |
||
| 362 | * @param mixed $data |
||
| 363 | * @return mixed |
||
| 364 | */ |
||
| 365 | public function toDate($data) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * A simple transformation |
||
| 372 | * |
||
| 373 | * @param mixed $data |
||
| 374 | * @return mixed |
||
| 375 | */ |
||
| 376 | public function dummyTransformation($data) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Set the transformer function |
||
| 383 | * |
||
| 384 | * @param callback $dateTransformation |
||
| 385 | */ |
||
| 386 | public function setDateTransformation($dateTransformation) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Set the transformer function |
||
| 393 | * |
||
| 394 | * @param callback $fileTransformation |
||
| 395 | */ |
||
| 396 | public function setFileTransformation($fileTransformation) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * The error handler that catches argument-related warnings |
||
| 403 | */ |
||
| 404 | public function argumentWarningHandler($errno, $errstr) |
||
| 410 | } |
||
| 411 |
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..