Complex classes like Response 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 Response, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
62 | class Response extends \yii\base\Response |
||
63 | { |
||
64 | /** |
||
65 | * @event ResponseEvent an event that is triggered at the beginning of [[send()]]. |
||
66 | */ |
||
67 | const EVENT_BEFORE_SEND = 'beforeSend'; |
||
68 | /** |
||
69 | * @event ResponseEvent an event that is triggered at the end of [[send()]]. |
||
70 | */ |
||
71 | const EVENT_AFTER_SEND = 'afterSend'; |
||
72 | /** |
||
73 | * @event ResponseEvent an event that is triggered right after [[prepare()]] is called in [[send()]]. |
||
74 | * You may respond to this event to filter the response content before it is sent to the client. |
||
75 | */ |
||
76 | const EVENT_AFTER_PREPARE = 'afterPrepare'; |
||
77 | const FORMAT_RAW = 'raw'; |
||
78 | const FORMAT_HTML = 'html'; |
||
79 | const FORMAT_JSON = 'json'; |
||
80 | const FORMAT_JSONP = 'jsonp'; |
||
81 | const FORMAT_XML = 'xml'; |
||
82 | |||
83 | /** |
||
84 | * @var string the response format. This determines how to convert [[data]] into [[content]] |
||
85 | * when the latter is not set. The value of this property must be one of the keys declared in the [[formatters]] array. |
||
86 | * By default, the following formats are supported: |
||
87 | * |
||
88 | * - [[FORMAT_RAW]]: the data will be treated as the response content without any conversion. |
||
89 | * No extra HTTP header will be added. |
||
90 | * - [[FORMAT_HTML]]: the data will be treated as the response content without any conversion. |
||
91 | * The "Content-Type" header will set as "text/html". |
||
92 | * - [[FORMAT_JSON]]: the data will be converted into JSON format, and the "Content-Type" |
||
93 | * header will be set as "application/json". |
||
94 | * - [[FORMAT_JSONP]]: the data will be converted into JSONP format, and the "Content-Type" |
||
95 | * header will be set as "text/javascript". Note that in this case `$data` must be an array |
||
96 | * with "data" and "callback" elements. The former refers to the actual data to be sent, |
||
97 | * while the latter refers to the name of the JavaScript callback. |
||
98 | * - [[FORMAT_XML]]: the data will be converted into XML format. Please refer to [[XmlResponseFormatter]] |
||
99 | * for more details. |
||
100 | * |
||
101 | * You may customize the formatting process or support additional formats by configuring [[formatters]]. |
||
102 | * @see formatters |
||
103 | */ |
||
104 | public $format = self::FORMAT_HTML; |
||
105 | /** |
||
106 | * @var string the MIME type (e.g. `application/json`) from the request ACCEPT header chosen for this response. |
||
107 | * This property is mainly set by [[\yii\filters\ContentNegotiator]]. |
||
108 | */ |
||
109 | public $acceptMimeType; |
||
110 | /** |
||
111 | * @var array the parameters (e.g. `['q' => 1, 'version' => '1.0']`) associated with the [[acceptMimeType|chosen MIME type]]. |
||
112 | * This is a list of name-value pairs associated with [[acceptMimeType]] from the ACCEPT HTTP header. |
||
113 | * This property is mainly set by [[\yii\filters\ContentNegotiator]]. |
||
114 | */ |
||
115 | public $acceptParams = []; |
||
116 | /** |
||
117 | * @var array the formatters for converting data into the response content of the specified [[format]]. |
||
118 | * The array keys are the format names, and the array values are the corresponding configurations |
||
119 | * for creating the formatter objects. |
||
120 | * @see format |
||
121 | * @see defaultFormatters |
||
122 | */ |
||
123 | public $formatters = []; |
||
124 | /** |
||
125 | * @var mixed the original response data. When this is not null, it will be converted into [[content]] |
||
126 | * according to [[format]] when the response is being sent out. |
||
127 | * @see content |
||
128 | */ |
||
129 | public $data; |
||
130 | /** |
||
131 | * @var string the response content. When [[data]] is not null, it will be converted into [[content]] |
||
132 | * according to [[format]] when the response is being sent out. |
||
133 | * @see data |
||
134 | */ |
||
135 | public $content; |
||
136 | /** |
||
137 | * @var resource|array the stream to be sent. This can be a stream handle or an array of stream handle, |
||
138 | * the begin position and the end position. Note that when this property is set, the [[data]] and [[content]] |
||
139 | * properties will be ignored by [[send()]]. |
||
140 | */ |
||
141 | public $stream; |
||
142 | /** |
||
143 | * @var string the charset of the text response. If not set, it will use |
||
144 | * the value of [[Application::charset]]. |
||
145 | */ |
||
146 | public $charset; |
||
147 | /** |
||
148 | * @var string the HTTP status description that comes together with the status code. |
||
149 | * @see httpStatuses |
||
150 | */ |
||
151 | public $statusText = 'OK'; |
||
152 | /** |
||
153 | * @var string the version of the HTTP protocol to use. If not set, it will be determined via `$_SERVER['SERVER_PROTOCOL']`, |
||
154 | * or '1.1' if that is not available. |
||
155 | */ |
||
156 | public $version; |
||
157 | /** |
||
158 | * @var bool whether the response has been sent. If this is true, calling [[send()]] will do nothing. |
||
159 | */ |
||
160 | public $isSent = false; |
||
161 | /** |
||
162 | * @var array list of HTTP status codes and the corresponding texts |
||
163 | */ |
||
164 | public static $httpStatuses = [ |
||
165 | 100 => 'Continue', |
||
166 | 101 => 'Switching Protocols', |
||
167 | 102 => 'Processing', |
||
168 | 118 => 'Connection timed out', |
||
169 | 200 => 'OK', |
||
170 | 201 => 'Created', |
||
171 | 202 => 'Accepted', |
||
172 | 203 => 'Non-Authoritative', |
||
173 | 204 => 'No Content', |
||
174 | 205 => 'Reset Content', |
||
175 | 206 => 'Partial Content', |
||
176 | 207 => 'Multi-Status', |
||
177 | 208 => 'Already Reported', |
||
178 | 210 => 'Content Different', |
||
179 | 226 => 'IM Used', |
||
180 | 300 => 'Multiple Choices', |
||
181 | 301 => 'Moved Permanently', |
||
182 | 302 => 'Found', |
||
183 | 303 => 'See Other', |
||
184 | 304 => 'Not Modified', |
||
185 | 305 => 'Use Proxy', |
||
186 | 306 => 'Reserved', |
||
187 | 307 => 'Temporary Redirect', |
||
188 | 308 => 'Permanent Redirect', |
||
189 | 310 => 'Too many Redirect', |
||
190 | 400 => 'Bad Request', |
||
191 | 401 => 'Unauthorized', |
||
192 | 402 => 'Payment Required', |
||
193 | 403 => 'Forbidden', |
||
194 | 404 => 'Not Found', |
||
195 | 405 => 'Method Not Allowed', |
||
196 | 406 => 'Not Acceptable', |
||
197 | 407 => 'Proxy Authentication Required', |
||
198 | 408 => 'Request Time-out', |
||
199 | 409 => 'Conflict', |
||
200 | 410 => 'Gone', |
||
201 | 411 => 'Length Required', |
||
202 | 412 => 'Precondition Failed', |
||
203 | 413 => 'Request Entity Too Large', |
||
204 | 414 => 'Request-URI Too Long', |
||
205 | 415 => 'Unsupported Media Type', |
||
206 | 416 => 'Requested range unsatisfiable', |
||
207 | 417 => 'Expectation failed', |
||
208 | 418 => 'I\'m a teapot', |
||
209 | 421 => 'Misdirected Request', |
||
210 | 422 => 'Unprocessable entity', |
||
211 | 423 => 'Locked', |
||
212 | 424 => 'Method failure', |
||
213 | 425 => 'Unordered Collection', |
||
214 | 426 => 'Upgrade Required', |
||
215 | 428 => 'Precondition Required', |
||
216 | 429 => 'Too Many Requests', |
||
217 | 431 => 'Request Header Fields Too Large', |
||
218 | 449 => 'Retry With', |
||
219 | 450 => 'Blocked by Windows Parental Controls', |
||
220 | 451 => 'Unavailable For Legal Reasons', |
||
221 | 500 => 'Internal Server Error', |
||
222 | 501 => 'Not Implemented', |
||
223 | 502 => 'Bad Gateway or Proxy Error', |
||
224 | 503 => 'Service Unavailable', |
||
225 | 504 => 'Gateway Time-out', |
||
226 | 505 => 'HTTP Version not supported', |
||
227 | 507 => 'Insufficient storage', |
||
228 | 508 => 'Loop Detected', |
||
229 | 509 => 'Bandwidth Limit Exceeded', |
||
230 | 510 => 'Not Extended', |
||
231 | 511 => 'Network Authentication Required', |
||
232 | ]; |
||
233 | |||
234 | /** |
||
235 | * @var int the HTTP status code to send with the response. |
||
236 | */ |
||
237 | private $_statusCode = 200; |
||
238 | /** |
||
239 | * @var HeaderCollection |
||
240 | */ |
||
241 | private $_headers; |
||
242 | |||
243 | |||
244 | /** |
||
245 | * Initializes this component. |
||
246 | */ |
||
247 | 217 | public function init() |
|
248 | { |
||
249 | 217 | if ($this->version === null) { |
|
250 | 217 | if (isset($_SERVER['SERVER_PROTOCOL']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.0') { |
|
251 | $this->version = '1.0'; |
||
252 | } else { |
||
253 | 217 | $this->version = '1.1'; |
|
254 | } |
||
255 | } |
||
256 | 217 | if ($this->charset === null) { |
|
257 | 217 | $this->charset = Yii::$app->charset; |
|
258 | } |
||
259 | 217 | $this->formatters = array_merge($this->defaultFormatters(), $this->formatters); |
|
260 | 217 | } |
|
261 | |||
262 | /** |
||
263 | * @return int the HTTP status code to send with the response. |
||
264 | */ |
||
265 | 41 | public function getStatusCode() |
|
269 | |||
270 | /** |
||
271 | * Sets the response status code. |
||
272 | * This method will set the corresponding status text if `$text` is null. |
||
273 | * @param int $value the status code |
||
274 | * @param string $text the status text. If not set, it will be set automatically based on the status code. |
||
275 | * @throws InvalidArgumentException if the status code is invalid. |
||
276 | * @return $this the response object itself |
||
277 | */ |
||
278 | 39 | public function setStatusCode($value, $text = null) |
|
295 | |||
296 | /** |
||
297 | * Sets the response status code based on the exception. |
||
298 | * @param \Exception|\Error $e the exception object. |
||
299 | * @throws InvalidArgumentException if the status code is invalid. |
||
300 | * @return $this the response object itself |
||
301 | * @since 2.0.12 |
||
302 | */ |
||
303 | 16 | public function setStatusCodeByException($e) |
|
313 | |||
314 | /** |
||
315 | * Returns the header collection. |
||
316 | * The header collection contains the currently registered HTTP headers. |
||
317 | * @return HeaderCollection the header collection |
||
318 | */ |
||
319 | 97 | public function getHeaders() |
|
327 | |||
328 | /** |
||
329 | * Sends the response to the client. |
||
330 | */ |
||
331 | 21 | public function send() |
|
344 | |||
345 | /** |
||
346 | * Clears the headers, cookies, content, status code of the response. |
||
347 | */ |
||
348 | public function clear() |
||
359 | |||
360 | /** |
||
361 | * Sends the response headers to the client. |
||
362 | */ |
||
363 | 21 | protected function sendHeaders() |
|
383 | |||
384 | /** |
||
385 | * Sends the cookies to the client. |
||
386 | */ |
||
387 | 21 | protected function sendCookies() |
|
407 | |||
408 | /** |
||
409 | * Sends the response content to the client. |
||
410 | */ |
||
411 | 21 | protected function sendContent() |
|
441 | |||
442 | /** |
||
443 | * Sends a file to the browser. |
||
444 | * |
||
445 | * Note that this method only prepares the response for file sending. The file is not sent |
||
446 | * until [[send()]] is called explicitly or implicitly. The latter is done after you return from a controller action. |
||
447 | * |
||
448 | * The following is an example implementation of a controller action that allows requesting files from a directory |
||
449 | * that is not accessible from web: |
||
450 | * |
||
451 | * ```php |
||
452 | * public function actionFile($filename) |
||
453 | * { |
||
454 | * $storagePath = Yii::getAlias('@app/files'); |
||
455 | * |
||
456 | * // check filename for allowed chars (do not allow ../ to avoid security issue: downloading arbitrary files) |
||
457 | * if (!preg_match('/^[a-z0-9]+\.[a-z0-9]+$/i', $filename) || !is_file("$storagePath/$filename")) { |
||
458 | * throw new \yii\web\NotFoundHttpException('The file does not exists.'); |
||
459 | * } |
||
460 | * return Yii::$app->response->sendFile("$storagePath/$filename", $filename); |
||
461 | * } |
||
462 | * ``` |
||
463 | * |
||
464 | * @param string $filePath the path of the file to be sent. |
||
465 | * @param string $attachmentName the file name shown to the user. If null, it will be determined from `$filePath`. |
||
466 | * @param array $options additional options for sending the file. The following options are supported: |
||
467 | * |
||
468 | * - `mimeType`: the MIME type of the content. If not set, it will be guessed based on `$filePath` |
||
469 | * - `inline`: boolean, whether the browser should open the file within the browser window. Defaults to false, |
||
470 | * meaning a download dialog will pop up. |
||
471 | * |
||
472 | * @return $this the response object itself |
||
473 | * @see sendContentAsFile() |
||
474 | * @see sendStreamAsFile() |
||
475 | * @see xSendFile() |
||
476 | */ |
||
477 | 7 | public function sendFile($filePath, $attachmentName = null, $options = []) |
|
490 | |||
491 | /** |
||
492 | * Sends the specified content as a file to the browser. |
||
493 | * |
||
494 | * Note that this method only prepares the response for file sending. The file is not sent |
||
495 | * until [[send()]] is called explicitly or implicitly. The latter is done after you return from a controller action. |
||
496 | * |
||
497 | * @param string $content the content to be sent. The existing [[content]] will be discarded. |
||
498 | * @param string $attachmentName the file name shown to the user. |
||
499 | * @param array $options additional options for sending the file. The following options are supported: |
||
500 | * |
||
501 | * - `mimeType`: the MIME type of the content. Defaults to 'application/octet-stream'. |
||
502 | * - `inline`: boolean, whether the browser should open the file within the browser window. Defaults to false, |
||
503 | * meaning a download dialog will pop up. |
||
504 | * |
||
505 | * @return $this the response object itself |
||
506 | * @throws RangeNotSatisfiableHttpException if the requested range is not satisfiable |
||
507 | * @see sendFile() for an example implementation. |
||
508 | */ |
||
509 | 1 | public function sendContentAsFile($content, $attachmentName, $options = []) |
|
538 | |||
539 | /** |
||
540 | * Sends the specified stream as a file to the browser. |
||
541 | * |
||
542 | * Note that this method only prepares the response for file sending. The file is not sent |
||
543 | * until [[send()]] is called explicitly or implicitly. The latter is done after you return from a controller action. |
||
544 | * |
||
545 | * @param resource $handle the handle of the stream to be sent. |
||
546 | * @param string $attachmentName the file name shown to the user. |
||
547 | * @param array $options additional options for sending the file. The following options are supported: |
||
548 | * |
||
549 | * - `mimeType`: the MIME type of the content. Defaults to 'application/octet-stream'. |
||
550 | * - `inline`: boolean, whether the browser should open the file within the browser window. Defaults to false, |
||
551 | * meaning a download dialog will pop up. |
||
552 | * - `fileSize`: the size of the content to stream this is useful when size of the content is known |
||
553 | * and the content is not seekable. Defaults to content size using `ftell()`. |
||
554 | * This option is available since version 2.0.4. |
||
555 | * |
||
556 | * @return $this the response object itself |
||
557 | * @throws RangeNotSatisfiableHttpException if the requested range is not satisfiable |
||
558 | * @see sendFile() for an example implementation. |
||
559 | */ |
||
560 | 7 | public function sendStreamAsFile($handle, $attachmentName, $options = []) |
|
592 | |||
593 | /** |
||
594 | * Sets a default set of HTTP headers for file downloading purpose. |
||
595 | * @param string $attachmentName the attachment file name |
||
596 | * @param string $mimeType the MIME type for the response. If null, `Content-Type` header will NOT be set. |
||
597 | * @param bool $inline whether the browser should open the file within the browser window. Defaults to false, |
||
598 | * meaning a download dialog will pop up. |
||
599 | * @param int $contentLength the byte length of the file being downloaded. If null, `Content-Length` header will NOT be set. |
||
600 | * @return $this the response object itself |
||
601 | */ |
||
602 | 4 | public function setDownloadHeaders($attachmentName, $mimeType = null, $inline = false, $contentLength = null) |
|
623 | |||
624 | /** |
||
625 | * Determines the HTTP range given in the request. |
||
626 | * @param int $fileSize the size of the file that will be used to validate the requested HTTP range. |
||
627 | * @return array|bool the range (begin, end), or false if the range request is invalid. |
||
628 | */ |
||
629 | 8 | protected function getHttpRange($fileSize) |
|
657 | |||
658 | /** |
||
659 | * Sends existing file to a browser as a download using x-sendfile. |
||
660 | * |
||
661 | * X-Sendfile is a feature allowing a web application to redirect the request for a file to the webserver |
||
662 | * that in turn processes the request, this way eliminating the need to perform tasks like reading the file |
||
663 | * and sending it to the user. When dealing with a lot of files (or very big files) this can lead to a great |
||
664 | * increase in performance as the web application is allowed to terminate earlier while the webserver is |
||
665 | * handling the request. |
||
666 | * |
||
667 | * The request is sent to the server through a special non-standard HTTP-header. |
||
668 | * When the web server encounters the presence of such header it will discard all output and send the file |
||
669 | * specified by that header using web server internals including all optimizations like caching-headers. |
||
670 | * |
||
671 | * As this header directive is non-standard different directives exists for different web servers applications: |
||
672 | * |
||
673 | * - Apache: [X-Sendfile](http://tn123.org/mod_xsendfile) |
||
674 | * - Lighttpd v1.4: [X-LIGHTTPD-send-file](http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file) |
||
675 | * - Lighttpd v1.5: [X-Sendfile](http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file) |
||
676 | * - Nginx: [X-Accel-Redirect](http://wiki.nginx.org/XSendfile) |
||
677 | * - Cherokee: [X-Sendfile and X-Accel-Redirect](http://www.cherokee-project.com/doc/other_goodies.html#x-sendfile) |
||
678 | * |
||
679 | * So for this method to work the X-SENDFILE option/module should be enabled by the web server and |
||
680 | * a proper xHeader should be sent. |
||
681 | * |
||
682 | * **Note** |
||
683 | * |
||
684 | * This option allows to download files that are not under web folders, and even files that are otherwise protected |
||
685 | * (deny from all) like `.htaccess`. |
||
686 | * |
||
687 | * **Side effects** |
||
688 | * |
||
689 | * If this option is disabled by the web server, when this method is called a download configuration dialog |
||
690 | * will open but the downloaded file will have 0 bytes. |
||
691 | * |
||
692 | * **Known issues** |
||
693 | * |
||
694 | * There is a Bug with Internet Explorer 6, 7 and 8 when X-SENDFILE is used over an SSL connection, it will show |
||
695 | * an error message like this: "Internet Explorer was not able to open this Internet site. The requested site |
||
696 | * is either unavailable or cannot be found.". You can work around this problem by removing the `Pragma`-header. |
||
697 | * |
||
698 | * **Example** |
||
699 | * |
||
700 | * ```php |
||
701 | * Yii::$app->response->xSendFile('/home/user/Pictures/picture1.jpg'); |
||
702 | * ``` |
||
703 | * |
||
704 | * @param string $filePath file name with full path |
||
705 | * @param string $attachmentName file name shown to the user. If null, it will be determined from `$filePath`. |
||
706 | * @param array $options additional options for sending the file. The following options are supported: |
||
707 | * |
||
708 | * - `mimeType`: the MIME type of the content. If not set, it will be guessed based on `$filePath` |
||
709 | * - `inline`: boolean, whether the browser should open the file within the browser window. Defaults to false, |
||
710 | * meaning a download dialog will pop up. |
||
711 | * - xHeader: string, the name of the x-sendfile header. Defaults to "X-Sendfile". |
||
712 | * |
||
713 | * @return $this the response object itself |
||
714 | * @see sendFile() |
||
715 | */ |
||
716 | public function xSendFile($filePath, $attachmentName = null, $options = []) |
||
742 | |||
743 | /** |
||
744 | * Returns Content-Disposition header value that is safe to use with both old and new browsers. |
||
745 | * |
||
746 | * Fallback name: |
||
747 | * |
||
748 | * - Causes issues if contains non-ASCII characters with codes less than 32 or more than 126. |
||
749 | * - Causes issues if contains urlencoded characters (starting with `%`) or `%` character. Some browsers interpret |
||
750 | * `filename="X"` as urlencoded name, some don't. |
||
751 | * - Causes issues if contains path separator characters such as `\` or `/`. |
||
752 | * - Since value is wrapped with `"`, it should be escaped as `\"`. |
||
753 | * - Since input could contain non-ASCII characters, fallback is obtained by transliteration. |
||
754 | * |
||
755 | * UTF name: |
||
756 | * |
||
757 | * - Causes issues if contains path separator characters such as `\` or `/`. |
||
758 | * - Should be urlencoded since headers are ASCII-only. |
||
759 | * - Could be omitted if it exactly matches fallback name. |
||
760 | * |
||
761 | * @param string $disposition |
||
762 | * @param string $attachmentName |
||
763 | * @return string |
||
764 | * |
||
765 | * @since 2.0.10 |
||
766 | */ |
||
767 | 4 | protected function getDispositionHeaderValue($disposition, $attachmentName) |
|
783 | |||
784 | /** |
||
785 | * Redirects the browser to the specified URL. |
||
786 | * |
||
787 | * This method adds a "Location" header to the current response. Note that it does not send out |
||
788 | * the header until [[send()]] is called. In a controller action you may use this method as follows: |
||
789 | * |
||
790 | * ```php |
||
791 | * return Yii::$app->getResponse()->redirect($url); |
||
792 | * ``` |
||
793 | * |
||
794 | * In other places, if you want to send out the "Location" header immediately, you should use |
||
795 | * the following code: |
||
796 | * |
||
797 | * ```php |
||
798 | * Yii::$app->getResponse()->redirect($url)->send(); |
||
799 | * return; |
||
800 | * ``` |
||
801 | * |
||
802 | * In AJAX mode, this normally will not work as expected unless there are some |
||
803 | * client-side JavaScript code handling the redirection. To help achieve this goal, |
||
804 | * this method will send out a "X-Redirect" header instead of "Location". |
||
805 | * |
||
806 | * If you use the "yii" JavaScript module, it will handle the AJAX redirection as |
||
807 | * described above. Otherwise, you should write the following JavaScript code to |
||
808 | * handle the redirection: |
||
809 | * |
||
810 | * ```javascript |
||
811 | * $document.ajaxComplete(function (event, xhr, settings) { |
||
812 | * var url = xhr && xhr.getResponseHeader('X-Redirect'); |
||
813 | * if (url) { |
||
814 | * window.location = url; |
||
815 | * } |
||
816 | * }); |
||
817 | * ``` |
||
818 | * |
||
819 | * @param string|array $url the URL to be redirected to. This can be in one of the following formats: |
||
820 | * |
||
821 | * - a string representing a URL (e.g. "http://example.com") |
||
822 | * - a string representing a URL alias (e.g. "@example.com") |
||
823 | * - an array in the format of `[$route, ...name-value pairs...]` (e.g. `['site/index', 'ref' => 1]`). |
||
824 | * Note that the route is with respect to the whole application, instead of relative to a controller or module. |
||
825 | * [[Url::to()]] will be used to convert the array into a URL. |
||
826 | * |
||
827 | * Any relative URL that starts with a single forward slash "/" will be converted |
||
828 | * into an absolute one by prepending it with the host info of the current request. |
||
829 | * |
||
830 | * @param int $statusCode the HTTP status code. Defaults to 302. |
||
831 | * See <https://tools.ietf.org/html/rfc2616#section-10> |
||
832 | * for details about HTTP status code |
||
833 | * @param bool $checkAjax whether to specially handle AJAX (and PJAX) requests. Defaults to true, |
||
834 | * meaning if the current request is an AJAX or PJAX request, then calling this method will cause the browser |
||
835 | * to redirect to the given URL. If this is false, a `Location` header will be sent, which when received as |
||
836 | * an AJAX/PJAX response, may NOT cause browser redirection. |
||
837 | * Takes effect only when request header `X-Ie-Redirect-Compatibility` is absent. |
||
838 | * @return $this the response object itself |
||
839 | */ |
||
840 | 3 | public function redirect($url, $statusCode = 302, $checkAjax = true) |
|
873 | |||
874 | /** |
||
875 | * Refreshes the current page. |
||
876 | * The effect of this method call is the same as the user pressing the refresh button of his browser |
||
877 | * (without re-posting data). |
||
878 | * |
||
879 | * In a controller action you may use this method like this: |
||
880 | * |
||
881 | * ```php |
||
882 | * return Yii::$app->getResponse()->refresh(); |
||
883 | * ``` |
||
884 | * |
||
885 | * @param string $anchor the anchor that should be appended to the redirection URL. |
||
886 | * Defaults to empty. Make sure the anchor starts with '#' if you want to specify it. |
||
887 | * @return Response the response object itself |
||
888 | */ |
||
889 | public function refresh($anchor = '') |
||
893 | |||
894 | private $_cookies; |
||
895 | |||
896 | /** |
||
897 | * Returns the cookie collection. |
||
898 | * |
||
899 | * Through the returned cookie collection, you add or remove cookies as follows, |
||
900 | * |
||
901 | * ```php |
||
902 | * // add a cookie |
||
903 | * $response->cookies->add(new Cookie([ |
||
904 | * 'name' => $name, |
||
905 | * 'value' => $value, |
||
906 | * ]); |
||
907 | * |
||
908 | * // remove a cookie |
||
909 | * $response->cookies->remove('name'); |
||
910 | * // alternatively |
||
911 | * unset($response->cookies['name']); |
||
912 | * ``` |
||
913 | * |
||
914 | * @return CookieCollection the cookie collection. |
||
915 | */ |
||
916 | 70 | public function getCookies() |
|
924 | |||
925 | /** |
||
926 | * @return bool whether this response has a valid [[statusCode]]. |
||
927 | */ |
||
928 | 39 | public function getIsInvalid() |
|
932 | |||
933 | /** |
||
934 | * @return bool whether this response is informational |
||
935 | */ |
||
936 | public function getIsInformational() |
||
940 | |||
941 | /** |
||
942 | * @return bool whether this response is successful |
||
943 | */ |
||
944 | public function getIsSuccessful() |
||
948 | |||
949 | /** |
||
950 | * @return bool whether this response is a redirection |
||
951 | */ |
||
952 | 1 | public function getIsRedirection() |
|
956 | |||
957 | /** |
||
958 | * @return bool whether this response indicates a client error |
||
959 | */ |
||
960 | public function getIsClientError() |
||
964 | |||
965 | /** |
||
966 | * @return bool whether this response indicates a server error |
||
967 | */ |
||
968 | public function getIsServerError() |
||
972 | |||
973 | /** |
||
974 | * @return bool whether this response is OK |
||
975 | */ |
||
976 | public function getIsOk() |
||
980 | |||
981 | /** |
||
982 | * @return bool whether this response indicates the current request is forbidden |
||
983 | */ |
||
984 | public function getIsForbidden() |
||
988 | |||
989 | /** |
||
990 | * @return bool whether this response indicates the currently requested resource is not found |
||
991 | */ |
||
992 | public function getIsNotFound() |
||
996 | |||
997 | /** |
||
998 | * @return bool whether this response is empty |
||
999 | */ |
||
1000 | public function getIsEmpty() |
||
1004 | |||
1005 | /** |
||
1006 | * @return array the formatters that are supported by default |
||
1007 | */ |
||
1008 | 217 | protected function defaultFormatters() |
|
1026 | |||
1027 | /** |
||
1028 | * Prepares for sending the response. |
||
1029 | * The default implementation will convert [[data]] into [[content]] and set headers accordingly. |
||
1030 | * @throws InvalidConfigException if the formatter for the specified format is invalid or [[format]] is not supported |
||
1031 | */ |
||
1032 | 21 | protected function prepare() |
|
1066 | } |
||
1067 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.