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 |
||
| 38 | class Api |
||
| 39 | { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var ApiCore |
||
| 43 | */ |
||
| 44 | private $coreMethods = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array remote methods provided by dokuwiki plugins - will be filled lazy via |
||
| 48 | * {@see dokuwiki\Remote\RemoteAPI#getPluginMethods} |
||
| 49 | */ |
||
| 50 | private $pluginMethods = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array contains custom calls to the api. Plugins can use the XML_CALL_REGISTER event. |
||
| 54 | * The data inside is 'custom.call.something' => array('plugin name', 'remote method name') |
||
| 55 | * |
||
| 56 | * The remote method name is the same as in the remote name returned by _getMethods(). |
||
| 57 | */ |
||
| 58 | private $pluginCustomCalls = null; |
||
| 59 | |||
| 60 | private $dateTransformation; |
||
| 61 | private $fileTransformation; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * constructor |
||
| 65 | */ |
||
| 66 | public function __construct() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Get all available methods with remote access. |
||
| 74 | * |
||
| 75 | * @return array with information to all available methods |
||
| 76 | * @throws RemoteException |
||
| 77 | */ |
||
| 78 | public function getMethods() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Call a method via remote api. |
||
| 85 | * |
||
| 86 | * @param string $method name of the method to call. |
||
| 87 | * @param array $args arguments to pass to the given method |
||
| 88 | * @return mixed result of method call, must be a primitive type. |
||
| 89 | * @throws RemoteException |
||
| 90 | */ |
||
| 91 | public function call($method, $args = array()) |
||
| 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) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Returns plugin calls that are registered via RPC_CALL_ADD action |
||
| 138 | * |
||
| 139 | * @return array with pairs of custom plugin calls |
||
| 140 | * @triggers RPC_CALL_ADD |
||
| 141 | */ |
||
| 142 | private function getCustomCallPlugins() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Call a plugin method |
||
| 154 | * |
||
| 155 | * @param string $pluginName |
||
| 156 | * @param string $method method name |
||
| 157 | * @param array $args |
||
| 158 | * @return mixed return of custom method |
||
| 159 | * @throws RemoteException |
||
| 160 | */ |
||
| 161 | private function callPlugin($pluginName, $method, $args) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Call a core method |
||
| 175 | * |
||
| 176 | * @param string $method name of method |
||
| 177 | * @param array $args |
||
| 178 | * @return mixed |
||
| 179 | * @throws RemoteException if method not exist |
||
| 180 | */ |
||
| 181 | private function callCoreMethod($method, $args) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Check if access should be checked |
||
| 194 | * |
||
| 195 | * @param array $methodMeta data about the method |
||
| 196 | * @throws AccessDeniedException |
||
| 197 | */ |
||
| 198 | private function checkAccess($methodMeta) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Check the number of parameters |
||
| 211 | * |
||
| 212 | * @param array $methodMeta data about the method |
||
| 213 | * @param array $args |
||
| 214 | * @throws RemoteException if wrong parameter count |
||
| 215 | */ |
||
| 216 | private function checkArgumentLength($methodMeta, $args) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Determine the name of the real method |
||
| 225 | * |
||
| 226 | * @param array $methodMeta list of data of the methods |
||
| 227 | * @param string $method name of method |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | private function getMethodName($methodMeta, $method) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Perform access check for current user |
||
| 241 | * |
||
| 242 | * @return bool true if the current user has access to remote api. |
||
| 243 | * @throws AccessDeniedException If remote access disabled |
||
| 244 | */ |
||
| 245 | public function hasAccess() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Requests access |
||
| 270 | * |
||
| 271 | * @return void |
||
| 272 | * @throws AccessDeniedException On denied access. |
||
| 273 | */ |
||
| 274 | public function forceAccess() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Collects all the methods of the enabled Remote Plugins |
||
| 283 | * |
||
| 284 | * @return array all plugin methods. |
||
| 285 | * @throws RemoteException if not implemented |
||
| 286 | */ |
||
| 287 | public function getPluginMethods() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Collects all the core methods |
||
| 316 | * |
||
| 317 | * @param ApiCore $apiCore this parameter is used for testing. Here you can pass a non-default RemoteAPICore |
||
| 318 | * instance. (for mocking) |
||
| 319 | * @return array all core methods. |
||
| 320 | */ |
||
| 321 | public function getCoreMethods($apiCore = null) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Transform file to xml |
||
| 335 | * |
||
| 336 | * @param mixed $data |
||
| 337 | * @return mixed |
||
| 338 | */ |
||
| 339 | public function toFile($data) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Transform date to xml |
||
| 346 | * |
||
| 347 | * @param mixed $data |
||
| 348 | * @return mixed |
||
| 349 | */ |
||
| 350 | public function toDate($data) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * A simple transformation |
||
| 357 | * |
||
| 358 | * @param mixed $data |
||
| 359 | * @return mixed |
||
| 360 | */ |
||
| 361 | public function dummyTransformation($data) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Set the transformer function |
||
| 368 | * |
||
| 369 | * @param callback $dateTransformation |
||
| 370 | */ |
||
| 371 | public function setDateTransformation($dateTransformation) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Set the transformer function |
||
| 378 | * |
||
| 379 | * @param callback $fileTransformation |
||
| 380 | */ |
||
| 381 | public function setFileTransformation($fileTransformation) |
||
| 385 | } |
||
| 386 |
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..