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 |
||
30 | abstract class AbstractReportingCloud |
||
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 debug flag of REST client |
||
76 | * |
||
77 | * @const DEFAULT_DEBUG |
||
78 | */ |
||
79 | const DEFAULT_DEBUG = false; |
||
80 | |||
81 | /** |
||
82 | * Backend username |
||
83 | * |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $username; |
||
87 | |||
88 | /** |
||
89 | * Backend password |
||
90 | * |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $password; |
||
94 | |||
95 | /** |
||
96 | * Backend base URI |
||
97 | * |
||
98 | * @var string |
||
99 | */ |
||
100 | protected $baseUri; |
||
101 | |||
102 | /** |
||
103 | * Backend version string |
||
104 | * |
||
105 | * @var string |
||
106 | */ |
||
107 | protected $version; |
||
108 | |||
109 | /** |
||
110 | * Backend timeout in seconds |
||
111 | * |
||
112 | * @var integer |
||
113 | */ |
||
114 | protected $timeout; |
||
115 | |||
116 | /** |
||
117 | * REST client to backend |
||
118 | * |
||
119 | * @var Client |
||
120 | */ |
||
121 | protected $client; |
||
122 | |||
123 | /** |
||
124 | * Debug flag of REST client |
||
125 | * |
||
126 | * @var boolean |
||
127 | */ |
||
128 | protected $debug; |
||
129 | |||
130 | /** |
||
131 | * AbstractReportingCloud constructor |
||
132 | * |
||
133 | * @param array $options |
||
134 | */ |
||
135 | 46 | public function __construct($options = []) |
|
161 | |||
162 | /** |
||
163 | * Return the REST client of the backend web service |
||
164 | * |
||
165 | * @return \GuzzleHttp\Client |
||
166 | */ |
||
167 | 14 | public function getClient() |
|
188 | |||
189 | /** |
||
190 | * Set the REST client of the backend web service |
||
191 | * |
||
192 | * @param Client $client REST client |
||
193 | * |
||
194 | * @return ReportingCloud |
||
195 | */ |
||
196 | 14 | public function setClient(Client $client) |
|
202 | |||
203 | /** |
||
204 | * Return the base URI of the backend web service |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | 18 | public function getBaseUri() |
|
216 | |||
217 | /** |
||
218 | * Set the base URI of the backend web service |
||
219 | * |
||
220 | * @param string $baseUri Base URI |
||
221 | * |
||
222 | * @return ReportingCloud |
||
223 | */ |
||
224 | 18 | public function setBaseUri($baseUri) |
|
230 | |||
231 | /** |
||
232 | * Get the timeout (in seconds) of the backend web service |
||
233 | * |
||
234 | * @return integer |
||
235 | */ |
||
236 | 17 | public function getTimeout() |
|
244 | |||
245 | /** |
||
246 | * Set the timeout (in seconds) of the backend web service |
||
247 | * |
||
248 | * @param integer $timeout Timeout |
||
249 | * |
||
250 | * @return ReportingCloud |
||
251 | */ |
||
252 | 17 | public function setTimeout($timeout) |
|
258 | |||
259 | /** |
||
260 | * Return the username |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | 17 | public function getUsername() |
|
268 | |||
269 | /** |
||
270 | * Set the username |
||
271 | * |
||
272 | * @param string $username Username |
||
273 | * |
||
274 | * @return ReportingCloud |
||
275 | */ |
||
276 | 43 | public function setUsername($username) |
|
282 | |||
283 | /** |
||
284 | * Return the password |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | 17 | public function getPassword() |
|
292 | |||
293 | /** |
||
294 | * Set the password |
||
295 | * |
||
296 | * @param string $password Password |
||
297 | * |
||
298 | * @return ReportingCloud |
||
299 | */ |
||
300 | 43 | public function setPassword($password) |
|
306 | |||
307 | /** |
||
308 | * Return the debug flag |
||
309 | * |
||
310 | * @return mixed |
||
311 | */ |
||
312 | 17 | public function getDebug() |
|
320 | |||
321 | /** |
||
322 | * Set the debug flag |
||
323 | * |
||
324 | * @param boolean $debug Debug flag |
||
325 | * |
||
326 | * @return ReportingCloud |
||
327 | */ |
||
328 | 17 | public function setDebug($debug) |
|
334 | |||
335 | /** |
||
336 | * Construct URI with version number |
||
337 | * |
||
338 | * @param string $uri URI |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | 13 | protected function uri($uri) |
|
346 | |||
347 | /** |
||
348 | * Get the version string of the backend web service |
||
349 | * |
||
350 | * @return string |
||
351 | */ |
||
352 | 16 | public function getVersion() |
|
360 | |||
361 | /** |
||
362 | * Set the version string of the backend web service |
||
363 | * |
||
364 | * @param string $version Version string |
||
365 | * |
||
366 | * @return ReportingCloud |
||
367 | */ |
||
368 | 2 | public function setVersion($version) |
|
374 | |||
375 | /** |
||
376 | * Request the URI with options |
||
377 | * |
||
378 | * @param string $method HTTP method |
||
379 | * @param string $uri URI |
||
380 | * @param array $options Options |
||
381 | * |
||
382 | * @return mixed|null|\Psr\Http\Message\ResponseInterface |
||
383 | * |
||
384 | * @throws RuntimeException |
||
385 | */ |
||
386 | 13 | protected function request($method, $uri, $options) |
|
413 | |||
414 | /** |
||
415 | * Using the passed propertyMap, recursively normalizes the keys of the passed array |
||
416 | * |
||
417 | * @param array $array Array |
||
418 | * @param PropertyMap $propertyMap PropertyMap |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | 3 | protected function normalizeArrayKeys($array, PropertyMap $propertyMap) |
|
439 | |||
440 | /** |
||
441 | * Assemble MergeSettings array to pass to ReportingCloud |
||
442 | * |
||
443 | * @param $mergeSettings MergeSettings array |
||
444 | * @return array |
||
445 | */ |
||
446 | 4 | protected function assembleMergeSettings($mergeSettings) |
|
469 | |||
470 | /** |
||
471 | * Return array of headers to be passed with each request to ReportingCloud |
||
472 | * |
||
473 | * @return array |
||
474 | */ |
||
475 | 11 | protected function headers() |
|
483 | |||
484 | } |