Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 20 | abstract class Client |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var array Options for the Client object. |
||
| 24 | * @since 1.0 |
||
| 25 | */ |
||
| 26 | protected $options; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array Contains access token key, secret and verifier. |
||
| 30 | * @since 1.0 |
||
| 31 | */ |
||
| 32 | protected $token = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Http The HTTP client object to use in sending HTTP requests. |
||
| 36 | * @since 1.0 |
||
| 37 | */ |
||
| 38 | protected $client; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Input The input object to use in retrieving GET/POST data. |
||
| 42 | * @since 1.0 |
||
| 43 | */ |
||
| 44 | protected $input; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var AbstractWebApplication The application object to send HTTP headers for redirects. |
||
| 48 | * @since 1.0 |
||
| 49 | */ |
||
| 50 | protected $application; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string Selects which version of OAuth to use: 1.0 or 1.0a. |
||
| 54 | * @since 1.0 |
||
| 55 | */ |
||
| 56 | protected $version; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Constructor. |
||
| 60 | * |
||
| 61 | * @param array $options OAuth1 Client options array. |
||
| 62 | * @param Http $client The HTTP client object. |
||
| 63 | * @param Input $input The input object |
||
| 64 | * @param AbstractWebApplication $application The application object |
||
| 65 | * @param string $version Specify the OAuth version. By default we are using 1.0a. |
||
| 66 | * |
||
| 67 | * @since 1.0 |
||
| 68 | */ |
||
| 69 | public function __construct($options = array(), Http $client, Input $input, AbstractWebApplication $application, $version = '1.0a') |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Method to form the oauth flow. |
||
| 80 | * |
||
| 81 | * @return string The access token. |
||
| 82 | * |
||
| 83 | * @since 1.0 |
||
| 84 | * @throws \DomainException |
||
| 85 | */ |
||
| 86 | public function authenticate() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Method used to get a request token. |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | * |
||
| 155 | * @since __DEPLOY_VERSION__ |
||
| 156 | * @throws \DomainException |
||
| 157 | */ |
||
| 158 | private function generateRequestToken() |
||
| 159 | { |
||
| 160 | // Set the callback URL. |
||
| 161 | if ($this->getOption('callback')) |
||
| 162 | { |
||
| 163 | $parameters = array( |
||
| 164 | 'oauth_callback' => $this->getOption('callback') |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | else |
||
| 168 | { |
||
| 169 | $parameters = array(); |
||
| 170 | } |
||
| 171 | |||
| 172 | // Make an OAuth request for the Request Token. |
||
| 173 | $response = $this->oauthRequest($this->getOption('requestTokenURL'), 'POST', $parameters); |
||
| 174 | |||
| 175 | parse_str($response->body, $params); |
||
| 176 | |||
| 177 | if (strcmp($this->version, '1.0a') === 0 && strcmp($params['oauth_callback_confirmed'], 'true') !== 0) |
||
| 178 | { |
||
| 179 | throw new \DomainException('Bad request token!'); |
||
| 180 | } |
||
| 181 | |||
| 182 | // Save the request token. |
||
| 183 | $this->token = array('key' => $params['oauth_token'], 'secret' => $params['oauth_token_secret']); |
||
| 184 | |||
| 185 | // Save the request token in session |
||
| 186 | $session = $this->application->getSession(); |
||
| 187 | $session->set('oauth_token.key', $this->token['key']); |
||
| 188 | $session->set('oauth_token.secret', $this->token['secret']); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Method used to authorise the application. |
||
| 193 | * |
||
| 194 | * @return void |
||
| 195 | * |
||
| 196 | * @since __DEPLOY_VERSION__ |
||
| 197 | */ |
||
| 198 | private function authorise() |
||
| 199 | { |
||
| 200 | $url = $this->getOption('authoriseURL') . '?oauth_token=' . $this->token['key']; |
||
| 201 | |||
| 202 | if ($this->getOption('scope')) |
||
| 203 | { |
||
| 204 | $scope = is_array($this->getOption('scope')) ? implode(' ', $this->getOption('scope')) : $this->getOption('scope'); |
||
| 205 | $url .= '&scope=' . urlencode($scope); |
||
| 206 | } |
||
| 207 | |||
| 208 | if ($this->getOption('sendheaders')) |
||
| 209 | { |
||
| 210 | $this->application->redirect($url); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Method used to get an access token. |
||
| 216 | * |
||
| 217 | * @return void |
||
| 218 | * |
||
| 219 | * @since __DEPLOY_VERSION__ |
||
| 220 | */ |
||
| 221 | private function generateAccessToken() |
||
| 222 | { |
||
| 223 | // Set the parameters. |
||
| 224 | $parameters = array( |
||
| 225 | 'oauth_token' => $this->token['key'] |
||
| 226 | ); |
||
| 227 | |||
| 228 | if (strcmp($this->version, '1.0a') === 0) |
||
| 229 | { |
||
| 230 | $parameters = array_merge($parameters, array('oauth_verifier' => $this->token['verifier'])); |
||
| 231 | } |
||
| 232 | |||
| 233 | // Make an OAuth request for the Access Token. |
||
| 234 | $response = $this->oauthRequest($this->getOption('accessTokenURL'), 'POST', $parameters); |
||
| 235 | |||
| 236 | parse_str($response->body, $params); |
||
| 237 | |||
| 238 | // Save the access token. |
||
| 239 | $this->token = array('key' => $params['oauth_token'], 'secret' => $params['oauth_token_secret']); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Method used to make an OAuth request. |
||
| 244 | * |
||
| 245 | * @param string $url The request URL. |
||
| 246 | * @param string $method The request method. |
||
| 247 | * @param array $parameters Array containing request parameters. |
||
| 248 | * @param mixed $data The POST request data. |
||
| 249 | * @param array $headers An array of name-value pairs to include in the header of the request |
||
| 250 | * |
||
| 251 | * @return object The Response object. |
||
| 252 | * |
||
| 253 | * @since 1.0 |
||
| 254 | * @throws \DomainException |
||
| 255 | */ |
||
| 256 | public function oauthRequest($url, $method, $parameters, $data = array(), $headers = array()) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Method to validate a response. |
||
| 318 | * |
||
| 319 | * @param string $url The request URL. |
||
| 320 | * @param Response $response The response to validate. |
||
| 321 | * |
||
| 322 | * @return void |
||
| 323 | * |
||
| 324 | * @since 1.0 |
||
| 325 | * @throws \DomainException |
||
| 326 | */ |
||
| 327 | abstract public function validateResponse($url, $response); |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Method used to create the header for the POST request. |
||
| 331 | * |
||
| 332 | * @param array $parameters Array containing request parameters. |
||
| 333 | * |
||
| 334 | * @return string The header. |
||
| 335 | * |
||
| 336 | * @since __DEPLOY_VERSION__ |
||
| 337 | */ |
||
| 338 | private function createHeader($parameters) |
||
| 339 | { |
||
| 340 | $header = 'OAuth '; |
||
| 341 | |||
| 342 | foreach ($parameters as $key => $value) |
||
| 343 | { |
||
| 344 | if (!strcmp($header, 'OAuth ')) |
||
| 345 | { |
||
| 346 | $header .= $key . '="' . $this->safeEncode($value) . '"'; |
||
| 347 | } |
||
| 348 | else |
||
| 349 | { |
||
| 350 | $header .= ', ' . $key . '="' . $value . '"'; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | return $header; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Method to create the URL formed string with the parameters. |
||
| 359 | * |
||
| 360 | * @param string $url The request URL. |
||
| 361 | * @param array $parameters Array containing request parameters. |
||
| 362 | * |
||
| 363 | * @return string The formed URL. |
||
| 364 | * |
||
| 365 | * @since 1.0 |
||
| 366 | */ |
||
| 367 | public function toUrl($url, $parameters) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Method used to sign requests. |
||
| 408 | * |
||
| 409 | * @param string $url The URL to sign. |
||
| 410 | * @param string $method The request method. |
||
| 411 | * @param array $parameters Array containing request parameters. |
||
| 412 | * |
||
| 413 | * @return array The array containing the request parameters, including signature. |
||
| 414 | * |
||
| 415 | * @since __DEPLOY_VERSION__ |
||
| 416 | */ |
||
| 417 | private function signRequest($url, $method, $parameters) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Prepare the signature base string. |
||
| 433 | * |
||
| 434 | * @param string $url The URL to sign. |
||
| 435 | * @param string $method The request method. |
||
| 436 | * @param array $parameters Array containing request parameters. |
||
| 437 | * |
||
| 438 | * @return string The base string. |
||
| 439 | * |
||
| 440 | * @since __DEPLOY_VERSION__ |
||
| 441 | */ |
||
| 442 | private function baseString($url, $method, $parameters) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Encodes the string or array passed in a way compatible with OAuth. |
||
| 483 | * If an array is passed each array value will will be encoded. |
||
| 484 | * |
||
| 485 | * @param mixed $data The scalar or array to encode. |
||
| 486 | * |
||
| 487 | * @return string $data encoded in a way compatible with OAuth. |
||
| 488 | * |
||
| 489 | * @since 1.0 |
||
| 490 | */ |
||
| 491 | public function safeEncode($data) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Method used to generate the current nonce. |
||
| 512 | * |
||
| 513 | * @return string The current nonce. |
||
| 514 | * |
||
| 515 | * @since 1.0 |
||
| 516 | */ |
||
| 517 | public static function generateNonce() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Prepares the OAuth signing key. |
||
| 528 | * |
||
| 529 | * @return string The prepared signing key. |
||
| 530 | * |
||
| 531 | * @since __DEPLOY_VERSION__ |
||
| 532 | */ |
||
| 533 | private function prepareSigningKey() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful; |
||
| 540 | * returns a 401 status code and an error message if not. |
||
| 541 | * |
||
| 542 | * @return array The decoded JSON response |
||
| 543 | * |
||
| 544 | * @since 1.0 |
||
| 545 | */ |
||
| 546 | abstract public function verifyCredentials(); |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Get an option from the OAuth1 Client instance. |
||
| 550 | * |
||
| 551 | * @param string $key The name of the option to get |
||
| 552 | * |
||
| 553 | * @return mixed The option value |
||
| 554 | * |
||
| 555 | * @since 1.0 |
||
| 556 | */ |
||
| 557 | public function getOption($key) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Set an option for the OAuth1 Client instance. |
||
| 564 | * |
||
| 565 | * @param string $key The name of the option to set |
||
| 566 | * @param mixed $value The option value to set |
||
| 567 | * |
||
| 568 | * @return Client This object for method chaining |
||
| 569 | * |
||
| 570 | * @since 1.0 |
||
| 571 | */ |
||
| 572 | public function setOption($key, $value) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Get the oauth token key or secret. |
||
| 581 | * |
||
| 582 | * @return array The oauth token key and secret. |
||
| 583 | * |
||
| 584 | * @since 1.0 |
||
| 585 | */ |
||
| 586 | public function getToken() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Set the oauth token. |
||
| 593 | * |
||
| 594 | * @param array $token The access token key and secret. |
||
| 595 | * |
||
| 596 | * @return Client This object for method chaining. |
||
| 597 | * |
||
| 598 | * @since 1.0 |
||
| 599 | */ |
||
| 600 | public function setToken($token) |
||
| 606 | } |
||
| 607 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.