Complex classes like Connection 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 Connection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class Connection extends Component |
||
37 | { |
||
38 | const EVENT_AFTER_OPEN = 'afterOpen'; |
||
39 | |||
40 | /** |
||
41 | * @var array Config |
||
42 | */ |
||
43 | public $config = []; |
||
44 | |||
45 | public $connectionTimeout = null; |
||
46 | |||
47 | public $dataTimeout = null; |
||
48 | |||
49 | public static $curl = null; |
||
50 | |||
51 | protected static $guzzle = null; |
||
52 | |||
53 | /** |
||
54 | * Authorization config. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $_auth; |
||
59 | |||
60 | public function setAuth($auth) |
||
64 | |||
65 | public function getAuth() |
||
73 | |||
74 | public function init() |
||
80 | |||
81 | public function getHandler() |
||
89 | |||
90 | /** |
||
91 | * Closes the connection when this component is being serialized. |
||
92 | * @return array |
||
93 | */ |
||
94 | public function __sleep() |
||
98 | |||
99 | /** |
||
100 | * Returns the name of the DB driver for the current [[dsn]]. |
||
101 | * |
||
102 | * @return string name of the DB driver |
||
103 | */ |
||
104 | public function getDriverName() |
||
108 | |||
109 | /** |
||
110 | * Creates a command for execution. |
||
111 | * |
||
112 | * @param array $config the configuration for the Command class |
||
113 | * |
||
114 | * @return Command the DB command |
||
115 | */ |
||
116 | public function createCommand($config = []) |
||
123 | |||
124 | /** |
||
125 | * Creates new query builder instance. |
||
126 | * |
||
127 | * @return QueryBuilder |
||
128 | */ |
||
129 | public function getQueryBuilder() |
||
133 | |||
134 | /** |
||
135 | * Performs GET HTTP request. |
||
136 | * @param string $url URL |
||
137 | * @param array $options URL options |
||
138 | * @param string $body request body |
||
139 | * @param bool $raw if response body contains JSON and should be decoded |
||
140 | * @throws HiArtException |
||
141 | * @throws \yii\base\InvalidConfigException |
||
142 | * @return mixed response |
||
143 | */ |
||
144 | public function get($url, $options = [], $body = null, $raw = false) |
||
148 | |||
149 | /** |
||
150 | * Performs HEAD HTTP request. |
||
151 | * @param string $url URL |
||
152 | * @param array $options URL options |
||
153 | * @param string $body request body |
||
154 | * @throws HiArtException |
||
155 | * @throws \yii\base\InvalidConfigException |
||
156 | * @return mixed response |
||
157 | */ |
||
158 | public function head($url, $options = [], $body = null) |
||
162 | |||
163 | /** |
||
164 | * Performs POST HTTP request. |
||
165 | * @param string $url URL |
||
166 | * @param array $options URL options |
||
167 | * @param string $body request body |
||
168 | * @param bool $raw if response body contains JSON and should be decoded |
||
169 | * @throws HiArtException |
||
170 | * @throws \yii\base\InvalidConfigException |
||
171 | * @return mixed response |
||
172 | */ |
||
173 | public function post($url, $options = [], $body = null, $raw = false) |
||
177 | |||
178 | /** |
||
179 | * Performs PUT HTTP request. |
||
180 | * @param string $url URL |
||
181 | * @param array $options URL options |
||
182 | * @param string $body request body |
||
183 | * @param bool $raw if response body contains JSON and should be decoded |
||
184 | * @throws HiArtException |
||
185 | * @throws \yii\base\InvalidConfigException |
||
186 | * @return mixed response |
||
187 | */ |
||
188 | public function put($url, $options = [], $body = null, $raw = false) |
||
192 | |||
193 | /** |
||
194 | * Performs DELETE HTTP request. |
||
195 | * @param string $url URL |
||
196 | * @param array $options URL options |
||
197 | * @param string $body request body |
||
198 | * @param bool $raw if response body contains JSON and should be decoded |
||
199 | * @throws HiArtException |
||
200 | * @throws \yii\base\InvalidConfigException |
||
201 | * @return mixed response |
||
202 | */ |
||
203 | public function delete($url, $options = [], $body = null, $raw = false) |
||
207 | |||
208 | /** |
||
209 | * XXX To be removed in favour of post(). |
||
210 | * @param $url |
||
211 | * @param array $options |
||
212 | * @return mixed |
||
213 | */ |
||
214 | public function perform($url, $options = []) |
||
218 | |||
219 | /** |
||
220 | * Make request and check for error. |
||
221 | * @param string $url URL |
||
222 | * @param array $options URL options |
||
223 | * @param string $body request body |
||
224 | * @param bool $raw if response body contains JSON and should be decoded |
||
225 | * @throws HiArtException |
||
226 | * @throws \yii\base\InvalidConfigException |
||
227 | * @return mixed response |
||
228 | */ |
||
229 | public function makeRequest($method, $url, $options = [], $body = null, $raw = false) |
||
236 | |||
237 | /** |
||
238 | * Creates URL. |
||
239 | * @param mixed $path path |
||
240 | * @param array $options URL options |
||
241 | * @return array |
||
242 | */ |
||
243 | private function createUrl($path, array $options = []) |
||
260 | |||
261 | protected function guzzleRequest($method, $url, $body = null, $raw = false) |
||
277 | |||
278 | public function getGuzzle() |
||
286 | |||
287 | /** |
||
288 | * Performs HTTP request. |
||
289 | * @param string $method method name |
||
290 | * @param string $url URL |
||
291 | * @param string $requestBody request body |
||
292 | * @param bool $raw if response body contains JSON and should be decoded |
||
293 | * @throws ErrorResponseException |
||
294 | * @throws HiArtException |
||
295 | * @return mixed if request failed |
||
296 | */ |
||
297 | protected function curlRequest($method, $url, $requestBody = null, $raw = false) |
||
423 | |||
424 | /** |
||
425 | * Try to decode error information if it is valid json, return it if not. |
||
426 | * @param $body |
||
427 | * @return mixed |
||
428 | */ |
||
429 | protected function decodeErrorBody($body) |
||
443 | |||
444 | /** |
||
445 | * Callback to test if API response has error. |
||
446 | */ |
||
447 | public $errorChecker; |
||
448 | |||
449 | /** |
||
450 | * Checks response with errorChecker callback and raises exception if error. |
||
451 | * @param array $response response data from API |
||
452 | * @param string $url request URL |
||
453 | * @param array $options request data |
||
454 | * @throws ErrorResponseException |
||
455 | * @return array |
||
456 | */ |
||
457 | protected function checkResponse($response, $url, $options) |
||
470 | } |
||
471 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..