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 | 2 | public static function newFromPage( $url ) { |
|
80 | 2 | $tempClient = new Client( array( 'headers' => array( 'User-Agent' => 'addwiki-mediawiki-client' ) ) ); |
|
81 | 2 | $pageXml = new SimpleXMLElement( $tempClient->get( $url )->getBody() ); |
|
82 | 2 | $rsdElement = $pageXml->xpath( 'head/link[@type="application/rsd+xml"][@href]' ); |
|
83 | 2 | $rsdXml = new SimpleXMLElement( $tempClient->get( $rsdElement[0]->attributes()['href'] )->getBody() ); |
|
84 | 2 | return self::newFromApiEndpoint( $rsdXml->service->apis->api->attributes()->apiLink->__toString() ); |
|
85 | } |
||
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 | 46 | public function __construct( $apiUrl, Client $client = null, MediawikiSession $session = null ) { |
|
108 | |||
109 | /** |
||
110 | * @return Client |
||
111 | */ |
||
112 | 40 | private function getClient() { |
|
113 | 40 | if( $this->client === null ) { |
|
114 | 8 | $middlewareFactory = new MiddlewareFactory(); |
|
115 | 8 | $middlewareFactory->setLogger( $this->logger ); |
|
116 | |||
117 | 8 | $handlerStack = HandlerStack::create( new CurlHandler() ); |
|
118 | 8 | $handlerStack->push( $middlewareFactory->retry() ); |
|
119 | |||
120 | 8 | $this->client = new Client( array( |
|
121 | 8 | 'cookies' => true, |
|
122 | 8 | 'handler' => $handlerStack, |
|
123 | 8 | ) ); |
|
124 | 8 | } |
|
125 | 40 | return $this->client; |
|
126 | } |
||
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 | 2 | public function getRequestAsync( Request $request ) { |
|
152 | 2 | $promise = $this->getClient()->getAsync( |
|
153 | 2 | $this->apiUrl, |
|
154 | 2 | $this->getClientRequestOptions( $request, 'query' ) |
|
155 | 2 | ); |
|
156 | |||
157 | return $promise->then( function( ResponseInterface $response ) { |
||
158 | 2 | return call_user_func( array( $this, 'decodeResponse' ), $response ); |
|
159 | 2 | } ); |
|
160 | } |
||
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 | 2 | public function postRequestAsync( Request $request ) { |
|
172 | 2 | $promise = $this->getClient()->postAsync( |
|
173 | 2 | $this->apiUrl, |
|
174 | 2 | $this->getClientRequestOptions( $request, 'form_params' ) |
|
175 | 2 | ); |
|
176 | |||
177 | 2 | return $promise->then( function( ResponseInterface $response ) { |
|
178 | 2 | return call_user_func( array( $this, 'decodeResponse' ), $response ); |
|
179 | 2 | } ); |
|
180 | } |
||
181 | |||
182 | /** |
||
183 | * @since 0.2 |
||
184 | * |
||
185 | * @param Request $request |
||
186 | * |
||
187 | * @return mixed Normally an array |
||
188 | */ |
||
189 | 18 | 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 | 18 | public function postRequest( Request $request ) { |
|
213 | |||
214 | /** |
||
215 | * @param ResponseInterface $response |
||
216 | * |
||
217 | * @return mixed |
||
218 | * @throws UsageException |
||
219 | */ |
||
220 | 40 | 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 | 40 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
243 | |||
244 | /** |
||
245 | * @return array |
||
246 | */ |
||
247 | 32 | private function getDefaultHeaders() { |
|
252 | |||
253 | 32 | private function getUserAgent() { |
|
260 | |||
261 | /** |
||
262 | * @param $result |
||
263 | */ |
||
264 | 32 | private function logWarnings( $result ) { |
|
271 | |||
272 | /** |
||
273 | * @param array $result |
||
274 | * |
||
275 | * @throws UsageException |
||
276 | */ |
||
277 | 32 | 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 | 32 | public function isLoggedin() { |
|
295 | |||
296 | /** |
||
297 | * @since 0.1 |
||
298 | * |
||
299 | * @param ApiUser $apiUser |
||
300 | * |
||
301 | * @throws UsageException |
||
302 | * @return bool success |
||
303 | */ |
||
304 | 4 | public function login( ApiUser $apiUser ) { |
|
320 | |||
321 | /** |
||
322 | * @param ApiUser $apiUser |
||
323 | * |
||
324 | * @return string[] |
||
325 | */ |
||
326 | 4 | private function getLoginParams( ApiUser $apiUser ) { |
|
337 | |||
338 | /** |
||
339 | * @param array $result |
||
340 | * |
||
341 | * @throws UsageException |
||
342 | */ |
||
343 | 2 | private function throwLoginUsageException( $result ) { |
|
352 | |||
353 | /** |
||
354 | * @param string $loginResult |
||
355 | * |
||
356 | * @return string |
||
357 | */ |
||
358 | 2 | private function getLoginExceptionMessage( $loginResult ) { |
|
380 | |||
381 | /** |
||
382 | * @since 0.1 |
||
383 | * |
||
384 | * @return bool success |
||
385 | */ |
||
386 | 4 | public function logout() { |
|
396 | |||
397 | /** |
||
398 | * @since 0.1 |
||
399 | * |
||
400 | * @param string $type |
||
401 | * |
||
402 | * @return string |
||
403 | */ |
||
404 | 4 | public function getToken( $type = 'csrf' ) { |
|
405 | 4 | return $this->session->getToken( $type ); |
|
406 | } |
||
407 | |||
408 | /** |
||
409 | * @since 0.1 |
||
410 | * |
||
411 | * Clears all tokens stored by the api |
||
412 | */ |
||
413 | 2 | public function clearTokens() { |
|
416 | |||
417 | /** |
||
418 | * @return string |
||
419 | */ |
||
420 | 8 | public function getVersion(){ |
|
435 | |||
436 | } |
||
437 |