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 | */ |
||
| 108 | public function __construct($hostname = null, $uri = null, array $parameters = null, $port = null) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | public function getHost() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function setHost($host) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | public function getPort() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | public function setPort($port) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | public function getUri() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | public function setUri($uri) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | public function getParameters() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritdoc} |
||
| 188 | */ |
||
| 189 | public function setParameters(array $parameters) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritdoc} |
||
| 202 | */ |
||
| 203 | public function getCleanParameters() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritdoc} |
||
| 210 | */ |
||
| 211 | public function enableParameterCleaning() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * {@inheritdoc} |
||
| 220 | */ |
||
| 221 | public function disableParameterCleaning() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | public function getHeaders() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | public function setHeaders(array $headers) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritdoc} |
||
| 248 | */ |
||
| 249 | public function getOptions() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | public function setOptions(array $options) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | */ |
||
| 267 | public function getBaseUrl() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function getFullUrl() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Type conversion method for parameters accepted by the API. Will |
||
| 282 | * by default automatically unset invalid parameters, this behaviour can be |
||
| 283 | * overridden with the second argument. |
||
| 284 | * |
||
| 285 | * @param mixed[] $requestParameters Array of parameters |
||
| 286 | * @param boolean $unsetInvalidParameters Boolean to clean out invalid |
||
| 287 | * parameters |
||
| 288 | * |
||
| 289 | * @return mixed[] Returns an array of parameters with API safe types |
||
| 290 | * @throws InvalidArgumentException When an invalid type is set for a |
||
| 291 | * valid parameter |
||
| 292 | */ |
||
| 293 | public function processParameters( |
||
| 345 | } |
||
| 346 |