Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Configuration 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 Configuration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Configuration |
||
14 | { |
||
15 | public static $global; |
||
16 | |||
17 | private $_environment = null; |
||
18 | private $_merchantId = null; |
||
19 | private $_publicKey = null; |
||
20 | private $_privateKey = null; |
||
21 | private $_clientId = null; |
||
22 | private $_clientSecret = null; |
||
23 | private $_accessToken = null; |
||
24 | private $_proxyHost = null; |
||
25 | private $_proxyPort = null; |
||
26 | private $_proxyType = null; |
||
27 | private $_proxyUser = null; |
||
28 | private $_proxyPassword = null; |
||
29 | private $_timeout = 60; |
||
30 | |||
31 | /** |
||
32 | * Braintree API version to use |
||
33 | * @access public |
||
34 | */ |
||
35 | const API_VERSION = 4; |
||
36 | |||
37 | public function __construct($attribs = []) |
||
68 | |||
69 | /** |
||
70 | * resets configuration to default |
||
71 | * @access public |
||
72 | */ |
||
73 | public static function reset() |
||
77 | |||
78 | public static function gateway() |
||
82 | |||
83 | public static function environment($value=null) |
||
91 | |||
92 | public static function merchantId($value=null) |
||
99 | |||
100 | public static function publicKey($value=null) |
||
107 | |||
108 | public static function privateKey($value=null) |
||
115 | |||
116 | /** |
||
117 | * Sets or gets the read timeout to use for making requests. |
||
118 | * |
||
119 | * @param integer $value If provided, sets the read timeout |
||
120 | * @return integer The read timeout used for connecting to Braintree |
||
121 | */ |
||
122 | public static function timeout($value=null) |
||
129 | |||
130 | /** |
||
131 | * Sets or gets the proxy host to use for connecting to Braintree |
||
132 | * |
||
133 | * @param string $value If provided, sets the proxy host |
||
134 | * @return string The proxy host used for connecting to Braintree |
||
135 | */ |
||
136 | public static function proxyHost($value = null) |
||
143 | |||
144 | /** |
||
145 | * Sets or gets the port of the proxy to use for connecting to Braintree |
||
146 | * |
||
147 | * @param string $value If provided, sets the port of the proxy |
||
148 | * @return string The port of the proxy used for connecting to Braintree |
||
149 | */ |
||
150 | public static function proxyPort($value = null) |
||
157 | |||
158 | /** |
||
159 | * Sets or gets the proxy type to use for connecting to Braintree. This value |
||
160 | * can be any of the CURLOPT_PROXYTYPE options in PHP cURL. |
||
161 | * |
||
162 | * @param string $value If provided, sets the proxy type |
||
163 | * @return string The proxy type used for connecting to Braintree |
||
164 | */ |
||
165 | public static function proxyType($value = null) |
||
172 | |||
173 | /** |
||
174 | * Specifies whether or not a proxy is properly configured |
||
175 | * |
||
176 | * @return bool true if a proxy is configured properly, false if not |
||
177 | */ |
||
178 | public static function isUsingProxy() |
||
184 | |||
185 | public static function proxyUser($value = null) |
||
192 | |||
193 | public static function proxyPassword($value = null) |
||
200 | |||
201 | /** |
||
202 | * Specified whether or not a username and password have been provided for |
||
203 | * use with an authenticated proxy |
||
204 | * |
||
205 | * @return bool true if both proxyUser and proxyPassword are present |
||
206 | */ |
||
207 | public static function isAuthenticatedProxy() |
||
213 | |||
214 | public static function assertGlobalHasAccessTokenOrKeys() |
||
218 | |||
219 | public function assertHasAccessTokenOrKeys() |
||
233 | |||
234 | public function assertHasClientCredentials() |
||
239 | |||
240 | public function assertHasClientId() |
||
246 | |||
247 | public function assertHasClientSecret() |
||
253 | |||
254 | public function getEnvironment() |
||
258 | |||
259 | /** |
||
260 | * Do not use this method directly. Pass in the environment to the constructor. |
||
261 | */ |
||
262 | public function setEnvironment($value) |
||
266 | |||
267 | public function getMerchantId() |
||
271 | |||
272 | /** |
||
273 | * Do not use this method directly. Pass in the merchantId to the constructor. |
||
274 | */ |
||
275 | public function setMerchantId($value) |
||
279 | |||
280 | public function getPublicKey() |
||
284 | |||
285 | public function getClientId() |
||
289 | |||
290 | /** |
||
291 | * Do not use this method directly. Pass in the publicKey to the constructor. |
||
292 | */ |
||
293 | public function setPublicKey($value) |
||
297 | |||
298 | public function getPrivateKey() |
||
302 | |||
303 | public function getClientSecret() |
||
307 | |||
308 | /** |
||
309 | * Do not use this method directly. Pass in the privateKey to the constructor. |
||
310 | */ |
||
311 | public function setPrivateKey($value) |
||
315 | |||
316 | private function setProxyHost($value) |
||
320 | |||
321 | public function getProxyHost() |
||
325 | |||
326 | private function setProxyPort($value) |
||
330 | |||
331 | public function getProxyPort() |
||
335 | |||
336 | private function setProxyType($value) |
||
340 | |||
341 | public function getProxyType() |
||
345 | |||
346 | private function setProxyUser($value) |
||
350 | |||
351 | public function getProxyUser() |
||
355 | |||
356 | private function setProxyPassword($value) |
||
360 | |||
361 | public function getProxyPassword() |
||
365 | |||
366 | private function setTimeout($value) |
||
370 | |||
371 | public function getTimeout() |
||
375 | |||
376 | public function getAccessToken() |
||
380 | |||
381 | public function isAccessToken() |
||
385 | |||
386 | public function isClientCredentials() |
||
390 | /** |
||
391 | * returns the base braintree gateway URL based on config values |
||
392 | * |
||
393 | * @access public |
||
394 | * @param none |
||
395 | * @return string braintree gateway URL |
||
396 | */ |
||
397 | public function baseUrl() |
||
401 | |||
402 | /** |
||
403 | * sets the merchant path based on merchant ID |
||
404 | * |
||
405 | * @access protected |
||
406 | * @param none |
||
407 | * @return string merchant path uri |
||
408 | */ |
||
409 | public function merchantPath() |
||
413 | |||
414 | /** |
||
415 | * sets the physical path for the location of the CA certs |
||
416 | * |
||
417 | * @access public |
||
418 | * @param none |
||
419 | * @return string filepath |
||
420 | */ |
||
421 | public function caFile($sslPath = NULL) |
||
434 | |||
435 | /** |
||
436 | * returns the port number depending on environment |
||
437 | * |
||
438 | * @access public |
||
439 | * @param none |
||
440 | * @return int portnumber |
||
441 | */ |
||
442 | public function portNumber() |
||
449 | |||
450 | /** |
||
451 | * returns http protocol depending on environment |
||
452 | * |
||
453 | * @access public |
||
454 | * @param none |
||
455 | * @return string http || https |
||
456 | */ |
||
457 | public function protocol() |
||
461 | |||
462 | /** |
||
463 | * returns gateway server name depending on environment |
||
464 | * |
||
465 | * @access public |
||
466 | * @param none |
||
467 | * @return string server domain name |
||
468 | */ |
||
469 | View Code Duplication | public function serverName() |
|
490 | |||
491 | View Code Duplication | public function authUrl() |
|
512 | |||
513 | /** |
||
514 | * returns boolean indicating SSL is on or off for this session, |
||
515 | * depending on environment |
||
516 | * |
||
517 | * @access public |
||
518 | * @param none |
||
519 | * @return boolean |
||
520 | */ |
||
521 | public function sslOn() |
||
538 | |||
539 | /** |
||
540 | * log message to default logger |
||
541 | * |
||
542 | * @param string $message |
||
543 | * |
||
544 | */ |
||
545 | public function logMessage($message) |
||
549 | } |
||
550 | Configuration::reset(); |
||
552 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.