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 | 57 | 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 | 16 | 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 | 16 | public function setClient(Client $client) |
|
213 | |||
214 | /** |
||
215 | * Return the base URI of the backend web service |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | 20 | 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 | 20 | public function setBaseUri($baseUri) |
|
241 | |||
242 | /** |
||
243 | * Get the timeout (in seconds) of the backend web service |
||
244 | * |
||
245 | * @return integer |
||
246 | */ |
||
247 | 19 | 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 | 19 | public function setTimeout($timeout) |
|
269 | |||
270 | /** |
||
271 | * Return the username |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | 19 | public function getUsername() |
|
279 | |||
280 | /** |
||
281 | * Set the username |
||
282 | * |
||
283 | * @param string $username Username |
||
284 | * |
||
285 | * @return ReportingCloud |
||
286 | */ |
||
287 | 54 | public function setUsername($username) |
|
293 | |||
294 | /** |
||
295 | * Return the password |
||
296 | * |
||
297 | * @return string |
||
298 | */ |
||
299 | 19 | public function getPassword() |
|
303 | |||
304 | /** |
||
305 | * Set the password |
||
306 | * |
||
307 | * @param string $password Password |
||
308 | * |
||
309 | * @return ReportingCloud |
||
310 | */ |
||
311 | 54 | public function setPassword($password) |
|
317 | |||
318 | /** |
||
319 | * Return the test flag |
||
320 | * |
||
321 | * @return mixed |
||
322 | */ |
||
323 | 17 | public function getTest() |
|
331 | |||
332 | /** |
||
333 | * Set the test flag |
||
334 | * |
||
335 | * @param boolean $test Test flag |
||
336 | * |
||
337 | * @return ReportingCloud |
||
338 | */ |
||
339 | 17 | public function setTest($test) |
|
345 | |||
346 | /** |
||
347 | * Return the debug flag |
||
348 | * |
||
349 | * @return mixed |
||
350 | */ |
||
351 | 19 | public function getDebug() |
|
359 | |||
360 | /** |
||
361 | * Set the debug flag |
||
362 | * |
||
363 | * @param boolean $debug Debug flag |
||
364 | * |
||
365 | * @return ReportingCloud |
||
366 | */ |
||
367 | 19 | public function setDebug($debug) |
|
373 | |||
374 | /** |
||
375 | * Get the version string of the backend web service |
||
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | 18 | 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 | 15 | protected function request($method, $uri, $options) |
|
420 | { |
||
421 | 15 | $ret = null; |
|
422 | |||
423 | 15 | $client = $this->getClient(); |
|
424 | |||
425 | try { |
||
426 | |||
427 | 15 | if (getenv('TRAVIS')) { |
|
428 | $options['curl'][CURLOPT_SSLVERSION] = CURL_SSLVERSION_TLSv1_1; |
||
429 | } |
||
430 | |||
431 | 15 | if ($this->getTest()) { |
|
432 | $options[RequestOptions::QUERY]['test'] = StaticFilter::execute($this->getTest(), 'BooleanToString'); |
||
433 | } |
||
434 | |||
435 | 15 | $ret = $client->request($method, $uri, $options); |
|
436 | |||
437 | 15 | } catch (\Exception $exception) { |
|
438 | |||
439 | // \GuzzleHttp\Exception\ClientException |
||
440 | // \GuzzleHttp\Exception\ServerException |
||
441 | |||
442 | 1 | $message = (string) $exception->getMessage(); |
|
443 | 1 | $code = (integer) $exception->getCode(); |
|
444 | |||
445 | 1 | throw new RuntimeException($message, $code); |
|
446 | } |
||
447 | |||
448 | 14 | return $ret; |
|
449 | } |
||
450 | |||
451 | /** |
||
452 | * Construct URI with version number |
||
453 | * |
||
454 | * @param string $uri URI |
||
455 | * |
||
456 | * @return string |
||
457 | */ |
||
458 | 15 | protected function uri($uri) |
|
462 | |||
463 | /** |
||
464 | * Using the passed propertyMap, recursively build array |
||
465 | * |
||
466 | * @param array $array Array |
||
467 | * @param PropertyMap $propertyMap PropertyMap |
||
468 | * |
||
469 | * @return array |
||
470 | */ |
||
471 | 3 | protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap) |
|
488 | |||
489 | /** |
||
490 | * Using passed mergeSettings array, build array for backend |
||
491 | * |
||
492 | * @param array $array MergeSettings array |
||
493 | * |
||
494 | * @return array |
||
495 | */ |
||
496 | 8 | protected function buildMergeSettingsArray(array $array) |
|
518 | |||
519 | /** |
||
520 | * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays) |
||
521 | * |
||
522 | * @param array $array FindAndReplaceData array |
||
523 | * |
||
524 | * @return array |
||
525 | */ |
||
526 | 4 | protected function buildFindAndReplaceDataArray(array $array) |
|
536 | |||
537 | } |