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 | |||
28 | protected $protocol = 'https'; |
||
29 | protected $url = ''; |
||
30 | protected $type = 'json'; |
||
31 | protected $output_type; |
||
32 | protected $_guzzleOptions = []; |
||
33 | protected $_custom_guzzle_options = []; |
||
34 | protected $_guzzle; |
||
35 | protected $_errors; |
||
36 | |||
37 | /** |
||
38 | * Client constructor. |
||
39 | * |
||
40 | * @param array $config |
||
41 | */ |
||
42 | public function __construct(array $config = []) |
||
50 | |||
51 | public function init() |
||
58 | |||
59 | /** |
||
60 | * @param bool $asString |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getError($asString = true) |
||
74 | |||
75 | public function validate(array $data) |
||
82 | |||
83 | /** |
||
84 | * @param array $data |
||
85 | * @return array |
||
86 | */ |
||
87 | public function filter(array $data) |
||
97 | |||
98 | /** |
||
99 | * @param $param |
||
100 | * @param $value |
||
101 | * @return mixed |
||
102 | */ |
||
103 | public function filterParam($param, $value) |
||
107 | |||
108 | /** |
||
109 | * @param $param |
||
110 | * @param $value |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function validateParam($param, $value) |
||
117 | |||
118 | public function setGuzzleOptions($array) |
||
122 | |||
123 | /** |
||
124 | * @param $url |
||
125 | * @return string |
||
126 | */ |
||
127 | protected function buildUrl($url) |
||
135 | |||
136 | /** |
||
137 | * @return GuzzleClient |
||
138 | */ |
||
139 | public function getGuzzle() |
||
143 | |||
144 | /** |
||
145 | * @param $urlRequest |
||
146 | * @param array $data |
||
147 | * @return string|\stdClass|\SimpleXMLElement |
||
148 | */ |
||
149 | public function getContent($urlRequest, $data = []) |
||
167 | |||
168 | /** |
||
169 | * @param $data |
||
170 | * @return mixed |
||
171 | */ |
||
172 | protected function unSerializeJson($data) |
||
176 | |||
177 | /** |
||
178 | * @param $data |
||
179 | * @return \SimpleXMLElement |
||
180 | */ |
||
181 | protected function unSerializeXml($data) |
||
185 | |||
186 | /** |
||
187 | * @param $data |
||
188 | * @return mixed|null|\SimpleXMLElement |
||
189 | */ |
||
190 | public function unSerialize($data) |
||
204 | |||
205 | /** |
||
206 | * @param $param |
||
207 | * @param $message |
||
208 | */ |
||
209 | public function addError($param, $message) |
||
213 | |||
214 | /** |
||
215 | * @param array $data |
||
216 | * @return array |
||
217 | */ |
||
218 | protected function beforePrepareData(array $data) |
||
222 | |||
223 | /** |
||
224 | * @param $data |
||
225 | * @return string|array |
||
226 | * @throws \Exception |
||
227 | */ |
||
228 | protected function prepareData(array $data) |
||
254 | |||
255 | /** |
||
256 | * @throws \Exception |
||
257 | */ |
||
258 | public function guzzleOptions() |
||
285 | } |
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: