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) |
|
124 | |||
125 | /** |
||
126 | * set CURLOPT_HTTPHEADER. |
||
127 | * |
||
128 | * @param string $action SOAP action |
||
129 | * @param int $version SOAP version |
||
130 | * @return void |
||
131 | */ |
||
132 | 12 | private function ___configHeader($action, $version) |
|
148 | |||
149 | /** |
||
150 | * set CURLOPT_ENCODING. |
||
151 | * |
||
152 | * @return void |
||
153 | */ |
||
154 | 12 | private function ___configCompression() |
|
166 | |||
167 | /** |
||
168 | * set CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT. |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | 12 | private function ___configTimeout() |
|
181 | |||
182 | /** |
||
183 | * set CURLOPT_HTTPAUTH. |
||
184 | * |
||
185 | * @return void |
||
186 | */ |
||
187 | 12 | private function ___configHttpAuthentication() |
|
199 | |||
200 | /** |
||
201 | * set proxy options. |
||
202 | * |
||
203 | * @return void |
||
204 | */ |
||
205 | 12 | private function ___configProxy() |
|
223 | |||
224 | /** |
||
225 | * Request cURL. |
||
226 | * |
||
227 | * @param[in] string $location SOAP address |
||
228 | * @throws \SoapFault |
||
229 | * @return mixed response body |
||
230 | */ |
||
231 | 12 | private function ___curlCall($location) |
|
317 | } |
||
318 |
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: