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 | 2 | public static function newFromPage( $url ) { |
|
98 | |||
99 | /** |
||
100 | * @param string $apiUrl The API Url |
||
101 | * @param ClientInterface|null $client Guzzle Client |
||
102 | * @param MediawikiSession|null $session Inject a custom session here |
||
103 | */ |
||
104 | 24 | public function __construct( $apiUrl, ClientInterface $client = null, |
|
119 | |||
120 | /** |
||
121 | * Get the API URL (the URL to which API requests are sent, usually ending in api.php). |
||
122 | * This is useful if you've created this object via MediawikiApi::newFromPage(). |
||
123 | * |
||
124 | * @since 2.3 |
||
125 | * |
||
126 | * @return string The API URL. |
||
127 | */ |
||
128 | public function getApiUrl() { |
||
131 | |||
132 | /** |
||
133 | * @return ClientInterface |
||
134 | */ |
||
135 | 21 | private function getClient() { |
|
143 | |||
144 | /** |
||
145 | * Sets a logger instance on the object |
||
146 | * |
||
147 | * @since 1.1 |
||
148 | * |
||
149 | * @param LoggerInterface $logger |
||
150 | * |
||
151 | * @return null |
||
152 | */ |
||
153 | public function setLogger( LoggerInterface $logger ) { |
||
157 | |||
158 | /** |
||
159 | * @since 2.0 |
||
160 | * |
||
161 | * @param Request $request |
||
162 | * |
||
163 | * @return PromiseInterface |
||
164 | * Normally promising an array, though can be mixed (json_decode result) |
||
165 | * Can throw UsageExceptions or RejectionExceptions |
||
166 | */ |
||
167 | 1 | public function getRequestAsync( Request $request ) { |
|
178 | |||
179 | /** |
||
180 | * @since 2.0 |
||
181 | * |
||
182 | * @param Request $request |
||
183 | * |
||
184 | * @return PromiseInterface |
||
185 | * Normally promising an array, though can be mixed (json_decode result) |
||
186 | * Can throw UsageExceptions or RejectionExceptions |
||
187 | */ |
||
188 | 1 | public function postRequestAsync( Request $request ) { |
|
199 | |||
200 | /** |
||
201 | * @since 0.2 |
||
202 | * |
||
203 | * @param Request $request |
||
204 | * |
||
205 | * @return mixed Normally an array |
||
206 | */ |
||
207 | 9 | public function getRequest( Request $request ) { |
|
216 | |||
217 | /** |
||
218 | * @since 0.2 |
||
219 | * |
||
220 | * @param Request $request |
||
221 | * |
||
222 | * @return mixed Normally an array |
||
223 | */ |
||
224 | 10 | public function postRequest( Request $request ) { |
|
233 | |||
234 | /** |
||
235 | * @param ResponseInterface $response |
||
236 | * |
||
237 | * @return mixed |
||
238 | * @throws UsageException |
||
239 | */ |
||
240 | 21 | private function decodeResponse( ResponseInterface $response ) { |
|
248 | |||
249 | /** |
||
250 | * @param Request $request |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | 9 | private function getPostRequestEncoding( Request $request ) { |
|
262 | |||
263 | /** |
||
264 | * @param Request $request |
||
265 | * @param string $paramsKey either 'query' or 'multipart' |
||
266 | * |
||
267 | * @throws RequestException |
||
268 | * |
||
269 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
270 | */ |
||
271 | 21 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
283 | |||
284 | /** |
||
285 | * @param array $params |
||
286 | * |
||
287 | * @return array |
||
288 | */ |
||
289 | 1 | private function encodeMultipartParams( $params ) { |
|
303 | |||
304 | /** |
||
305 | * @return array |
||
306 | */ |
||
307 | 17 | private function getDefaultHeaders() { |
|
312 | |||
313 | 17 | private function getUserAgent() { |
|
320 | |||
321 | /** |
||
322 | * @param $result |
||
323 | */ |
||
324 | 17 | private function logWarnings( $result ) { |
|
337 | |||
338 | /** |
||
339 | * @param array $result |
||
340 | * |
||
341 | * @throws UsageException |
||
342 | */ |
||
343 | 17 | private function throwUsageExceptions( $result ) { |
|
352 | |||
353 | /** |
||
354 | * @since 0.1 |
||
355 | * |
||
356 | * @return bool|string false or the name of the current user |
||
357 | */ |
||
358 | 17 | public function isLoggedin() { |
|
361 | |||
362 | /** |
||
363 | * @since 0.1 |
||
364 | * |
||
365 | * @param ApiUser $apiUser |
||
366 | * |
||
367 | * @throws UsageException |
||
368 | * @return bool success |
||
369 | */ |
||
370 | 2 | public function login( ApiUser $apiUser ) { |
|
388 | |||
389 | /** |
||
390 | * @param ApiUser $apiUser |
||
391 | * |
||
392 | * @return string[] |
||
393 | */ |
||
394 | 2 | private function getLoginParams( ApiUser $apiUser ) { |
|
405 | |||
406 | /** |
||
407 | * @param array $result |
||
408 | * |
||
409 | * @throws UsageException |
||
410 | */ |
||
411 | 1 | private function throwLoginUsageException( $result ) { |
|
422 | |||
423 | /** |
||
424 | * @since 0.1 |
||
425 | * |
||
426 | * @return bool success |
||
427 | */ |
||
428 | 2 | public function logout() { |
|
438 | |||
439 | /** |
||
440 | * @since 0.1 |
||
441 | * |
||
442 | * @param string $type |
||
443 | * |
||
444 | * @return string |
||
445 | */ |
||
446 | 2 | public function getToken( $type = 'csrf' ) { |
|
449 | |||
450 | /** |
||
451 | * @since 0.1 |
||
452 | * |
||
453 | * Clears all tokens stored by the api |
||
454 | */ |
||
455 | 1 | public function clearTokens() { |
|
458 | |||
459 | /** |
||
460 | * @return string |
||
461 | */ |
||
462 | 4 | public function getVersion() { |
|
477 | |||
478 | } |
||
479 |