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 |
||
21 | class MediawikiApi implements LoggerAwareInterface { |
||
22 | |||
23 | /** |
||
24 | * @var ClientInterface |
||
25 | */ |
||
26 | private $client; |
||
27 | |||
28 | /** |
||
29 | * @var bool|string |
||
30 | */ |
||
31 | private $isLoggedIn; |
||
32 | |||
33 | /** |
||
34 | * @var MediawikiSession |
||
35 | */ |
||
36 | private $session; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $version; |
||
42 | |||
43 | /** |
||
44 | * @var LoggerInterface |
||
45 | */ |
||
46 | private $logger; |
||
47 | |||
48 | /** |
||
49 | * @param string $apiUrl The API Url |
||
50 | * @param ClientInterface|null $client Guzzle Client |
||
51 | 23 | * @param MediawikiSession|null $session Inject a custom session here |
|
52 | 23 | */ |
|
53 | 4 | public function __construct( $apiUrl, ClientInterface $client = null, MediawikiSession $session = null ) { |
|
70 | |||
71 | /** |
||
72 | * Sets a logger instance on the object |
||
73 | * |
||
74 | * @since 1.1 |
||
75 | * |
||
76 | * @param LoggerInterface $logger |
||
77 | * |
||
78 | * @return null |
||
79 | */ |
||
80 | public function setLogger( LoggerInterface $logger ) { |
||
84 | |||
85 | /** |
||
86 | * @since 2.0 |
||
87 | * |
||
88 | 8 | * @param Request $request |
|
89 | 8 | * |
|
90 | 8 | * @return PromiseInterface |
|
91 | * Normally promising an array, though can be mixed (json_decode result) |
||
92 | 8 | * Can throw UsageExceptions or RejectionExceptions |
|
93 | 8 | */ |
|
94 | public function getRequestAsync( Request $request ) { |
||
104 | 8 | ||
105 | 8 | /** |
|
106 | * @since 2.0 |
||
107 | 8 | * |
|
108 | 8 | * @param Request $request |
|
109 | * |
||
110 | 7 | * @return PromiseInterface |
|
111 | * Normally promising an array, though can be mixed (json_decode result) |
||
112 | * Can throw UsageExceptions or RejectionExceptions |
||
113 | */ |
||
114 | public function postRequestAsync( Request $request ) { |
||
124 | |||
125 | /** |
||
126 | * @since 0.2 |
||
127 | * |
||
128 | * @param Request $request |
||
129 | * |
||
130 | * @return mixed Normally an array |
||
131 | */ |
||
132 | 8 | public function getRequest( Request $request ) { |
|
140 | |||
141 | /** |
||
142 | * @since 0.2 |
||
143 | * |
||
144 | * @param Request $request |
||
145 | * |
||
146 | 16 | * @return mixed Normally an array |
|
147 | */ |
||
148 | 16 | public function postRequest( Request $request ) { |
|
156 | 16 | ||
157 | /** |
||
158 | 16 | * @param ResponseInterface $response |
|
159 | 16 | * |
|
160 | * @return mixed |
||
161 | * @throws UsageException |
||
162 | 16 | */ |
|
163 | 16 | private function decodeResponse( ResponseInterface $response ) { |
|
171 | |||
172 | /** |
||
173 | 16 | * @param Request $request |
|
174 | 16 | * @param string $paramsKey either 'query' or 'form_params' |
|
175 | * |
||
176 | * @throws RequestException |
||
177 | * |
||
178 | * @return array as needed by ClientInterface::get and ClientInterface::post |
||
179 | 16 | */ |
|
180 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
||
186 | 16 | ||
187 | 16 | /** |
|
188 | 2 | * @return array |
|
189 | 2 | */ |
|
190 | 2 | private function getDefaultHeaders() { |
|
195 | |||
196 | private function getUserAgent() { |
||
203 | |||
204 | /** |
||
205 | * @param $result |
||
206 | */ |
||
207 | private function logWarnings( $result ) { |
||
214 | 2 | ||
215 | /** |
||
216 | * @param array $result |
||
217 | 2 | * |
|
218 | 2 | * @throws UsageException |
|
219 | 2 | */ |
|
220 | private function throwUsageExceptions( $result ) { |
||
229 | 2 | ||
230 | 1 | /** |
|
231 | 1 | * @since 0.1 |
|
232 | * |
||
233 | * @return bool|string false or the name of the current user |
||
234 | 1 | */ |
|
235 | 1 | public function isLoggedin() { |
|
238 | |||
239 | /** |
||
240 | * @since 0.1 |
||
241 | * |
||
242 | * @param ApiUser $apiUser |
||
243 | * |
||
244 | 1 | * @throws UsageException |
|
245 | 1 | * @return bool success |
|
246 | */ |
||
247 | 1 | public function login( ApiUser $apiUser ) { |
|
272 | |||
273 | /** |
||
274 | * @param array $result |
||
275 | * |
||
276 | * @throws UsageException |
||
277 | 1 | */ |
|
278 | private function throwLoginUsageException( $result ) { |
||
337 | |||
338 | /** |
||
339 | * @since 0.1 |
||
340 | * @return bool success |
||
341 | 4 | */ |
|
342 | 4 | public function logout() { |
|
352 | 4 | ||
353 | 4 | /** |
|
354 | 4 | * @since 0.1 |
|
355 | * |
||
356 | * @param string $type |
||
357 | * |
||
358 | * @return string |
||
359 | */ |
||
360 | public function getToken( $type = 'csrf' ) { |
||
363 | |||
364 | /** |
||
365 | * @since 0.1 |
||
366 | * Clears all tokens stored by the api |
||
367 | */ |
||
368 | public function clearTokens() { |
||
371 | |||
372 | /** |
||
373 | * @return string |
||
374 | */ |
||
375 | public function getVersion(){ |
||
390 | |||
391 | } |
||
392 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: