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 | 59 | public function __construct(array $options = []) |
|
169 | |||
170 | /** |
||
171 | * Setters and getters |
||
172 | * ================================================================================================================= |
||
173 | */ |
||
174 | |||
175 | /** |
||
176 | * Return the REST client of the backend web service |
||
177 | * |
||
178 | * @return \GuzzleHttp\Client |
||
179 | */ |
||
180 | 18 | public function getClient() |
|
201 | |||
202 | /** |
||
203 | * Set the REST client of the backend web service |
||
204 | * |
||
205 | * @param Client $client REST client |
||
206 | * |
||
207 | * @return ReportingCloud |
||
208 | */ |
||
209 | 18 | public function setClient(Client $client) |
|
215 | |||
216 | /** |
||
217 | * Return the base URI of the backend web service |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | 22 | public function getBaseUri() |
|
229 | |||
230 | /** |
||
231 | * Set the base URI of the backend web service |
||
232 | * |
||
233 | * @param string $baseUri Base URI |
||
234 | * |
||
235 | * @return ReportingCloud |
||
236 | */ |
||
237 | 22 | public function setBaseUri($baseUri) |
|
243 | |||
244 | /** |
||
245 | * Get the timeout (in seconds) of the backend web service |
||
246 | * |
||
247 | * @return integer |
||
248 | */ |
||
249 | 21 | public function getTimeout() |
|
257 | |||
258 | /** |
||
259 | * Set the timeout (in seconds) of the backend web service |
||
260 | * |
||
261 | * @param integer $timeout Timeout |
||
262 | * |
||
263 | * @return ReportingCloud |
||
264 | */ |
||
265 | 21 | public function setTimeout($timeout) |
|
271 | |||
272 | /** |
||
273 | * Return the username |
||
274 | * |
||
275 | * @return string |
||
276 | */ |
||
277 | 21 | public function getUsername() |
|
281 | |||
282 | /** |
||
283 | * Set the username |
||
284 | * |
||
285 | * @param string $username Username |
||
286 | * |
||
287 | * @return ReportingCloud |
||
288 | */ |
||
289 | 56 | public function setUsername($username) |
|
295 | |||
296 | /** |
||
297 | * Return the password |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | 21 | public function getPassword() |
|
305 | |||
306 | /** |
||
307 | * Set the password |
||
308 | * |
||
309 | * @param string $password Password |
||
310 | * |
||
311 | * @return ReportingCloud |
||
312 | */ |
||
313 | 56 | public function setPassword($password) |
|
319 | |||
320 | /** |
||
321 | * Return the test flag |
||
322 | * |
||
323 | * @return mixed |
||
324 | */ |
||
325 | 19 | public function getTest() |
|
333 | |||
334 | /** |
||
335 | * Set the test flag |
||
336 | * |
||
337 | * @param boolean $test Test flag |
||
338 | * |
||
339 | * @return ReportingCloud |
||
340 | */ |
||
341 | 19 | public function setTest($test) |
|
347 | |||
348 | /** |
||
349 | * Return the debug flag |
||
350 | * |
||
351 | * @return mixed |
||
352 | */ |
||
353 | 21 | public function getDebug() |
|
361 | |||
362 | /** |
||
363 | * Set the debug flag |
||
364 | * |
||
365 | * @param boolean $debug Debug flag |
||
366 | * |
||
367 | * @return ReportingCloud |
||
368 | */ |
||
369 | 21 | public function setDebug($debug) |
|
375 | |||
376 | /** |
||
377 | * Get the version string of the backend web service |
||
378 | * |
||
379 | * @return string |
||
380 | */ |
||
381 | 20 | public function getVersion() |
|
389 | |||
390 | /** |
||
391 | * Set the version string of the backend web service |
||
392 | * |
||
393 | * @param string $version Version string |
||
394 | * |
||
395 | * @return ReportingCloud |
||
396 | */ |
||
397 | 2 | public function setVersion($version) |
|
403 | |||
404 | |||
405 | /** |
||
406 | * Utility methods |
||
407 | * ================================================================================================================= |
||
408 | */ |
||
409 | |||
410 | /** |
||
411 | * Request the URI with options |
||
412 | * |
||
413 | * @param string $method HTTP method |
||
414 | * @param string $uri URI |
||
415 | * @param array $options Options |
||
416 | * |
||
417 | * @return mixed|null|\Psr\Http\Message\ResponseInterface |
||
418 | * |
||
419 | * @throws RuntimeException |
||
420 | */ |
||
421 | 17 | protected function request($method, $uri, $options) |
|
452 | |||
453 | /** |
||
454 | * Construct URI with version number |
||
455 | * |
||
456 | * @param string $uri URI |
||
457 | * |
||
458 | * @return string |
||
459 | */ |
||
460 | 17 | protected function uri($uri) |
|
464 | |||
465 | /** |
||
466 | * Using the passed propertyMap, recursively build array |
||
467 | * |
||
468 | * @param array $array Array |
||
469 | * @param PropertyMap $propertyMap PropertyMap |
||
470 | * |
||
471 | * @return array |
||
472 | */ |
||
473 | 5 | protected function buildPropertyMapArray($array, PropertyMap $propertyMap) |
|
490 | |||
491 | /** |
||
492 | * Using passed mergeSettings array, build array for backend |
||
493 | * |
||
494 | * @param array $array MergeSettings array |
||
495 | * |
||
496 | * @return array |
||
497 | */ |
||
498 | 8 | protected function buildMergeSettingsArray($array) |
|
520 | |||
521 | /** |
||
522 | * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays) |
||
523 | * |
||
524 | * @param array $array FindAndReplaceData array |
||
525 | * |
||
526 | * @return array |
||
527 | */ |
||
528 | 4 | protected function buildFindAndReplaceDataArray($array) |
|
538 | |||
539 | } |