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 |
||
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 |
||
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 ) { |
|
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 | * Get the API URL (the URL to which API requests are sent, usually ending in api.php). |
||
108 | * This is useful if you've created this object via MediawikiApi::newFromPage(). |
||
109 | * |
||
110 | * @since 2.3 |
||
111 | * |
||
112 | * @return string The API URL. |
||
113 | */ |
||
114 | public function getApiUrl() { |
||
117 | |||
118 | /** |
||
119 | * @return ClientInterface |
||
120 | */ |
||
121 | 21 | private function getClient() { |
|
129 | |||
130 | /** |
||
131 | * Sets a logger instance on the object |
||
132 | * |
||
133 | * @since 1.1 |
||
134 | * |
||
135 | * @param LoggerInterface $logger |
||
136 | * |
||
137 | * @return null |
||
138 | */ |
||
139 | public function setLogger( LoggerInterface $logger ) { |
||
143 | |||
144 | /** |
||
145 | * @since 2.0 |
||
146 | * |
||
147 | * @param Request $request |
||
148 | * |
||
149 | * @return PromiseInterface |
||
150 | * Normally promising an array, though can be mixed (json_decode result) |
||
151 | * Can throw UsageExceptions or RejectionExceptions |
||
152 | */ |
||
153 | 1 | public function getRequestAsync( Request $request ) { |
|
164 | |||
165 | /** |
||
166 | * @since 2.0 |
||
167 | * |
||
168 | * @param Request $request |
||
169 | * |
||
170 | * @return PromiseInterface |
||
171 | * Normally promising an array, though can be mixed (json_decode result) |
||
172 | * Can throw UsageExceptions or RejectionExceptions |
||
173 | */ |
||
174 | 1 | public function postRequestAsync( Request $request ) { |
|
185 | |||
186 | /** |
||
187 | * @since 0.2 |
||
188 | * |
||
189 | * @param Request $request |
||
190 | * |
||
191 | * @return mixed Normally an array |
||
192 | */ |
||
193 | 9 | public function getRequest( Request $request ) { |
|
202 | |||
203 | /** |
||
204 | * @since 0.2 |
||
205 | * |
||
206 | * @param Request $request |
||
207 | * |
||
208 | * @return mixed Normally an array |
||
209 | */ |
||
210 | 10 | public function postRequest( Request $request ) { |
|
219 | |||
220 | /** |
||
221 | * @param ResponseInterface $response |
||
222 | * |
||
223 | * @return mixed |
||
224 | * @throws UsageException |
||
225 | */ |
||
226 | 21 | private function decodeResponse( ResponseInterface $response ) { |
|
234 | |||
235 | /** |
||
236 | * @param Request $request |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | 9 | private function getPostRequestEncoding( Request $request ) { |
|
248 | |||
249 | /** |
||
250 | * @param Request $request |
||
251 | * @param string $paramsKey either 'query' or 'multipart' |
||
252 | * |
||
253 | * @throws RequestException |
||
254 | * |
||
255 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
256 | */ |
||
257 | 21 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
269 | |||
270 | /** |
||
271 | * @param array $params |
||
272 | * |
||
273 | * @return array |
||
274 | */ |
||
275 | 1 | private function encodeMultipartParams( $params ) { |
|
289 | |||
290 | /** |
||
291 | * @return array |
||
292 | */ |
||
293 | 17 | private function getDefaultHeaders() { |
|
298 | |||
299 | 17 | private function getUserAgent() { |
|
306 | |||
307 | /** |
||
308 | * @param $result |
||
309 | */ |
||
310 | 17 | private function logWarnings( $result ) { |
|
317 | |||
318 | /** |
||
319 | * @param array $result |
||
320 | * |
||
321 | * @throws UsageException |
||
322 | */ |
||
323 | 17 | private function throwUsageExceptions( $result ) { |
|
332 | |||
333 | /** |
||
334 | * @since 0.1 |
||
335 | * |
||
336 | * @return bool|string false or the name of the current user |
||
337 | */ |
||
338 | 17 | public function isLoggedin() { |
|
341 | |||
342 | /** |
||
343 | * @since 0.1 |
||
344 | * |
||
345 | * @param ApiUser $apiUser |
||
346 | * |
||
347 | * @throws UsageException |
||
348 | * @return bool success |
||
349 | */ |
||
350 | 2 | public function login( ApiUser $apiUser ) { |
|
367 | |||
368 | /** |
||
369 | * @param ApiUser $apiUser |
||
370 | * |
||
371 | * @return string[] |
||
372 | */ |
||
373 | 2 | private function getLoginParams( ApiUser $apiUser ) { |
|
384 | |||
385 | /** |
||
386 | * @param array $result |
||
387 | * |
||
388 | * @throws UsageException |
||
389 | */ |
||
390 | 1 | private function throwLoginUsageException( $result ) { |
|
399 | |||
400 | /** |
||
401 | * @param string $loginResult |
||
402 | * |
||
403 | * @return string |
||
404 | */ |
||
405 | 1 | private function getLoginExceptionMessage( $loginResult ) { |
|
427 | |||
428 | /** |
||
429 | * @since 0.1 |
||
430 | * |
||
431 | * @return bool success |
||
432 | */ |
||
433 | 2 | public function logout() { |
|
443 | |||
444 | /** |
||
445 | * @since 0.1 |
||
446 | * |
||
447 | * @param string $type |
||
448 | * |
||
449 | * @return string |
||
450 | */ |
||
451 | 2 | public function getToken( $type = 'csrf' ) { |
|
454 | |||
455 | /** |
||
456 | * @since 0.1 |
||
457 | * |
||
458 | * Clears all tokens stored by the api |
||
459 | */ |
||
460 | 1 | public function clearTokens() { |
|
463 | |||
464 | /** |
||
465 | * @return string |
||
466 | */ |
||
467 | 4 | public function getVersion(){ |
|
482 | |||
483 | } |
||
484 |