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 LoggerAwareInterface { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Client|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.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 | * @since 2.0.0 |
||
| 72 | * |
||
| 73 | * @param string $url e.g. https://en.wikipedia.org OR https://de.wikipedia.org/wiki/Berlin |
||
| 74 | * |
||
| 75 | * @return self returns a MediawikiApi instance using the apiEndpoint provided by the RSD |
||
| 76 | * file accessible on all Mediawiki pages |
||
| 77 | * |
||
| 78 | * @see https://en.wikipedia.org/wiki/Really_Simple_Discovery |
||
| 79 | */ |
||
| 80 | 1 | public static function newFromPage( $url ) { |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @access private |
||
| 90 | * |
||
| 91 | * @param string $apiUrl The API Url |
||
| 92 | * @param Client|null $client Guzzle Client |
||
| 93 | * @param MediawikiSession|null $session Inject a custom session here |
||
| 94 | */ |
||
| 95 | 23 | public function __construct( $apiUrl, Client $client = null, MediawikiSession $session = null ) { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @return Client |
||
| 112 | */ |
||
| 113 | 20 | private function getClient() { |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Sets a logger instance on the object |
||
| 124 | * |
||
| 125 | * @since 1.1 |
||
| 126 | * |
||
| 127 | * @param LoggerInterface $logger |
||
| 128 | * |
||
| 129 | * @return null |
||
| 130 | */ |
||
| 131 | public function setLogger( LoggerInterface $logger ) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @since 2.0 |
||
| 138 | * |
||
| 139 | * @param Request $request |
||
| 140 | * |
||
| 141 | * @return PromiseInterface |
||
| 142 | * Normally promising an array, though can be mixed (json_decode result) |
||
| 143 | * Can throw UsageExceptions or RejectionExceptions |
||
| 144 | */ |
||
| 145 | 1 | public function getRequestAsync( Request $request ) { |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @since 2.0 |
||
| 158 | * |
||
| 159 | * @param Request $request |
||
| 160 | * |
||
| 161 | * @return PromiseInterface |
||
| 162 | * Normally promising an array, though can be mixed (json_decode result) |
||
| 163 | * Can throw UsageExceptions or RejectionExceptions |
||
| 164 | */ |
||
| 165 | 1 | public function postRequestAsync( Request $request ) { |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @since 0.2 |
||
| 178 | * |
||
| 179 | * @param Request $request |
||
| 180 | * |
||
| 181 | * @return mixed Normally an array |
||
| 182 | */ |
||
| 183 | 9 | public function getRequest( Request $request ) { |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @since 0.2 |
||
| 194 | * |
||
| 195 | * @param Request $request |
||
| 196 | * |
||
| 197 | * @return mixed Normally an array |
||
| 198 | */ |
||
| 199 | 9 | public function postRequest( Request $request ) { |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param ResponseInterface $response |
||
| 210 | * |
||
| 211 | * @return mixed |
||
| 212 | * @throws UsageException |
||
| 213 | */ |
||
| 214 | 20 | private function decodeResponse( ResponseInterface $response ) { |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @param Request $request |
||
| 225 | * @param string $paramsKey either 'query' or 'form_params' |
||
| 226 | * |
||
| 227 | * @throws RequestException |
||
| 228 | * |
||
| 229 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
| 230 | */ |
||
| 231 | 20 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | 16 | private function getDefaultHeaders() { |
|
| 246 | |||
| 247 | 16 | private function getUserAgent() { |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @param $result |
||
| 257 | */ |
||
| 258 | 16 | private function logWarnings( $result ) { |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param array $result |
||
| 268 | * |
||
| 269 | * @throws UsageException |
||
| 270 | */ |
||
| 271 | 16 | private function throwUsageExceptions( $result ) { |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @since 0.1 |
||
| 283 | * |
||
| 284 | * @return bool|string false or the name of the current user |
||
| 285 | */ |
||
| 286 | 16 | public function isLoggedin() { |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @since 0.1 |
||
| 292 | * |
||
| 293 | * @param ApiUser $apiUser |
||
| 294 | * |
||
| 295 | * @throws UsageException |
||
| 296 | * @return bool success |
||
| 297 | */ |
||
| 298 | 2 | public function login( ApiUser $apiUser ) { |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @param ApiUser $apiUser |
||
| 317 | * |
||
| 318 | * @return string[] |
||
| 319 | */ |
||
| 320 | 2 | private function getLoginParams( ApiUser $apiUser ) { |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @param array $result |
||
| 334 | * |
||
| 335 | * @throws UsageException |
||
| 336 | */ |
||
| 337 | 1 | private function throwLoginUsageException( $result ) { |
|
| 346 | |||
| 347 | /** |
||
| 348 | * @param string $loginResult |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | 1 | private function getLoginExceptionMessage( $loginResult ) { |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @since 0.1 |
||
| 377 | * |
||
| 378 | * @return bool success |
||
| 379 | */ |
||
| 380 | 2 | public function logout() { |
|
| 390 | |||
| 391 | /** |
||
| 392 | * @since 0.1 |
||
| 393 | * |
||
| 394 | * @param string $type |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | 2 | public function getToken( $type = 'csrf' ) { |
|
| 401 | |||
| 402 | /** |
||
| 403 | * @since 0.1 |
||
| 404 | * |
||
| 405 | * Clears all tokens stored by the api |
||
| 406 | */ |
||
| 407 | 1 | public function clearTokens() { |
|
| 410 | |||
| 411 | /** |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | 4 | public function getVersion(){ |
|
| 429 | |||
| 430 | } |
||
| 431 |