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 | class Client |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | private $config = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $fileName; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var ClientProxy |
||
| 34 | */ |
||
| 35 | private $proxy; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | public $report; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $files; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var OauthTokenService |
||
| 49 | */ |
||
| 50 | private $oauthTokenService; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var ApiDetails |
||
| 54 | */ |
||
| 55 | private $apiDetails; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var ClientProxy |
||
| 59 | */ |
||
| 60 | private $clientProxy; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var Time |
||
| 64 | */ |
||
| 65 | private $timeHelper; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var File |
||
| 69 | */ |
||
| 70 | private $fileHelper; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Client constructor. |
||
| 74 | * |
||
| 75 | * @param OauthTokenService $oauthTokenService |
||
| 76 | * @param ApiDetails $apiDetails |
||
| 77 | * @param ClientProxy $clientProxy |
||
| 78 | * @param File $file |
||
| 79 | * @param Csv $csv |
||
| 80 | * @param Time $timeHelper |
||
| 81 | */ |
||
| 82 | public function __construct(OauthTokenService $oauthTokenService, ApiDetails $apiDetails, ClientProxy $clientProxy, File $file, Csv $csv, Time $timeHelper) |
||
| 100 | |||
| 101 | public function setApiDetails(ApiDetails $apiDetails) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Sets the configuration |
||
| 108 | * |
||
| 109 | * @param $config |
||
| 110 | */ |
||
| 111 | public function setConfig($config) |
||
| 117 | |||
| 118 | public function getRefreshToken() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param array $columns |
||
| 125 | * @param string $name |
||
| 126 | * @param $timePeriod |
||
| 127 | * @param null $fileLocation |
||
| 128 | * |
||
| 129 | * @return array|string |
||
| 130 | */ |
||
| 131 | public function get(array $columns, $name = 'GeoLocationPerformanceReport', $timePeriod = ReportTimePeriod::LastWeek, $fileLocation = null) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $wsdl |
||
| 159 | * @param string $accessToken |
||
| 160 | */ |
||
| 161 | private function setProxy($wsdl, $accessToken) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | private function getCacheDir() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param $reportRequest |
||
| 181 | * @param $name |
||
| 182 | * @param $downloadFile |
||
| 183 | * |
||
| 184 | * @throws \Exception |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | private function getFilesFromReportRequest($reportRequest, $name, $downloadFile, ReportInterface $report) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * SubmitGenerateReport helper method calls the corresponding Bing Ads service operation |
||
| 202 | * to request the report identifier. The identifier is used to check report generation status |
||
| 203 | * before downloading the report. |
||
| 204 | * |
||
| 205 | * @param mixed $report |
||
| 206 | * @param string $name |
||
| 207 | * |
||
| 208 | * @return string ReportRequestId |
||
| 209 | */ |
||
| 210 | private function submitGenerateReport($report, $name) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param mixed $report |
||
| 224 | * @param string $name |
||
| 225 | * |
||
| 226 | * @return SoapVar |
||
| 227 | */ |
||
| 228 | private function getReportRequest($report, $name) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Check if the report is ready for download |
||
| 237 | * if not wait 10 sec and retry. (up to 6,5 hour) |
||
| 238 | * After 30 tries check every 1 minute |
||
| 239 | * After 34 tries check every 5 minutes |
||
| 240 | * After 39 tries check every 15 minutes |
||
| 241 | * After 43 tries check every 30 minutes |
||
| 242 | * |
||
| 243 | * @param string $reportRequestId |
||
| 244 | * @param int $count |
||
| 245 | * @param int $maxCount |
||
| 246 | * @param int $sleep |
||
| 247 | * @param bool $incrementTime |
||
| 248 | * |
||
| 249 | * @throws Exceptions\ReportRequestErrorException |
||
| 250 | * @throws Exceptions\RequestTimeoutException |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | private function waitForStatus($reportRequestId, $count = 1, $maxCount = 48, $sleep = 10, $incrementTime = true) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Check the status of the report request. The guidance of how often to poll |
||
| 292 | * for status is from every five to 15 minutes depending on the amount |
||
| 293 | * of data being requested. For smaller reports, you can poll every couple |
||
| 294 | * of minutes. You should stop polling and try again later if the request |
||
| 295 | * is taking longer than an hour. |
||
| 296 | * |
||
| 297 | * @param $reportRequestId |
||
| 298 | * |
||
| 299 | * @return string ReportRequestStatus |
||
| 300 | */ |
||
| 301 | private function pollGenerateReport($reportRequestId) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param array|null $files |
||
| 314 | * |
||
| 315 | * @return self |
||
| 316 | */ |
||
| 317 | private function fixFile(ReportInterface $report, array $files = null) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Move first file form array $this->files to the target location |
||
| 335 | * |
||
| 336 | * @param string $target |
||
| 337 | * |
||
| 338 | * @return self |
||
| 339 | */ |
||
| 340 | private function moveFirstFile($target) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Clear Bundle Cache directory |
||
| 350 | * |
||
| 351 | * @param bool $allFiles delete all files in bundles cache, if false deletes only extracted files ($this->files) |
||
| 352 | * |
||
| 353 | * @return self |
||
| 354 | * |
||
| 355 | * @codeCoverageIgnore |
||
| 356 | */ |
||
| 357 | public function clearCache($allFiles = false) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param SoapFault $e |
||
| 377 | * |
||
| 378 | * @throws Exceptions\SoapInternalErrorException |
||
| 379 | * @throws Exceptions\SoapInvalidCredentialsException |
||
| 380 | * @throws Exceptions\SoapNoCompleteDataAvailableException |
||
| 381 | * @throws Exceptions\SoapReportingServiceInvalidReportIdException |
||
| 382 | * @throws Exceptions\SoapUnknownErrorException |
||
| 383 | * @throws Exceptions\SoapUserIsNotAuthorizedException |
||
| 384 | */ |
||
| 385 | private function parseSoapFault(SoapFault $e) |
||
| 415 | } |
||
| 416 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: