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 |
||
| 22 | abstract class Client |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Options for the Client object. |
||
| 26 | * |
||
| 27 | * @var array|\ArrayAccess |
||
| 28 | * @since 1.0 |
||
| 29 | */ |
||
| 30 | protected $options; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Contains access token key, secret and verifier. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | * @since 1.0 |
||
| 37 | */ |
||
| 38 | protected $token = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The HTTP client object to use in sending HTTP requests. |
||
| 42 | * |
||
| 43 | * @var Http |
||
| 44 | * @since 1.0 |
||
| 45 | */ |
||
| 46 | protected $client; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The input object to use in retrieving GET/POST data. |
||
| 50 | * |
||
| 51 | * @var Input |
||
| 52 | * @since 1.0 |
||
| 53 | */ |
||
| 54 | protected $input; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The application object to send HTTP headers for redirects. |
||
| 58 | * |
||
| 59 | * @var SessionAwareWebApplicationInterface |
||
| 60 | * @since 1.0 |
||
| 61 | */ |
||
| 62 | protected $application; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Selects which version of OAuth to use: 1.0 or 1.0a. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | * @since 1.0 |
||
| 69 | */ |
||
| 70 | protected $version; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Constructor. |
||
| 74 | * |
||
| 75 | * @param SessionAwareWebApplicationInterface $application The application object |
||
| 76 | * @param Http $client The HTTP client object. |
||
| 77 | * @param Input $input The input object |
||
| 78 | * @param array|\ArrayAccess $options OAuth1 Client options. |
||
| 79 | * @param string $version Specify the OAuth version. By default we are using 1.0a. |
||
| 80 | * |
||
| 81 | * @since 1.0 |
||
| 82 | */ |
||
| 83 | public function __construct( |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Method to form the oauth flow. |
||
| 107 | * |
||
| 108 | * @return array|null The access token. |
||
| 109 | * |
||
| 110 | * @since 1.0 |
||
| 111 | * @throws \DomainException |
||
| 112 | */ |
||
| 113 | public function authenticate() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Method used to get a request token. |
||
| 176 | * |
||
| 177 | * @return void |
||
| 178 | * |
||
| 179 | * @since 1.1.2 |
||
| 180 | * @throws \DomainException |
||
| 181 | */ |
||
| 182 | private function generateRequestToken() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Method used to authorise the application. |
||
| 213 | * |
||
| 214 | * @return void |
||
| 215 | * |
||
| 216 | * @since 1.1.2 |
||
| 217 | */ |
||
| 218 | private function authorise() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Method used to get an access token. |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | * |
||
| 239 | * @since 1.1.2 |
||
| 240 | */ |
||
| 241 | private function generateAccessToken() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Method used to make an OAuth request. |
||
| 264 | * |
||
| 265 | * @param string $url The request URL. |
||
| 266 | * @param string $method The request method. |
||
| 267 | * @param array $parameters Array containing request parameters. |
||
| 268 | * @param mixed $data The POST request data. |
||
| 269 | * @param array $headers An array of name-value pairs to include in the header of the request |
||
| 270 | * |
||
| 271 | * @return \Joomla\Http\Response |
||
| 272 | * |
||
| 273 | * @since 1.0 |
||
| 274 | * @throws \DomainException |
||
| 275 | */ |
||
| 276 | public function oauthRequest($url, $method, $parameters, $data = [], $headers = []) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Method to validate a response. |
||
| 345 | * |
||
| 346 | * @param string $url The request URL. |
||
| 347 | * @param Response $response The response to validate. |
||
| 348 | * |
||
| 349 | * @return void |
||
| 350 | * |
||
| 351 | * @since 1.0 |
||
| 352 | * @throws \DomainException |
||
| 353 | */ |
||
| 354 | abstract public function validateResponse($url, $response); |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Method used to create the header for the POST request. |
||
| 358 | * |
||
| 359 | * @param array $parameters Array containing request parameters. |
||
| 360 | * |
||
| 361 | * @return string The header. |
||
| 362 | * |
||
| 363 | * @since 1.1.2 |
||
| 364 | */ |
||
| 365 | private function createHeader(array $parameters): string |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Method to create the URL formed string with the parameters. |
||
| 386 | * |
||
| 387 | * @param string $url The request URL. |
||
| 388 | * @param array $parameters Array containing request parameters. |
||
| 389 | * |
||
| 390 | * @return string The formed URL. |
||
| 391 | * |
||
| 392 | * @since 1.0 |
||
| 393 | */ |
||
| 394 | public function toUrl($url, $parameters) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Method used to sign requests. |
||
| 404 | * |
||
| 405 | * @param string $url The URL to sign. |
||
| 406 | * @param string $method The request method. |
||
| 407 | * @param array $parameters Array containing request parameters. |
||
| 408 | * |
||
| 409 | * @return array The array containing the request parameters, including signature. |
||
| 410 | * |
||
| 411 | * @since 1.1.2 |
||
| 412 | */ |
||
| 413 | private function signRequest(string $url, string $method, array $parameters): array |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Prepare the signature base string. |
||
| 429 | * |
||
| 430 | * @param string $url The URL to sign. |
||
| 431 | * @param string $method The request method. |
||
| 432 | * @param array $parameters Array containing request parameters. |
||
| 433 | * |
||
| 434 | * @return string The base string. |
||
| 435 | * |
||
| 436 | * @since 1.1.2 |
||
| 437 | */ |
||
| 438 | private function baseString(string $url, string $method, array $parameters): string |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Encodes the string or array passed in a way compatible with OAuth. |
||
| 479 | * If an array is passed each array value will will be encoded. |
||
| 480 | * |
||
| 481 | * @param mixed $data The scalar or array to encode. |
||
| 482 | * |
||
| 483 | * @return string $data encoded in a way compatible with OAuth. |
||
| 484 | * |
||
| 485 | * @since 1.0 |
||
| 486 | */ |
||
| 487 | public function safeEncode($data) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Method used to generate the current nonce. |
||
| 508 | * |
||
| 509 | * @return string The current nonce. |
||
| 510 | * |
||
| 511 | * @since 1.0 |
||
| 512 | */ |
||
| 513 | public static function generateNonce() |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Prepares the OAuth signing key. |
||
| 521 | * |
||
| 522 | * @return string The prepared signing key. |
||
| 523 | * |
||
| 524 | * @since 1.1.2 |
||
| 525 | */ |
||
| 526 | private function prepareSigningKey(): string |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful; |
||
| 533 | * returns a 401 status code and an error message if not. |
||
| 534 | * |
||
| 535 | * @return array The decoded JSON response |
||
| 536 | * |
||
| 537 | * @since 1.0 |
||
| 538 | */ |
||
| 539 | abstract public function verifyCredentials(); |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Get an option from the OAuth1 Client instance. |
||
| 543 | * |
||
| 544 | * @param string $key The name of the option to get |
||
| 545 | * @param mixed $default Optional default value if the option does not exist |
||
| 546 | * |
||
| 547 | * @return mixed The option value |
||
| 548 | * |
||
| 549 | * @since 1.0 |
||
| 550 | */ |
||
| 551 | public function getOption($key, $default = null) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Set an option for the OAuth1 Client instance. |
||
| 558 | * |
||
| 559 | * @param string $key The name of the option to set |
||
| 560 | * @param mixed $value The option value to set |
||
| 561 | * |
||
| 562 | * @return $this |
||
| 563 | * |
||
| 564 | * @since 1.0 |
||
| 565 | */ |
||
| 566 | public function setOption($key, $value) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Get the oauth token key or secret. |
||
| 575 | * |
||
| 576 | * @return array The oauth token key and secret. |
||
| 577 | * |
||
| 578 | * @since 1.0 |
||
| 579 | */ |
||
| 580 | public function getToken() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Set the oauth token. |
||
| 587 | * |
||
| 588 | * @param array $token The access token key and secret. |
||
| 589 | * |
||
| 590 | * @return $this |
||
| 591 | * |
||
| 592 | * @since 1.0 |
||
| 593 | */ |
||
| 594 | public function setToken($token) |
||
| 600 | } |
||
| 601 |
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.