Complex classes like Domrobot 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 Domrobot, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Domrobot |
||
16 | { |
||
17 | private $debug = false; |
||
18 | private $address; |
||
19 | private $language; |
||
20 | private $customer = ""; |
||
21 | private $clTRID = null; |
||
22 | |||
23 | private $_ver = "2.4"; |
||
24 | private $_cachedir; |
||
25 | private $_cookiefile = NULL; |
||
26 | private $loginResult = NULL; |
||
27 | |||
28 | /** |
||
29 | * Domrobot constructor. |
||
30 | * @param $address |
||
31 | * @param string $locale |
||
32 | * @param $cache_dir |
||
33 | * @param $username |
||
34 | * @param $password |
||
35 | * @param null $sharedSecret |
||
36 | */ |
||
37 | public function __construct($address, $locale = 'en', $cache_dir, $username, $password, $sharedSecret = null) |
||
45 | |||
46 | /** |
||
47 | * @param string $name |
||
48 | * @param $ip |
||
49 | * @param string $domain |
||
50 | * @param string $type |
||
51 | * @param int $ttl |
||
52 | * @return mixed|string |
||
53 | */ |
||
54 | public function createRecord($name, $ip, $domain = 'somedomainnameyouwishtoupgrade.de', $type = 'A', $ttl = 3600) |
||
72 | |||
73 | /** |
||
74 | * @param integer $id |
||
75 | * @param $ip |
||
76 | * @return mixed |
||
77 | */ |
||
78 | public function updateRecord($id, $ip) |
||
86 | |||
87 | /** |
||
88 | * @param integer $id |
||
89 | * @param bool $testing |
||
90 | * @return mixed |
||
91 | */ |
||
92 | public function deleteRecord($id, $testing = false) |
||
100 | |||
101 | /** |
||
102 | * @param $username |
||
103 | * @param $password |
||
104 | * @param null $sharedSecret |
||
105 | * @return mixed |
||
106 | */ |
||
107 | public function login($username, $password, $sharedSecret = null) |
||
134 | |||
135 | /** |
||
136 | * @param string $object |
||
137 | * @param string $method |
||
138 | * @param array $params |
||
139 | * @return mixed |
||
140 | */ |
||
141 | public function call($object, $method, array $params = array()) |
||
175 | |||
176 | /** |
||
177 | * @param $secret |
||
178 | * @return string |
||
179 | */ |
||
180 | private function _getSecretCode($secret) |
||
204 | |||
205 | /** |
||
206 | * @param $secret |
||
207 | * @return string|false |
||
208 | */ |
||
209 | private function _base32Decode($secret) |
||
250 | |||
251 | /** |
||
252 | * @return string[] |
||
253 | */ |
||
254 | private function _getBase32LookupTable() |
||
264 | |||
265 | /** |
||
266 | * logout on destruct |
||
267 | */ |
||
268 | public function __destruct() |
||
272 | |||
273 | /** |
||
274 | * @return mixed |
||
275 | */ |
||
276 | public function logout() |
||
284 | |||
285 | /** |
||
286 | * @return string |
||
287 | */ |
||
288 | public function getLanguage() |
||
292 | |||
293 | /** |
||
294 | * @param string $language |
||
295 | */ |
||
296 | public function setLanguage($language) |
||
300 | |||
301 | /** |
||
302 | * @return bool |
||
303 | */ |
||
304 | public function getDebug() |
||
308 | |||
309 | /** |
||
310 | * @param bool $debug |
||
311 | */ |
||
312 | public function setDebug($debug = false) |
||
316 | |||
317 | /** |
||
318 | * @return string |
||
319 | */ |
||
320 | public function getCookiefile() |
||
324 | |||
325 | /** |
||
326 | * @param $file |
||
327 | * @throws \Exception |
||
328 | */ |
||
329 | public function setCookiefile($file) |
||
336 | |||
337 | /** |
||
338 | * @return string |
||
339 | */ |
||
340 | public function getCustomer() |
||
344 | |||
345 | /** |
||
346 | * @param $customer |
||
347 | */ |
||
348 | public function setCustomer($customer) |
||
352 | |||
353 | /** |
||
354 | * @return string |
||
355 | */ |
||
356 | public function getClTrId() |
||
360 | |||
361 | /** |
||
362 | * @param $clTrId |
||
363 | */ |
||
364 | public function setClTrId($clTrId) |
||
368 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: