Skip to content

Commit c13ca9e

Browse files
abnegateclaude
andcommitted
fix: use correct query package branch and resolve PHPStan errors
- Switch utopia-php/query from dev-main to dev-feat-builder (has Parser/Type classes) - Fix ordered_imports lint issues in Swoole.php and EdgeIntegrationTest.php - Add PHPDoc type annotations to satisfy PHPStan level=max - Fix unpack() false check, null-safe fclose, and remove unused property Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5d9b048 commit c13ca9e

File tree

7 files changed

+25
-7
lines changed

7 files changed

+25
-7
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"php": ">=8.4",
1414
"ext-swoole": ">=6.0",
1515
"ext-redis": "*",
16-
"utopia-php/query": "dev-main"
16+
"utopia-php/query": "dev-feat-builder"
1717
},
1818
"require-dev": {
1919
"phpunit/phpunit": "12.*",

src/Adapter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ public function recordBytes(
117117
$this->byteCounters[$resourceId]['outbound'] += $outbound;
118118
}
119119

120+
/**
121+
* @param array<string, mixed> $metadata Activity metadata
122+
*/
120123
public function track(string $resourceId, array $metadata = []): void
121124
{
122125
$now = time();

src/Adapter/TCP.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,12 @@ protected function parseMongoDatabaseId(string $data): string
382382
throw new \Exception('Invalid MongoDB database name');
383383
}
384384

385-
$strLen = \unpack('V', \substr($data, $offset, 4))[1];
385+
$unpacked = \unpack('V', \substr($data, $offset, 4));
386+
if ($unpacked === false) {
387+
throw new \Exception('Invalid MongoDB database name');
388+
}
389+
/** @var int $strLen */
390+
$strLen = $unpacked[1];
386391
$offset += 4;
387392

388393
if ($offset + $strLen > \strlen($data)) {

src/Server/TCP/Swoole.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use Swoole\Coroutine\Client;
77
use Swoole\Server;
88
use Utopia\Proxy\Adapter\TCP as TCPAdapter;
9-
use Utopia\Query\Type as QueryType;
109
use Utopia\Proxy\Resolver;
1110
use Utopia\Proxy\Resolver\ReadWriteResolver;
11+
use Utopia\Query\Type as QueryType;
1212

1313
/**
1414
* High-performance TCP proxy server (Swoole Implementation)
@@ -352,6 +352,7 @@ public function onReceive(Server $server, int $fd, int $reactorId, string $data)
352352
protected function startForwarding(Server $server, int $clientFd, Client $backendClient): void
353353
{
354354
$bufferSize = $this->config->recvBufferSize;
355+
/** @var \Swoole\Coroutine\Socket $backendSocket */
355356
$backendSocket = $backendClient->exportSocket();
356357

357358
$databaseId = $this->clientDatabaseIds[$clientFd] ?? null;
@@ -360,6 +361,7 @@ protected function startForwarding(Server $server, int $clientFd, Client $backen
360361

361362
Coroutine::create(function () use ($server, $clientFd, $backendSocket, $bufferSize, $databaseId, $adapter) {
362363
while ($server->exist($clientFd)) {
364+
/** @var string|false $data */
363365
$data = $backendSocket->recv($bufferSize);
364366
if ($data === false || $data === '') {
365367
break;

src/Server/TCP/SwooleCoroutine.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public function onWorkerStart(int $workerId = 0): void
136136

137137
protected function handleConnection(Connection $connection, int $port): void
138138
{
139+
/** @var \Swoole\Coroutine\Socket $clientSocket */
139140
$clientSocket = $connection->exportSocket();
140141
$clientId = spl_object_id($connection);
141142
$adapter = $this->adapters[$port];
@@ -146,6 +147,7 @@ protected function handleConnection(Connection $connection, int $port): void
146147
}
147148

148149
// Wait for first packet to establish backend connection
150+
/** @var string|false $data */
149151
$data = $clientSocket->recv($bufferSize);
150152
if ($data === false || $data === '') {
151153
$clientSocket->close();
@@ -163,6 +165,7 @@ protected function handleConnection(Connection $connection, int $port): void
163165

164166
// The TLS handshake is handled by Swoole at the transport layer.
165167
// Read the real startup message that follows.
168+
/** @var string|false $data */
166169
$data = $clientSocket->recv($bufferSize);
167170
if ($data === false || $data === '') {
168171
$clientSocket->close();
@@ -174,6 +177,7 @@ protected function handleConnection(Connection $connection, int $port): void
174177
try {
175178
$databaseId = $adapter->parseDatabaseId($data, $clientId);
176179
$backendClient = $adapter->getBackendConnection($databaseId, $clientId);
180+
/** @var \Swoole\Coroutine\Socket $backendSocket */
177181
$backendSocket = $backendClient->exportSocket();
178182

179183
// Notify connect
@@ -182,6 +186,7 @@ protected function handleConnection(Connection $connection, int $port): void
182186
// Start backend -> client forwarding in separate coroutine
183187
Coroutine::create(function () use ($clientSocket, $backendSocket, $bufferSize, $adapter, $databaseId): void {
184188
while (true) {
189+
/** @var string|false $data */
185190
$data = $backendSocket->recv($bufferSize);
186191
if ($data === false || $data === '') {
187192
break;
@@ -206,6 +211,7 @@ protected function handleConnection(Connection $connection, int $port): void
206211

207212
// Client -> backend forwarding in current coroutine
208213
while (true) {
214+
/** @var string|false $data */
209215
$data = $clientSocket->recv($bufferSize);
210216
if ($data === false || $data === '') {
211217
break;

tests/Integration/EdgeIntegrationTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
use Utopia\Proxy\Adapter\TCP as TCPAdapter;
77
use Utopia\Proxy\ConnectionResult;
88
use Utopia\Proxy\Protocol;
9-
use Utopia\Query\Type as QueryType;
109
use Utopia\Proxy\Resolver;
1110
use Utopia\Proxy\Resolver\Exception as ResolverException;
1211
use Utopia\Proxy\Resolver\ReadWriteResolver;
1312
use Utopia\Proxy\Resolver\Result;
13+
use Utopia\Query\Type as QueryType;
1414

1515
/**
1616
* Integration test for the protocol-proxy's ability to resolve database
@@ -644,6 +644,7 @@ public function testStatsAggregateAcrossOperations(): void
644644
$this->assertGreaterThan(0.0, $stats['cacheHitRate']);
645645
$this->assertSame(0, $stats['routingErrors']);
646646

647+
/** @var array<string, mixed> $resolverStats */
647648
$resolverStats = $stats['resolver'];
648649
$this->assertSame(1, $resolverStats['connects']);
649650
$this->assertSame(1, $resolverStats['disconnects']);

tests/Performance/PerformanceTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ final class PerformanceTest extends TestCase
3838
{
3939
private string $host;
4040
private int $port;
41-
private int $mysqlPort;
4241
private int $iterations;
4342
private int $warmupIterations;
4443
private string $databaseId;
@@ -98,7 +97,6 @@ protected function setUp(): void
9897

9998
$this->host = getenv('PERF_PROXY_HOST') ?: '127.0.0.1';
10099
$this->port = (int) (getenv('PERF_PROXY_PORT') ?: 5432);
101-
$this->mysqlPort = (int) (getenv('PERF_PROXY_MYSQL_PORT') ?: 3306);
102100
$this->iterations = (int) (getenv('PERF_ITERATIONS') ?: 1000);
103101
$this->warmupIterations = (int) (getenv('PERF_WARMUP_ITERATIONS') ?: 100);
104102
$this->databaseId = getenv('PERF_DATABASE_ID') ?: 'test-db';
@@ -484,7 +482,10 @@ public function testConnectionPoolExhaustion(): void
484482
// Verify we can still connect after closing some connections
485483
$closedCount = min(100, count($sockets));
486484
for ($i = 0; $i < $closedCount; $i++) {
487-
fclose(array_pop($sockets));
485+
$sock = array_pop($sockets);
486+
if ($sock !== null) {
487+
fclose($sock);
488+
}
488489
}
489490

490491
// Small delay for the proxy to process disconnections

0 commit comments

Comments
 (0)