• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 8677009716

13 Apr 2024 11:45PM UTC coverage: 84.44% (-2.2%) from 86.607%
8677009716

push

github

web-flow
Merge pull request #8776 from kenjis/fix-findQualifiedNameFromPath-Cannot-declare-class-v3

fix: Cannot declare class CodeIgniter\Config\Services, because the name is already in use

0 of 3 new or added lines in 1 file covered. (0.0%)

478 existing lines in 72 files now uncovered.

20318 of 24062 relevant lines covered (84.44%)

188.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.33
/system/HTTP/Response.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\HTTP;
15

16
use CodeIgniter\Cookie\Cookie;
17
use CodeIgniter\Cookie\CookieStore;
18
use CodeIgniter\HTTP\Exceptions\HTTPException;
19
use Config\App;
20
use Config\Cookie as CookieConfig;
21
use Config\Services;
22

23
/**
24
 * Representation of an outgoing, server-side response.
25
 *
26
 * Per the HTTP specification, this interface includes properties for
27
 * each of the following:
28
 *
29
 * - Protocol version
30
 * - Status code and reason phrase
31
 * - Headers
32
 * - Message body
33
 *
34
 * @see \CodeIgniter\HTTP\ResponseTest
35
 */
36
class Response extends Message implements ResponseInterface
37
{
38
    use ResponseTrait;
39

40
    /**
41
     * HTTP status codes
42
     *
43
     * @var array
44
     */
45
    protected static $statusCodes = [
46
        // 1xx: Informational
47
        100 => 'Continue',
48
        101 => 'Switching Protocols',
49
        102 => 'Processing', // http://www.iana.org/go/rfc2518
50
        103 => 'Early Hints', // http://www.ietf.org/rfc/rfc8297.txt
51
        // 2xx: Success
52
        200 => 'OK',
53
        201 => 'Created',
54
        202 => 'Accepted',
55
        203 => 'Non-Authoritative Information', // 1.1
56
        204 => 'No Content',
57
        205 => 'Reset Content',
58
        206 => 'Partial Content',
59
        207 => 'Multi-Status', // http://www.iana.org/go/rfc4918
60
        208 => 'Already Reported', // http://www.iana.org/go/rfc5842
61
        226 => 'IM Used', // 1.1; http://www.ietf.org/rfc/rfc3229.txt
62
        // 3xx: Redirection
63
        300 => 'Multiple Choices',
64
        301 => 'Moved Permanently',
65
        302 => 'Found', // Formerly 'Moved Temporarily'
66
        303 => 'See Other', // 1.1
67
        304 => 'Not Modified',
68
        305 => 'Use Proxy', // 1.1
69
        306 => 'Switch Proxy', // No longer used
70
        307 => 'Temporary Redirect', // 1.1
71
        308 => 'Permanent Redirect', // 1.1; Experimental; http://www.ietf.org/rfc/rfc7238.txt
72
        // 4xx: Client error
73
        400 => 'Bad Request',
74
        401 => 'Unauthorized',
75
        402 => 'Payment Required',
76
        403 => 'Forbidden',
77
        404 => 'Not Found',
78
        405 => 'Method Not Allowed',
79
        406 => 'Not Acceptable',
80
        407 => 'Proxy Authentication Required',
81
        408 => 'Request Timeout',
82
        409 => 'Conflict',
83
        410 => 'Gone',
84
        411 => 'Length Required',
85
        412 => 'Precondition Failed',
86
        413 => 'Content Too Large', // https://www.iana.org/assignments/http-status-codes/http-status-codes.xml
87
        414 => 'URI Too Long', // https://www.iana.org/assignments/http-status-codes/http-status-codes.xml
88
        415 => 'Unsupported Media Type',
89
        416 => 'Requested Range Not Satisfiable',
90
        417 => 'Expectation Failed',
91
        418 => "I'm a teapot", // April's Fools joke; http://www.ietf.org/rfc/rfc2324.txt
92
        // 419 (Authentication Timeout) is a non-standard status code with unknown origin
93
        421 => 'Misdirected Request', // http://www.iana.org/go/rfc7540 Section 9.1.2
94
        422 => 'Unprocessable Content', // https://www.iana.org/assignments/http-status-codes/http-status-codes.xml
95
        423 => 'Locked', // http://www.iana.org/go/rfc4918
96
        424 => 'Failed Dependency', // http://www.iana.org/go/rfc4918
97
        425 => 'Too Early', // https://datatracker.ietf.org/doc/draft-ietf-httpbis-replay/
98
        426 => 'Upgrade Required',
99
        428 => 'Precondition Required', // 1.1; http://www.ietf.org/rfc/rfc6585.txt
100
        429 => 'Too Many Requests', // 1.1; http://www.ietf.org/rfc/rfc6585.txt
101
        431 => 'Request Header Fields Too Large', // 1.1; http://www.ietf.org/rfc/rfc6585.txt
102
        451 => 'Unavailable For Legal Reasons', // http://tools.ietf.org/html/rfc7725
103
        499 => 'Client Closed Request', // http://lxr.nginx.org/source/src/http/ngx_http_request.h#0133
104
        // 5xx: Server error
105
        500 => 'Internal Server Error',
106
        501 => 'Not Implemented',
107
        502 => 'Bad Gateway',
108
        503 => 'Service Unavailable',
109
        504 => 'Gateway Timeout',
110
        505 => 'HTTP Version Not Supported',
111
        506 => 'Variant Also Negotiates', // 1.1; http://www.ietf.org/rfc/rfc2295.txt
112
        507 => 'Insufficient Storage', // http://www.iana.org/go/rfc4918
113
        508 => 'Loop Detected', // http://www.iana.org/go/rfc5842
114
        510 => 'Not Extended', // http://www.ietf.org/rfc/rfc2774.txt
115
        511 => 'Network Authentication Required', // http://www.ietf.org/rfc/rfc6585.txt
116
        599 => 'Network Connect Timeout Error', // https://httpstatuses.com/599
117
    ];
118

119
    /**
120
     * The current reason phrase for this response.
121
     * If empty string, will use the default provided for the status code.
122
     *
123
     * @var string
124
     */
125
    protected $reason = '';
126

127
    /**
128
     * The current status code for this response.
129
     * The status code is a 3-digit integer result code of the server's attempt
130
     * to understand and satisfy the request.
131
     *
132
     * @var int
133
     */
134
    protected $statusCode = 200;
135

136
    /**
137
     * If true, will not write output. Useful during testing.
138
     *
139
     * @var bool
140
     *
141
     * @internal Used for framework testing, should not be relied on otherwise
142
     */
143
    protected $pretend = false;
144

145
    /**
146
     * Constructor
147
     *
148
     * @param App $config
149
     *
150
     * @todo Recommend removing reliance on config injection
151
     *
152
     * @deprecated 4.5.0 The param $config is no longer used.
153
     */
154
    public function __construct($config) // @phpstan-ignore-line
155
    {
156
        // Default to a non-caching page.
157
        // Also ensures that a Cache-control header exists.
158
        $this->noCache();
724✔
159

160
        // We need CSP object even if not enabled to avoid calls to non existing methods
161
        $this->CSP = Services::csp();
724✔
162

163
        $this->cookieStore = new CookieStore([]);
724✔
164

165
        $cookie = config(CookieConfig::class);
724✔
166

167
        Cookie::setDefaults($cookie);
724✔
168

169
        // Default to an HTML Content-Type. Devs can override if needed.
170
        $this->setContentType('text/html');
724✔
171
    }
172

173
    /**
174
     * Turns "pretend" mode on or off to aid in testing.
175
     *
176
     * Note that this is not a part of the interface so
177
     * should not be relied on outside of internal testing.
178
     *
179
     * @return $this
180
     *
181
     * @internal For testing purposes only.
182
     * @testTag only available to test code
183
     */
184
    public function pretend(bool $pretend = true)
185
    {
186
        $this->pretend = $pretend;
99✔
187

188
        return $this;
99✔
189
    }
190

191
    /**
192
     * Gets the response status code.
193
     *
194
     * The status code is a 3-digit integer result code of the server's attempt
195
     * to understand and satisfy the request.
196
     *
197
     * @return int Status code.
198
     */
199
    public function getStatusCode(): int
200
    {
201
        if (empty($this->statusCode)) {
183✔
202
            throw HTTPException::forMissingResponseStatus();
1✔
203
        }
204

205
        return $this->statusCode;
182✔
206
    }
207

208
    /**
209
     * Gets the response response phrase associated with the status code.
210
     *
211
     * @see http://tools.ietf.org/html/rfc7231#section-6
212
     * @see http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
213
     *
214
     * @deprecated Use getReasonPhrase()
215
     *
216
     * @codeCoverageIgnore
217
     */
218
    public function getReason(): string
219
    {
UNCOV
220
        return $this->getReasonPhrase();
×
221
    }
222

223
    /**
224
     * Gets the response reason phrase associated with the status code.
225
     *
226
     * Because a reason phrase is not a required element in a response
227
     * status line, the reason phrase value MAY be null. Implementations MAY
228
     * choose to return the default RFC 7231 recommended reason phrase (or those
229
     * listed in the IANA HTTP Status Code Registry) for the response's
230
     * status code.
231
     *
232
     * @see http://tools.ietf.org/html/rfc7231#section-6
233
     * @see http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
234
     *
235
     * @return string Reason phrase; must return an empty string if none present.
236
     */
237
    public function getReasonPhrase()
238
    {
239
        if ($this->reason === '') {
76✔
240
            return ! empty($this->statusCode) ? static::$statusCodes[$this->statusCode] : '';
36✔
241
        }
242

243
        return $this->reason;
40✔
244
    }
245
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc