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 LoggerAwareInterface { |
||
26 | |||
27 | /** |
||
28 | * @var Client|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 | * @since 2.0.0 |
||
54 | * |
||
55 | * @param string $apiEndpoint e.g. https://en.wikipedia.org/w/api.php |
||
56 | * |
||
57 | 23 | * @return self returns a MediawikiApi instance using $apiEndpoint |
|
58 | 23 | */ |
|
59 | 4 | public static function newFromApiEndpoint( $apiEndpoint ) { |
|
62 | 19 | ||
63 | 19 | /** |
|
64 | * @since 2.0.0 |
||
65 | 19 | * |
|
66 | 16 | * @param string $url e.g. https://en.wikipedia.org OR https://de.wikipedia.org/wiki/Berlin |
|
67 | * |
||
68 | * @return self returns a MediawikiApi instance using the apiEndpoint provided by the RSD |
||
69 | 16 | * file accessible on all Mediawiki pages |
|
70 | * |
||
71 | 19 | * @see https://en.wikipedia.org/wiki/Really_Simple_Discovery |
|
72 | 19 | */ |
|
73 | 19 | public static function newFromPage( $url ) { |
|
76 | 19 | ||
77 | /** |
||
78 | * @access private |
||
79 | * |
||
80 | * @param string $apiUrl The API Url |
||
81 | 16 | * @param Client|null $client Guzzle Client |
|
82 | 16 | * @param MediawikiSession|null $session Inject a custom session here |
|
83 | */ |
||
84 | public function __construct( $apiUrl, Client $client = null, MediawikiSession $session = null ) { |
||
98 | |||
99 | /** |
||
100 | * @return Client |
||
101 | */ |
||
102 | private function getClient() { |
||
117 | |||
118 | /** |
||
119 | * Sets a logger instance on the object |
||
120 | * |
||
121 | * @since 1.1 |
||
122 | * |
||
123 | * @param LoggerInterface $logger |
||
124 | * |
||
125 | * @return null |
||
126 | */ |
||
127 | public function setLogger( LoggerInterface $logger ) { |
||
131 | |||
132 | /** |
||
133 | * @since 2.0 |
||
134 | * |
||
135 | * @param Request $request |
||
136 | * |
||
137 | * @return PromiseInterface |
||
138 | * Normally promising an array, though can be mixed (json_decode result) |
||
139 | * Can throw UsageExceptions or RejectionExceptions |
||
140 | */ |
||
141 | public function getRequestAsync( Request $request ) { |
||
151 | |||
152 | /** |
||
153 | * @since 2.0 |
||
154 | * |
||
155 | * @param Request $request |
||
156 | * |
||
157 | * @return PromiseInterface |
||
158 | 8 | * Normally promising an array, though can be mixed (json_decode result) |
|
159 | 8 | * Can throw UsageExceptions or RejectionExceptions |
|
160 | 8 | */ |
|
161 | 8 | public function postRequestAsync( Request $request ) { |
|
171 | |||
172 | /** |
||
173 | * @since 0.2 |
||
174 | 8 | * |
|
175 | 8 | * @param Request $request |
|
176 | 8 | * |
|
177 | 8 | * @return mixed Normally an array |
|
178 | 8 | */ |
|
179 | public function getRequest( Request $request ) { |
||
187 | |||
188 | /** |
||
189 | 16 | * @since 0.2 |
|
190 | 16 | * |
|
191 | * @param Request $request |
||
192 | 16 | * |
|
193 | 16 | * @return mixed Normally an array |
|
194 | */ |
||
195 | 14 | public function postRequest( Request $request ) { |
|
203 | |||
204 | /** |
||
205 | * @param ResponseInterface $response |
||
206 | 16 | * |
|
207 | * @return mixed |
||
208 | 16 | * @throws UsageException |
|
209 | 16 | */ |
|
210 | 16 | private function decodeResponse( ResponseInterface $response ) { |
|
218 | 16 | ||
219 | 16 | /** |
|
220 | * @param Request $request |
||
221 | * @param string $paramsKey either 'query' or 'form_params' |
||
222 | 16 | * |
|
223 | 16 | * @throws RequestException |
|
224 | 16 | * |
|
225 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
226 | */ |
||
227 | 16 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
233 | 16 | ||
234 | 16 | /** |
|
235 | * @return array |
||
236 | */ |
||
237 | private function getDefaultHeaders() { |
||
242 | |||
243 | private function getUserAgent() { |
||
250 | 2 | ||
251 | /** |
||
252 | 2 | * @param $result |
|
253 | */ |
||
254 | 14 | private function logWarnings( $result ) { |
|
261 | 16 | ||
262 | 16 | /** |
|
263 | * @param array $result |
||
264 | * |
||
265 | * @throws UsageException |
||
266 | */ |
||
267 | private function throwUsageExceptions( $result ) { |
||
276 | |||
277 | 2 | /** |
|
278 | 2 | * @since 0.1 |
|
279 | 2 | * |
|
280 | * @return bool|string false or the name of the current user |
||
281 | 2 | */ |
|
282 | public function isLoggedin() { |
||
285 | 2 | ||
286 | 2 | /** |
|
287 | 2 | * @since 0.1 |
|
288 | 2 | * |
|
289 | 2 | * @param ApiUser $apiUser |
|
290 | 1 | * |
|
291 | 1 | * @throws UsageException |
|
292 | * @return bool success |
||
293 | */ |
||
294 | 1 | public function login( ApiUser $apiUser ) { |
|
319 | 1 | ||
320 | /** |
||
321 | * @param array $result |
||
322 | * |
||
323 | * @throws UsageException |
||
324 | */ |
||
325 | 1 | private function throwLoginUsageException( $result ) { |
|
384 | |||
385 | /** |
||
386 | * @since 0.1 |
||
387 | * |
||
388 | * @return bool success |
||
389 | */ |
||
390 | public function logout() { |
||
400 | |||
401 | 4 | /** |
|
402 | 4 | * @since 0.1 |
|
403 | 4 | * |
|
404 | 4 | * @param string $type |
|
405 | 4 | * |
|
406 | 4 | * @return string |
|
407 | 4 | */ |
|
408 | 4 | public function getToken( $type = 'csrf' ) { |
|
411 | 4 | ||
412 | 4 | /** |
|
413 | 4 | * @since 0.1 |
|
414 | 4 | * |
|
415 | * Clears all tokens stored by the api |
||
416 | */ |
||
417 | public function clearTokens() { |
||
420 | |||
421 | /** |
||
422 | * @return string |
||
423 | */ |
||
424 | public function getVersion(){ |
||
439 | |||
440 | } |
||
441 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.