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 |
||
| 25 | class MediawikiApi implements MediawikiApiInterface, LoggerAwareInterface { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var ClientInterface|null Should be accessed through getClient |
||
| 29 | */ |
||
| 30 | private $client = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool|string |
||
| 34 | */ |
||
| 35 | private $isLoggedIn; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var MediawikiSession |
||
| 39 | */ |
||
| 40 | private $session; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $version; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var LoggerInterface |
||
| 49 | */ |
||
| 50 | private $logger; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private $apiUrl; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @since 2.0.0 |
||
| 59 | * |
||
| 60 | * @param string $apiEndpoint e.g. https://en.wikipedia.org/w/api.php |
||
| 61 | * |
||
| 62 | * @return self returns a MediawikiApi instance using $apiEndpoint |
||
| 63 | */ |
||
| 64 | public static function newFromApiEndpoint( $apiEndpoint ) { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @since 2.0.0 |
||
| 70 | * |
||
| 71 | * @param string $url e.g. https://en.wikipedia.org OR https://de.wikipedia.org/wiki/Berlin |
||
| 72 | * |
||
| 73 | * @return self returns a MediawikiApi instance using the apiEndpoint provided by the RSD |
||
| 74 | * file accessible on all Mediawiki pages |
||
| 75 | * |
||
| 76 | * @see https://en.wikipedia.org/wiki/Really_Simple_Discovery |
||
| 77 | */ |
||
| 78 | 1 | public static function newFromPage( $url ) { |
|
| 79 | 1 | $tempClient = new Client( array( 'headers' => array( 'User-Agent' => 'addwiki-mediawiki-client' ) ) ); |
|
| 80 | 1 | $pageXml = new SimpleXMLElement( $tempClient->get( $url )->getBody() ); |
|
| 81 | 1 | $rsdElement = $pageXml->xpath( 'head/link[@type="application/rsd+xml"][@href]' ); |
|
| 82 | 1 | $rsdXml = new SimpleXMLElement( $tempClient->get( $rsdElement[0]->attributes()['href'] )->getBody() ); |
|
| 83 | return self::newFromApiEndpoint( $rsdXml->service->apis->api->attributes()->apiLink->__toString() ); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $apiUrl The API Url |
||
| 88 | * @param ClientInterface|null $client Guzzle Client |
||
| 89 | * @param MediawikiSession|null $session Inject a custom session here |
||
| 90 | */ |
||
| 91 | 24 | public function __construct( $apiUrl, ClientInterface $client = null, MediawikiSession $session = null ) { |
|
| 105 | |||
| 106 | /** |
||
| 107 | * @return ClientInterface |
||
| 108 | */ |
||
| 109 | 21 | private function getClient() { |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Sets a logger instance on the object |
||
| 120 | * |
||
| 121 | * @since 1.1 |
||
| 122 | * |
||
| 123 | * @param LoggerInterface $logger |
||
| 124 | * |
||
| 125 | * @return null |
||
| 126 | */ |
||
| 127 | public function setLogger( LoggerInterface $logger ) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @since 2.0 |
||
| 134 | * |
||
| 135 | * @param Request $request |
||
| 136 | * |
||
| 137 | * @return PromiseInterface |
||
| 138 | * Normally promising an array, though can be mixed (json_decode result) |
||
| 139 | * Can throw UsageExceptions or RejectionExceptions |
||
| 140 | */ |
||
| 141 | 1 | public function getRequestAsync( Request $request ) { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @since 2.0 |
||
| 155 | * |
||
| 156 | * @param Request $request |
||
| 157 | * |
||
| 158 | * @return PromiseInterface |
||
| 159 | * Normally promising an array, though can be mixed (json_decode result) |
||
| 160 | * Can throw UsageExceptions or RejectionExceptions |
||
| 161 | */ |
||
| 162 | 1 | public function postRequestAsync( Request $request ) { |
|
| 163 | 1 | $promise = $this->getClient()->requestAsync( |
|
| 164 | 1 | 'POST', |
|
| 165 | 1 | $this->apiUrl, |
|
| 166 | 1 | $this->getClientRequestOptions( $request, $this->getPostRequestEncoding( $request ) ) |
|
| 167 | ); |
||
| 168 | |||
| 169 | return $promise->then( function( ResponseInterface $response ) { |
||
| 170 | 1 | return call_user_func( array( $this, 'decodeResponse' ), $response ); |
|
| 171 | 1 | } ); |
|
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @since 0.2 |
||
| 176 | * |
||
| 177 | * @param Request $request |
||
| 178 | * |
||
| 179 | * @return mixed Normally an array |
||
| 180 | */ |
||
| 181 | 9 | public function getRequest( Request $request ) { |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @since 0.2 |
||
| 193 | * |
||
| 194 | * @param Request $request |
||
| 195 | * |
||
| 196 | * @return mixed Normally an array |
||
| 197 | */ |
||
| 198 | 10 | public function postRequest( Request $request ) { |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param ResponseInterface $response |
||
| 210 | * |
||
| 211 | * @return mixed |
||
| 212 | * @throws UsageException |
||
| 213 | */ |
||
| 214 | 21 | private function decodeResponse( ResponseInterface $response ) { |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @param Request $request |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | 9 | private function getPostRequestEncoding( Request $request ) { |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param Request $request |
||
| 239 | * @param string $paramsKey either 'query' or 'multipart' |
||
| 240 | * |
||
| 241 | * @throws RequestException |
||
| 242 | * |
||
| 243 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
| 244 | */ |
||
| 245 | 21 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @param array $params |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | private function encodeMultipartParams( $params ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | 17 | private function getDefaultHeaders() { |
|
| 278 | |||
| 279 | 17 | private function getUserAgent() { |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param $result |
||
| 289 | */ |
||
| 290 | 17 | private function logWarnings( $result ) { |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @param array $result |
||
| 300 | * |
||
| 301 | * @throws UsageException |
||
| 302 | */ |
||
| 303 | 17 | private function throwUsageExceptions( $result ) { |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @since 0.1 |
||
| 315 | * |
||
| 316 | * @return bool|string false or the name of the current user |
||
| 317 | */ |
||
| 318 | 17 | public function isLoggedin() { |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @since 0.1 |
||
| 324 | * |
||
| 325 | * @param ApiUser $apiUser |
||
| 326 | * |
||
| 327 | * @throws UsageException |
||
| 328 | * @return bool success |
||
| 329 | */ |
||
| 330 | 2 | public function login( ApiUser $apiUser ) { |
|
| 346 | |||
| 347 | /** |
||
| 348 | * @param ApiUser $apiUser |
||
| 349 | * |
||
| 350 | * @return string[] |
||
| 351 | */ |
||
| 352 | 2 | private function getLoginParams( ApiUser $apiUser ) { |
|
| 363 | |||
| 364 | /** |
||
| 365 | * @param array $result |
||
| 366 | * |
||
| 367 | * @throws UsageException |
||
| 368 | */ |
||
| 369 | 1 | private function throwLoginUsageException( $result ) { |
|
| 378 | |||
| 379 | /** |
||
| 380 | * @param string $loginResult |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | 1 | private function getLoginExceptionMessage( $loginResult ) { |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @since 0.1 |
||
| 409 | * |
||
| 410 | * @return bool success |
||
| 411 | */ |
||
| 412 | 2 | public function logout() { |
|
| 422 | |||
| 423 | /** |
||
| 424 | * @since 0.1 |
||
| 425 | * |
||
| 426 | * @param string $type |
||
| 427 | * |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | 2 | public function getToken( $type = 'csrf' ) { |
|
| 433 | |||
| 434 | /** |
||
| 435 | * @since 0.1 |
||
| 436 | * |
||
| 437 | * Clears all tokens stored by the api |
||
| 438 | */ |
||
| 439 | 1 | public function clearTokens() { |
|
| 442 | |||
| 443 | /** |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | 4 | public function getVersion(){ |
|
| 461 | |||
| 462 | } |
||
| 463 |