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) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Call a core method |
||
| 178 | * |
||
| 179 | * @param string $method name of method |
||
| 180 | * @param array $args |
||
| 181 | * @return mixed |
||
| 182 | * @throws RemoteException if method not exist |
||
| 183 | */ |
||
| 184 | private function callCoreMethod($method, $args) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Check if access should be checked |
||
| 201 | * |
||
| 202 | * @param array $methodMeta data about the method |
||
| 203 | * @throws AccessDeniedException |
||
| 204 | */ |
||
| 205 | private function checkAccess($methodMeta) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Check the number of parameters |
||
| 218 | * |
||
| 219 | * @param array $methodMeta data about the method |
||
| 220 | * @param array $args |
||
| 221 | * @throws RemoteException if wrong parameter count |
||
| 222 | */ |
||
| 223 | private function checkArgumentLength($methodMeta, $args) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Determine the name of the real method |
||
| 232 | * |
||
| 233 | * @param array $methodMeta list of data of the methods |
||
| 234 | * @param string $method name of method |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | private function getMethodName($methodMeta, $method) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Perform access check for current user |
||
| 248 | * |
||
| 249 | * @return bool true if the current user has access to remote api. |
||
| 250 | * @throws AccessDeniedException If remote access disabled |
||
| 251 | */ |
||
| 252 | public function hasAccess() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Requests access |
||
| 277 | * |
||
| 278 | * @return void |
||
| 279 | * @throws AccessDeniedException On denied access. |
||
| 280 | */ |
||
| 281 | public function forceAccess() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Collects all the methods of the enabled Remote Plugins |
||
| 290 | * |
||
| 291 | * @return array all plugin methods. |
||
| 292 | * @throws RemoteException if not implemented |
||
| 293 | */ |
||
| 294 | public function getPluginMethods() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Collects all the core methods |
||
| 325 | * |
||
| 326 | * @param ApiCore $apiCore this parameter is used for testing. Here you can pass a non-default RemoteAPICore |
||
| 327 | * instance. (for mocking) |
||
| 328 | * @return array all core methods. |
||
| 329 | */ |
||
| 330 | public function getCoreMethods($apiCore = null) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Transform file to xml |
||
| 344 | * |
||
| 345 | * @param mixed $data |
||
| 346 | * @return mixed |
||
| 347 | */ |
||
| 348 | public function toFile($data) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Transform date to xml |
||
| 355 | * |
||
| 356 | * @param mixed $data |
||
| 357 | * @return mixed |
||
| 358 | */ |
||
| 359 | public function toDate($data) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * A simple transformation |
||
| 366 | * |
||
| 367 | * @param mixed $data |
||
| 368 | * @return mixed |
||
| 369 | */ |
||
| 370 | public function dummyTransformation($data) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Set the transformer function |
||
| 377 | * |
||
| 378 | * @param callback $dateTransformation |
||
| 379 | */ |
||
| 380 | public function setDateTransformation($dateTransformation) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Set the transformer function |
||
| 387 | * |
||
| 388 | * @param callback $fileTransformation |
||
| 389 | */ |
||
| 390 | public function setFileTransformation($fileTransformation) |
||
| 394 | } |
||
| 395 |
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..