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 |
||
26 | class MediawikiApi implements LoggerAwareInterface { |
||
27 | |||
28 | /** |
||
29 | * @var Client|null Should be accessed through getClient |
||
30 | */ |
||
31 | private $client = null; |
||
32 | |||
33 | /** |
||
34 | * @var bool|string |
||
35 | */ |
||
36 | private $isLoggedIn; |
||
37 | |||
38 | /** |
||
39 | * @var MediawikiSession |
||
40 | */ |
||
41 | private $session; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $version; |
||
47 | |||
48 | /** |
||
49 | * @var LoggerInterface |
||
50 | */ |
||
51 | private $logger; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | private $apiUrl; |
||
57 | |||
58 | /** |
||
59 | * @since 2.0.0 |
||
60 | * |
||
61 | * @param string $apiEndpoint e.g. https://en.wikipedia.org/w/api.php |
||
62 | * |
||
63 | * @return self returns a MediawikiApi instance using $apiEndpoint |
||
64 | */ |
||
65 | public static function newFromApiEndpoint( $apiEndpoint ) { |
||
68 | |||
69 | /** |
||
70 | * @since 2.0.0 |
||
71 | * |
||
72 | * @param string $url e.g. https://en.wikipedia.org OR https://de.wikipedia.org/wiki/Berlin |
||
73 | * |
||
74 | * @return self returns a MediawikiApi instance using the apiEndpoint provided by the RSD |
||
75 | * file accessible on all Mediawiki pages |
||
76 | * |
||
77 | * @see https://en.wikipedia.org/wiki/Really_Simple_Discovery |
||
78 | */ |
||
79 | public static function newFromPage( $url ) { |
||
86 | |||
87 | /** |
||
88 | * @access private |
||
89 | * |
||
90 | * @param string $apiUrl The API Url |
||
91 | * @param Client|null $client Guzzle Client |
||
92 | * @param MediawikiSession|null $session Inject a custom session here |
||
93 | */ |
||
94 | 23 | public function __construct( $apiUrl, Client $client = null, MediawikiSession $session = null ) { |
|
108 | |||
109 | /** |
||
110 | * @return Client |
||
111 | */ |
||
112 | 16 | private function getClient() { |
|
127 | |||
128 | /** |
||
129 | * Sets a logger instance on the object |
||
130 | * |
||
131 | * @since 1.1 |
||
132 | * |
||
133 | * @param LoggerInterface $logger |
||
134 | * |
||
135 | * @return null |
||
136 | */ |
||
137 | public function setLogger( LoggerInterface $logger ) { |
||
141 | |||
142 | /** |
||
143 | * @since 2.0 |
||
144 | * |
||
145 | * @param Request $request |
||
146 | * |
||
147 | * @return PromiseInterface |
||
148 | * Normally promising an array, though can be mixed (json_decode result) |
||
149 | * Can throw UsageExceptions or RejectionExceptions |
||
150 | */ |
||
151 | 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 | public function postRequestAsync( Request $request ) { |
||
181 | |||
182 | /** |
||
183 | * @since 0.2 |
||
184 | * |
||
185 | * @param Request $request |
||
186 | * |
||
187 | * @return mixed Normally an array |
||
188 | */ |
||
189 | 8 | public function getRequest( Request $request ) { |
|
197 | |||
198 | /** |
||
199 | * @since 0.2 |
||
200 | * |
||
201 | * @param Request $request |
||
202 | * |
||
203 | * @return mixed Normally an array |
||
204 | */ |
||
205 | 8 | public function postRequest( Request $request ) { |
|
213 | |||
214 | /** |
||
215 | * @param ResponseInterface $response |
||
216 | * |
||
217 | * @return mixed |
||
218 | * @throws UsageException |
||
219 | */ |
||
220 | 16 | private function decodeResponse( ResponseInterface $response ) { |
|
228 | |||
229 | /** |
||
230 | * @param Request $request |
||
231 | * @param string $paramsKey either 'query' or 'form_params' |
||
232 | * |
||
233 | * @throws RequestException |
||
234 | * |
||
235 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
236 | */ |
||
237 | 16 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
243 | |||
244 | /** |
||
245 | * @return array |
||
246 | */ |
||
247 | 16 | private function getDefaultHeaders() { |
|
252 | |||
253 | 16 | private function getUserAgent() { |
|
260 | |||
261 | /** |
||
262 | * @param $result |
||
263 | */ |
||
264 | 16 | private function logWarnings( $result ) { |
|
271 | |||
272 | /** |
||
273 | * @param array $result |
||
274 | * |
||
275 | * @throws UsageException |
||
276 | */ |
||
277 | 16 | private function throwUsageExceptions( $result ) { |
|
286 | |||
287 | /** |
||
288 | * @since 0.1 |
||
289 | * |
||
290 | * @return bool|string false or the name of the current user |
||
291 | */ |
||
292 | 16 | public function isLoggedin() { |
|
295 | |||
296 | /** |
||
297 | * @since 0.1 |
||
298 | * |
||
299 | * @param ApiUser $apiUser |
||
300 | * |
||
301 | * @throws UsageException |
||
302 | * @return bool success |
||
303 | */ |
||
304 | 2 | public function login( ApiUser $apiUser ) { |
|
320 | |||
321 | /** |
||
322 | * @param ApiUser $apiUser |
||
323 | * |
||
324 | * @return string[] |
||
325 | */ |
||
326 | 2 | private function getLoginParams( ApiUser $apiUser ) { |
|
337 | |||
338 | /** |
||
339 | * @param array $result |
||
340 | * |
||
341 | * @throws UsageException |
||
342 | */ |
||
343 | 1 | private function throwLoginUsageException( $result ) { |
|
352 | |||
353 | /** |
||
354 | * @param string $loginResult |
||
355 | * |
||
356 | * @return string |
||
357 | */ |
||
358 | 1 | private function getLoginExceptionMessage( $loginResult ) { |
|
380 | |||
381 | /** |
||
382 | * @since 0.1 |
||
383 | * |
||
384 | * @return bool success |
||
385 | */ |
||
386 | 2 | public function logout() { |
|
396 | |||
397 | /** |
||
398 | * @since 0.1 |
||
399 | * |
||
400 | * @param string $type |
||
401 | * |
||
402 | * @return string |
||
403 | */ |
||
404 | public function getToken( $type = 'csrf' ) { |
||
407 | |||
408 | /** |
||
409 | * @since 0.1 |
||
410 | * |
||
411 | * Clears all tokens stored by the api |
||
412 | */ |
||
413 | 1 | public function clearTokens() { |
|
416 | |||
417 | /** |
||
418 | * @return string |
||
419 | */ |
||
420 | 4 | public function getVersion(){ |
|
435 | |||
436 | } |
||
437 |