Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Client |
||
11 | { |
||
12 | public $login; |
||
13 | public $password; |
||
14 | public $proxy; |
||
15 | public $method = 'GET'; |
||
16 | public $postDataInBody = false; |
||
17 | public $useAuth = true; |
||
18 | |||
19 | /** |
||
20 | * @var ResponseInterface |
||
21 | */ |
||
22 | public $request; |
||
23 | |||
24 | const TYPE_JSON = 'json'; |
||
25 | const TYPE_XML = 'xml'; |
||
26 | const TYPE_FORM = 'form'; |
||
27 | const TYPE_MULTIPART = 'multipart'; |
||
28 | |||
29 | protected $protocol = 'https'; |
||
30 | protected $url = ''; |
||
31 | protected $type = 'json'; |
||
32 | protected $output_type; |
||
33 | protected $_guzzleOptions = []; |
||
34 | protected $_custom_guzzle_options = []; |
||
35 | protected $_guzzle; |
||
36 | protected $_errors; |
||
37 | |||
38 | /** |
||
39 | * Client constructor. |
||
40 | * |
||
41 | * @param array $config |
||
42 | */ |
||
43 | public function __construct(array $config = []) |
||
51 | |||
52 | public function init() |
||
59 | |||
60 | /** |
||
61 | * @param bool $asString |
||
62 | * @return string |
||
63 | */ |
||
64 | public function getError($asString = true) |
||
75 | |||
76 | public function validate(array $data) |
||
83 | |||
84 | /** |
||
85 | * @param array $data |
||
86 | * @return array |
||
87 | */ |
||
88 | public function filter(array $data) |
||
98 | |||
99 | /** |
||
100 | * @param $param |
||
101 | * @param $value |
||
102 | * @return mixed |
||
103 | */ |
||
104 | public function filterParam($param, $value) |
||
108 | |||
109 | /** |
||
110 | * @param $param |
||
111 | * @param $value |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function validateParam($param, $value) |
||
118 | |||
119 | /** |
||
120 | * @param $array |
||
121 | */ |
||
122 | public function setGuzzleOptions($array) |
||
126 | |||
127 | /** |
||
128 | * @return array |
||
129 | */ |
||
130 | protected function customGuzzleOptions() |
||
134 | |||
135 | /** |
||
136 | * @param $a |
||
137 | * @param $b |
||
138 | * @return array|mixed |
||
139 | */ |
||
140 | protected static function merge($a, $b) |
||
163 | |||
164 | /** |
||
165 | * @param $url |
||
166 | * @return string |
||
167 | */ |
||
168 | protected function buildUrl($url) |
||
176 | |||
177 | /** |
||
178 | * @return GuzzleClient |
||
179 | */ |
||
180 | public function getGuzzle() |
||
184 | |||
185 | /** |
||
186 | * @param $urlRequest |
||
187 | * @param array $data |
||
188 | * @return string|\stdClass|\SimpleXMLElement |
||
189 | */ |
||
190 | public function getContent($urlRequest, $data = []) |
||
210 | |||
211 | /** |
||
212 | * @param $data |
||
213 | * @return mixed |
||
214 | */ |
||
215 | protected function unSerializeJson($data) |
||
219 | |||
220 | /** |
||
221 | * @param $data |
||
222 | * @return \SimpleXMLElement |
||
223 | */ |
||
224 | protected function unSerializeXml($data) |
||
228 | |||
229 | /** |
||
230 | * @param $data |
||
231 | * @return mixed|null|\SimpleXMLElement |
||
232 | */ |
||
233 | public function unSerialize($data) |
||
247 | |||
248 | /** |
||
249 | * @param $param |
||
250 | * @param $message |
||
251 | */ |
||
252 | public function addError($param, $message) |
||
256 | |||
257 | /** |
||
258 | * @param array $data |
||
259 | * @return array |
||
260 | */ |
||
261 | protected function beforePrepareData(array $data) |
||
265 | |||
266 | /** |
||
267 | * @param $data |
||
268 | * @return string|array |
||
269 | * @throws \Exception |
||
270 | */ |
||
271 | protected function prepareData(array $data) |
||
310 | |||
311 | /** |
||
312 | * @throws \Exception |
||
313 | */ |
||
314 | public function guzzleOptions() |
||
342 | } |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: