Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Request extends Http { |
||
17 | protected static $curlAlias = array( |
||
18 | 'url' => 'CURLOPT_URL', |
||
19 | 'uri' => 'CURLOPT_URL', |
||
20 | 'debug' => 'CURLOPT_VERBOSE',//for debug verbose |
||
21 | 'method' => 'CURLOPT_CUSTOMREQUEST', |
||
22 | 'data' => 'CURLOPT_POSTFIELDS', // array or string , file begin with '@' |
||
23 | 'ua' => 'CURLOPT_USERAGENT', |
||
24 | 'timeout' => 'CURLOPT_TIMEOUT', // (secs) 0 means indefinitely |
||
25 | 'connect_timeout' => 'CURLOPT_CONNECTTIMEOUT', |
||
26 | 'referer' => 'CURLOPT_REFERER', |
||
27 | 'binary' => 'CURLOPT_BINARYTRANSFER', |
||
28 | 'port' => 'CURLOPT_PORT', |
||
29 | 'header' => 'CURLOPT_HEADER', // TRUE:include header |
||
30 | 'headers' => 'CURLOPT_HTTPHEADER', // array |
||
31 | 'download' => 'CURLOPT_FILE', // writing file stream (using fopen()), default is STDOUT |
||
32 | 'upload' => 'CURLOPT_INFILE', // reading file stream |
||
33 | 'transfer' => 'CURLOPT_RETURNTRANSFER', // TRUE:return string; FALSE:output directly (curl_exec) |
||
34 | 'follow_location' => 'CURLOPT_FOLLOWLOCATION', |
||
35 | 'timeout_ms' => 'CURLOPT_TIMEOUT_MS', // milliseconds, libcurl version > 7.36.0 , |
||
36 | ); |
||
37 | public $curlHandle; |
||
38 | protected $options = array( |
||
39 | 'CURLOPT_MAXREDIRS' => 10, |
||
40 | 'header' => true, |
||
41 | 'method' => self::GET, |
||
42 | 'transfer' => true, |
||
43 | 'follow_location' => true, |
||
44 | 'timeout' => 0); |
||
45 | protected $endCallback; |
||
46 | protected $withURIQuery; |
||
47 | protected static $logger; |
||
48 | protected $uri; |
||
49 | 2 | ||
50 | protected function __construct() { |
||
53 | 2 | ||
54 | 2 | public static function create() { |
|
57 | 1 | ||
58 | 1 | public function endCallback() { |
|
61 | 2 | ||
62 | 2 | public function hasEndCallback() { |
|
65 | 2 | ||
66 | 2 | public function onEnd(callable$callback) { |
|
73 | 2 | ||
74 | 2 | public function getURI() { |
|
77 | |||
78 | /** |
||
79 | * @param $field alias or field name |
||
80 | * @return bool|mixed |
||
81 | 2 | */ |
|
82 | public function getIni($field) { |
||
88 | 2 | ||
89 | |||
90 | public function addQuery($data) { |
||
102 | |||
103 | public function post($uri, array $payload = array(), array $options = array()) { |
||
106 | |||
107 | protected function ini($method, $url, array $data = array(), array $options = array()) { |
||
113 | 2 | ||
114 | 2 | public function addOptions(array $options = array()) { |
|
119 | 2 | ||
120 | 2 | /* no body */ |
|
121 | 2 | ||
122 | function put($uri, array $payload = array(), array $options = array()) { |
||
125 | 2 | ||
126 | function patch($uri, array $payload = array(), array $options = array()) { |
||
129 | 1 | ||
130 | public function get($uri, array $options = array()) { |
||
133 | 2 | ||
134 | function options($uri, array $options = array()) { |
||
137 | |||
138 | function head($uri, array $options = array()) { |
||
141 | |||
142 | function delete($uri, array $options = array()) { |
||
145 | |||
146 | function trace($uri, array $options = array()) { |
||
149 | 1 | ||
150 | 1 | /** |
|
151 | * @return Response |
||
152 | */ |
||
153 | 1 | public function send() { |
|
162 | |||
163 | public function applyOptions() { |
||
169 | |||
170 | protected function prepare() { |
||
226 | 2 | ||
227 | protected static function filterAndRaw(array &$options) { |
||
238 | |||
239 | /** |
||
240 | 2 | * @param $key |
|
241 | 2 | * @return mixed |
|
242 | */ |
||
243 | protected static function optionAlias($key) { |
||
261 | 2 | ||
262 | 2 | private static function log(Response $response) |
|
274 | |||
275 | public static function setLogger($logger) |
||
279 | } |
||
280 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.