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 CurlSoapClient 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 CurlSoapClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class CurlSoapClient extends SoapClient |
||
18 | { |
||
19 | protected $curl = null; ///< cURL handle |
||
20 | protected $redirect_max; ///< max redirect counts |
||
21 | protected $curl_timeout; ///< cURL request time-out seconds |
||
22 | private $redirect_count = 0; |
||
23 | |||
24 | 12 | public function __construct($wsdl, array $options) |
|
25 | { |
||
26 | 12 | parent::__construct($wsdl, $options); |
|
1 ignored issue
–
show
|
|||
27 | 12 | $this->redirect_max = 5; |
|
28 | 12 | if (isset($options['redirect_max'])) { |
|
29 | 1 | $this->redirect_max = (int)$options['redirect_max']; |
|
30 | 1 | } |
|
31 | 12 | $this->curl_timeout = 30; |
|
32 | 12 | if (isset($options['curl_timeout'])) { |
|
33 | 1 | $this->curl_timeout = (int)$options['curl_timeout']; |
|
34 | 1 | } |
|
35 | 12 | $this->curl = curl_init(); |
|
36 | 12 | $this->_cookies = array(); |
|
1 ignored issue
–
show
|
|||
37 | 12 | } |
|
38 | |||
39 | 12 | public function __destruct() |
|
40 | { |
||
41 | 12 | if (isset($this->curl)) { |
|
42 | 12 | curl_close($this->curl); |
|
43 | 12 | } |
|
44 | 12 | } |
|
45 | |||
46 | 1 | public function ___curlSetOpt($option, $value) |
|
50 | |||
51 | 1 | public function __getCookies() |
|
55 | |||
56 | 1 | public function __setCookie($name, $value = null) |
|
64 | |||
65 | /** |
||
66 | * Execute SOAP requests. |
||
67 | * |
||
68 | * @param string $request SOAP request |
||
69 | * @param string $location SOAP address |
||
70 | * @param string $action SOAP action |
||
71 | * @param int $version SOAP version |
||
72 | * @param int $one_way |
||
73 | * @throws \Exception |
||
74 | * @throws \SoapFault |
||
75 | * @return mixed (string) SOAP response / (object) SoapFault object |
||
76 | */ |
||
77 | 12 | public function __doRequest($request, $location, $action, $version, $one_way = 0) |
|
111 | |||
112 | /** |
||
113 | * set CURLOPT_HTTPHEADER. |
||
114 | * |
||
115 | * @param string $action SOAP action |
||
116 | * @param int $version SOAP version |
||
117 | * @return void |
||
118 | */ |
||
119 | 12 | private function ___configHeader($action, $version) |
|
135 | |||
136 | /** |
||
137 | * set CURLOPT_ENCODING. |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | 12 | private function ___configCompression() |
|
153 | |||
154 | /** |
||
155 | * set CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT. |
||
156 | * |
||
157 | * @return void |
||
158 | */ |
||
159 | 12 | private function ___configTimeout() |
|
168 | |||
169 | /** |
||
170 | * set CURLOPT_HTTPAUTH. |
||
171 | * |
||
172 | * @return void |
||
173 | */ |
||
174 | 12 | private function ___configHttpAuthentication() |
|
186 | |||
187 | /** |
||
188 | * set proxy options. |
||
189 | * |
||
190 | * @return void |
||
191 | */ |
||
192 | 12 | private function ___configProxy() |
|
210 | |||
211 | /** |
||
212 | * Request cURL. |
||
213 | * |
||
214 | * @param[in] string $location SOAP address |
||
215 | * @throws \SoapFault |
||
216 | * @return mixed response body |
||
217 | */ |
||
218 | 12 | private function ___curlCall($location) |
|
304 | } |
||
305 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: