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 | public function __construct(OauthTokenService $oauthTokenService, ApiDetails $apiDetails, ClientProxy $clientProxy, File $file, Csv $csv, Time $timeHelper) |
||
| 107 | |||
| 108 | public function setApiDetails(ApiDetails $apiDetails) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Sets the configuration |
||
| 115 | * |
||
| 116 | * @param $config |
||
| 117 | */ |
||
| 118 | public function setConfig($config) |
||
| 124 | |||
| 125 | 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 | public function get(array $columns, $name = 'GeoLocationPerformanceReport', $timePeriod = ReportTimePeriod::LastWeek, $fileLocation = null) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $wsdl |
||
| 168 | * @param string $accessToken |
||
| 169 | */ |
||
| 170 | private function setProxy($wsdl, $accessToken) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | private function getCacheDir() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param ReportRequest $reportRequest |
||
| 190 | * @param string $name |
||
| 191 | * @param string $downloadFile |
||
| 192 | * @param ReportInterface $report |
||
| 193 | * |
||
| 194 | * @throws Exception |
||
| 195 | * |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | private function getFilesFromReportRequest(ReportRequest $reportRequest, $name, $downloadFile, ReportInterface $report) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * SubmitGenerateReport helper method calls the corresponding Bing Ads service operation |
||
| 214 | * to request the report identifier. The identifier is used to check report generation status |
||
| 215 | * before downloading the report. |
||
| 216 | * |
||
| 217 | * @param mixed $report |
||
| 218 | * @param string $name |
||
| 219 | * |
||
| 220 | * @return string ReportRequestId |
||
| 221 | */ |
||
| 222 | private function submitGenerateReport($report, $name) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param mixed $report |
||
| 236 | * @param string $name |
||
| 237 | * |
||
| 238 | * @return SoapVar |
||
| 239 | */ |
||
| 240 | private function getReportRequest($report, $name) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Check if the report is ready for download |
||
| 249 | * if not wait 10 sec and retry. (up to 6,5 hour) |
||
| 250 | * After 30 tries check every 1 minute |
||
| 251 | * After 34 tries check every 5 minutes |
||
| 252 | * After 39 tries check every 15 minutes |
||
| 253 | * After 43 tries check every 30 minutes |
||
| 254 | * |
||
| 255 | * @param string $reportRequestId |
||
| 256 | * @param int $count |
||
| 257 | * @param int $maxCount |
||
| 258 | * @param int $sleep |
||
| 259 | * @param bool $incrementTime |
||
| 260 | * |
||
| 261 | * @throws Exceptions\ReportRequestErrorException |
||
| 262 | * @throws Exceptions\RequestTimeoutException |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | private function waitForStatus($reportRequestId, $count = 1, $maxCount = 48, $sleep = 10, $incrementTime = true) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Check the status of the report request. The guidance of how often to poll |
||
| 304 | * for status is from every five to 15 minutes depending on the amount |
||
| 305 | * of data being requested. For smaller reports, you can poll every couple |
||
| 306 | * of minutes. You should stop polling and try again later if the request |
||
| 307 | * is taking longer than an hour. |
||
| 308 | * |
||
| 309 | * @param string $reportRequestId |
||
| 310 | * |
||
| 311 | * @return string ReportRequestStatus |
||
| 312 | */ |
||
| 313 | private function pollGenerateReport($reportRequestId) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param array|null $files |
||
| 326 | * |
||
| 327 | * @return self |
||
| 328 | */ |
||
| 329 | private function fixFile(ReportInterface $report, array $files = null) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Move first file form array $this->files to the target location |
||
| 347 | * |
||
| 348 | * @param string $target |
||
| 349 | * |
||
| 350 | * @return self |
||
| 351 | */ |
||
| 352 | private function moveFirstFile($target) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Clear Bundle Cache directory |
||
| 362 | * |
||
| 363 | * @param bool $allFiles delete all files in bundles cache, if false deletes only extracted files ($this->files) |
||
| 364 | * |
||
| 365 | * @return self |
||
| 366 | * |
||
| 367 | * @codeCoverageIgnore |
||
| 368 | */ |
||
| 369 | public function clearCache($allFiles = false) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param SoapFault $e |
||
| 389 | * |
||
| 390 | * @throws Exceptions\SoapInternalErrorException |
||
| 391 | * @throws Exceptions\SoapInvalidCredentialsException |
||
| 392 | * @throws Exceptions\SoapNoCompleteDataAvailableException |
||
| 393 | * @throws Exceptions\SoapReportingServiceInvalidReportIdException |
||
| 394 | * @throws Exceptions\SoapUnknownErrorException |
||
| 395 | * @throws Exceptions\SoapUserIsNotAuthorizedException |
||
| 396 | */ |
||
| 397 | private function parseSoapFault(SoapFault $e) |
||
| 427 | } |
||
| 428 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.