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 | * Default date/time format of backend is 'ISO 8601' |
||
33 | * |
||
34 | * Note, last letter is 'P' and not 'O': |
||
35 | * |
||
36 | * O - Difference to Greenwich time (GMT) in hours (e.g. +0200) |
||
37 | * P - Difference to Greenwich time (GMT) with colon between hours and minutes (e.g. +02:00) |
||
38 | * |
||
39 | * Backend uses the 'P' variant |
||
40 | * |
||
41 | * @const DEFAULT_DATE_FORMAT |
||
42 | */ |
||
43 | const DEFAULT_DATE_FORMAT = 'Y-m-d\TH:i:sP'; |
||
44 | |||
45 | /** |
||
46 | * Default time zone of backend |
||
47 | * |
||
48 | * @const DEFAULT_TIME_ZONE |
||
49 | */ |
||
50 | const DEFAULT_TIME_ZONE = 'UTC'; |
||
51 | |||
52 | /** |
||
53 | * Default base URI of backend |
||
54 | * |
||
55 | * @const DEFAULT_BASE_URI |
||
56 | */ |
||
57 | const DEFAULT_BASE_URI = 'https://api.reporting.cloud'; |
||
58 | |||
59 | /** |
||
60 | * Default version string of backend |
||
61 | * |
||
62 | * @const DEFAULT_VERSION |
||
63 | */ |
||
64 | const DEFAULT_VERSION = 'v1'; |
||
65 | |||
66 | /** |
||
67 | * Default timeout of backend in seconds |
||
68 | * |
||
69 | * @const DEFAULT_TIMEOUT |
||
70 | */ |
||
71 | const DEFAULT_TIMEOUT = 120; // seconds |
||
72 | |||
73 | /** |
||
74 | * Default test flag of backend |
||
75 | * |
||
76 | * @const DEFAULT_TEST |
||
77 | */ |
||
78 | const DEFAULT_TEST = false; |
||
79 | |||
80 | /** |
||
81 | * Default debug flag of REST client |
||
82 | * |
||
83 | * @const DEFAULT_DEBUG |
||
84 | */ |
||
85 | const DEFAULT_DEBUG = false; |
||
86 | |||
87 | /** |
||
88 | * Backend username |
||
89 | * |
||
90 | * @var string |
||
91 | */ |
||
92 | protected $username; |
||
93 | |||
94 | /** |
||
95 | * Backend password |
||
96 | * |
||
97 | * @var string |
||
98 | */ |
||
99 | protected $password; |
||
100 | |||
101 | /** |
||
102 | * When true, backend prints "TEST MODE" water mark into output document, and API call does not count against quota |
||
103 | * |
||
104 | * @var boolean |
||
105 | */ |
||
106 | protected $test; |
||
107 | |||
108 | /** |
||
109 | * Backend base URI |
||
110 | * |
||
111 | * @var string |
||
112 | */ |
||
113 | protected $baseUri; |
||
114 | |||
115 | /** |
||
116 | * Backend version string |
||
117 | * |
||
118 | * @var string |
||
119 | */ |
||
120 | protected $version; |
||
121 | |||
122 | /** |
||
123 | * Backend timeout in seconds |
||
124 | * |
||
125 | * @var integer |
||
126 | */ |
||
127 | protected $timeout; |
||
128 | |||
129 | /** |
||
130 | * REST client to backend |
||
131 | * |
||
132 | * @var Client |
||
133 | */ |
||
134 | protected $client; |
||
135 | |||
136 | /** |
||
137 | * Debug flag of REST client |
||
138 | * |
||
139 | * @var boolean |
||
140 | */ |
||
141 | protected $debug; |
||
142 | |||
143 | /** |
||
144 | * AbstractReportingCloud constructor |
||
145 | * |
||
146 | * @param array $options |
||
147 | */ |
||
148 | 59 | public function __construct($options = []) |
|
180 | |||
181 | /** |
||
182 | * Setters and getters |
||
183 | * ================================================================================================================= |
||
184 | */ |
||
185 | |||
186 | /** |
||
187 | * Return the REST client of the backend web service |
||
188 | * |
||
189 | * @return \GuzzleHttp\Client |
||
190 | */ |
||
191 | 18 | public function getClient() |
|
213 | |||
214 | /** |
||
215 | * Set the REST client of the backend web service |
||
216 | * |
||
217 | * @param Client $client REST client |
||
218 | * |
||
219 | * @return ReportingCloud |
||
220 | */ |
||
221 | 18 | public function setClient(Client $client) |
|
227 | |||
228 | /** |
||
229 | * Return the base URI of the backend web service |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | 22 | public function getBaseUri() |
|
241 | |||
242 | /** |
||
243 | * Set the base URI of the backend web service |
||
244 | * |
||
245 | * @param string $baseUri Base URI |
||
246 | * |
||
247 | * @return ReportingCloud |
||
248 | */ |
||
249 | 22 | public function setBaseUri($baseUri) |
|
255 | |||
256 | /** |
||
257 | * Get the timeout (in seconds) of the backend web service |
||
258 | * |
||
259 | * @return integer |
||
260 | */ |
||
261 | 21 | public function getTimeout() |
|
269 | |||
270 | /** |
||
271 | * Set the timeout (in seconds) of the backend web service |
||
272 | * |
||
273 | * @param integer $timeout Timeout |
||
274 | * |
||
275 | * @return ReportingCloud |
||
276 | */ |
||
277 | 21 | public function setTimeout($timeout) |
|
283 | |||
284 | /** |
||
285 | * Return the username |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | 21 | public function getUsername() |
|
293 | |||
294 | /** |
||
295 | * Set the username |
||
296 | * |
||
297 | * @param string $username Username |
||
298 | * |
||
299 | * @return ReportingCloud |
||
300 | */ |
||
301 | 56 | public function setUsername($username) |
|
307 | |||
308 | /** |
||
309 | * Return the password |
||
310 | * |
||
311 | * @return string |
||
312 | */ |
||
313 | 21 | public function getPassword() |
|
317 | |||
318 | /** |
||
319 | * Set the password |
||
320 | * |
||
321 | * @param string $password Password |
||
322 | * |
||
323 | * @return ReportingCloud |
||
324 | */ |
||
325 | 56 | public function setPassword($password) |
|
331 | |||
332 | /** |
||
333 | * Return the test flag |
||
334 | * |
||
335 | * @return mixed |
||
336 | */ |
||
337 | 19 | public function getTest() |
|
345 | |||
346 | /** |
||
347 | * Set the test flag |
||
348 | * |
||
349 | * @param boolean $test Test flag |
||
350 | * |
||
351 | * @return ReportingCloud |
||
352 | */ |
||
353 | 19 | public function setTest($test) |
|
359 | |||
360 | /** |
||
361 | * Return the debug flag |
||
362 | * |
||
363 | * @return mixed |
||
364 | */ |
||
365 | 21 | public function getDebug() |
|
373 | |||
374 | /** |
||
375 | * Set the debug flag |
||
376 | * |
||
377 | * @param boolean $debug Debug flag |
||
378 | * |
||
379 | * @return ReportingCloud |
||
380 | */ |
||
381 | 21 | public function setDebug($debug) |
|
387 | |||
388 | /** |
||
389 | * Get the version string of the backend web service |
||
390 | * |
||
391 | * @return string |
||
392 | */ |
||
393 | 20 | public function getVersion() |
|
401 | |||
402 | /** |
||
403 | * Set the version string of the backend web service |
||
404 | * |
||
405 | * @param string $version Version string |
||
406 | * |
||
407 | * @return ReportingCloud |
||
408 | */ |
||
409 | 2 | public function setVersion($version) |
|
415 | |||
416 | |||
417 | /** |
||
418 | * Utility methods |
||
419 | * ================================================================================================================= |
||
420 | */ |
||
421 | |||
422 | /** |
||
423 | * Request the URI with options |
||
424 | * |
||
425 | * @param string $method HTTP method |
||
426 | * @param string $uri URI |
||
427 | * @param array $options Options |
||
428 | * |
||
429 | * @return mixed|null|\Psr\Http\Message\ResponseInterface |
||
430 | * |
||
431 | * @throws RuntimeException |
||
432 | */ |
||
433 | 17 | protected function request($method, $uri, $options) |
|
464 | |||
465 | /** |
||
466 | * Construct URI with version number |
||
467 | * |
||
468 | * @param string $uri URI |
||
469 | * |
||
470 | * @return string |
||
471 | */ |
||
472 | 17 | protected function uri($uri) |
|
476 | |||
477 | /** |
||
478 | * Using the passed propertyMap, recursively build array |
||
479 | * |
||
480 | * @param array $array Array |
||
481 | * @param PropertyMap $propertyMap PropertyMap |
||
482 | * |
||
483 | * @return array |
||
484 | */ |
||
485 | 5 | protected function buildPropertyMapArray($array, PropertyMap $propertyMap) |
|
502 | |||
503 | /** |
||
504 | * Using passed mergeSettings array, build array for backend |
||
505 | * |
||
506 | * @param array $array MergeSettings array |
||
507 | * |
||
508 | * @return array |
||
509 | */ |
||
510 | 8 | protected function buildMergeSettingsArray($array) |
|
532 | |||
533 | /** |
||
534 | * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays) |
||
535 | * |
||
536 | * @param array $array FindAndReplaceData array |
||
537 | * |
||
538 | * @return array |
||
539 | */ |
||
540 | 4 | protected function buildFindAndReplaceDataArray($array) |
|
550 | |||
551 | } |