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 | class Client |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private $config = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $fileName; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var ClientProxy |
||
| 36 | */ |
||
| 37 | private $proxy; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | public $report; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $files; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var OauthTokenService |
||
| 51 | */ |
||
| 52 | private $oauthTokenService; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var ApiDetails |
||
| 56 | */ |
||
| 57 | private $apiDetails; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var ClientProxy |
||
| 61 | */ |
||
| 62 | private $clientProxy; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var File |
||
| 66 | */ |
||
| 67 | private $fileHelper; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Csv |
||
| 71 | */ |
||
| 72 | private $csvHelper; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var Time |
||
| 76 | */ |
||
| 77 | private $timeHelper; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Client constructor. |
||
| 81 | * |
||
| 82 | * @param OauthTokenService $oauthTokenService |
||
| 83 | * @param ApiDetails $apiDetails |
||
| 84 | * @param ClientProxy $clientProxy |
||
| 85 | * @param File $file |
||
| 86 | * @param Csv $csv |
||
| 87 | * @param Time $timeHelper |
||
| 88 | */ |
||
| 89 | 25 | public function __construct(OauthTokenService $oauthTokenService, ApiDetails $apiDetails, ClientProxy $clientProxy, File $file, Csv $csv, Time $timeHelper) |
|
| 107 | |||
| 108 | 1 | public function setApiDetails(ApiDetails $apiDetails) |
|
| 109 | { |
||
| 110 | 1 | $this->apiDetails = $apiDetails; |
|
| 111 | 1 | } |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Sets the configuration |
||
| 115 | * |
||
| 116 | * @param $config |
||
| 117 | */ |
||
| 118 | 24 | public function setConfig($config) |
|
| 124 | |||
| 125 | 1 | public function getRefreshToken() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param array $columns |
||
| 132 | * @param string $name |
||
| 133 | * @param $timePeriod |
||
| 134 | * @param null|string $fileLocation |
||
| 135 | * |
||
| 136 | * @return array|string |
||
| 137 | */ |
||
| 138 | 24 | public function getReport(array $columns, $name = 'GeoLocationPerformanceReport', $timePeriod = ReportTimePeriod::LastWeek, $fileLocation = null) |
|
| 158 | 1 | ||
| 159 | /** |
||
| 160 | 1 | * @return AccessToken |
|
| 161 | */ |
||
| 162 | 2 | protected function getOauthToken() |
|
| 171 | |||
| 172 | 24 | /** |
|
| 173 | 24 | * @param string $wsdl |
|
| 174 | * @param string $accessToken |
||
| 175 | */ |
||
| 176 | private function setProxy($wsdl, $accessToken) |
||
| 180 | 24 | ||
| 181 | 24 | /** |
|
| 182 | 1 | * @return string |
|
| 183 | 1 | */ |
|
| 184 | private function getCacheDir() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param ReportRequest $reportRequest |
||
| 196 | * @param string $name |
||
| 197 | * @param string $downloadFile |
||
| 198 | 24 | * @param ReportInterface $report |
|
| 199 | * |
||
| 200 | 24 | * @throws Exception |
|
| 201 | 6 | * |
|
| 202 | 3 | * @return array|string |
|
| 203 | 3 | */ |
|
| 204 | 3 | private function getFilesFromReportRequest(ReportRequest $reportRequest, $name, $downloadFile, ReportInterface $report) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * SubmitGenerateReport helper method calls the corresponding Bing Ads service operation |
||
| 220 | * to request the report identifier. The identifier is used to check report generation status |
||
| 221 | * before downloading the report. |
||
| 222 | 24 | * |
|
| 223 | * @param mixed $report |
||
| 224 | 24 | * @param string $name |
|
| 225 | * |
||
| 226 | 24 | * @return string ReportRequestId |
|
| 227 | */ |
||
| 228 | 24 | private function submitGenerateReport($report, $name) |
|
| 239 | |||
| 240 | 24 | /** |
|
| 241 | * @param mixed $report |
||
| 242 | 24 | * @param string $name |
|
| 243 | * |
||
| 244 | 24 | * @return SoapVar |
|
| 245 | */ |
||
| 246 | private function getReportRequest($report, $name) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Check if the report is ready for download |
||
| 255 | * if not wait 10 sec and retry. (up to 6,5 hour) |
||
| 256 | * After 30 tries check every 1 minute |
||
| 257 | * After 34 tries check every 5 minutes |
||
| 258 | * After 39 tries check every 15 minutes |
||
| 259 | * After 43 tries check every 30 minutes |
||
| 260 | * |
||
| 261 | * @param string $reportRequestId |
||
| 262 | * @param int $count |
||
| 263 | * @param int $maxCount |
||
| 264 | * @param int $sleep |
||
| 265 | * @param bool $incrementTime |
||
| 266 | 6 | * |
|
| 267 | * @throws Exceptions\ReportRequestErrorException |
||
| 268 | 6 | * @throws Exceptions\RequestTimeoutException |
|
| 269 | 1 | * |
|
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | 6 | private function waitForStatus($reportRequestId, $count = 1, $maxCount = 48, $sleep = 10, $incrementTime = true) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Check the status of the report request. The guidance of how often to poll |
||
| 310 | * for status is from every five to 15 minutes depending on the amount |
||
| 311 | * of data being requested. For smaller reports, you can poll every couple |
||
| 312 | * of minutes. You should stop polling and try again later if the request |
||
| 313 | 6 | * is taking longer than an hour. |
|
| 314 | * |
||
| 315 | 6 | * @param string $reportRequestId |
|
| 316 | 6 | * |
|
| 317 | * @return string ReportRequestStatus |
||
| 318 | 6 | */ |
|
| 319 | 1 | private function pollGenerateReport($reportRequestId) |
|
| 329 | 3 | ||
| 330 | /** |
||
| 331 | 3 | * @param array|null $files |
|
| 332 | 3 | * |
|
| 333 | 3 | * @return self |
|
| 334 | 3 | */ |
|
| 335 | 3 | private function fixFile(ReportInterface $report, array $files = null) |
|
| 350 | |||
| 351 | /** |
||
| 352 | 1 | * Move first file form array $this->files to the target location |
|
| 353 | * |
||
| 354 | 1 | * @param string $target |
|
| 355 | 1 | * |
|
| 356 | * @return self |
||
| 357 | 1 | */ |
|
| 358 | private function moveFirstFile($target) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Clear Bundle Cache directory |
||
| 368 | * |
||
| 369 | * @param bool $allFiles delete all files in bundles cache, if false deletes only extracted files ($this->files) |
||
| 370 | * |
||
| 371 | * @return self |
||
| 372 | * |
||
| 373 | * @codeCoverageIgnore |
||
| 374 | */ |
||
| 375 | public function clearCache($allFiles = false) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param SoapFault $e |
||
| 395 | * |
||
| 396 | * @throws Exceptions\SoapInternalErrorException |
||
| 397 | 19 | * @throws Exceptions\SoapInvalidCredentialsException |
|
| 398 | * @throws Exceptions\SoapNoCompleteDataAvailableException |
||
| 399 | 19 | * @throws Exceptions\SoapReportingServiceInvalidReportIdException |
|
| 400 | 19 | * @throws Exceptions\SoapUnknownErrorException |
|
| 401 | 7 | * @throws Exceptions\SoapUserIsNotAuthorizedException |
|
| 402 | 19 | */ |
|
| 403 | 12 | private function parseSoapFault(SoapFault $e) |
|
| 434 | } |
||
| 435 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.