Complex classes like MediawikiApi 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 MediawikiApi, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class MediawikiApi implements MediawikiApiInterface, LoggerAwareInterface { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var ClientInterface|null Should be accessed through getClient |
||
| 30 | */ |
||
| 31 | private $client = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var bool|string |
||
| 35 | */ |
||
| 36 | private $isLoggedIn; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var MediawikiSession |
||
| 40 | */ |
||
| 41 | private $session; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $version; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var LoggerInterface |
||
| 50 | */ |
||
| 51 | private $logger; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $apiUrl; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @since 2.0 |
||
| 60 | * |
||
| 61 | * @param string $apiEndpoint e.g. https://en.wikipedia.org/w/api.php |
||
| 62 | * |
||
| 63 | * @return self returns a MediawikiApi instance using $apiEndpoint |
||
| 64 | */ |
||
| 65 | public static function newFromApiEndpoint( $apiEndpoint ) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Create a new MediawikiApi object from a URL to any page in a MediaWiki website. |
||
| 71 | * |
||
| 72 | * @since 2.0 |
||
| 73 | * @see https://en.wikipedia.org/wiki/Really_Simple_Discovery |
||
| 74 | * |
||
| 75 | * @param string $url e.g. https://en.wikipedia.org OR https://de.wikipedia.org/wiki/Berlin |
||
| 76 | * @return self returns a MediawikiApi instance using the apiEndpoint provided by the RSD |
||
| 77 | * file accessible on all Mediawiki pages |
||
| 78 | * @throws UsageException If the RSD URL could not be found in the page's HTML. |
||
| 79 | */ |
||
| 80 | 2 | public static function newFromPage( $url ) { |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $apiUrl The API Url |
||
| 105 | * @param ClientInterface|null $client Guzzle Client |
||
| 106 | * @param MediawikiSession|null $session Inject a custom session here |
||
| 107 | */ |
||
| 108 | 24 | public function __construct( $apiUrl, ClientInterface $client = null, MediawikiSession $session = null ) { |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Get the API URL (the URL to which API requests are sent, usually ending in api.php). |
||
| 125 | * This is useful if you've created this object via MediawikiApi::newFromPage(). |
||
| 126 | * |
||
| 127 | * @since 2.3 |
||
| 128 | * |
||
| 129 | * @return string The API URL. |
||
| 130 | */ |
||
| 131 | public function getApiUrl() { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return ClientInterface |
||
| 137 | */ |
||
| 138 | 21 | private function getClient() { |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Sets a logger instance on the object |
||
| 149 | * |
||
| 150 | * @since 1.1 |
||
| 151 | * |
||
| 152 | * @param LoggerInterface $logger |
||
| 153 | * |
||
| 154 | * @return null |
||
| 155 | */ |
||
| 156 | public function setLogger( LoggerInterface $logger ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @since 2.0 |
||
| 163 | * |
||
| 164 | * @param Request $request |
||
| 165 | * |
||
| 166 | * @return PromiseInterface |
||
| 167 | * Normally promising an array, though can be mixed (json_decode result) |
||
| 168 | * Can throw UsageExceptions or RejectionExceptions |
||
| 169 | */ |
||
| 170 | 1 | public function getRequestAsync( Request $request ) { |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @since 2.0 |
||
| 184 | * |
||
| 185 | * @param Request $request |
||
| 186 | * |
||
| 187 | * @return PromiseInterface |
||
| 188 | * Normally promising an array, though can be mixed (json_decode result) |
||
| 189 | * Can throw UsageExceptions or RejectionExceptions |
||
| 190 | */ |
||
| 191 | 1 | public function postRequestAsync( Request $request ) { |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @since 0.2 |
||
| 205 | * |
||
| 206 | * @param Request $request |
||
| 207 | * |
||
| 208 | * @return mixed Normally an array |
||
| 209 | */ |
||
| 210 | 9 | public function getRequest( Request $request ) { |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @since 0.2 |
||
| 222 | * |
||
| 223 | * @param Request $request |
||
| 224 | * |
||
| 225 | * @return mixed Normally an array |
||
| 226 | */ |
||
| 227 | 10 | public function postRequest( Request $request ) { |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param ResponseInterface $response |
||
| 239 | * |
||
| 240 | * @return mixed |
||
| 241 | * @throws UsageException |
||
| 242 | */ |
||
| 243 | 21 | private function decodeResponse( ResponseInterface $response ) { |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param Request $request |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | 9 | private function getPostRequestEncoding( Request $request ) { |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param Request $request |
||
| 268 | * @param string $paramsKey either 'query' or 'multipart' |
||
| 269 | * |
||
| 270 | * @throws RequestException |
||
| 271 | * |
||
| 272 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
| 273 | */ |
||
| 274 | 21 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param array $params |
||
| 289 | * |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | 1 | private function encodeMultipartParams( $params ) { |
|
| 306 | |||
| 307 | /** |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | 17 | private function getDefaultHeaders() { |
|
| 315 | |||
| 316 | 17 | private function getUserAgent() { |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @param $result |
||
| 326 | */ |
||
| 327 | 17 | private function logWarnings( $result ) { |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param array $result |
||
| 342 | * |
||
| 343 | * @throws UsageException |
||
| 344 | */ |
||
| 345 | 17 | private function throwUsageExceptions( $result ) { |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @since 0.1 |
||
| 357 | * |
||
| 358 | * @return bool|string false or the name of the current user |
||
| 359 | */ |
||
| 360 | 17 | public function isLoggedin() { |
|
| 363 | |||
| 364 | /** |
||
| 365 | * @since 0.1 |
||
| 366 | * |
||
| 367 | * @param ApiUser $apiUser |
||
| 368 | * |
||
| 369 | * @throws UsageException |
||
| 370 | * @return bool success |
||
| 371 | */ |
||
| 372 | 2 | public function login( ApiUser $apiUser ) { |
|
| 389 | |||
| 390 | /** |
||
| 391 | * @param ApiUser $apiUser |
||
| 392 | * |
||
| 393 | * @return string[] |
||
| 394 | */ |
||
| 395 | 2 | private function getLoginParams( ApiUser $apiUser ) { |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @param array $result |
||
| 409 | * |
||
| 410 | * @throws UsageException |
||
| 411 | */ |
||
| 412 | 1 | private function throwLoginUsageException( $result ) { |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @since 0.1 |
||
| 426 | * |
||
| 427 | * @return bool success |
||
| 428 | */ |
||
| 429 | 2 | public function logout() { |
|
| 439 | |||
| 440 | /** |
||
| 441 | * @since 0.1 |
||
| 442 | * |
||
| 443 | * @param string $type |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | 2 | public function getToken( $type = 'csrf' ) { |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @since 0.1 |
||
| 453 | * |
||
| 454 | * Clears all tokens stored by the api |
||
| 455 | */ |
||
| 456 | 1 | public function clearTokens() { |
|
| 459 | |||
| 460 | /** |
||
| 461 | * @return string |
||
| 462 | */ |
||
| 463 | 4 | public function getVersion(){ |
|
| 478 | |||
| 479 | } |
||
| 480 |