Complex classes like SS_HTTPRequest 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 SS_HTTPRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class SS_HTTPRequest implements ArrayAccess { |
||
18 | |||
19 | /** |
||
20 | * @var string $url |
||
21 | */ |
||
22 | protected $url; |
||
23 | |||
24 | /** |
||
25 | * The non-extension parts of the passed URL as an array, originally exploded by the "/" separator. |
||
26 | * All elements of the URL are loaded in here, |
||
27 | * and subsequently popped out of the array by {@link shift()}. |
||
28 | * Only use this structure for internal request handling purposes. |
||
29 | */ |
||
30 | protected $dirParts; |
||
31 | |||
32 | /** |
||
33 | * @var string $extension The URL extension (if present) |
||
34 | */ |
||
35 | protected $extension; |
||
36 | |||
37 | /** |
||
38 | * @var string $httpMethod The HTTP method in all uppercase: GET/PUT/POST/DELETE/HEAD |
||
39 | */ |
||
40 | protected $httpMethod; |
||
41 | |||
42 | /** |
||
43 | * @var array $getVars Contains alls HTTP GET parameters passed into this request. |
||
44 | */ |
||
45 | protected $getVars = array(); |
||
46 | |||
47 | /** |
||
48 | * @var array $postVars Contains alls HTTP POST parameters passed into this request. |
||
49 | */ |
||
50 | protected $postVars = array(); |
||
51 | |||
52 | /** |
||
53 | * HTTP Headers like "Content-Type: text/xml" |
||
54 | * |
||
55 | * @see http://en.wikipedia.org/wiki/List_of_HTTP_headers |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $headers = array(); |
||
59 | |||
60 | /** |
||
61 | * Raw HTTP body, used by PUT and POST requests. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $body; |
||
66 | |||
67 | /** |
||
68 | * @var array $allParams Contains an associative array of all |
||
69 | * arguments matched in all calls to {@link RequestHandler->handleRequest()}. |
||
70 | * It's a "historical record" that's specific to the current call of |
||
71 | * {@link handleRequest()}, and is only complete once the "last call" to that method is made. |
||
72 | */ |
||
73 | protected $allParams = array(); |
||
74 | |||
75 | /** |
||
76 | * @var array $latestParams Contains an associative array of all |
||
77 | * arguments matched in the current call from {@link RequestHandler->handleRequest()}, |
||
78 | * as denoted with a "$"-prefix in the $url_handlers definitions. |
||
79 | * Contains different states throughout its lifespan, so just useful |
||
80 | * while processed in {@link RequestHandler} and to get the last |
||
81 | * processes arguments. |
||
82 | */ |
||
83 | protected $latestParams = array(); |
||
84 | |||
85 | /** |
||
86 | * @var array $routeParams Contains an associative array of all arguments |
||
87 | * explicitly set in the route table for the current request. |
||
88 | * Useful for passing generic arguments via custom routes. |
||
89 | * |
||
90 | * E.g. The "Locale" parameter would be assigned "en_NZ" below |
||
91 | * |
||
92 | * Director: |
||
93 | * rules: |
||
94 | * 'en_NZ/$URLSegment!//$Action/$ID/$OtherID': |
||
95 | * Controller: 'ModelAsController' |
||
96 | * Locale: 'en_NZ' |
||
97 | */ |
||
98 | protected $routeParams = array(); |
||
99 | |||
100 | protected $unshiftedButParsedParts = 0; |
||
101 | |||
102 | /** |
||
103 | * Construct a SS_HTTPRequest from a URL relative to the site root. |
||
104 | */ |
||
105 | public function __construct($httpMethod, $url, $getVars = array(), $postVars = array(), $body = null) { |
||
113 | |||
114 | /** |
||
115 | * Allow the setting of a URL |
||
116 | * |
||
117 | * This is here so that RootURLController can change the URL of the request |
||
118 | * without us loosing all the other info attached (like headers) |
||
119 | * |
||
120 | * @param string The new URL |
||
121 | * |
||
122 | * @return SS_HTTPRequest The updated request |
||
123 | */ |
||
124 | public function setUrl($url) { |
||
140 | |||
141 | /** |
||
142 | * @return bool |
||
143 | */ |
||
144 | public function isGET() { |
||
147 | |||
148 | /** |
||
149 | * @return bool |
||
150 | */ |
||
151 | public function isPOST() { |
||
154 | |||
155 | /** |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function isPUT() { |
||
161 | |||
162 | /** |
||
163 | * @return bool |
||
164 | */ |
||
165 | public function isDELETE() { |
||
168 | |||
169 | /** |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function isHEAD() { |
||
175 | |||
176 | /** |
||
177 | * @param string $body |
||
178 | * @return SS_HTTPRequest $this |
||
179 | */ |
||
180 | public function setBody($body) { |
||
184 | |||
185 | /** |
||
186 | * @return null|string |
||
187 | */ |
||
188 | public function getBody() { |
||
191 | |||
192 | /** |
||
193 | * @return array |
||
194 | */ |
||
195 | public function getVars() { |
||
198 | |||
199 | /** |
||
200 | * @return array |
||
201 | */ |
||
202 | public function postVars() { |
||
205 | |||
206 | /** |
||
207 | * Returns all combined HTTP GET and POST parameters |
||
208 | * passed into this request. If a parameter with the same |
||
209 | * name exists in both arrays, the POST value is returned. |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | public function requestVars() { |
||
216 | |||
217 | /** |
||
218 | * @param string $name |
||
219 | * @return mixed |
||
220 | */ |
||
221 | public function getVar($name) { |
||
224 | |||
225 | /** |
||
226 | * @param string $name |
||
227 | * @return mixed |
||
228 | */ |
||
229 | public function postVar($name) { |
||
232 | |||
233 | /** |
||
234 | * @param string $name |
||
235 | * @return mixed |
||
236 | */ |
||
237 | public function requestVar($name) { |
||
241 | |||
242 | /** |
||
243 | * Returns a possible file extension found in parsing the URL |
||
244 | * as denoted by a "."-character near the end of the URL. |
||
245 | * Doesn't necessarily have to belong to an existing file, |
||
246 | * as extensions can be also used for content-type-switching. |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | public function getExtension() { |
||
253 | |||
254 | /** |
||
255 | * Checks if the {@link SS_HTTPRequest->getExtension()} on this request matches one of the more common media types |
||
256 | * embedded into a webpage - e.g. css, png. |
||
257 | * |
||
258 | * This is useful for things like determining wether to display a fully rendered error page or not. Note that the |
||
259 | * media file types is not at all comprehensive. |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function isMedia() { |
||
266 | |||
267 | /** |
||
268 | * Add a HTTP header to the response, replacing any header of the same name. |
||
269 | * |
||
270 | * @param string $header Example: "Content-Type" |
||
271 | * @param string $value Example: "text/xml" |
||
272 | */ |
||
273 | public function addHeader($header, $value) { |
||
276 | |||
277 | /** |
||
278 | * @return array |
||
279 | */ |
||
280 | public function getHeaders() { |
||
283 | |||
284 | /** |
||
285 | * Remove an existing HTTP header |
||
286 | * |
||
287 | * @param string $header |
||
288 | * @return mixed |
||
289 | */ |
||
290 | public function getHeader($header) { |
||
293 | |||
294 | /** |
||
295 | * Remove an existing HTTP header by its name, |
||
296 | * e.g. "Content-Type". |
||
297 | * |
||
298 | * @param string $header |
||
299 | * @return SS_HTTPRequest $this |
||
300 | */ |
||
301 | public function removeHeader($header) { |
||
305 | |||
306 | /** |
||
307 | * Returns the URL used to generate the page |
||
308 | * |
||
309 | * @param bool $includeGetVars whether or not to include the get parameters\ |
||
310 | * @return string |
||
311 | */ |
||
312 | public function getURL($includeGetVars = false) { |
||
331 | |||
332 | /** |
||
333 | * Returns true if this request an ajax request, |
||
334 | * based on custom HTTP ajax added by common JavaScript libraries, |
||
335 | * or based on an explicit "ajax" request parameter. |
||
336 | * |
||
337 | * @return boolean |
||
338 | */ |
||
339 | public function isAjax() { |
||
345 | |||
346 | /** |
||
347 | * Enables the existence of a key-value pair in the request to be checked using |
||
348 | * array syntax, so isset($request['title']) will check for $_POST['title'] and $_GET['title'] |
||
349 | * |
||
350 | * @param unknown_type $offset |
||
351 | * @return boolean |
||
352 | */ |
||
353 | public function offsetExists($offset) { |
||
358 | |||
359 | /** |
||
360 | * Access a request variable using array syntax. eg: $request['title'] instead of $request->postVar('title') |
||
361 | * |
||
362 | * @param unknown_type $offset |
||
363 | * @return unknown |
||
364 | */ |
||
365 | public function offsetGet($offset) { |
||
368 | |||
369 | /** |
||
370 | * @ignore |
||
371 | */ |
||
372 | public function offsetSet($offset, $value) {} |
||
373 | |||
374 | /** |
||
375 | * @ignore |
||
376 | */ |
||
377 | public function offsetUnset($offset) {} |
||
378 | |||
379 | /** |
||
380 | * Construct an SS_HTTPResponse that will deliver a file to the client. |
||
381 | * Caution: Since it requires $fileData to be passed as binary data (no stream support), |
||
382 | * it's only advisable to send small files through this method. |
||
383 | * |
||
384 | * @static |
||
385 | * @param $fileData |
||
386 | * @param $fileName |
||
387 | * @param null $mimeType |
||
388 | * @return SS_HTTPResponse |
||
389 | */ |
||
390 | public static function send_file($fileData, $fileName, $mimeType = null) { |
||
402 | |||
403 | /** |
||
404 | * Matches a URL pattern |
||
405 | * The pattern can contain a number of segments, separated by / (and an extension indicated by a .) |
||
406 | * |
||
407 | * The parts can be either literals, or, if they start with a $ they are interpreted as variables. |
||
408 | * - Literals must be provided in order to match |
||
409 | * - $Variables are optional |
||
410 | * - However, if you put ! at the end of a variable, then it becomes mandatory. |
||
411 | * |
||
412 | * For example: |
||
413 | * - admin/crm/list will match admin/crm/$Action/$ID/$OtherID, but it won't match admin/crm/$Action!/$ClassName! |
||
414 | * |
||
415 | * The pattern can optionally start with an HTTP method and a space. For example, "POST $Controller/$Action". |
||
416 | * This is used to define a rule that only matches on a specific HTTP method. |
||
417 | * |
||
418 | * @param $pattern |
||
419 | * @param bool $shiftOnSuccess |
||
420 | * @return array|bool |
||
421 | */ |
||
422 | public function match($pattern, $shiftOnSuccess = false) { |
||
507 | |||
508 | /** |
||
509 | * @return array |
||
510 | */ |
||
511 | public function allParams() { |
||
514 | |||
515 | /** |
||
516 | * Shift all the parameter values down a key space, and return the shifted value. |
||
517 | * |
||
518 | * @return string |
||
519 | */ |
||
520 | public function shiftAllParams() { |
||
536 | |||
537 | /** |
||
538 | * @return array |
||
539 | */ |
||
540 | public function latestParams() { |
||
543 | |||
544 | /** |
||
545 | * @param string $name |
||
546 | * @return string|null |
||
547 | */ |
||
548 | public function latestParam($name) { |
||
552 | |||
553 | /** |
||
554 | * @return array |
||
555 | */ |
||
556 | public function routeParams() { |
||
559 | |||
560 | /** |
||
561 | * @param $params |
||
562 | * @return SS_HTTPRequest $this |
||
563 | */ |
||
564 | public function setRouteParams($params) { |
||
568 | |||
569 | /** |
||
570 | * @return array |
||
571 | */ |
||
572 | public function params() { |
||
575 | |||
576 | /** |
||
577 | * Finds a named URL parameter (denoted by "$"-prefix in $url_handlers) |
||
578 | * from the full URL, or a parameter specified in the route table |
||
579 | * |
||
580 | * @param string $name |
||
581 | * @return string Value of the URL parameter (if found) |
||
582 | */ |
||
583 | public function param($name) { |
||
588 | |||
589 | /** |
||
590 | * Returns the unparsed part of the original URL |
||
591 | * separated by commas. This is used by {@link RequestHandler->handleRequest()} |
||
592 | * to determine if further URL processing is necessary. |
||
593 | * |
||
594 | * @return string Partial URL |
||
595 | */ |
||
596 | public function remaining() { |
||
599 | |||
600 | /** |
||
601 | * Returns true if this is a URL that will match without shifting off any of the URL. |
||
602 | * This is used by the request handler to prevent infinite parsing loops. |
||
603 | * |
||
604 | * @param $pattern |
||
605 | * @return bool |
||
606 | */ |
||
607 | public function isEmptyPattern($pattern) { |
||
614 | |||
615 | /** |
||
616 | * Shift one or more parts off the beginning of the URL. |
||
617 | * If you specify shifting more than 1 item off, then the items will be returned as an array |
||
618 | * |
||
619 | * @param int $count Shift Count |
||
620 | * @return String|Array |
||
621 | */ |
||
622 | public function shift($count = 1) { |
||
637 | |||
638 | /** |
||
639 | * Returns true if the URL has been completely parsed. |
||
640 | * This will respect parsed but unshifted directory parts. |
||
641 | * |
||
642 | * @return bool |
||
643 | */ |
||
644 | public function allParsed() { |
||
647 | |||
648 | /** |
||
649 | * Returns the client IP address which |
||
650 | * originated this request. |
||
651 | * |
||
652 | * @return string |
||
653 | */ |
||
654 | public function getIP() { |
||
678 | |||
679 | /** |
||
680 | * Extract an IP address from a header value that has been obtained. Accepts single IP or comma separated string of |
||
681 | * IPs |
||
682 | * |
||
683 | * @param string $headerValue The value from a trusted header |
||
684 | * @return string The IP address |
||
685 | */ |
||
686 | protected function getIPFromHeaderValue($headerValue) { |
||
700 | |||
701 | /** |
||
702 | * Returns all mimetypes from the HTTP "Accept" header |
||
703 | * as an array. |
||
704 | * |
||
705 | * @param boolean $includeQuality Don't strip away optional "quality indicators", e.g. "application/xml;q=0.9" |
||
706 | * (Default: false) |
||
707 | * @return array |
||
708 | */ |
||
709 | public function getAcceptMimetypes($includeQuality = false) { |
||
717 | |||
718 | /** |
||
719 | * @return string HTTP method (all uppercase) |
||
720 | */ |
||
721 | public function httpMethod() { |
||
724 | |||
725 | /** |
||
726 | * Explicitly set the HTTP method for this request. |
||
727 | * @param string $method |
||
728 | * @return $this |
||
729 | */ |
||
730 | public function setHttpMethod($method) { |
||
738 | |||
739 | /** |
||
740 | * @param string $method |
||
741 | * @return bool |
||
742 | */ |
||
743 | private static function isValidHttpMethod($method) { |
||
746 | |||
747 | /** |
||
748 | * Gets the "real" HTTP method for a request. This method is no longer used to mitigate the risk of web cache |
||
749 | * poisoning. |
||
750 | * |
||
751 | * @see https://www.silverstripe.org/download/security-releases/CVE-2019-19326 |
||
752 | * @param string $origMethod Original HTTP method from the browser request |
||
753 | * @param array $postVars |
||
754 | * @return string HTTP method (all uppercase) |
||
755 | * @deprecated 3.7.5 |
||
756 | */ |
||
757 | public static function detect_method($origMethod, $postVars) { |
||
767 | } |
||
768 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.