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 |
||
27 | class Request implements RequestInterface |
||
28 | { |
||
29 | /** |
||
30 | * Protocol for api request. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $protocol = 'https'; |
||
35 | |||
36 | /** |
||
37 | * Hostname for api request. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $host; |
||
42 | |||
43 | /** |
||
44 | * Api port. |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $port = 80; |
||
49 | |||
50 | /** |
||
51 | * Request uri. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $uri; |
||
56 | |||
57 | /** |
||
58 | * Parameters for request. |
||
59 | * |
||
60 | * @var mixed[] |
||
61 | */ |
||
62 | protected $parameters = array(); |
||
63 | |||
64 | /** |
||
65 | * Boolean for status of parameter handling. When set to true invalid |
||
66 | * parameters will be cleaned out before being sent to the API. |
||
67 | * |
||
68 | * @var boolean |
||
69 | */ |
||
70 | protected $cleanParameters = true; |
||
71 | |||
72 | /** |
||
73 | * A list of parameters the Content API accepts. |
||
74 | * |
||
75 | * For a list of accepted parameters find the variable allowed_params in |
||
76 | * the following file: |
||
77 | * https://github.com/superdesk/superdesk-content-api/blob/master/content_api/items/service.py |
||
78 | * |
||
79 | * @var string[] |
||
80 | */ |
||
81 | protected $validParameters = array( |
||
82 | 'start_date', 'end_date', 'q', 'max_results', 'page', |
||
83 | 'include_fields', 'exclude_fields' |
||
84 | ); |
||
85 | |||
86 | /** |
||
87 | * Request headers. |
||
88 | * |
||
89 | * @var string[] |
||
90 | */ |
||
91 | protected $headers = array(); |
||
92 | |||
93 | /** |
||
94 | * Request options, usuably in clients, etc. |
||
95 | * |
||
96 | * @var mixed[] |
||
97 | */ |
||
98 | protected $options = array(); |
||
99 | |||
100 | /** |
||
101 | * Construct a request object. |
||
102 | * |
||
103 | * @param string $hostname Host name |
||
104 | * @param string $uri Request uri |
||
105 | * @param mixed[] $parameters Parameters |
||
106 | * @param int $port Port |
||
107 | * @param string $protocol Protocol |
||
108 | */ |
||
109 | public function __construct( |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function getProtocol() |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function setProtocol($protocol) |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function getHost() |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function setHost($host) |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function getPort() |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | public function setPort($port) |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function getUri() |
||
206 | |||
207 | /** |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | public function setUri($uri) |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | public function getParameters() |
||
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | public function setParameters(array $parameters) |
||
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | public function getCleanParameters() |
||
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | public function enableParameterCleaning() |
||
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | public function disableParameterCleaning() |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | public function getHeaders() |
||
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | public function setHeaders(array $headers) |
||
284 | |||
285 | /** |
||
286 | * {@inheritdoc} |
||
287 | */ |
||
288 | public function getOptions() |
||
292 | |||
293 | /** |
||
294 | * {@inheritdoc} |
||
295 | */ |
||
296 | public function setOptions(array $options) |
||
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | public function getBaseUrl() |
||
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | public function getFullUrl() |
||
318 | |||
319 | /** |
||
320 | * Type conversion method for parameters accepted by the API. Will |
||
321 | * by default automatically unset invalid parameters, this behaviour can be |
||
322 | * overridden with the second argument. |
||
323 | * |
||
324 | * @param mixed[] $requestParameters Array of parameters |
||
325 | * @param boolean $unsetInvalidParameters Boolean to clean out invalid |
||
326 | * parameters |
||
327 | * |
||
328 | * @return mixed[] Returns an array of parameters with API safe types |
||
329 | * @throws InvalidArgumentException When an invalid type is set for a |
||
330 | * valid parameter |
||
331 | */ |
||
332 | public function processParameters( |
||
384 | } |
||
385 |