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 ) { |
|
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 | 23 | 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 | * @return string The API URL. |
||
110 | */ |
||
111 | public function getApiUrl() { |
||
114 | |||
115 | /** |
||
116 | * @return ClientInterface |
||
117 | */ |
||
118 | 20 | private function getClient() { |
|
126 | |||
127 | /** |
||
128 | * Sets a logger instance on the object |
||
129 | * |
||
130 | * @since 1.1 |
||
131 | * |
||
132 | * @param LoggerInterface $logger |
||
133 | * |
||
134 | * @return null |
||
135 | */ |
||
136 | public function setLogger( LoggerInterface $logger ) { |
||
140 | |||
141 | /** |
||
142 | * @since 2.0 |
||
143 | * |
||
144 | * @param Request $request |
||
145 | * |
||
146 | * @return PromiseInterface |
||
147 | * Normally promising an array, though can be mixed (json_decode result) |
||
148 | * Can throw UsageExceptions or RejectionExceptions |
||
149 | */ |
||
150 | 1 | public function getRequestAsync( Request $request ) { |
|
161 | |||
162 | /** |
||
163 | * @since 2.0 |
||
164 | * |
||
165 | * @param Request $request |
||
166 | * |
||
167 | * @return PromiseInterface |
||
168 | * Normally promising an array, though can be mixed (json_decode result) |
||
169 | * Can throw UsageExceptions or RejectionExceptions |
||
170 | */ |
||
171 | 1 | public function postRequestAsync( Request $request ) { |
|
182 | |||
183 | /** |
||
184 | * @since 0.2 |
||
185 | * |
||
186 | * @param Request $request |
||
187 | * |
||
188 | * @return mixed Normally an array |
||
189 | */ |
||
190 | 9 | public function getRequest( 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 postRequest( Request $request ) { |
|
216 | |||
217 | /** |
||
218 | * @param ResponseInterface $response |
||
219 | * |
||
220 | * @return mixed |
||
221 | * @throws UsageException |
||
222 | */ |
||
223 | 18 | private function decodeResponse( ResponseInterface $response ) { |
|
231 | |||
232 | /** |
||
233 | * @param Request $request |
||
234 | * @param string $paramsKey either 'query' or 'form_params' |
||
235 | * |
||
236 | * @throws RequestException |
||
237 | * |
||
238 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
239 | */ |
||
240 | 20 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
246 | |||
247 | /** |
||
248 | * @return array |
||
249 | */ |
||
250 | 16 | private function getDefaultHeaders() { |
|
255 | |||
256 | 16 | private function getUserAgent() { |
|
263 | |||
264 | /** |
||
265 | * @param $result |
||
266 | */ |
||
267 | 16 | private function logWarnings( $result ) { |
|
274 | |||
275 | /** |
||
276 | * @param array $result |
||
277 | * |
||
278 | * @throws UsageException |
||
279 | */ |
||
280 | 16 | private function throwUsageExceptions( $result ) { |
|
289 | |||
290 | /** |
||
291 | * @since 0.1 |
||
292 | * |
||
293 | * @return bool|string false or the name of the current user |
||
294 | */ |
||
295 | 16 | public function isLoggedin() { |
|
298 | |||
299 | /** |
||
300 | * @since 0.1 |
||
301 | * |
||
302 | * @param ApiUser $apiUser |
||
303 | * |
||
304 | * @throws UsageException |
||
305 | * @return bool success |
||
306 | */ |
||
307 | 2 | public function login( ApiUser $apiUser ) { |
|
323 | |||
324 | /** |
||
325 | * @param ApiUser $apiUser |
||
326 | * |
||
327 | * @return string[] |
||
328 | */ |
||
329 | 2 | private function getLoginParams( ApiUser $apiUser ) { |
|
340 | |||
341 | /** |
||
342 | * @param array $result |
||
343 | * |
||
344 | * @throws UsageException |
||
345 | */ |
||
346 | 1 | private function throwLoginUsageException( $result ) { |
|
355 | |||
356 | /** |
||
357 | * @param string $loginResult |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | 1 | private function getLoginExceptionMessage( $loginResult ) { |
|
383 | |||
384 | /** |
||
385 | * @since 0.1 |
||
386 | * |
||
387 | * @return bool success |
||
388 | */ |
||
389 | 2 | public function logout() { |
|
399 | |||
400 | /** |
||
401 | * @since 0.1 |
||
402 | * |
||
403 | * @param string $type |
||
404 | * |
||
405 | * @return string |
||
406 | */ |
||
407 | 2 | public function getToken( $type = 'csrf' ) { |
|
410 | |||
411 | /** |
||
412 | * @since 0.1 |
||
413 | * |
||
414 | * Clears all tokens stored by the api |
||
415 | */ |
||
416 | 1 | public function clearTokens() { |
|
419 | |||
420 | /** |
||
421 | * @return string |
||
422 | */ |
||
423 | 4 | public function getVersion(){ |
|
438 | |||
439 | } |
||
440 |