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 | * @param MediawikiSession|null $session Inject a custom session here |
||
| 52 | */ |
||
| 53 | 23 | public function __construct( $apiUrl, ClientInterface $client = null, MediawikiSession $session = null ) { |
|
| 54 | 23 | if( !is_string( $apiUrl ) ) { |
|
| 55 | 4 | throw new InvalidArgumentException( '$apiUrl must be a string' ); |
|
| 56 | } |
||
| 57 | 19 | if( $client === null ) { |
|
| 58 | 3 | $client = new Client(); |
|
| 59 | 3 | } |
|
| 60 | 19 | if( $session === null ) { |
|
| 61 | 19 | $session = new MediawikiSession( $this ); |
|
| 62 | 19 | } |
|
| 63 | |||
| 64 | 19 | $this->apiUrl = $apiUrl; |
|
|
|
|||
| 65 | 19 | $this->client = $client; |
|
| 66 | 19 | $this->session = $session; |
|
| 67 | |||
| 68 | 19 | $this->logger = new NullLogger(); |
|
| 69 | 19 | } |
|
| 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 ) { |
||
| 81 | $this->logger = $logger; |
||
| 82 | $this->session->setLogger( $logger ); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @since 2.0 |
||
| 87 | * |
||
| 88 | * @param Request $request |
||
| 89 | * |
||
| 90 | * @return PromiseInterface |
||
| 91 | * Normally promising an array, though can be mixed (json_decode result) |
||
| 92 | * Can throw UsageExceptions or RejectionExceptions |
||
| 93 | */ |
||
| 94 | public function getRequestAsync( Request $request ) { |
||
| 95 | $promise = $this->client->getAsync( |
||
| 96 | $this->apiUrl, |
||
| 97 | $this->getClientRequestOptions( $request, 'query' ) |
||
| 98 | ); |
||
| 99 | |||
| 100 | return $promise->then( function( ResponseInterface $response ) { |
||
| 101 | return call_user_func( array( $this, 'decodeResponse' ), $response ); |
||
| 102 | } ); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @since 2.0 |
||
| 107 | * |
||
| 108 | * @param Request $request |
||
| 109 | * |
||
| 110 | * @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 ) { |
||
| 115 | $promise = $this->client->postAsync( |
||
| 116 | $this->apiUrl, |
||
| 117 | $this->getClientRequestOptions( $request, 'form_params' ) |
||
| 118 | ); |
||
| 119 | |||
| 120 | return $promise->then( function( ResponseInterface $response ) { |
||
| 121 | return call_user_func( array( $this, 'decodeResponse' ), $response ); |
||
| 122 | } ); |
||
| 123 | } |
||
| 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 ) { |
|
| 133 | 8 | $response = $this->client->get( |
|
| 134 | 8 | $this->apiUrl, |
|
| 135 | 8 | $this->getClientRequestOptions( $request, 'query' ) |
|
| 136 | 8 | ); |
|
| 137 | |||
| 138 | 8 | return $this->decodeResponse( $response ); |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @since 0.2 |
||
| 143 | * |
||
| 144 | * @param Request $request |
||
| 145 | * |
||
| 146 | * @return mixed Normally an array |
||
| 147 | */ |
||
| 148 | 8 | public function postRequest( Request $request ) { |
|
| 149 | 8 | $response = $this->client->post( |
|
| 150 | 8 | $this->apiUrl, |
|
| 151 | 8 | $this->getClientRequestOptions( $request, 'form_params' ) |
|
| 152 | 8 | ); |
|
| 153 | |||
| 154 | 8 | return $this->decodeResponse( $response ); |
|
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param ResponseInterface $response |
||
| 159 | * |
||
| 160 | * @return mixed |
||
| 161 | * @throws UsageException |
||
| 162 | */ |
||
| 163 | 16 | private function decodeResponse( ResponseInterface $response ) { |
|
| 164 | 16 | $resultArray = json_decode( $response->getBody(), true ); |
|
| 165 | |||
| 166 | 16 | $this->logWarnings( $resultArray ); |
|
| 167 | 16 | $this->throwUsageExceptions( $resultArray ); |
|
| 168 | |||
| 169 | 14 | return $resultArray; |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param Request $request |
||
| 174 | * @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 | */ |
||
| 180 | 16 | private function getClientRequestOptions( Request $request, $paramsKey ) { |
|
| 181 | return array( |
||
| 182 | 16 | $paramsKey => array_merge( $request->getParams(), array( 'format' => 'json' ) ), |
|
| 183 | 16 | 'headers' => array_merge( $this->getDefaultHeaders(), $request->getHeaders() ), |
|
| 184 | 16 | ); |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | 16 | private function getDefaultHeaders() { |
|
| 195 | |||
| 196 | 16 | private function getUserAgent() { |
|
| 197 | 16 | $loggedIn = $this->isLoggedin(); |
|
| 198 | 16 | if( $loggedIn ) { |
|
| 199 | return 'addwiki-mediawiki-client/' . $loggedIn; |
||
| 200 | } |
||
| 201 | 16 | return 'addwiki-mediawiki-client'; |
|
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param $result |
||
| 206 | */ |
||
| 207 | 16 | private function logWarnings( $result ) { |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @param array $result |
||
| 217 | * |
||
| 218 | * @throws UsageException |
||
| 219 | */ |
||
| 220 | 16 | private function throwUsageExceptions( $result ) { |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @since 0.1 |
||
| 232 | * |
||
| 233 | * @return bool|string false or the name of the current user |
||
| 234 | */ |
||
| 235 | 16 | public function isLoggedin() { |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @since 0.1 |
||
| 241 | * |
||
| 242 | * @param ApiUser $apiUser |
||
| 243 | * |
||
| 244 | * @throws UsageException |
||
| 245 | * @return bool success |
||
| 246 | */ |
||
| 247 | 2 | public function login( ApiUser $apiUser ) { |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @param array $result |
||
| 275 | * |
||
| 276 | * @throws UsageException |
||
| 277 | */ |
||
| 278 | 1 | private function throwLoginUsageException( $result ) { |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @since 0.1 |
||
| 340 | * @return bool success |
||
| 341 | */ |
||
| 342 | 2 | public function logout() { |
|
| 352 | |||
| 353 | /** |
||
| 354 | * @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 | 1 | public function clearTokens() { |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | 4 | 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: