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 |
||
| 23 | class Client |
||
| 24 | { |
||
| 25 | const CACHE_SUBDIRECTORY = 'BingAdsApiBundle'; |
||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $config = []; |
||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $fileName; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var ClientProxy |
||
| 37 | */ |
||
| 38 | private $proxy; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | public $report; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $files; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var OauthTokenService |
||
| 52 | */ |
||
| 53 | private $oauthTokenService; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var ApiDetails |
||
| 57 | */ |
||
| 58 | private $apiDetails; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var ClientProxy |
||
| 62 | */ |
||
| 63 | private $clientProxy; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var File |
||
| 67 | */ |
||
| 68 | private $fileHelper; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var Csv |
||
| 72 | */ |
||
| 73 | private $csvHelper; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var Time |
||
| 77 | */ |
||
| 78 | private $timeHelper; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var Filesystem |
||
| 82 | */ |
||
| 83 | private $filesystem; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param OauthTokenService $oauthTokenService |
||
| 87 | * @param ApiDetails $apiDetails |
||
| 88 | * @param ClientProxy $clientProxy |
||
| 89 | 25 | * @param File $file |
|
| 90 | 1 | * @param Csv $csv |
|
| 91 | 25 | * @param Time $timeHelper |
|
| 92 | 25 | * @param Filesystem $fileSystem |
|
| 93 | 25 | */ |
|
| 94 | 25 | public function __construct(OauthTokenService $oauthTokenService, ApiDetails $apiDetails, ClientProxy $clientProxy, File $file, Csv $csv, Time $timeHelper, Filesystem $fileSystem) |
|
| 113 | |||
| 114 | public function setApiDetails(ApiDetails $apiDetails) |
||
| 118 | 24 | ||
| 119 | /** |
||
| 120 | 24 | * Sets the configuration |
|
| 121 | 24 | * |
|
| 122 | 24 | * @param $config |
|
| 123 | 24 | */ |
|
| 124 | public function setConfig($config) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param $cacheDir |
||
| 134 | */ |
||
| 135 | public function setCacheDir($cacheDir) |
||
| 139 | |||
| 140 | 24 | public function getRefreshToken() |
|
| 144 | 24 | ||
| 145 | 24 | /** |
|
| 146 | * @param string $reportName |
||
| 147 | 24 | * @param array $columns |
|
| 148 | 24 | * @param $timePeriod |
|
| 149 | * @param null|string $fileLocation |
||
| 150 | 24 | */ |
|
| 151 | 24 | public function getReport($reportName, array $columns, $timePeriod = ReportTimePeriod::LastWeek, $fileLocation) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @return AccessToken |
||
| 170 | 24 | */ |
|
| 171 | protected function getOauthToken() |
||
| 180 | 24 | ||
| 181 | 24 | /** |
|
| 182 | 1 | * @param string $wsdl |
|
| 183 | 1 | * @param string $accessToken |
|
| 184 | */ |
||
| 185 | 24 | private function setProxy($wsdl, $accessToken) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | private function getCacheDir() |
||
| 199 | |||
| 200 | 24 | /** |
|
| 201 | 6 | * @param ReportRequest $reportRequest |
|
| 202 | 3 | * @param string $name |
|
| 203 | 3 | * @param string $downloadFile |
|
| 204 | 3 | * @param ReportInterface $report |
|
| 205 | 3 | * |
|
| 206 | 3 | * @throws Exception |
|
| 207 | 3 | * |
|
| 208 | * @return array|string |
||
| 209 | 3 | */ |
|
| 210 | private function getFilesFromReportRequest(ReportRequest $reportRequest, $name, $downloadFile, ReportInterface $report) |
||
| 222 | 24 | ||
| 223 | /** |
||
| 224 | 24 | * SubmitGenerateReport helper method calls the corresponding Bing Ads service operation |
|
| 225 | * to request the report identifier. The identifier is used to check report generation status |
||
| 226 | 24 | * before downloading the report. |
|
| 227 | * |
||
| 228 | 24 | * @param mixed $report |
|
| 229 | 18 | * @param string $name |
|
| 230 | 18 | * |
|
| 231 | * @return string ReportRequestId |
||
| 232 | */ |
||
| 233 | private function submitGenerateReport($report, $name) |
||
| 244 | 24 | ||
| 245 | /** |
||
| 246 | * @param mixed $report |
||
| 247 | * @param string $name |
||
| 248 | * |
||
| 249 | * @return SoapVar |
||
| 250 | */ |
||
| 251 | private function getReportRequest($report, $name) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Check if the report is ready for download |
||
| 260 | * if not wait 10 sec and retry. (up to 6,5 hour) |
||
| 261 | * After 30 tries check every 1 minute |
||
| 262 | * After 34 tries check every 5 minutes |
||
| 263 | * After 39 tries check every 15 minutes |
||
| 264 | * After 43 tries check every 30 minutes |
||
| 265 | * |
||
| 266 | 6 | * @param string $reportRequestId |
|
| 267 | * @param int $count |
||
| 268 | 6 | * @param int $maxCount |
|
| 269 | 1 | * @param int $sleep |
|
| 270 | * @param bool $incrementTime |
||
| 271 | * |
||
| 272 | 6 | * @throws Exceptions\ReportRequestErrorException |
|
| 273 | 5 | * @throws Exceptions\RequestTimeoutException |
|
| 274 | 1 | * |
|
| 275 | 1 | * @return string |
|
| 276 | 1 | */ |
|
| 277 | private function waitForStatus($reportRequestId, $count = 1, $maxCount = 48, $sleep = 10, $incrementTime = true) |
||
| 312 | |||
| 313 | 6 | /** |
|
| 314 | * Check the status of the report request. The guidance of how often to poll |
||
| 315 | 6 | * for status is from every five to 15 minutes depending on the amount |
|
| 316 | 6 | * of data being requested. For smaller reports, you can poll every couple |
|
| 317 | * of minutes. You should stop polling and try again later if the request |
||
| 318 | 6 | * is taking longer than an hour. |
|
| 319 | 1 | * |
|
| 320 | 1 | * @param string $reportRequestId |
|
| 321 | * |
||
| 322 | * @return string ReportRequestStatus |
||
| 323 | */ |
||
| 324 | private function pollGenerateReport($reportRequestId) |
||
| 334 | 3 | ||
| 335 | 3 | /** |
|
| 336 | 3 | * @param array|null $files |
|
| 337 | 3 | * |
|
| 338 | 3 | * @return self |
|
| 339 | 3 | */ |
|
| 340 | 3 | private function fixFile(ReportInterface $report, array $files) |
|
| 354 | 1 | ||
| 355 | 1 | /** |
|
| 356 | * @param array $files |
||
| 357 | 1 | * @param string $target |
|
| 358 | */ |
||
| 359 | private function moveFirstFile(array $files, $target) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Clear Bundle Cache directory |
||
| 367 | * |
||
| 368 | * @param bool $allFiles delete all files in bundles cache, if false deletes only extracted files ($this->files) |
||
| 369 | * |
||
| 370 | * @return self |
||
| 371 | * |
||
| 372 | * @codeCoverageIgnore |
||
| 373 | */ |
||
| 374 | public function clearCache($allFiles = false) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param SoapFault $e |
||
| 394 | * |
||
| 395 | * @throws Exceptions\SoapInternalErrorException |
||
| 396 | * @throws Exceptions\SoapInvalidCredentialsException |
||
| 397 | 19 | * @throws Exceptions\SoapNoCompleteDataAvailableException |
|
| 398 | * @throws Exceptions\SoapReportingServiceInvalidReportIdException |
||
| 399 | 19 | * @throws Exceptions\SoapUnknownErrorException |
|
| 400 | 19 | * @throws Exceptions\SoapUserIsNotAuthorizedException |
|
| 401 | 7 | */ |
|
| 402 | 19 | private function parseSoapFault(SoapFault $e) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * @param $reportName |
||
| 436 | * |
||
| 437 | * @throws InvalidReportNameException |
||
| 438 | */ |
||
| 439 | private function ensureValidReportName($reportName) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @return Filesystem |
||
| 448 | */ |
||
| 449 | private function getFileSystem() |
||
| 453 | |||
| 454 | private function createCacheDirIfNotExists() |
||
| 462 | } |
||
| 463 |
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: