Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Client implements ClientInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Constant for authentication method. Indicates the default, but deprecated |
||
| 37 | * login with username and token in URL. |
||
| 38 | */ |
||
| 39 | const AUTH_URL_TOKEN = 'url_token'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constant for authentication method. Not indicates the new login, but allows |
||
| 43 | * usage of unauthenticated rate limited requests for given client_id + client_secret |
||
| 44 | */ |
||
| 45 | const AUTH_URL_CLIENT_ID = 'url_client_id'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constant for authentication method. Indicates the new favored login method |
||
| 49 | * with username and password via HTTP Authentication. |
||
| 50 | */ |
||
| 51 | const AUTH_HTTP_PASSWORD = 'http_password'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Constant for authentication method. Indicates the new login method with |
||
| 55 | * with username and token via HTTP Authentication. |
||
| 56 | */ |
||
| 57 | const AUTH_HTTP_TOKEN = 'http_token'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | private $options = array( |
||
| 63 | 'base_url' => 'https://api.trello.com/', |
||
| 64 | 'user_agent' => 'php-trello-api (http://github.com/cdaguerre/php-trello-api)', |
||
| 65 | 'timeout' => 10, |
||
| 66 | 'api_limit' => 5000, |
||
| 67 | 'api_version' => 1, |
||
| 68 | 'cache_dir' => null, |
||
| 69 | ); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The Buzz instance used to communicate with Trello |
||
| 73 | * |
||
| 74 | * @var HttpClientInterface |
||
| 75 | */ |
||
| 76 | private $httpClient; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Instantiate a new Trello client |
||
| 80 | * |
||
| 81 | * @param null|HttpClientInterface $httpClient Trello http client |
||
| 82 | */ |
||
| 83 | 347 | public function __construct(HttpClientInterface $httpClient = null) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Get an API by name |
||
| 90 | * |
||
| 91 | * @param string $name |
||
| 92 | * |
||
| 93 | * @return ApiInterface |
||
| 94 | * |
||
| 95 | * @throws InvalidArgumentException if the requested api does not exist |
||
| 96 | */ |
||
| 97 | 42 | public function api($name) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Authenticate a user for all next requests |
||
| 149 | * |
||
| 150 | * @param string $tokenOrLogin Trello private token/username/client ID |
||
| 151 | * @param null|string $password Trello password/secret (optionally can contain $authMethod) |
||
| 152 | * @param null|string $authMethod One of the AUTH_* class constants |
||
| 153 | * |
||
| 154 | * @throws InvalidArgumentException If no authentication method was given |
||
| 155 | */ |
||
| 156 | 7 | public function authenticate($tokenOrLogin, $password = null, $authMethod = null) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Get Http Client |
||
| 176 | * |
||
| 177 | * @return HttpClient |
||
| 178 | */ |
||
| 179 | 15 | public function getHttpClient() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Set Http Client |
||
| 190 | * |
||
| 191 | * @param HttpClientInterface $httpClient |
||
| 192 | */ |
||
| 193 | 294 | public function setHttpClient(HttpClientInterface $httpClient) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Clears used headers |
||
| 200 | */ |
||
| 201 | 1 | public function clearHeaders() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Set headers |
||
| 208 | * |
||
| 209 | * @param array $headers |
||
| 210 | */ |
||
| 211 | 1 | public function setHeaders(array $headers) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Get option by name |
||
| 218 | * |
||
| 219 | * @param string $name the option's name |
||
| 220 | * |
||
| 221 | * @return mixed |
||
| 222 | * |
||
| 223 | * @throws InvalidArgumentException |
||
| 224 | */ |
||
| 225 | public function getOption($name) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Set option |
||
| 236 | * |
||
| 237 | * @param string $name |
||
| 238 | * @param mixed $value |
||
| 239 | * |
||
| 240 | * @throws InvalidArgumentException if the option is not defined |
||
| 241 | * @throws InvalidArgumentException if the api version is set to an unsupported one |
||
| 242 | */ |
||
| 243 | public function setOption($name, $value) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns an array of valid API versions supported by this client. |
||
| 258 | * |
||
| 259 | * @return integer[] |
||
| 260 | */ |
||
| 261 | public function getSupportedApiVersions() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Proxies $this->members() to $this->api('members') |
||
| 268 | * |
||
| 269 | * @param string $name method name |
||
| 270 | * @param array $args arguments |
||
| 271 | * |
||
| 272 | * @return ApiInterface |
||
| 273 | * |
||
| 274 | * @throws BadMethodCallException |
||
| 275 | */ |
||
| 276 | 21 | public function __call($name, $args) |
|
| 284 | } |
||
| 285 |