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 |
||
| 27 | class MediawikiApi implements MediawikiApiInterface, LoggerAwareInterface { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var ClientInterface|null Should be accessed through getClient |
||
| 31 | */ |
||
| 32 | private $client = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var bool|string |
||
| 36 | */ |
||
| 37 | private $isLoggedIn; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var MediawikiSession |
||
| 41 | */ |
||
| 42 | private $session; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $version; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var LoggerInterface |
||
| 51 | */ |
||
| 52 | private $logger; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $apiUrl; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @since 2.0 |
||
| 61 | * |
||
| 62 | * @param string $apiEndpoint e.g. https://en.wikipedia.org/w/api.php |
||
| 63 | * |
||
| 64 | * @return self returns a MediawikiApi instance using $apiEndpoint |
||
| 65 | */ |
||
| 66 | public static function newFromApiEndpoint( $apiEndpoint ) { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Create a new MediawikiApi object from a URL to any page in a MediaWiki website. |
||
| 72 | * |
||
| 73 | * @since 2.0 |
||
| 74 | * @see https://en.wikipedia.org/wiki/Really_Simple_Discovery |
||
| 75 | * |
||
| 76 | * @param string $url e.g. https://en.wikipedia.org OR https://de.wikipedia.org/wiki/Berlin |
||
| 77 | * @return self returns a MediawikiApi instance using the apiEndpoint provided by the RSD |
||
| 78 | * file accessible on all Mediawiki pages |
||
| 79 | * @throws RsdException If the RSD URL could not be found in the page's HTML. |
||
| 80 | */ |
||
| 81 | 3 | public static function newFromPage( $url ) { |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $apiUrl The API Url |
||
| 116 | * @param ClientInterface|null $client Guzzle Client |
||
| 117 | * @param MediawikiSession|null $session Inject a custom session here |
||
| 118 | */ |
||
| 119 | 24 | public function __construct( $apiUrl, ClientInterface $client = null, MediawikiSession $session = null ) { |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Get the API URL (the URL to which API requests are sent, usually ending in api.php). |
||
| 136 | * This is useful if you've created this object via MediawikiApi::newFromPage(). |
||
| 137 | * |
||
| 138 | * @since 2.3 |
||
| 139 | * |
||
| 140 | * @return string The API URL. |
||
| 141 | */ |
||
| 142 | public function getApiUrl() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return ClientInterface |
||
| 148 | */ |
||
| 149 | 21 | private function getClient() { |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Sets a logger instance on the object |
||
| 160 | * |
||
| 161 | * @since 1.1 |
||
| 162 | * |
||
| 163 | * @param LoggerInterface $logger |
||
| 164 | * |
||
| 165 | * @return null |
||
| 166 | */ |
||
| 167 | public function setLogger( LoggerInterface $logger ) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @since 2.0 |
||
| 174 | * |
||
| 175 | * @param Request $request |
||
| 176 | * |
||
| 177 | * @return PromiseInterface |
||
| 178 | * Normally promising an array, though can be mixed (json_decode result) |
||
| 179 | * Can throw UsageExceptions or RejectionExceptions |
||
| 180 | */ |
||
| 181 | 1 | public function getRequestAsync( Request $request ) { |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @since 2.0 |
||
| 195 | * |
||
| 196 | * @param Request $request |
||
| 197 | * |
||
| 198 | * @return PromiseInterface |
||
| 199 | * Normally promising an array, though can be mixed (json_decode result) |
||
| 200 | * Can throw UsageExceptions or RejectionExceptions |
||
| 201 | */ |
||
| 202 | 1 | public function postRequestAsync( Request $request ) { |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @since 0.2 |
||
| 216 | * |
||
| 217 | * @param Request $request |
||
| 218 | * |
||
| 219 | * @return mixed Normally an array |
||
| 220 | */ |
||
| 221 | 9 | public function getRequest( Request $request ) { |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @since 0.2 |
||
| 233 | * |
||
| 234 | * @param Request $request |
||
| 235 | * |
||
| 236 | * @return mixed Normally an array |
||
| 237 | */ |
||
| 238 | 10 | public function postRequest( Request $request ) { |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @param ResponseInterface $response |
||
| 250 | * |
||
| 251 | * @return mixed |
||
| 252 | * @throws UsageException |
||
| 253 | */ |
||
| 254 | 21 | private function decodeResponse( ResponseInterface $response ) { |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @param Request $request |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | 9 | private function getPostRequestEncoding( Request $request ) { |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param Request $request |
||
| 279 | * @param string $paramsKey either 'query' or 'multipart' |
||
| 280 | * |
||
| 281 | * @throws RequestException |
||
| 282 | * |
||
| 283 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
| 284 | */ |
||
| 285 | 21 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @param array $params |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | 1 | private function encodeMultipartParams( $params ) { |
|
| 317 | |||
| 318 | /** |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | 17 | private function getDefaultHeaders() { |
|
| 326 | |||
| 327 | 17 | private function getUserAgent() { |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @param $result |
||
| 337 | */ |
||
| 338 | 17 | private function logWarnings( $result ) { |
|
| 350 | |||
| 351 | /** |
||
| 352 | * @param array $result |
||
| 353 | * |
||
| 354 | * @throws UsageException |
||
| 355 | */ |
||
| 356 | 17 | private function throwUsageExceptions( $result ) { |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @since 0.1 |
||
| 368 | * |
||
| 369 | * @return bool|string false or the name of the current user |
||
| 370 | */ |
||
| 371 | 17 | public function isLoggedin() { |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @since 0.1 |
||
| 377 | * |
||
| 378 | * @param ApiUser $apiUser |
||
| 379 | * |
||
| 380 | * @throws UsageException |
||
| 381 | * @return bool success |
||
| 382 | */ |
||
| 383 | 2 | public function login( ApiUser $apiUser ) { |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @param ApiUser $apiUser |
||
| 403 | * |
||
| 404 | * @return string[] |
||
| 405 | */ |
||
| 406 | 2 | private function getLoginParams( ApiUser $apiUser ) { |
|
| 417 | |||
| 418 | /** |
||
| 419 | * @param array $result |
||
| 420 | * |
||
| 421 | * @throws UsageException |
||
| 422 | */ |
||
| 423 | 1 | private function throwLoginUsageException( $result ) { |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @since 0.1 |
||
| 437 | * |
||
| 438 | * @return bool success |
||
| 439 | */ |
||
| 440 | 2 | public function logout() { |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @since 0.1 |
||
| 453 | * |
||
| 454 | * @param string $type |
||
| 455 | * |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | 2 | public function getToken( $type = 'csrf' ) { |
|
| 461 | |||
| 462 | /** |
||
| 463 | * @since 0.1 |
||
| 464 | * |
||
| 465 | * Clears all tokens stored by the api |
||
| 466 | */ |
||
| 467 | 1 | public function clearTokens() { |
|
| 470 | |||
| 471 | /** |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | 4 | public function getVersion(){ |
|
| 489 | |||
| 490 | } |
||
| 491 |