3535 * }
3636 *
3737 * @see Request
38+ *
39+ * @api
3840 */
3941class HttpWorker implements HttpWorkerInterface
4042{
4143 private static ?int $ codec = null ;
4244
4345 public function __construct (
4446 private readonly WorkerInterface $ worker ,
45- ) {
46- }
47+ ) {}
4748
4849 public function getWorker (): WorkerInterface
4950 {
@@ -83,13 +84,13 @@ public function waitRequest(): ?Request
8384 * @param array<array-key, array<array-key, string>> $headers
8485 * @throws \JsonException
8586 */
86- public function respond (int $ status , string |Generator $ body = '' , array $ headers = [], bool $ endOfStream = true ): void
87+ public function respond (int $ status , string |\ Generator $ body = '' , array $ headers = [], bool $ endOfStream = true ): void
8788 {
8889 if ($ status < 200 && $ status >= 100 && $ body !== '' ) {
8990 throw new \InvalidArgumentException ('Unable to send a body with informational status code. ' );
9091 }
9192
92- if ($ body instanceof Generator) {
93+ if ($ body instanceof \ Generator) {
9394 $ this ->respondStream ($ status , $ body , $ headers , $ endOfStream );
9495 return ;
9596 }
@@ -101,7 +102,7 @@ public function respond(int $status, string|Generator $body = '', array $headers
101102 /**
102103 * @param array<array-key, array<array-key, string>> $headers
103104 */
104- private function respondStream (int $ status , Generator $ body , array $ headers = [], bool $ endOfStream = true ): void
105+ private function respondStream (int $ status , \ Generator $ body , array $ headers = [], bool $ endOfStream = true ): void
105106 {
106107 $ worker = $ this ->worker instanceof StreamWorkerInterface
107108 ? $ this ->worker ->withStreamMode ()
@@ -110,20 +111,20 @@ private function respondStream(int $status, Generator $body, array $headers = []
110111 do {
111112 if (!$ body ->valid ()) {
112113 // End of generator
113- $ content = (string )$ body ->getReturn ();
114+ $ content = (string ) $ body ->getReturn ();
114115 if ($ endOfStream === false && $ content === '' ) {
115116 // We don't need to send an empty frame if the stream is not ended
116117 return ;
117118 }
118119 /** @psalm-suppress TooManyArguments */
119120 $ worker ->respond (
120121 $ this ->createRespondPayload ($ status , $ content , $ headers , $ endOfStream ),
121- static ::$ codec
122+ static ::$ codec,
122123 );
123124 break ;
124125 }
125126
126- $ content = (string )$ body ->current ();
127+ $ content = (string ) $ body ->current ();
127128 if ($ worker ->getPayload (StreamStop::class) !== null ) {
128129 $ body ->throw (new StreamStoppedException ());
129130
@@ -160,12 +161,12 @@ private function arrayToRequest(string $body, array $context): Request
160161 protocol: $ context ['protocol ' ],
161162 method: $ context ['method ' ],
162163 uri: $ context ['uri ' ],
163- headers: $ this ->filterHeaders ((array )($ context ['headers ' ] ?? [])),
164- cookies: (array )($ context ['cookies ' ] ?? []),
165- uploads: (array )($ context ['uploads ' ] ?? []),
164+ headers: $ this ->filterHeaders ((array ) ($ context ['headers ' ] ?? [])),
165+ cookies: (array ) ($ context ['cookies ' ] ?? []),
166+ uploads: (array ) ($ context ['uploads ' ] ?? []),
166167 attributes: [
167168 Request::PARSED_BODY_ATTRIBUTE_NAME => $ context ['parsed ' ],
168- ] + (array )($ context ['attributes ' ] ?? []),
169+ ] + (array ) ($ context ['attributes ' ] ?? []),
169170 query: $ query ,
170171 body: $ body ,
171172 parsed: $ context ['parsed ' ],
@@ -253,7 +254,7 @@ private function arrayToHeaderValue(array $headers = []): array
253254 */
254255 foreach ($ headers as $ key => $ value ) {
255256 /** @psalm-suppress DocblockTypeContradiction */
256- $ value = \array_filter (\is_array ($ value ) ? $ value : [$ value ], static fn (mixed $ v ): bool => \is_string ($ v ));
257+ $ value = \array_filter (\is_array ($ value ) ? $ value : [$ value ], static fn (mixed $ v ): bool => \is_string ($ v ));
257258 if ($ value !== []) {
258259 $ result [$ key ] = new HeaderValue (['value ' => $ value ]);
259260 }
@@ -270,7 +271,7 @@ private function createRespondPayload(int $status, string $body, array $headers
270271 $ head = static ::$ codec === Frame::CODEC_PROTO
271272 ? (new Response (['status ' => $ status , 'headers ' => $ this ->arrayToHeaderValue ($ headers )]))
272273 ->serializeToString ()
273- : \json_encode (['status ' => $ status , 'headers ' => $ headers ?: (object )[]], \JSON_THROW_ON_ERROR );
274+ : \json_encode (['status ' => $ status , 'headers ' => $ headers ?: (object ) []], \JSON_THROW_ON_ERROR );
274275
275276 return new Payload (body: $ body , header: $ head , eos: $ eos );
276277 }
0 commit comments