Complex classes like AbstractReportingCloud 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 AbstractReportingCloud, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | abstract class AbstractReportingCloud |
||
| 30 | { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Default date/time format of backend is 'ISO 8601' |
||
| 34 | * |
||
| 35 | * Note, last letter is 'P' and not 'O': |
||
| 36 | * |
||
| 37 | * O - Difference to Greenwich time (GMT) in hours (e.g. +0200) |
||
| 38 | * P - Difference to Greenwich time (GMT) with colon between hours and minutes (e.g. +02:00) |
||
| 39 | * |
||
| 40 | * Backend uses the 'P' variant |
||
| 41 | * |
||
| 42 | * @const DEFAULT_DATE_FORMAT |
||
| 43 | */ |
||
| 44 | const DEFAULT_DATE_FORMAT = 'Y-m-d\TH:i:sP'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Default time zone of backend |
||
| 48 | * |
||
| 49 | * @const DEFAULT_TIME_ZONE |
||
| 50 | */ |
||
| 51 | const DEFAULT_TIME_ZONE = 'UTC'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Default base URI of backend |
||
| 55 | * |
||
| 56 | * @const DEFAULT_BASE_URI |
||
| 57 | */ |
||
| 58 | const DEFAULT_BASE_URI = 'https://api.reporting.cloud'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Default version string of backend |
||
| 62 | * |
||
| 63 | * @const DEFAULT_VERSION |
||
| 64 | */ |
||
| 65 | const DEFAULT_VERSION = 'v1'; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Default timeout of backend in seconds |
||
| 69 | * |
||
| 70 | * @const DEFAULT_TIMEOUT |
||
| 71 | */ |
||
| 72 | const DEFAULT_TIMEOUT = 120; // seconds |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Default test flag of backend |
||
| 76 | * |
||
| 77 | * @const DEFAULT_TEST |
||
| 78 | */ |
||
| 79 | const DEFAULT_TEST = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Default debug flag of REST client |
||
| 83 | * |
||
| 84 | * @const DEFAULT_DEBUG |
||
| 85 | */ |
||
| 86 | const DEFAULT_DEBUG = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Backend username |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected $username; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Backend password |
||
| 97 | * |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | protected $password; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * When true, backend prints "TEST MODE" water mark into output document, and API call does not count against quota |
||
| 104 | * |
||
| 105 | * @var boolean |
||
| 106 | */ |
||
| 107 | protected $test; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Backend base URI |
||
| 111 | * |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | protected $baseUri; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Backend version string |
||
| 118 | * |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | protected $version; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Backend timeout in seconds |
||
| 125 | * |
||
| 126 | * @var integer |
||
| 127 | */ |
||
| 128 | protected $timeout; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * REST client to backend |
||
| 132 | * |
||
| 133 | * @var Client |
||
| 134 | */ |
||
| 135 | protected $client; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Debug flag of REST client |
||
| 139 | * |
||
| 140 | * @var boolean |
||
| 141 | */ |
||
| 142 | protected $debug; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * AbstractReportingCloud constructor |
||
| 146 | * |
||
| 147 | * @param array $options |
||
| 148 | */ |
||
| 149 | 58 | public function __construct(array $options = []) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Setters and getters |
||
| 170 | * ================================================================================================================= |
||
| 171 | */ |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Return the REST client of the backend web service |
||
| 175 | * |
||
| 176 | * @return \GuzzleHttp\Client |
||
| 177 | */ |
||
| 178 | 17 | public function getClient() |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Set the REST client of the backend web service |
||
| 202 | * |
||
| 203 | * @param Client $client REST client |
||
| 204 | * |
||
| 205 | * @return ReportingCloud |
||
| 206 | */ |
||
| 207 | 17 | public function setClient(Client $client) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Return the base URI of the backend web service |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | 21 | public function getBaseUri() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Set the base URI of the backend web service |
||
| 230 | * |
||
| 231 | * @param string $baseUri Base URI |
||
| 232 | * |
||
| 233 | * @return ReportingCloud |
||
| 234 | */ |
||
| 235 | 21 | public function setBaseUri($baseUri) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Get the timeout (in seconds) of the backend web service |
||
| 244 | * |
||
| 245 | * @return integer |
||
| 246 | */ |
||
| 247 | 20 | public function getTimeout() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Set the timeout (in seconds) of the backend web service |
||
| 258 | * |
||
| 259 | * @param integer $timeout Timeout |
||
| 260 | * |
||
| 261 | * @return ReportingCloud |
||
| 262 | */ |
||
| 263 | 20 | public function setTimeout($timeout) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Return the username |
||
| 272 | * |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | 20 | public function getUsername() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Set the username |
||
| 282 | * |
||
| 283 | * @param string $username Username |
||
| 284 | * |
||
| 285 | * @return ReportingCloud |
||
| 286 | */ |
||
| 287 | 55 | public function setUsername($username) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Return the password |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | 20 | public function getPassword() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Set the password |
||
| 306 | * |
||
| 307 | * @param string $password Password |
||
| 308 | * |
||
| 309 | * @return ReportingCloud |
||
| 310 | */ |
||
| 311 | 55 | public function setPassword($password) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Return the test flag |
||
| 320 | * |
||
| 321 | * @return mixed |
||
| 322 | */ |
||
| 323 | 18 | public function getTest() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Set the test flag |
||
| 334 | * |
||
| 335 | * @param boolean $test Test flag |
||
| 336 | * |
||
| 337 | * @return ReportingCloud |
||
| 338 | */ |
||
| 339 | 18 | public function setTest($test) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Return the debug flag |
||
| 348 | * |
||
| 349 | * @return mixed |
||
| 350 | */ |
||
| 351 | 20 | public function getDebug() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Set the debug flag |
||
| 362 | * |
||
| 363 | * @param boolean $debug Debug flag |
||
| 364 | * |
||
| 365 | * @return ReportingCloud |
||
| 366 | */ |
||
| 367 | 20 | public function setDebug($debug) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Get the version string of the backend web service |
||
| 376 | * |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | 19 | public function getVersion() |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Set the version string of the backend web service |
||
| 390 | * |
||
| 391 | * @param string $version Version string |
||
| 392 | * |
||
| 393 | * @return ReportingCloud |
||
| 394 | */ |
||
| 395 | 2 | public function setVersion($version) |
|
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * Utility methods |
||
| 405 | * ================================================================================================================= |
||
| 406 | */ |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Request the URI with options |
||
| 410 | * |
||
| 411 | * @param string $method HTTP method |
||
| 412 | * @param string $uri URI |
||
| 413 | * @param array $options Options |
||
| 414 | * |
||
| 415 | * @return mixed|null|\Psr\Http\Message\ResponseInterface |
||
| 416 | * |
||
| 417 | * @throws RuntimeException |
||
| 418 | */ |
||
| 419 | 16 | protected function request($method, $uri, $options) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Construct URI with version number |
||
| 449 | * |
||
| 450 | * @param string $uri URI |
||
| 451 | * |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | 16 | protected function uri($uri) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Using the passed propertyMap, recursively build array |
||
| 461 | * |
||
| 462 | * @param array $array Array |
||
| 463 | * @param PropertyMap $propertyMap PropertyMap |
||
| 464 | * |
||
| 465 | * @return array |
||
| 466 | */ |
||
| 467 | 3 | protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Using passed mergeSettings array, build array for backend |
||
| 487 | * |
||
| 488 | * @param array $array MergeSettings array |
||
| 489 | * |
||
| 490 | * @return array |
||
| 491 | */ |
||
| 492 | 8 | protected function buildMergeSettingsArray(array $array) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays) |
||
| 517 | * |
||
| 518 | * @param array $array FindAndReplaceData array |
||
| 519 | * |
||
| 520 | * @return array |
||
| 521 | */ |
||
| 522 | 4 | protected function buildFindAndReplaceDataArray(array $array) |
|
| 532 | |||
| 533 | } |