1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Koded package. |
||
5 | * |
||
6 | * (c) Mihail Binev <[email protected]> |
||
7 | * |
||
8 | * Please view the LICENSE distributed with this source code |
||
9 | * for the full copyright and license information. |
||
10 | * |
||
11 | */ |
||
12 | |||
13 | namespace Koded\Http; |
||
14 | |||
15 | use InvalidArgumentException; |
||
16 | use JsonSerializable; |
||
17 | use Koded\Http\Interfaces\{HttpStatus, Request, Response}; |
||
18 | |||
19 | /** |
||
20 | * Class ServerResponse |
||
21 | * |
||
22 | */ |
||
23 | class ServerResponse implements Response, JsonSerializable |
||
24 | { |
||
25 | use HeaderTrait, MessageTrait, CookieTrait, JsonSerializeTrait; |
||
26 | |||
27 | private const E_CLIENT_RESPONSE_SEND = 'Cannot send the client response.'; |
||
28 | private const E_INVALID_STATUS_CODE = 'Invalid status code %s, expected range between [100-599]'; |
||
29 | |||
30 | protected $statusCode = HttpStatus::OK; |
||
31 | protected $reasonPhrase = 'OK'; |
||
32 | |||
33 | /** |
||
34 | * ServerResponse constructor. |
||
35 | * |
||
36 | * @param mixed $content [optional] |
||
37 | * @param int $statusCode [optional] |
||
38 | * @param array $headers [optional] |
||
39 | */ |
||
40 | 51 | public function __construct($content = '', int $statusCode = HttpStatus::OK, array $headers = []) |
|
41 | { |
||
42 | 51 | $this->setStatus($this, $statusCode); |
|
43 | 51 | $this->setHeaders($headers); |
|
44 | 51 | $this->stream = create_stream($content); |
|
45 | 51 | } |
|
46 | |||
47 | 33 | public function getStatusCode(): int |
|
48 | { |
||
49 | 33 | return $this->statusCode; |
|
50 | } |
||
51 | |||
52 | 6 | public function withStatus($code, $reasonPhrase = ''): Response |
|
53 | { |
||
54 | 6 | return $this->setStatus(clone $this, (int)$code, (string)$reasonPhrase); |
|
55 | } |
||
56 | |||
57 | 9 | public function getReasonPhrase(): string |
|
58 | { |
||
59 | 9 | return (string)$this->reasonPhrase; |
|
60 | } |
||
61 | |||
62 | 2 | public function getContentType(): string |
|
63 | { |
||
64 | 2 | return $this->getHeaderLine('Content-Type') ?: 'text/html'; |
|
65 | } |
||
66 | |||
67 | 4 | public function send(): string |
|
68 | { |
||
69 | 4 | $this->prepareResponse(); |
|
70 | |||
71 | 4 | if (headers_sent()) { |
|
72 | 1 | return (string)$this->stream; |
|
73 | } |
||
74 | |||
75 | // Headers |
||
76 | 3 | foreach ($this->getHeaders() as $name => $values) { |
|
77 | 2 | header($name . ':' . join(',', (array)$values), false, $this->statusCode); |
|
0 ignored issues
–
show
|
|||
78 | } |
||
79 | |||
80 | // Status header |
||
81 | 3 | header(sprintf('HTTP/%s %d %s', $this->getProtocolVersion(), $this->getStatusCode(), $this->getReasonPhrase()), |
|
82 | 3 | true, $this->statusCode |
|
83 | ); |
||
84 | |||
85 | 3 | return (string)$this->stream; |
|
86 | } |
||
87 | |||
88 | 51 | protected function setStatus(ServerResponse $instance, int $statusCode, string $reasonPhrase = ''): ServerResponse |
|
89 | { |
||
90 | 51 | if ($statusCode < 100 || $statusCode > 599) { |
|
91 | 1 | throw new InvalidArgumentException( |
|
92 | 1 | sprintf(self::E_INVALID_STATUS_CODE, $statusCode), HttpStatus::UNPROCESSABLE_ENTITY |
|
93 | ); |
||
94 | } |
||
95 | |||
96 | 51 | $instance->statusCode = (int)$statusCode; |
|
97 | 51 | $instance->reasonPhrase = $reasonPhrase ? (string)$reasonPhrase : StatusCode::CODE[$statusCode]; |
|
98 | |||
99 | 51 | return $instance; |
|
100 | } |
||
101 | |||
102 | 4 | protected function prepareResponse(): void |
|
103 | { |
||
104 | 4 | if (in_array($this->getStatusCode(), [100, 101, 102, 204, 304])) { |
|
105 | 1 | $this->stream = create_stream(null); |
|
106 | 1 | unset($this->headersMap['content-length'], $this->headers['Content-Length']); |
|
107 | 1 | unset($this->headersMap['content-type'], $this->headers['Content-Type']); |
|
108 | |||
109 | 1 | return; |
|
110 | } |
||
111 | |||
112 | 3 | if ($size = $this->stream->getSize()) { |
|
113 | 3 | $this->normalizeHeader('Content-Length', $size, true); |
|
114 | } |
||
115 | |||
116 | 3 | if (Request::HEAD === strtoupper($_SERVER['REQUEST_METHOD'] ?? '')) { |
|
117 | 2 | $this->stream = create_stream(null); |
|
118 | } |
||
119 | |||
120 | 3 | if ($this->hasHeader('Transfer-Encoding') || !$size) { |
|
121 | 1 | unset($this->headersMap['content-length'], $this->headers['Content-Length']); |
|
122 | } |
||
123 | 3 | } |
|
124 | } |
||
125 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.