Complex classes like RequestParameters 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 RequestParameters, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class RequestParameters |
||
24 | { |
||
25 | const DEFAULT_PAGE = 1; |
||
26 | const DEFAULT_MAX_RESULTS = 25; |
||
27 | |||
28 | /** |
||
29 | * Start date. |
||
30 | * |
||
31 | * @var DateTime|null |
||
32 | */ |
||
33 | protected $startDate; |
||
34 | |||
35 | /** |
||
36 | * End date. |
||
37 | * |
||
38 | * @var DateTime|null |
||
39 | */ |
||
40 | protected $endDate; |
||
41 | |||
42 | /** |
||
43 | * Query for text search. |
||
44 | * |
||
45 | * @var string|null |
||
46 | */ |
||
47 | protected $query; |
||
48 | |||
49 | /** |
||
50 | * Page. |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $page = self::DEFAULT_PAGE; |
||
55 | |||
56 | /** |
||
57 | * Max results per page. |
||
58 | * |
||
59 | * @var int |
||
60 | */ |
||
61 | protected $maxResults = self::DEFAULT_MAX_RESULTS; |
||
62 | |||
63 | /** |
||
64 | * Include fields list. These fields will be set in your packages or items. |
||
65 | * |
||
66 | * @var array|null |
||
67 | */ |
||
68 | protected $includeFields; |
||
69 | |||
70 | /** |
||
71 | * Exclude fields lists. These fields won't be set in your packages or items. |
||
72 | * |
||
73 | * @var array|null |
||
74 | */ |
||
75 | protected $excludeFields; |
||
76 | |||
77 | /** |
||
78 | * Mapping array, which links correct parameter name for API with proper |
||
79 | * method. |
||
80 | * |
||
81 | * @var string[] |
||
82 | */ |
||
83 | protected $propertyMapping = array( |
||
84 | 'start_date' => 'StartDate', |
||
85 | 'end_date' => 'EndDate', |
||
86 | 'q' => 'Query', |
||
87 | 'page' => 'Page', |
||
88 | 'max_results' => 'MaxResults', |
||
89 | 'include_fields' => 'IncludeFields', |
||
90 | 'exclude_fields' => 'ExcludeFields', |
||
91 | ); |
||
92 | |||
93 | /** |
||
94 | * Returns start date. |
||
95 | * |
||
96 | * @return DateTime|null |
||
97 | */ |
||
98 | public function getStartDate() |
||
102 | |||
103 | /** |
||
104 | * Sets start date. Will accept a string formatted 'yyyy-mm-dd' or DateTime |
||
105 | * object. |
||
106 | * |
||
107 | * @param string|DateTime|null $startDate |
||
108 | * |
||
109 | * @return self |
||
110 | */ |
||
111 | public function setStartDate($startDate) |
||
117 | |||
118 | /** |
||
119 | * Returns end date. |
||
120 | * |
||
121 | * @return DateTime|null |
||
122 | */ |
||
123 | public function getEndDate() |
||
127 | |||
128 | /** |
||
129 | * Sets end date. Will accept string formatted 'yyyy-mm-dd' or DateTIme |
||
130 | * object. |
||
131 | * |
||
132 | * @param string|DateTime|null $endDate |
||
133 | * |
||
134 | * @return self |
||
135 | */ |
||
136 | public function setEndDate($endDate) |
||
142 | |||
143 | /** |
||
144 | * Returns text query. |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getQuery() |
||
152 | |||
153 | /** |
||
154 | * Sets query parameters. |
||
155 | * |
||
156 | * @param string|null $query |
||
157 | * |
||
158 | * @return self |
||
159 | */ |
||
160 | public function setQuery($query) |
||
170 | |||
171 | /** |
||
172 | * Returns page number. |
||
173 | * |
||
174 | * @return int |
||
175 | */ |
||
176 | public function getPage() |
||
180 | |||
181 | /** |
||
182 | * Sets page number. If null is supplied, resets to class default. |
||
183 | * |
||
184 | * @param int|null $page |
||
185 | * |
||
186 | * @return self |
||
187 | */ |
||
188 | public function setPage($page) |
||
198 | |||
199 | /** |
||
200 | * Returns maximum results per page. |
||
201 | * |
||
202 | * @return int |
||
203 | */ |
||
204 | public function getMaxResults() |
||
208 | |||
209 | /** |
||
210 | * Sets maximum results per page. If null is supplied, resets to class |
||
211 | * default. |
||
212 | * |
||
213 | * @param int|nul $maxResults |
||
214 | * |
||
215 | * @return self |
||
216 | */ |
||
217 | public function setMaxResults($maxResults) |
||
227 | |||
228 | /** |
||
229 | * Returns include fields. |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | public function getIncludeFields() |
||
237 | |||
238 | /** |
||
239 | * Sets include fields. |
||
240 | * |
||
241 | * @param array|null $includeFields |
||
242 | * |
||
243 | * @return self |
||
244 | */ |
||
245 | public function setIncludeFields($includeFields) |
||
251 | |||
252 | /** |
||
253 | * Gets exclude fields. |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | public function getExcludeFields() |
||
261 | |||
262 | /** |
||
263 | * Sets exclude fields |
||
264 | * |
||
265 | * @param array|null |
||
266 | * |
||
267 | * @return self |
||
268 | */ |
||
269 | public function setExcludeFields($excludeFields) |
||
275 | |||
276 | /** |
||
277 | * Validate date parameter input and converts to DateTime. |
||
278 | * |
||
279 | * @param string|DateTime $date When string format yyyy-mm-dd should be used |
||
280 | * |
||
281 | * @return DateTime |
||
282 | * @throws InvalidArgumentException |
||
283 | */ |
||
284 | private function validateDate($date) |
||
298 | |||
299 | /** |
||
300 | * Arguments of type string and array are valid. String will be split on , |
||
301 | * and converted to an array. |
||
302 | * |
||
303 | * @param string|array $value |
||
304 | * |
||
305 | * @return array |
||
306 | * @throws InvalidArgumentException |
||
307 | */ |
||
308 | private function validateStringOrArray($value) |
||
318 | |||
319 | /** |
||
320 | * Validates if value is numeric. |
||
321 | * |
||
322 | * @param string|int $value |
||
323 | * |
||
324 | * @return int |
||
325 | * @throws InvalidArgumentException |
||
326 | */ |
||
327 | private function validateNumeric($value) |
||
337 | |||
338 | /** |
||
339 | * Helper function for setting a property. Sets a proprety to a value, by |
||
340 | * pulling it through a validator function. If null is specified as value |
||
341 | * then property will be set to null as well.. |
||
342 | * |
||
343 | * @param string $property Property name |
||
344 | * @param mixed $value Value of the property |
||
345 | * @param string $validator Name of the validator method |
||
346 | * |
||
347 | * @throws InvalidArgumentException |
||
348 | */ |
||
349 | private function setProperty($property, $value, $validator) |
||
361 | |||
362 | /** |
||
363 | * Internal method to get the correct method when only a query parameter |
||
364 | * key is available. |
||
365 | * |
||
366 | * @param string $queryKey [description] |
||
367 | * @param string $action This will be prepended to the method name, |
||
368 | * defaults to 'get'. |
||
369 | * |
||
370 | * @return string|null Returns the method or null when the key doesn't exist. |
||
371 | */ |
||
372 | private function getMethodForQueryKey($queryKey, $action = 'get') |
||
380 | |||
381 | /** |
||
382 | * Returns all properties either as an array or http query string. |
||
383 | * |
||
384 | * @param boolean $buildHttpQuery Build query from array |
||
385 | * |
||
386 | * @return mixed[]|string |
||
387 | */ |
||
388 | public function getAllParameters($buildHttpQuery = false) |
||
406 | |||
407 | /** |
||
408 | * Uses an array of query parameters and sets the values for the current |
||
409 | * RequestParameters object. Does an internal check whether the query key |
||
410 | * is a valid API query key. |
||
411 | * |
||
412 | * @param array $requestParameters |
||
413 | * |
||
414 | * @return self Returns the current object with the properties set |
||
415 | */ |
||
416 | public function setQueryParameterArray(array $requestParameters = array()) |
||
427 | } |
||
428 |