Skip to content

Commit 5245bfd

Browse files
committed
Apply code style
1 parent e62ed65 commit 5245bfd

File tree

14 files changed

+245
-268
lines changed

14 files changed

+245
-268
lines changed

src/GlobalState.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44

55
namespace Spiral\RoadRunner\Http;
66

7-
use function time;
8-
use function microtime;
9-
use function strtoupper;
10-
use function str_replace;
11-
use function implode;
12-
137
final class GlobalState
148
{
159
/**
@@ -35,22 +29,22 @@ public static function enrichServerVars(Request $request): array
3529
$server = self::$cachedServer;
3630

3731
$server['REQUEST_URI'] = $request->uri;
38-
$server['REQUEST_TIME'] = time();
39-
$server['REQUEST_TIME_FLOAT'] = microtime(true);
32+
$server['REQUEST_TIME'] = \time();
33+
$server['REQUEST_TIME_FLOAT'] = \microtime(true);
4034
$server['REMOTE_ADDR'] = $request->getRemoteAddr();
4135
$server['REQUEST_METHOD'] = $request->method;
4236
$server['HTTP_USER_AGENT'] = '';
4337

4438
foreach ($request->headers as $key => $value) {
45-
$key = strtoupper(str_replace('-', '_', $key));
39+
$key = \strtoupper(\str_replace('-', '_', $key));
4640

4741
if ($key == 'CONTENT_TYPE' || $key == 'CONTENT_LENGTH') {
48-
$server[$key] = implode(', ', $value);
42+
$server[$key] = \implode(', ', $value);
4943

5044
continue;
5145
}
5246

53-
$server['HTTP_' . $key] = implode(', ', $value);
47+
$server['HTTP_' . $key] = \implode(', ', $value);
5448
}
5549

5650
return $server;

src/HttpWorker.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ class HttpWorker implements HttpWorkerInterface
4242

4343
public function __construct(
4444
private readonly WorkerInterface $worker,
45-
) {
46-
}
45+
) {}
4746

4847
public function getWorker(): WorkerInterface
4948
{
@@ -83,13 +82,13 @@ public function waitRequest(): ?Request
8382
* @param array<array-key, array<array-key, string>> $headers
8483
* @throws \JsonException
8584
*/
86-
public function respond(int $status, string|Generator $body = '', array $headers = [], bool $endOfStream = true): void
85+
public function respond(int $status, string|\Generator $body = '', array $headers = [], bool $endOfStream = true): void
8786
{
8887
if ($status < 200 && $status >= 100 && $body !== '') {
8988
throw new \InvalidArgumentException('Unable to send a body with informational status code.');
9089
}
9190

92-
if ($body instanceof Generator) {
91+
if ($body instanceof \Generator) {
9392
$this->respondStream($status, $body, $headers, $endOfStream);
9493
return;
9594
}
@@ -101,7 +100,7 @@ public function respond(int $status, string|Generator $body = '', array $headers
101100
/**
102101
* @param array<array-key, array<array-key, string>> $headers
103102
*/
104-
private function respondStream(int $status, Generator $body, array $headers = [], bool $endOfStream = true): void
103+
private function respondStream(int $status, \Generator $body, array $headers = [], bool $endOfStream = true): void
105104
{
106105
$worker = $this->worker instanceof StreamWorkerInterface
107106
? $this->worker->withStreamMode()
@@ -110,20 +109,20 @@ private function respondStream(int $status, Generator $body, array $headers = []
110109
do {
111110
if (!$body->valid()) {
112111
// End of generator
113-
$content = (string)$body->getReturn();
112+
$content = (string) $body->getReturn();
114113
if ($endOfStream === false && $content === '') {
115114
// We don't need to send an empty frame if the stream is not ended
116115
return;
117116
}
118117
/** @psalm-suppress TooManyArguments */
119118
$worker->respond(
120119
$this->createRespondPayload($status, $content, $headers, $endOfStream),
121-
static::$codec
120+
static::$codec,
122121
);
123122
break;
124123
}
125124

126-
$content = (string)$body->current();
125+
$content = (string) $body->current();
127126
if ($worker->getPayload(StreamStop::class) !== null) {
128127
$body->throw(new StreamStoppedException());
129128

@@ -160,12 +159,12 @@ private function arrayToRequest(string $body, array $context): Request
160159
protocol: $context['protocol'],
161160
method: $context['method'],
162161
uri: $context['uri'],
163-
headers: $this->filterHeaders((array)($context['headers'] ?? [])),
164-
cookies: (array)($context['cookies'] ?? []),
165-
uploads: (array)($context['uploads'] ?? []),
162+
headers: $this->filterHeaders((array) ($context['headers'] ?? [])),
163+
cookies: (array) ($context['cookies'] ?? []),
164+
uploads: (array) ($context['uploads'] ?? []),
166165
attributes: [
167166
Request::PARSED_BODY_ATTRIBUTE_NAME => $context['parsed'],
168-
] + (array)($context['attributes'] ?? []),
167+
] + (array) ($context['attributes'] ?? []),
169168
query: $query,
170169
body: $body,
171170
parsed: $context['parsed'],
@@ -253,7 +252,7 @@ private function arrayToHeaderValue(array $headers = []): array
253252
*/
254253
foreach ($headers as $key => $value) {
255254
/** @psalm-suppress DocblockTypeContradiction */
256-
$value = \array_filter(\is_array($value) ? $value : [$value], static fn (mixed $v): bool => \is_string($v));
255+
$value = \array_filter(\is_array($value) ? $value : [$value], static fn(mixed $v): bool => \is_string($v));
257256
if ($value !== []) {
258257
$result[$key] = new HeaderValue(['value' => $value]);
259258
}
@@ -270,7 +269,7 @@ private function createRespondPayload(int $status, string $body, array $headers
270269
$head = static::$codec === Frame::CODEC_PROTO
271270
? (new Response(['status' => $status, 'headers' => $this->arrayToHeaderValue($headers)]))
272271
->serializeToString()
273-
: \json_encode(['status' => $status, 'headers' => $headers ?: (object)[]], \JSON_THROW_ON_ERROR);
272+
: \json_encode(['status' => $status, 'headers' => $headers ?: (object) []], \JSON_THROW_ON_ERROR);
274273

275274
return new Payload(body: $body, header: $head, eos: $eos);
276275
}

src/HttpWorkerInterface.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Generator;
88
use Spiral\RoadRunner\WorkerAwareInterface;
9-
use Stringable;
109

1110
/**
1211
* @psalm-import-type HeadersList from Request
@@ -22,13 +21,13 @@ public function waitRequest(): ?Request;
2221
* Send response to the application server.
2322
*
2423
* @param int $status Http status code
25-
* @param Generator<mixed, scalar|Stringable, mixed, Stringable|scalar|null>|string $body Body of response.
24+
* @param \Generator<mixed, scalar|\Stringable, mixed, \Stringable|scalar|null>|string $body Body of response.
2625
* If the body is a generator, then each yielded value will be sent as a separated stream chunk.
2726
* Returned value will be sent as a last stream package.
2827
* Note: Stream response is supported by RoadRunner since version 2023.3
2928
* @param HeadersList|array<array-key, array<array-key, string>> $headers $headers An associative array of the
3029
* message's headers. Each key MUST be a header name, and each value MUST be an array of strings for
3130
* that header.
3231
*/
33-
public function respond(int $status, string|Generator $body, array $headers = []): void;
32+
public function respond(int $status, string|\Generator $body, array $headers = []): void;
3433
}

src/PSR7Worker.php

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Spiral\RoadRunner\Http;
66

7-
use Generator;
87
use Psr\Http\Message\ResponseInterface;
98
use Psr\Http\Message\ServerRequestFactoryInterface;
109
use Psr\Http\Message\ServerRequestInterface;
@@ -13,7 +12,6 @@
1312
use Psr\Http\Message\UploadedFileFactoryInterface;
1413
use Psr\Http\Message\UploadedFileInterface;
1514
use Spiral\RoadRunner\WorkerInterface;
16-
use Stringable;
1715

1816
/**
1917
* Manages PSR-7 request and response.
@@ -32,7 +30,6 @@ class PSR7Worker implements PSR7WorkerInterface
3230

3331
private readonly HttpWorker $httpWorker;
3432

35-
3633
/**
3734
* @var string[] Valid values for HTTP protocol version
3835
*/
@@ -91,38 +88,11 @@ public function respond(ResponseInterface $response): void
9188
$response->getStatusCode(),
9289
$this->chunkSize > 0
9390
? $this->streamToGenerator($response->getBody())
94-
: (string)$response->getBody(),
95-
$response->getHeaders()
91+
: (string) $response->getBody(),
92+
$response->getHeaders(),
9693
);
9794
}
9895

99-
/**
100-
* @return Generator<mixed, scalar|Stringable, mixed, Stringable|scalar|null> Compatible
101-
* with {@see HttpWorker::respondStream}.
102-
*/
103-
private function streamToGenerator(StreamInterface $stream): Generator
104-
{
105-
$stream->rewind();
106-
$size = $stream->getSize();
107-
if ($size !== null && $size < $this->chunkSize) {
108-
return (string)$stream;
109-
}
110-
$sum = 0;
111-
while (!$stream->eof()) {
112-
if ($size === null) {
113-
$chunk = $stream->read($this->chunkSize);
114-
} else {
115-
$left = $size - $sum;
116-
$chunk = $stream->read(\min($this->chunkSize, $left));
117-
if ($left <= $this->chunkSize && \strlen($chunk) === $left) {
118-
return $chunk;
119-
}
120-
}
121-
$sum += \strlen($chunk);
122-
yield $chunk;
123-
}
124-
}
125-
12696
/**
12797
* Returns altered copy of _SERVER variable. Sets ip-address,
12898
* request-time and other values.
@@ -158,7 +128,7 @@ protected function mapRequest(Request $httpRequest, array $server): ServerReques
158128
$request = $this->requestFactory->createServerRequest(
159129
$httpRequest->method,
160130
$httpRequest->uri,
161-
$server
131+
$server,
162132
);
163133

164134
$request = $request
@@ -205,7 +175,7 @@ protected function wrapUploads(array $files): array
205175
continue;
206176
}
207177

208-
if (\UPLOAD_ERR_OK === $file['error']) {
178+
if ($file['error'] === \UPLOAD_ERR_OK) {
209179
$stream = $this->streamFactory->createStreamFromFile($file['tmpName']);
210180
} else {
211181
$stream = $this->streamFactory->createStream();
@@ -216,7 +186,7 @@ protected function wrapUploads(array $files): array
216186
$file['size'],
217187
$file['error'],
218188
$file['name'],
219-
$file['mime']
189+
$file['mime'],
220190
);
221191
}
222192

@@ -241,4 +211,31 @@ private static function fetchProtocolVersion(string $version): string
241211

242212
return $v;
243213
}
214+
215+
/**
216+
* @return \Generator<mixed, scalar|\Stringable, mixed, \Stringable|scalar|null> Compatible
217+
* with {@see HttpWorker::respondStream}.
218+
*/
219+
private function streamToGenerator(StreamInterface $stream): \Generator
220+
{
221+
$stream->rewind();
222+
$size = $stream->getSize();
223+
if ($size !== null && $size < $this->chunkSize) {
224+
return (string) $stream;
225+
}
226+
$sum = 0;
227+
while (!$stream->eof()) {
228+
if ($size === null) {
229+
$chunk = $stream->read($this->chunkSize);
230+
} else {
231+
$left = $size - $sum;
232+
$chunk = $stream->read(\min($this->chunkSize, $left));
233+
if ($left <= $this->chunkSize && \strlen($chunk) === $left) {
234+
return $chunk;
235+
}
236+
}
237+
$sum += \strlen($chunk);
238+
yield $chunk;
239+
}
240+
}
244241
}

src/Request.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,11 @@ public function __construct(
4949
public readonly array $query = [],
5050
public readonly string $body = '',
5151
public readonly bool $parsed = false,
52-
) {
53-
}
52+
) {}
5453

5554
public function getRemoteAddr(): string
5655
{
57-
return (string)($this->attributes['ipAddress'] ?? $this->remoteAddr);
56+
return (string) ($this->attributes['ipAddress'] ?? $this->remoteAddr);
5857
}
5958

6059
/**
@@ -63,7 +62,7 @@ public function getRemoteAddr(): string
6362
public function getParsedBody(): ?array
6463
{
6564
if ($this->parsed) {
66-
return (array)\json_decode($this->body, true, 512, \JSON_THROW_ON_ERROR);
65+
return (array) \json_decode($this->body, true, 512, \JSON_THROW_ON_ERROR);
6766
}
6867

6968
return null;

tests/Feature/StreamResponseTest.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Spiral\RoadRunner\Tests\Http\Feature;
66

7-
use Exception;
87
use PHPUnit\Framework\TestCase;
98
use Spiral\Goridge\SocketRelay;
109
use Spiral\RoadRunner\Http\Exception\StreamStoppedException;
@@ -22,20 +21,6 @@ class StreamResponseTest extends TestCase
2221
private Worker $worker;
2322
private $serverAddress = 'tcp://127.0.0.1:6002';
2423

25-
protected function setUp(): void
26-
{
27-
parent::setUp();
28-
ServerRunner::start();
29-
ServerRunner::getBuffer();
30-
}
31-
32-
protected function tearDown(): void
33-
{
34-
unset($this->relay, $this->worker);
35-
ServerRunner::stop();
36-
parent::tearDown();
37-
}
38-
3924
/**
4025
* Regular case
4126
*/
@@ -123,7 +108,7 @@ public function testExceptionInGenerator(): void
123108
(function () {
124109
yield 'Hel';
125110
yield 'lo,';
126-
throw new Exception('test');
111+
throw new \Exception('test');
127112
})(),
128113
);
129114

@@ -163,6 +148,20 @@ public function testStopStreamAfterStreamEnd(): void
163148
$this->assertFalse($this->getWorker()->hasPayload());
164149
}
165150

151+
protected function setUp(): void
152+
{
153+
parent::setUp();
154+
ServerRunner::start();
155+
ServerRunner::getBuffer();
156+
}
157+
158+
protected function tearDown(): void
159+
{
160+
unset($this->relay, $this->worker);
161+
ServerRunner::stop();
162+
parent::tearDown();
163+
}
164+
166165
private function getRelay(): SocketRelay
167166
{
168167
return $this->relay ??= SocketRelay::create($this->serverAddress);

0 commit comments

Comments
 (0)