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) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Call a core method |
||
| 174 | * |
||
| 175 | * @param string $method name of method |
||
| 176 | * @param array $args |
||
| 177 | * @return mixed |
||
| 178 | * @throws RemoteException if method not exist |
||
| 179 | */ |
||
| 180 | private function callCoreMethod($method, $args) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Check if access should be checked |
||
| 193 | * |
||
| 194 | * @param array $methodMeta data about the method |
||
| 195 | * @throws AccessDeniedException |
||
| 196 | */ |
||
| 197 | private function checkAccess($methodMeta) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Check the number of parameters |
||
| 210 | * |
||
| 211 | * @param array $methodMeta data about the method |
||
| 212 | * @param array $args |
||
| 213 | * @throws RemoteException if wrong parameter count |
||
| 214 | */ |
||
| 215 | private function checkArgumentLength($methodMeta, $args) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Determine the name of the real method |
||
| 224 | * |
||
| 225 | * @param array $methodMeta list of data of the methods |
||
| 226 | * @param string $method name of method |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | private function getMethodName($methodMeta, $method) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Perform access check for current user |
||
| 240 | * |
||
| 241 | * @return bool true if the current user has access to remote api. |
||
| 242 | * @throws AccessDeniedException If remote access disabled |
||
| 243 | */ |
||
| 244 | public function hasAccess() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Requests access |
||
| 269 | * |
||
| 270 | * @return void |
||
| 271 | * @throws AccessDeniedException On denied access. |
||
| 272 | */ |
||
| 273 | public function forceAccess() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Collects all the methods of the enabled Remote Plugins |
||
| 282 | * |
||
| 283 | * @return array all plugin methods. |
||
| 284 | * @throws RemoteException if not implemented |
||
| 285 | */ |
||
| 286 | public function getPluginMethods() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Collects all the core methods |
||
| 317 | * |
||
| 318 | * @param ApiCore $apiCore this parameter is used for testing. Here you can pass a non-default RemoteAPICore |
||
| 319 | * instance. (for mocking) |
||
| 320 | * @return array all core methods. |
||
| 321 | */ |
||
| 322 | public function getCoreMethods($apiCore = null) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Transform file to xml |
||
| 336 | * |
||
| 337 | * @param mixed $data |
||
| 338 | * @return mixed |
||
| 339 | */ |
||
| 340 | public function toFile($data) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Transform date to xml |
||
| 347 | * |
||
| 348 | * @param mixed $data |
||
| 349 | * @return mixed |
||
| 350 | */ |
||
| 351 | public function toDate($data) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * A simple transformation |
||
| 358 | * |
||
| 359 | * @param mixed $data |
||
| 360 | * @return mixed |
||
| 361 | */ |
||
| 362 | public function dummyTransformation($data) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Set the transformer function |
||
| 369 | * |
||
| 370 | * @param callback $dateTransformation |
||
| 371 | */ |
||
| 372 | public function setDateTransformation($dateTransformation) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Set the transformer function |
||
| 379 | * |
||
| 380 | * @param callback $fileTransformation |
||
| 381 | */ |
||
| 382 | public function setFileTransformation($fileTransformation) |
||
| 386 | } |
||
| 387 |
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..