Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
31 | class Request implements ArrayAccess |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * Array of parameters parsed from the URL. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | public $params = [ |
||
40 | 'plugin' => null, |
||
41 | 'controller' => null, |
||
42 | 'action' => null, |
||
43 | '_ext' => null, |
||
44 | 'pass' => [] |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Array of POST data. Will contain form data as well as uploaded files. |
||
49 | * In PUT/PATCH/DELETE requests this property will contain the form-urlencoded |
||
50 | * data. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | public $data = []; |
||
55 | |||
56 | /** |
||
57 | * Array of querystring arguments |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | public $query = []; |
||
62 | |||
63 | /** |
||
64 | * Array of cookie data. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | public $cookies = []; |
||
69 | |||
70 | /** |
||
71 | * Array of environment data. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $_environment = []; |
||
76 | |||
77 | /** |
||
78 | * The URL string used for the request. |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | public $url; |
||
83 | |||
84 | /** |
||
85 | * Base URL path. |
||
86 | * |
||
87 | * @var string |
||
88 | */ |
||
89 | public $base; |
||
90 | |||
91 | /** |
||
92 | * webroot path segment for the request. |
||
93 | * |
||
94 | * @var string |
||
95 | */ |
||
96 | public $webroot = '/'; |
||
97 | |||
98 | /** |
||
99 | * The full address to the current request |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | public $here; |
||
104 | |||
105 | /** |
||
106 | * Whether or not to trust HTTP_X headers set by most load balancers. |
||
107 | * Only set to true if your application runs behind load balancers/proxies |
||
108 | * that you control. |
||
109 | * |
||
110 | * @var bool |
||
111 | */ |
||
112 | public $trustProxy = false; |
||
113 | |||
114 | /** |
||
115 | * The built in detectors used with `is()` can be modified with `addDetector()`. |
||
116 | * |
||
117 | * There are several ways to specify a detector, see Cake\Network\Request::addDetector() for the |
||
118 | * various formats and ways to define detectors. |
||
119 | * |
||
120 | * @var array |
||
121 | */ |
||
122 | protected static $_detectors = [ |
||
123 | 'get' => ['env' => 'REQUEST_METHOD', 'value' => 'GET'], |
||
124 | 'post' => ['env' => 'REQUEST_METHOD', 'value' => 'POST'], |
||
125 | 'put' => ['env' => 'REQUEST_METHOD', 'value' => 'PUT'], |
||
126 | 'patch' => ['env' => 'REQUEST_METHOD', 'value' => 'PATCH'], |
||
127 | 'delete' => ['env' => 'REQUEST_METHOD', 'value' => 'DELETE'], |
||
128 | 'head' => ['env' => 'REQUEST_METHOD', 'value' => 'HEAD'], |
||
129 | 'options' => ['env' => 'REQUEST_METHOD', 'value' => 'OPTIONS'], |
||
130 | 'ssl' => ['env' => 'HTTPS', 'options' => [1, 'on']], |
||
131 | 'ajax' => ['env' => 'HTTP_X_REQUESTED_WITH', 'value' => 'XMLHttpRequest'], |
||
132 | 'flash' => ['env' => 'HTTP_USER_AGENT', 'pattern' => '/^(Shockwave|Adobe) Flash/'], |
||
133 | 'requested' => ['param' => 'requested', 'value' => 1], |
||
134 | 'json' => ['accept' => ['application/json'], 'param' => '_ext', 'value' => 'json'], |
||
135 | 'xml' => ['accept' => ['application/xml', 'text/xml'], 'param' => '_ext', 'value' => 'xml'], |
||
136 | ]; |
||
137 | |||
138 | /** |
||
139 | * Instance cache for results of is(something) calls |
||
140 | * |
||
141 | * @var array |
||
142 | */ |
||
143 | protected $_detectorCache = []; |
||
144 | |||
145 | /** |
||
146 | * Copy of php://input. Since this stream can only be read once in most SAPI's |
||
147 | * keep a copy of it so users don't need to know about that detail. |
||
148 | * |
||
149 | * @var string |
||
150 | */ |
||
151 | protected $_input = ''; |
||
152 | |||
153 | /** |
||
154 | * Instance of a Session object relative to this request |
||
155 | * |
||
156 | * @var \Cake\Network\Session |
||
157 | */ |
||
158 | protected $_session; |
||
159 | |||
160 | /** |
||
161 | * Wrapper method to create a new request from PHP superglobals. |
||
162 | * |
||
163 | * Uses the $_GET, $_POST, $_FILES, $_COOKIE, $_SERVER, $_ENV and php://input data to construct |
||
164 | * the request. |
||
165 | * |
||
166 | * @return \Cake\Network\Request |
||
167 | */ |
||
168 | public static function createFromGlobals() |
||
189 | |||
190 | /** |
||
191 | * Create a new request object. |
||
192 | * |
||
193 | * You can supply the data as either an array or as a string. If you use |
||
194 | * a string you can only supply the URL for the request. Using an array will |
||
195 | * let you provide the following keys: |
||
196 | * |
||
197 | * - `post` POST data or non query string data |
||
198 | * - `query` Additional data from the query string. |
||
199 | * - `files` Uploaded file data formatted like $_FILES. |
||
200 | * - `cookies` Cookies for this request. |
||
201 | * - `environment` $_SERVER and $_ENV data. |
||
202 | * - `url` The URL without the base path for the request. |
||
203 | * - `base` The base URL for the request. |
||
204 | * - `webroot` The webroot directory for the request. |
||
205 | * - `input` The data that would come from php://input this is useful for simulating |
||
206 | * - `session` An instance of a Session object |
||
207 | * requests with put, patch or delete data. |
||
208 | * |
||
209 | * @param string|array $config An array of request data to create a request with. |
||
210 | */ |
||
211 | public function __construct($config = []) |
||
231 | |||
232 | /** |
||
233 | * Process the config/settings data into properties. |
||
234 | * |
||
235 | * @param array $config The config data to use. |
||
236 | * @return void |
||
237 | */ |
||
238 | protected function _setConfig($config) |
||
266 | |||
267 | /** |
||
268 | * Sets the REQUEST_METHOD environment variable based on the simulated _method |
||
269 | * HTTP override value. The 'ORIGINAL_REQUEST_METHOD' is also preserved, if you |
||
270 | * want the read the non-simulated HTTP method the client used. |
||
271 | * |
||
272 | * @param array $data Array of post data. |
||
273 | * @return array |
||
274 | */ |
||
275 | protected function _processPost($data) |
||
303 | |||
304 | /** |
||
305 | * Process the GET parameters and move things into the object. |
||
306 | * |
||
307 | * @param array $query The array to which the parsed keys/values are being added. |
||
308 | * @return array An array containing the parsed querystring keys/values. |
||
309 | */ |
||
310 | protected function _processGet($query) |
||
322 | |||
323 | /** |
||
324 | * Get the request uri. Looks in PATH_INFO first, as this is the exact value we need prepared |
||
325 | * by PHP. Following that, REQUEST_URI, PHP_SELF, HTTP_X_REWRITE_URL and argv are checked in that order. |
||
326 | * Each of these server variables have the base path, and query strings stripped off |
||
327 | * |
||
328 | * @param array $config Configuration to set. |
||
329 | * @return string URI The CakePHP request path that is being accessed. |
||
330 | */ |
||
331 | protected static function _url($config) |
||
372 | |||
373 | /** |
||
374 | * Returns a base URL and sets the proper webroot |
||
375 | * |
||
376 | * If CakePHP is called with index.php in the URL even though |
||
377 | * URL Rewriting is activated (and thus not needed) it swallows |
||
378 | * the unnecessary part from $base to prevent issue #3318. |
||
379 | * |
||
380 | * @return array Base URL, webroot dir ending in / |
||
381 | * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3318 |
||
382 | */ |
||
383 | protected static function _base() |
||
431 | |||
432 | /** |
||
433 | * Process uploaded files and move things onto the post data. |
||
434 | * |
||
435 | * @param array $post Post data to merge files onto. |
||
436 | * @param array $files Uploaded files to merge in. |
||
437 | * @return array merged post + file data. |
||
438 | */ |
||
439 | protected function _processFiles($post, $files) |
||
453 | |||
454 | /** |
||
455 | * Recursively walks the FILES array restructuring the data |
||
456 | * into something sane and usable. |
||
457 | * |
||
458 | * @param array $data The data being built |
||
459 | * @param array $post The post data being traversed |
||
460 | * @param string $path The dot separated path to insert $data into. |
||
461 | * @param string $field The terminal field in the path. This is one of the |
||
462 | * $_FILES properties e.g. name, tmp_name, size, error |
||
463 | * @return array The restructured FILES data |
||
464 | */ |
||
465 | protected function _processFileData($data, $post, $path = '', $field = '') |
||
485 | |||
486 | /** |
||
487 | * Get the content type used in this request. |
||
488 | * |
||
489 | * @return string |
||
490 | */ |
||
491 | public function contentType() |
||
499 | |||
500 | /** |
||
501 | * Returns the instance of the Session object for this request |
||
502 | * |
||
503 | * If a session object is passed as first argument it will be set as |
||
504 | * the session to use for this request |
||
505 | * |
||
506 | * @param \Cake\Network\Session|null $session the session object to use |
||
507 | * @return \Cake\Network\Session |
||
508 | */ |
||
509 | public function session(Session $session = null) |
||
516 | |||
517 | /** |
||
518 | * Get the IP the client is using, or says they are using. |
||
519 | * |
||
520 | * @return string The client IP. |
||
521 | */ |
||
522 | public function clientIp() |
||
543 | |||
544 | /** |
||
545 | * Returns the referer that referred this request. |
||
546 | * |
||
547 | * @param bool $local Attempt to return a local address. |
||
548 | * Local addresses do not contain hostnames. |
||
549 | * @return string The referring address for this request. |
||
550 | */ |
||
551 | public function referer($local = false) |
||
569 | |||
570 | /** |
||
571 | * Missing method handler, handles wrapping older style isAjax() type methods |
||
572 | * |
||
573 | * @param string $name The method called |
||
574 | * @param array $params Array of parameters for the method call |
||
575 | * @return mixed |
||
576 | * @throws \BadMethodCallException when an invalid method is called. |
||
577 | */ |
||
578 | public function __call($name, $params) |
||
586 | |||
587 | /** |
||
588 | * Magic get method allows access to parsed routing parameters directly on the object. |
||
589 | * |
||
590 | * Allows access to `$this->params['controller']` via `$this->controller` |
||
591 | * |
||
592 | * @param string $name The property being accessed. |
||
593 | * @return mixed Either the value of the parameter or null. |
||
594 | */ |
||
595 | public function __get($name) |
||
602 | |||
603 | /** |
||
604 | * Magic isset method allows isset/empty checks |
||
605 | * on routing parameters. |
||
606 | * |
||
607 | * @param string $name The property being accessed. |
||
608 | * @return bool Existence |
||
609 | */ |
||
610 | public function __isset($name) |
||
614 | |||
615 | /** |
||
616 | * Check whether or not a Request is a certain type. |
||
617 | * |
||
618 | * Uses the built in detection rules as well as additional rules |
||
619 | * defined with Cake\Network\CakeRequest::addDetector(). Any detector can be called |
||
620 | * as `is($type)` or `is$Type()`. |
||
621 | * |
||
622 | * @param string|array $type The type of request you want to check. If an array |
||
623 | * this method will return true if the request matches any type. |
||
624 | * @return bool Whether or not the request is the type you are checking. |
||
625 | */ |
||
626 | public function is($type) |
||
644 | |||
645 | /** |
||
646 | * Clears the instance detector cache, used by the is() function |
||
647 | * |
||
648 | * @return void |
||
649 | */ |
||
650 | public function clearDetectorCache() |
||
654 | |||
655 | /** |
||
656 | * Worker for the public is() function |
||
657 | * |
||
658 | * @param string|array $type The type of request you want to check. If an array |
||
659 | * this method will return true if the request matches any type. |
||
660 | * @return bool Whether or not the request is the type you are checking. |
||
661 | */ |
||
662 | protected function _is($type) |
||
682 | |||
683 | /** |
||
684 | * Detects if a specific accept header is present. |
||
685 | * |
||
686 | * @param array $detect Detector options array. |
||
687 | * @return bool Whether or not the request is the type you are checking. |
||
688 | */ |
||
689 | protected function _acceptHeaderDetector($detect) |
||
699 | |||
700 | /** |
||
701 | * Detects if a specific header is present. |
||
702 | * |
||
703 | * @param array $detect Detector options array. |
||
704 | * @return bool Whether or not the request is the type you are checking. |
||
705 | */ |
||
706 | protected function _headerDetector($detect) |
||
719 | |||
720 | /** |
||
721 | * Detects if a specific request parameter is present. |
||
722 | * |
||
723 | * @param array $detect Detector options array. |
||
724 | * @return bool Whether or not the request is the type you are checking. |
||
725 | */ |
||
726 | protected function _paramDetector($detect) |
||
738 | |||
739 | /** |
||
740 | * Detects if a specific environment variable is present. |
||
741 | * |
||
742 | * @param array $detect Detector options array. |
||
743 | * @return bool Whether or not the request is the type you are checking. |
||
744 | */ |
||
745 | protected function _environmentDetector($detect) |
||
761 | |||
762 | /** |
||
763 | * Check that a request matches all the given types. |
||
764 | * |
||
765 | * Allows you to test multiple types and union the results. |
||
766 | * See Request::is() for how to add additional types and the |
||
767 | * built-in types. |
||
768 | * |
||
769 | * @param array $types The types to check. |
||
770 | * @return bool Success. |
||
771 | * @see \Cake\Network\Request::is() |
||
772 | */ |
||
773 | public function isAll(array $types) |
||
778 | |||
779 | /** |
||
780 | * Add a new detector to the list of detectors that a request can use. |
||
781 | * There are several different formats and types of detectors that can be set. |
||
782 | * |
||
783 | * ### Callback detectors |
||
784 | * |
||
785 | * Callback detectors allow you to provide a callable to handle the check. |
||
786 | * The callback will receive the request object as its only parameter. |
||
787 | * |
||
788 | * ``` |
||
789 | * addDetector('custom', function ($request) { //Return a boolean }); |
||
790 | * addDetector('custom', ['SomeClass', 'somemethod']); |
||
791 | * ``` |
||
792 | * |
||
793 | * ### Environment value comparison |
||
794 | * |
||
795 | * An environment value comparison, compares a value fetched from `env()` to a known value |
||
796 | * the environment value is equality checked against the provided value. |
||
797 | * |
||
798 | * e.g `addDetector('post', ['env' => 'REQUEST_METHOD', 'value' => 'POST'])` |
||
799 | * |
||
800 | * ### Pattern value comparison |
||
801 | * |
||
802 | * Pattern value comparison allows you to compare a value fetched from `env()` to a regular expression. |
||
803 | * |
||
804 | * ``` |
||
805 | * addDetector('iphone', ['env' => 'HTTP_USER_AGENT', 'pattern' => '/iPhone/i']); |
||
806 | * ``` |
||
807 | * |
||
808 | * ### Option based comparison |
||
809 | * |
||
810 | * Option based comparisons use a list of options to create a regular expression. Subsequent calls |
||
811 | * to add an already defined options detector will merge the options. |
||
812 | * |
||
813 | * ``` |
||
814 | * addDetector('mobile', ['env' => 'HTTP_USER_AGENT', 'options' => ['Fennec']]); |
||
815 | * ``` |
||
816 | * |
||
817 | * ### Request parameter detectors |
||
818 | * |
||
819 | * Allows for custom detectors on the request parameters. |
||
820 | * |
||
821 | * e.g `addDetector('requested', ['param' => 'requested', 'value' => 1]` |
||
822 | * |
||
823 | * You can also make parameter detectors that accept multiple values |
||
824 | * using the `options` key. This is useful when you want to check |
||
825 | * if a request parameter is in a list of options. |
||
826 | * |
||
827 | * `addDetector('extension', ['param' => 'ext', 'options' => ['pdf', 'csv']]` |
||
828 | * |
||
829 | * @param string $name The name of the detector. |
||
830 | * @param callable|array $callable A callable or options array for the detector definition. |
||
831 | * @return void |
||
832 | */ |
||
833 | public static function addDetector($name, $callable) |
||
845 | |||
846 | /** |
||
847 | * Add parameters to the request's parsed parameter set. This will overwrite any existing parameters. |
||
848 | * This modifies the parameters available through `$request->params`. |
||
849 | * |
||
850 | * @param array $params Array of parameters to merge in |
||
851 | * @return $this The current object, you can chain this method. |
||
852 | */ |
||
853 | public function addParams(array $params) |
||
858 | |||
859 | /** |
||
860 | * Add paths to the requests' paths vars. This will overwrite any existing paths. |
||
861 | * Provides an easy way to modify, here, webroot and base. |
||
862 | * |
||
863 | * @param array $paths Array of paths to merge in |
||
864 | * @return $this The current object, you can chain this method. |
||
865 | */ |
||
866 | public function addPaths(array $paths) |
||
875 | |||
876 | /** |
||
877 | * Get the value of the current requests URL. Will include querystring arguments. |
||
878 | * |
||
879 | * @param bool $base Include the base path, set to false to trim the base path off. |
||
880 | * @return string The current request URL including query string args. |
||
881 | */ |
||
882 | public function here($base = true) |
||
893 | |||
894 | /** |
||
895 | * Read an HTTP header from the Request information. |
||
896 | * |
||
897 | * @param string $name Name of the header you want. |
||
898 | * @return string|null Either null on no header being set or the value of the header. |
||
899 | */ |
||
900 | public function header($name) |
||
905 | |||
906 | /** |
||
907 | * Get the HTTP method used for this request. |
||
908 | * There are a few ways to specify a method. |
||
909 | * |
||
910 | * - If your client supports it you can use native HTTP methods. |
||
911 | * - You can set the HTTP-X-Method-Override header. |
||
912 | * - You can submit an input with the name `_method` |
||
913 | * |
||
914 | * Any of these 3 approaches can be used to set the HTTP method used |
||
915 | * by CakePHP internally, and will effect the result of this method. |
||
916 | * |
||
917 | * @return string The name of the HTTP method used. |
||
918 | */ |
||
919 | public function method() |
||
923 | |||
924 | /** |
||
925 | * Get the host that the request was handled on. |
||
926 | * |
||
927 | * @return string |
||
928 | */ |
||
929 | public function host() |
||
936 | |||
937 | /** |
||
938 | * Get the port the request was handled on. |
||
939 | * |
||
940 | * @return string |
||
941 | */ |
||
942 | public function port() |
||
949 | |||
950 | /** |
||
951 | * Get the current url scheme used for the request. |
||
952 | * |
||
953 | * e.g. 'http', or 'https' |
||
954 | * |
||
955 | * @return string The scheme used for the request. |
||
956 | */ |
||
957 | public function scheme() |
||
964 | |||
965 | /** |
||
966 | * Get the domain name and include $tldLength segments of the tld. |
||
967 | * |
||
968 | * @param int $tldLength Number of segments your tld contains. For example: `example.com` contains 1 tld. |
||
969 | * While `example.co.uk` contains 2. |
||
970 | * @return string Domain name without subdomains. |
||
971 | */ |
||
972 | public function domain($tldLength = 1) |
||
978 | |||
979 | /** |
||
980 | * Get the subdomains for a host. |
||
981 | * |
||
982 | * @param int $tldLength Number of segments your tld contains. For example: `example.com` contains 1 tld. |
||
983 | * While `example.co.uk` contains 2. |
||
984 | * @return array An array of subdomains. |
||
985 | */ |
||
986 | public function subdomains($tldLength = 1) |
||
991 | |||
992 | /** |
||
993 | * Find out which content types the client accepts or check if they accept a |
||
994 | * particular type of content. |
||
995 | * |
||
996 | * #### Get all types: |
||
997 | * |
||
998 | * ``` |
||
999 | * $this->request->accepts(); |
||
1000 | * ``` |
||
1001 | * |
||
1002 | * #### Check for a single type: |
||
1003 | * |
||
1004 | * ``` |
||
1005 | * $this->request->accepts('application/json'); |
||
1006 | * ``` |
||
1007 | * |
||
1008 | * This method will order the returned content types by the preference values indicated |
||
1009 | * by the client. |
||
1010 | * |
||
1011 | * @param string|null $type The content type to check for. Leave null to get all types a client accepts. |
||
1012 | * @return array|bool Either an array of all the types the client accepts or a boolean if they accept the |
||
1013 | * provided type. |
||
1014 | */ |
||
1015 | public function accepts($type = null) |
||
1027 | |||
1028 | /** |
||
1029 | * Parse the HTTP_ACCEPT header and return a sorted array with content types |
||
1030 | * as the keys, and pref values as the values. |
||
1031 | * |
||
1032 | * Generally you want to use Cake\Network\Request::accept() to get a simple list |
||
1033 | * of the accepted content types. |
||
1034 | * |
||
1035 | * @return array An array of prefValue => [content/types] |
||
1036 | */ |
||
1037 | public function parseAccept() |
||
1041 | |||
1042 | /** |
||
1043 | * Get the languages accepted by the client, or check if a specific language is accepted. |
||
1044 | * |
||
1045 | * Get the list of accepted languages: |
||
1046 | * |
||
1047 | * ``` \Cake\Network\Request::acceptLanguage(); ``` |
||
1048 | * |
||
1049 | * Check if a specific language is accepted: |
||
1050 | * |
||
1051 | * ``` \Cake\Network\Request::acceptLanguage('es-es'); ``` |
||
1052 | * |
||
1053 | * @param string|null $language The language to test. |
||
1054 | * @return array|bool If a $language is provided, a boolean. Otherwise the array of accepted languages. |
||
1055 | */ |
||
1056 | public function acceptLanguage($language = null) |
||
1074 | |||
1075 | /** |
||
1076 | * Parse Accept* headers with qualifier options. |
||
1077 | * |
||
1078 | * Only qualifiers will be extracted, any other accept extensions will be |
||
1079 | * discarded as they are not frequently used. |
||
1080 | * |
||
1081 | * @param string $header Header to parse. |
||
1082 | * @return array |
||
1083 | */ |
||
1084 | protected function _parseAcceptWithQualifier($header) |
||
1114 | |||
1115 | /** |
||
1116 | * Provides a read accessor for `$this->query`. Allows you |
||
1117 | * to use a syntax similar to `CakeSession` for reading URL query data. |
||
1118 | * |
||
1119 | * @param string $name Query string variable name |
||
1120 | * @return mixed The value being read |
||
1121 | */ |
||
1122 | public function query($name) |
||
1126 | |||
1127 | /** |
||
1128 | * Provides a read/write accessor for `$this->data`. Allows you |
||
1129 | * to use a syntax similar to `Cake\Model\Datasource\Session` for reading post data. |
||
1130 | * |
||
1131 | * ### Reading values. |
||
1132 | * |
||
1133 | * ``` |
||
1134 | * $request->data('Post.title'); |
||
1135 | * ``` |
||
1136 | * |
||
1137 | * When reading values you will get `null` for keys/values that do not exist. |
||
1138 | * |
||
1139 | * ### Writing values |
||
1140 | * |
||
1141 | * ``` |
||
1142 | * $request->data('Post.title', 'New post!'); |
||
1143 | * ``` |
||
1144 | * |
||
1145 | * You can write to any value, even paths/keys that do not exist, and the arrays |
||
1146 | * will be created for you. |
||
1147 | * |
||
1148 | * @param string|null $name Dot separated name of the value to read/write |
||
1149 | * @return mixed|$this Either the value being read, or this so you can chain consecutive writes. |
||
1150 | */ |
||
1151 | public function data($name = null) |
||
1163 | |||
1164 | /** |
||
1165 | * Safely access the values in $this->params. |
||
1166 | * |
||
1167 | * @param string $name The name of the parameter to get. |
||
1168 | * @return mixed|$this The value of the provided parameter. Will |
||
1169 | * return false if the parameter doesn't exist or is falsey. |
||
1170 | */ |
||
1171 | public function param($name) |
||
1183 | |||
1184 | /** |
||
1185 | * Read data from `php://input`. Useful when interacting with XML or JSON |
||
1186 | * request body content. |
||
1187 | * |
||
1188 | * Getting input with a decoding function: |
||
1189 | * |
||
1190 | * ``` |
||
1191 | * $this->request->input('json_decode'); |
||
1192 | * ``` |
||
1193 | * |
||
1194 | * Getting input using a decoding function, and additional params: |
||
1195 | * |
||
1196 | * ``` |
||
1197 | * $this->request->input('Xml::build', ['return' => 'DOMDocument']); |
||
1198 | * ``` |
||
1199 | * |
||
1200 | * Any additional parameters are applied to the callback in the order they are given. |
||
1201 | * |
||
1202 | * @param string|null $callback A decoding callback that will convert the string data to another |
||
1203 | * representation. Leave empty to access the raw input data. You can also |
||
1204 | * supply additional parameters for the decoding callback using var args, see above. |
||
1205 | * @return string The decoded/processed request data. |
||
1206 | */ |
||
1207 | public function input($callback = null) |
||
1218 | |||
1219 | /** |
||
1220 | * Read cookie data from the request's cookie data. |
||
1221 | * |
||
1222 | * @param string $key The key you want to read. |
||
1223 | * @return null|string Either the cookie value, or null if the value doesn't exist. |
||
1224 | */ |
||
1225 | public function cookie($key) |
||
1232 | |||
1233 | /** |
||
1234 | * Get/Set value from the request's environment data. |
||
1235 | * Fallback to using env() if key not set in $environment property. |
||
1236 | * |
||
1237 | * @param string $key The key you want to read/write from/to. |
||
1238 | * @param string|null $value Value to set. Default null. |
||
1239 | * @param string|null $default Default value when trying to retrieve an environment |
||
1240 | * variable's value that does not exist. The value parameter must be null. |
||
1241 | * @return $this|string|null This instance if used as setter, |
||
1242 | * if used as getter either the environment value, or null if the value doesn't exist. |
||
1243 | */ |
||
1244 | public function env($key, $value = null, $default = null) |
||
1258 | |||
1259 | /** |
||
1260 | * Allow only certain HTTP request methods, if the request method does not match |
||
1261 | * a 405 error will be shown and the required "Allow" response header will be set. |
||
1262 | * |
||
1263 | * Example: |
||
1264 | * |
||
1265 | * $this->request->allowMethod('post'); |
||
1266 | * or |
||
1267 | * $this->request->allowMethod(['post', 'delete']); |
||
1268 | * |
||
1269 | * If the request would be GET, response header "Allow: POST, DELETE" will be set |
||
1270 | * and a 405 error will be returned. |
||
1271 | * |
||
1272 | * @param string|array $methods Allowed HTTP request methods. |
||
1273 | * @return bool true |
||
1274 | * @throws \Cake\Network\Exception\MethodNotAllowedException |
||
1275 | */ |
||
1276 | public function allowMethod($methods) |
||
1289 | |||
1290 | /** |
||
1291 | * Read data from php://input, mocked in tests. |
||
1292 | * |
||
1293 | * @return string contents of php://input |
||
1294 | */ |
||
1295 | protected function _readInput() |
||
1305 | |||
1306 | /** |
||
1307 | * Modify data originally from `php://input`. Useful for altering json/xml data |
||
1308 | * in middleware or DispatcherFilters before it gets to RequestHandlerComponent |
||
1309 | * |
||
1310 | * @param string $input A string to replace original parsed data from input() |
||
1311 | * @return void |
||
1312 | */ |
||
1313 | public function setInput($input) |
||
1317 | |||
1318 | /** |
||
1319 | * Array access read implementation |
||
1320 | * |
||
1321 | * @param string $name Name of the key being accessed. |
||
1322 | * @return mixed |
||
1323 | */ |
||
1324 | public function offsetGet($name) |
||
1337 | |||
1338 | /** |
||
1339 | * Array access write implementation |
||
1340 | * |
||
1341 | * @param string $name Name of the key being written |
||
1342 | * @param mixed $value The value being written. |
||
1343 | * @return void |
||
1344 | */ |
||
1345 | public function offsetSet($name, $value) |
||
1349 | |||
1350 | /** |
||
1351 | * Array access isset() implementation |
||
1352 | * |
||
1353 | * @param string $name thing to check. |
||
1354 | * @return bool |
||
1355 | */ |
||
1356 | public function offsetExists($name) |
||
1360 | |||
1361 | /** |
||
1362 | * Array access unset() implementation |
||
1363 | * |
||
1364 | * @param string $name Name to unset. |
||
1365 | * @return void |
||
1366 | */ |
||
1367 | public function offsetUnset($name) |
||
1371 | } |
||
1372 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: