Skip to content

Commit 7763987

Browse files
committed
Fix stan
1 parent 0ef30e8 commit 7763987

File tree

8 files changed

+98
-50
lines changed

8 files changed

+98
-50
lines changed

phpstan.neon

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
parameters:
2-
level: 8
2+
level: max
33
paths:
44
- src
55
- tests
6-
reportUnmatchedIgnoredErrors: false
7-
ignoreErrors:
8-
- '#Function Swoole\\Coroutine\\run not found#'
9-
- '#Instantiated class Swoole\\Http\\Client not found#'
6+
scanFiles:
7+
- vendor/swoole/ide-helper/src/swoole_library/src/core/Coroutine/functions.php
8+
scanDirectories:
9+
- vendor/swoole/ide-helper/src/swoole

src/Adapter/Swoole.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use CURLFile;
88
use Swoole\Coroutine;
99
use Swoole\Coroutine\Http\Client as CoClient;
10-
use Swoole\Http\Client as SyncClient;
1110
use Throwable;
1211
use Utopia\Fetch\Adapter;
1312
use Utopia\Fetch\Chunk;
@@ -110,7 +109,17 @@ public function __construct(
110109
*/
111110
public static function isAvailable(): bool
112111
{
113-
return class_exists(CoClient::class) || class_exists(SyncClient::class);
112+
return class_exists(CoClient::class) || class_exists('Swoole\\Http\\Client');
113+
}
114+
115+
/**
116+
* Get the sync client class name
117+
*
118+
* @return string
119+
*/
120+
private static function getSyncClientClass(): string
121+
{
122+
return 'Swoole' . '\\' . 'Http' . '\\' . 'Client';
114123
}
115124

116125
/**
@@ -129,8 +138,12 @@ private function getClient(string $host, int $port, bool $ssl): CoClient
129138
if ($this->coroutines) {
130139
$this->clients[$key] = new CoClient($host, $port, $ssl);
131140
} else {
141+
$syncClientClass = self::getSyncClientClass();
142+
if (!class_exists($syncClientClass)) {
143+
throw new Exception('Swoole sync HTTP client is not available');
144+
}
132145
/** @var CoClient $client */
133-
$client = new SyncClient($host, $port, $ssl);
146+
$client = new $syncClientClass($host, $port, $ssl);
134147
$this->clients[$key] = $client;
135148
}
136149
}
@@ -202,7 +215,7 @@ private function configureBody(CoClient $client, mixed $body, array $headers): v
202215
} else {
203216
$client->setData($body);
204217
}
205-
} else {
218+
} elseif (is_string($body)) {
206219
$client->setData($body);
207220
}
208221
}
@@ -352,7 +365,8 @@ private function executeRequest(
352365
} while (true);
353366

354367
$responseHeaders = array_change_key_case($client->headers ?? [], CASE_LOWER);
355-
$responseStatusCode = $client->getStatusCode();
368+
$statusCode = $client->getStatusCode();
369+
$responseStatusCode = is_int($statusCode) ? $statusCode : 0;
356370

357371
return new Response(
358372
statusCode: $responseStatusCode,
@@ -394,7 +408,8 @@ public function send(
394408
$response = null;
395409
$exception = null;
396410

397-
\Swoole\Coroutine\run(function () use ($url, $method, $body, $headers, $options, $chunkCallback, &$response, &$exception) {
411+
$coRun = 'Swoole\\Coroutine\\run';
412+
$coRun(function () use ($url, $method, $body, $headers, $options, $chunkCallback, &$response, &$exception) {
398413
try {
399414
$response = $this->executeRequest($url, $method, $body, $headers, $options, $chunkCallback);
400415
} catch (Throwable $e) {

src/Client.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Client
3939

4040
/** @var array<int> $retryStatusCodes */
4141
private array $retryStatusCodes = [500, 503];
42-
private mixed $jsonEncodeFlags;
42+
private ?int $jsonEncodeFlags = null;
4343
private Adapter $adapter;
4444

4545
/**
@@ -314,7 +314,7 @@ public function fetch(
314314
'userAgent' => $this->userAgent
315315
];
316316

317-
$sendRequest = function () use ($url, $method, $body, $options, $chunks) {
317+
$sendRequest = function () use ($url, $method, $body, $options, $chunks): Response {
318318
return $this->adapter->send(
319319
url: $url,
320320
method: $method,
@@ -326,6 +326,7 @@ public function fetch(
326326
};
327327

328328
if ($this->maxRetries > 0) {
329+
/** @var Response $response */
329330
$response = $this->withRetries($sendRequest);
330331
} else {
331332
$response = $sendRequest();

src/Response.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,19 @@ public function getStatusCode(): int
8080
*/
8181
public function text(): string
8282
{
83-
return \strval($this->body);
83+
if ($this->body === null) {
84+
return '';
85+
}
86+
if (is_string($this->body)) {
87+
return $this->body;
88+
}
89+
if (is_scalar($this->body)) {
90+
return \strval($this->body);
91+
}
92+
if (is_object($this->body) && method_exists($this->body, '__toString')) {
93+
return (string) $this->body;
94+
}
95+
return '';
8496
}
8597

8698
/**
@@ -90,7 +102,8 @@ public function text(): string
90102
*/
91103
public function json(): mixed
92104
{
93-
$data = \json_decode($this->body, true);
105+
$bodyString = is_string($this->body) ? $this->body : '';
106+
$data = \json_decode($bodyString, true);
94107

95108
// Check for JSON errors using json_last_error()
96109
if (\json_last_error() !== JSON_ERROR_NONE) {
@@ -106,9 +119,10 @@ public function json(): mixed
106119
*/
107120
public function blob(): string
108121
{
122+
$bodyString = is_string($this->body) ? $this->body : '';
109123
$bin = [];
110-
for ($i = 0, $j = strlen($this->body); $i < $j; $i++) {
111-
$bin[] = decbin(ord($this->body[$i]));
124+
for ($i = 0, $j = strlen($bodyString); $i < $j; $i++) {
125+
$bin[] = decbin(ord($bodyString[$i]));
112126
}
113127
return implode(" ", $bin);
114128
}

tests/Adapter/CurlTest.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected function setUp(): void
2222
public function testGetRequest(): void
2323
{
2424
$response = $this->adapter->send(
25-
url: 'localhost:8000',
25+
url: '127.0.0.1:8000',
2626
method: 'GET',
2727
body: null,
2828
headers: [],
@@ -38,6 +38,7 @@ public function testGetRequest(): void
3838
$this->assertInstanceOf(Response::class, $response);
3939
$this->assertSame(200, $response->getStatusCode());
4040
$data = $response->json();
41+
$this->assertIsArray($data);
4142
$this->assertSame('GET', $data['method']);
4243
}
4344

@@ -48,7 +49,7 @@ public function testPostWithJsonBody(): void
4849
{
4950
$body = json_encode(['name' => 'John Doe', 'age' => 30]);
5051
$response = $this->adapter->send(
51-
url: 'localhost:8000',
52+
url: '127.0.0.1:8000',
5253
method: 'POST',
5354
body: $body,
5455
headers: ['content-type' => 'application/json'],
@@ -64,6 +65,7 @@ public function testPostWithJsonBody(): void
6465
$this->assertInstanceOf(Response::class, $response);
6566
$this->assertSame(200, $response->getStatusCode());
6667
$data = $response->json();
68+
$this->assertIsArray($data);
6769
$this->assertSame('POST', $data['method']);
6870
$this->assertSame($body, $data['body']);
6971
}
@@ -74,7 +76,7 @@ public function testPostWithJsonBody(): void
7476
public function testCustomTimeout(): void
7577
{
7678
$response = $this->adapter->send(
77-
url: 'localhost:8000',
79+
url: '127.0.0.1:8000',
7880
method: 'GET',
7981
body: null,
8082
headers: [],
@@ -97,7 +99,7 @@ public function testCustomTimeout(): void
9799
public function testRedirectHandling(): void
98100
{
99101
$response = $this->adapter->send(
100-
url: 'localhost:8000/redirect',
102+
url: '127.0.0.1:8000/redirect',
101103
method: 'GET',
102104
body: null,
103105
headers: [],
@@ -113,6 +115,7 @@ public function testRedirectHandling(): void
113115
$this->assertInstanceOf(Response::class, $response);
114116
$this->assertSame(200, $response->getStatusCode());
115117
$data = $response->json();
118+
$this->assertIsArray($data);
116119
$this->assertSame('redirectedPage', $data['page']);
117120
}
118121

@@ -122,7 +125,7 @@ public function testRedirectHandling(): void
122125
public function testRedirectDisabled(): void
123126
{
124127
$response = $this->adapter->send(
125-
url: 'localhost:8000/redirect',
128+
url: '127.0.0.1:8000/redirect',
126129
method: 'GET',
127130
body: null,
128131
headers: [],
@@ -146,7 +149,7 @@ public function testChunkCallback(): void
146149
{
147150
$chunks = [];
148151
$response = $this->adapter->send(
149-
url: 'localhost:8000/chunked',
152+
url: '127.0.0.1:8000/chunked',
150153
method: 'GET',
151154
body: null,
152155
headers: [],
@@ -181,7 +184,7 @@ public function testFormDataBody(): void
181184
{
182185
$body = ['name' => 'John Doe', 'age' => '30'];
183186
$response = $this->adapter->send(
184-
url: 'localhost:8000',
187+
url: '127.0.0.1:8000',
185188
method: 'POST',
186189
body: $body,
187190
headers: ['content-type' => 'application/x-www-form-urlencoded'],
@@ -204,7 +207,7 @@ public function testFormDataBody(): void
204207
public function testResponseHeaders(): void
205208
{
206209
$response = $this->adapter->send(
207-
url: 'localhost:8000',
210+
url: '127.0.0.1:8000',
208211
method: 'GET',
209212
body: null,
210213
headers: [],
@@ -254,7 +257,7 @@ public function testFileUpload(): void
254257
];
255258

256259
$response = $this->adapter->send(
257-
url: 'localhost:8000',
260+
url: '127.0.0.1:8000',
258261
method: 'POST',
259262
body: $body,
260263
headers: ['content-type' => 'multipart/form-data'],
@@ -270,7 +273,10 @@ public function testFileUpload(): void
270273
$this->assertInstanceOf(Response::class, $response);
271274
$this->assertSame(200, $response->getStatusCode());
272275
$data = $response->json();
276+
$this->assertIsArray($data);
277+
$this->assertIsString($data['files']);
273278
$files = json_decode($data['files'], true);
279+
$this->assertIsArray($files);
274280
$this->assertSame('logo.png', $files['file']['name']);
275281
}
276282
}

tests/Adapter/SwooleTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function testGetRequest(): void
4444
$this->assertInstanceOf(Response::class, $response);
4545
$this->assertSame(200, $response->getStatusCode());
4646
$data = $response->json();
47+
$this->assertIsArray($data);
4748
$this->assertSame('GET', $data['method']);
4849
}
4950

@@ -74,6 +75,7 @@ public function testPostWithJsonBody(): void
7475
$this->assertInstanceOf(Response::class, $response);
7576
$this->assertSame(200, $response->getStatusCode());
7677
$data = $response->json();
78+
$this->assertIsArray($data);
7779
$this->assertSame('POST', $data['method']);
7880
$this->assertSame($body, $data['body']);
7981
}
@@ -131,6 +133,7 @@ public function testRedirectHandling(): void
131133
$this->assertInstanceOf(Response::class, $response);
132134
$this->assertSame(200, $response->getStatusCode());
133135
$data = $response->json();
136+
$this->assertIsArray($data);
134137
$this->assertSame('redirectedPage', $data['page']);
135138
}
136139

0 commit comments

Comments
 (0)