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 |
||
| 10 | class Client |
||
| 11 | { |
||
| 12 | /** @var string */ |
||
| 13 | public $key; |
||
| 14 | /** @var bool */ |
||
| 15 | public $oauth; |
||
| 16 | |||
| 17 | /** @var bool */ |
||
| 18 | public $oauth2; |
||
| 19 | |||
| 20 | /** @var int */ |
||
| 21 | public $userId; |
||
| 22 | |||
| 23 | /** @var \GuzzleHttp\Client */ |
||
| 24 | public $client; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Guzzle allows options into its request method. Prepare for some defaults |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $clientOptions = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * if set to false, no Response object is created, but the one from Guzzle is directly returned |
||
| 34 | * comes in handy own error handling |
||
| 35 | * |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | protected $wrapResponse = true; |
||
| 39 | |||
| 40 | /** @var string */ |
||
| 41 | private $user_agent = "SevenShores_Hubspot_PHP/1.0.0-rc.1 (https://github.com/ryanwinchester/hubspot-php)"; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int Number of requests remaining this second |
||
| 45 | */ |
||
| 46 | private $requestsRemainingThisSecond = null; |
||
| 47 | |||
| 48 | private $requestsRemainingThisSecondTime = null; |
||
| 49 | |||
| 50 | /** @var int Max number of exceptions we can handle before aborting the re-try */ |
||
| 51 | private $exceptionMax = null; |
||
| 52 | |||
| 53 | /** @var int Delay before retry in microseconds when an exception occurs */ |
||
| 54 | private $exceptionDelay = null; |
||
| 55 | |||
| 56 | /** @var int Delay before we wait before requests when API reports low request credits */ |
||
| 57 | private $backOffDelay = null; |
||
| 58 | |||
| 59 | /** @var bool Handle rate limits automatically */ |
||
| 60 | private $handleRateLimits = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Make it, baby. |
||
| 64 | * |
||
| 65 | * @param array $config Configuration array |
||
| 66 | * @param GuzzleClient $client The Http Client (Defaults to Guzzle) |
||
| 67 | * @param array $clientOptions options to be passed to Guzzle upon each request |
||
| 68 | * @param bool $wrapResponse wrap request response in own Response object |
||
| 69 | */ |
||
| 70 | public function __construct($config = [], $client = null, $clientOptions = [], $wrapResponse = true) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Send the request... |
||
| 101 | * |
||
| 102 | * @param string $method The HTTP request verb |
||
| 103 | * @param string $endpoint The Hubspot API endpoint |
||
| 104 | * @param array $options An array of options to send with the request |
||
| 105 | * @param string $query_string A query string to send with the request |
||
| 106 | * @param boolean $requires_auth Whether or not this HubSpot API endpoint requires authentication |
||
| 107 | * @return \SevenShores\Hubspot\Http\Response|ResponseInterface |
||
| 108 | * @throws \SevenShores\Hubspot\Exceptions\BadRequest |
||
| 109 | */ |
||
| 110 | public function request($method, $endpoint, $options = [], $query_string = null, $requires_auth = true) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Generate the full endpoint url, including query string. |
||
| 212 | * |
||
| 213 | * @param string $endpoint The HubSpot API endpoint. |
||
| 214 | * @param string $query_string The query string to send to the endpoint. |
||
| 215 | * @param boolean $requires_auth Whether or not this HubSpot API endpoint requires authentication |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | protected function generateUrl($endpoint, $query_string = null, $requires_auth = true) |
||
| 240 | } |
||
| 241 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.