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 | * Options for the Client object. |
||
| 24 | * |
||
| 25 | * @var array|\ArrayAccess |
||
| 26 | * @since 1.0 |
||
| 27 | */ |
||
| 28 | protected $options; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Contains access token key, secret and verifier. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | * @since 1.0 |
||
| 35 | */ |
||
| 36 | protected $token = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The HTTP client object to use in sending HTTP requests. |
||
| 40 | * |
||
| 41 | * @var Http |
||
| 42 | * @since 1.0 |
||
| 43 | */ |
||
| 44 | protected $client; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The input object to use in retrieving GET/POST data. |
||
| 48 | * |
||
| 49 | * @var Input |
||
| 50 | * @since 1.0 |
||
| 51 | */ |
||
| 52 | protected $input; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The application object to send HTTP headers for redirects. |
||
| 56 | * |
||
| 57 | * @var AbstractWebApplication |
||
| 58 | * @since 1.0 |
||
| 59 | */ |
||
| 60 | protected $application; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Selects which version of OAuth to use: 1.0 or 1.0a. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | * @since 1.0 |
||
| 67 | */ |
||
| 68 | protected $version; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Constructor. |
||
| 72 | * |
||
| 73 | * @param AbstractWebApplication $application The application object |
||
| 74 | * @param Http $client The HTTP client object. |
||
| 75 | * @param Input $input The input object |
||
| 76 | * @param array|\ArrayAccess $options OAuth1 Client options. |
||
| 77 | * @param string $version Specify the OAuth version. By default we are using 1.0a. |
||
| 78 | * |
||
| 79 | * @since 1.0 |
||
| 80 | */ |
||
| 81 | public function __construct(AbstractWebApplication $application, Http $client = null, Input $input = null, $options = [], $version = '1.0a') |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Method to form the oauth flow. |
||
| 99 | * |
||
| 100 | * @return string The access token. |
||
| 101 | * |
||
| 102 | * @since 1.0 |
||
| 103 | * @throws \DomainException |
||
| 104 | */ |
||
| 105 | public function authenticate() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Method used to get a request token. |
||
| 168 | * |
||
| 169 | * @return void |
||
| 170 | * |
||
| 171 | * @since 1.0 |
||
| 172 | * @throws \DomainException |
||
| 173 | */ |
||
| 174 | private function generateRequestToken() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Method used to authorise the application. |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | * |
||
| 208 | * @since 1.0 |
||
| 209 | */ |
||
| 210 | private function authorise() |
||
| 211 | { |
||
| 212 | $url = $this->getOption('authoriseURL') . '?oauth_token=' . $this->token['key']; |
||
| 213 | |||
| 214 | if ($this->getOption('scope')) |
||
| 215 | { |
||
| 216 | $scope = is_array($this->getOption('scope')) ? implode(' ', $this->getOption('scope')) : $this->getOption('scope'); |
||
| 217 | $url .= '&scope=' . urlencode($scope); |
||
| 218 | } |
||
| 219 | |||
| 220 | if ($this->getOption('sendheaders')) |
||
| 221 | { |
||
| 222 | $this->application->redirect($url); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Method used to get an access token. |
||
| 228 | * |
||
| 229 | * @return void |
||
| 230 | * |
||
| 231 | * @since 1.0 |
||
| 232 | */ |
||
| 233 | private function generateAccessToken() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Method used to make an OAuth request. |
||
| 256 | * |
||
| 257 | * @param string $url The request URL. |
||
| 258 | * @param string $method The request method. |
||
| 259 | * @param array $parameters Array containing request parameters. |
||
| 260 | * @param mixed $data The POST request data. |
||
| 261 | * @param array $headers An array of name-value pairs to include in the header of the request |
||
| 262 | * |
||
| 263 | * @return \Joomla\Http\Response |
||
| 264 | * |
||
| 265 | * @since 1.0 |
||
| 266 | * @throws \DomainException |
||
| 267 | */ |
||
| 268 | public function oauthRequest($url, $method, $parameters, $data = [], $headers = []) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Method to validate a response. |
||
| 329 | * |
||
| 330 | * @param string $url The request URL. |
||
| 331 | * @param Response $response The response to validate. |
||
| 332 | * |
||
| 333 | * @return void |
||
| 334 | * |
||
| 335 | * @since 1.0 |
||
| 336 | * @throws \DomainException |
||
| 337 | */ |
||
| 338 | abstract public function validateResponse($url, $response); |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Method used to create the header for the POST request. |
||
| 342 | * |
||
| 343 | * @param array $parameters Array containing request parameters. |
||
| 344 | * |
||
| 345 | * @return string The header. |
||
| 346 | * |
||
| 347 | * @since 1.0 |
||
| 348 | */ |
||
| 349 | private function createHeader($parameters) |
||
| 350 | { |
||
| 351 | $header = 'OAuth '; |
||
| 352 | |||
| 353 | foreach ($parameters as $key => $value) |
||
| 354 | { |
||
| 355 | if (!strcmp($header, 'OAuth ')) |
||
| 356 | { |
||
| 357 | $header .= $key . '="' . $this->safeEncode($value) . '"'; |
||
| 358 | } |
||
| 359 | else |
||
| 360 | { |
||
| 361 | $header .= ', ' . $key . '="' . $value . '"'; |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | return $header; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Method to create the URL formed string with the parameters. |
||
| 370 | * |
||
| 371 | * @param string $url The request URL. |
||
| 372 | * @param array $parameters Array containing request parameters. |
||
| 373 | * |
||
| 374 | * @return string The formed URL. |
||
| 375 | * |
||
| 376 | * @since 1.0 |
||
| 377 | */ |
||
| 378 | public function toUrl($url, $parameters) |
||
| 379 | { |
||
| 380 | foreach ($parameters as $key => $value) |
||
| 381 | { |
||
| 382 | if (is_array($value)) |
||
| 383 | { |
||
| 384 | foreach ($value as $k => $v) |
||
| 385 | { |
||
| 386 | if (strpos($url, '?') === false) |
||
| 387 | { |
||
| 388 | $url .= '?'; |
||
| 389 | } |
||
| 390 | else |
||
| 391 | { |
||
| 392 | $url .= '&'; |
||
| 393 | } |
||
| 394 | |||
| 395 | $url .= $key . '=' . $v; |
||
| 396 | } |
||
| 397 | } |
||
| 398 | else |
||
| 399 | { |
||
| 400 | if (strpos($value, ' ') !== false) |
||
| 401 | { |
||
| 402 | $value = $this->safeEncode($value); |
||
| 403 | } |
||
| 404 | |||
| 405 | if (strpos($url, '?') === false) |
||
| 406 | { |
||
| 407 | $url .= '?'; |
||
| 408 | } |
||
| 409 | else |
||
| 410 | { |
||
| 411 | $url .= '&'; |
||
| 412 | } |
||
| 413 | |||
| 414 | $url .= $key . '=' . $value; |
||
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 418 | return $url; |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Method used to sign requests. |
||
| 423 | * |
||
| 424 | * @param string $url The URL to sign. |
||
| 425 | * @param string $method The request method. |
||
| 426 | * @param array $parameters Array containing request parameters. |
||
| 427 | * |
||
| 428 | * @return array The array containing the request parameters, including signature. |
||
| 429 | * |
||
| 430 | * @since 1.0 |
||
| 431 | */ |
||
| 432 | private function signRequest($url, $method, $parameters) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Prepare the signature base string. |
||
| 448 | * |
||
| 449 | * @param string $url The URL to sign. |
||
| 450 | * @param string $method The request method. |
||
| 451 | * @param array $parameters Array containing request parameters. |
||
| 452 | * |
||
| 453 | * @return string The base string. |
||
| 454 | * |
||
| 455 | * @since 1.0 |
||
| 456 | */ |
||
| 457 | private function baseString($url, $method, $parameters) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Encodes the string or array passed in a way compatible with OAuth. |
||
| 498 | * If an array is passed each array value will will be encoded. |
||
| 499 | * |
||
| 500 | * @param mixed $data The scalar or array to encode. |
||
| 501 | * |
||
| 502 | * @return string $data encoded in a way compatible with OAuth. |
||
| 503 | * |
||
| 504 | * @since 1.0 |
||
| 505 | */ |
||
| 506 | public function safeEncode($data) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Method used to generate the current nonce. |
||
| 527 | * |
||
| 528 | * @return string The current nonce. |
||
| 529 | * |
||
| 530 | * @since 1.0 |
||
| 531 | */ |
||
| 532 | public static function generateNonce() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Prepares the OAuth signing key. |
||
| 540 | * |
||
| 541 | * @return string The prepared signing key. |
||
| 542 | * |
||
| 543 | * @since 1.0 |
||
| 544 | */ |
||
| 545 | private function prepareSigningKey() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful; |
||
| 552 | * returns a 401 status code and an error message if not. |
||
| 553 | * |
||
| 554 | * @return array The decoded JSON response |
||
| 555 | * |
||
| 556 | * @since 1.0 |
||
| 557 | */ |
||
| 558 | abstract public function verifyCredentials(); |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Get an option from the OAuth1 Client instance. |
||
| 562 | * |
||
| 563 | * @param string $key The name of the option to get |
||
| 564 | * @param mixed $default Optional default value if the option does not exist |
||
| 565 | * |
||
| 566 | * @return mixed The option value |
||
| 567 | * |
||
| 568 | * @since 1.0 |
||
| 569 | */ |
||
| 570 | public function getOption($key, $default = null) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Set an option for the OAuth1 Client instance. |
||
| 577 | * |
||
| 578 | * @param string $key The name of the option to set |
||
| 579 | * @param mixed $value The option value to set |
||
| 580 | * |
||
| 581 | * @return $this |
||
| 582 | * |
||
| 583 | * @since 1.0 |
||
| 584 | */ |
||
| 585 | public function setOption($key, $value) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Get the oauth token key or secret. |
||
| 594 | * |
||
| 595 | * @return array The oauth token key and secret. |
||
| 596 | * |
||
| 597 | * @since 1.0 |
||
| 598 | */ |
||
| 599 | public function getToken() |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Set the oauth token. |
||
| 606 | * |
||
| 607 | * @param array $token The access token key and secret. |
||
| 608 | * |
||
| 609 | * @return $this |
||
| 610 | * |
||
| 611 | * @since 1.0 |
||
| 612 | */ |
||
| 613 | public function setToken($token) |
||
| 619 | } |
||
| 620 |
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.