Skip to content

Commit e0fabdd

Browse files
committed
(fix): Resolve all PHPStan max-level errors
1 parent d9b7202 commit e0fabdd

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

src/Adapter.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,16 @@ public function route(string $resourceId): ConnectionResult
207207
try {
208208
if ($this->callback !== null) {
209209
$resolved = ($this->callback)($resourceId);
210-
$result = $resolved instanceof Resolver\Result
211-
? $resolved
212-
: new Resolver\Result(endpoint: (string) $resolved);
210+
if ($resolved instanceof Resolver\Result) {
211+
$result = $resolved;
212+
} elseif (\is_string($resolved)) {
213+
$result = new Resolver\Result(endpoint: $resolved);
214+
} else {
215+
throw new ResolverException(
216+
'Resolve callback must return Result or string',
217+
ResolverException::INTERNAL
218+
);
219+
}
213220
} elseif ($this->resolver !== null) {
214221
$result = $this->resolver->resolve($resourceId);
215222
} else {

src/Server/TCP/Swoole.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function __construct(
8888

8989
// Build Config from array or named params
9090
if (\is_array($config)) {
91+
/** @var array{host?: string, ports?: array<int, int>, workers?: int, max_connections?: int, max_coroutine?: int, socket_buffer_size?: int, buffer_output_size?: int, reactor_num?: int|null, dispatch_mode?: int, enable_reuse_port?: bool, backlog?: int, package_max_length?: int, tcp_keepidle?: int, tcp_keepinterval?: int, tcp_keepcount?: int, enable_coroutine?: bool, max_wait_time?: int, log_level?: int, log_connections?: bool, recv_buffer_size?: int, backend_connect_timeout?: float, skip_validation?: bool, read_write_split?: bool, tls?: TLS|null, adapter_factory?: \Closure|null} $config */
9192
$this->config = self::buildConfig($config, $host, $ports, $workers);
9293
} elseif ($config instanceof Config) {
9394
$this->config = $config;
@@ -138,9 +139,8 @@ public function __construct(
138139
}
139140

140141
/**
141-
* Build a Config object from an associative array of settings
142-
*
143-
* @param array<string, mixed> $settings
142+
* @param array{host?: string, ports?: array<int, int>, workers?: int, max_connections?: int, max_coroutine?: int, socket_buffer_size?: int, buffer_output_size?: int, reactor_num?: int|null, dispatch_mode?: int, enable_reuse_port?: bool, backlog?: int, package_max_length?: int, tcp_keepidle?: int, tcp_keepinterval?: int, tcp_keepcount?: int, enable_coroutine?: bool, max_wait_time?: int, log_level?: int, log_connections?: bool, recv_buffer_size?: int, backend_connect_timeout?: float, skip_validation?: bool, read_write_split?: bool, tls?: TLS|null, adapter_factory?: \Closure|null} $settings
143+
* @param array<int, int>|null $ports
144144
*/
145145
protected static function buildConfig(
146146
array $settings,
@@ -244,6 +244,7 @@ public function onWorkerStart(Server $server, int $workerId): void
244244
// Initialize TCP adapter per worker per port
245245
foreach ($this->config->ports as $port) {
246246
if ($this->config->adapterFactory !== null) {
247+
/** @var TCPAdapter $adapter */
247248
$adapter = ($this->config->adapterFactory)($port);
248249
} else {
249250
$adapter = new TCPAdapter($this->resolver, port: $port);

tests/AdapterFactoryTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public function testAdapterFactoryClosureIsInvokable(): void
3838
};
3939

4040
$config = new Config(adapterFactory: $factory);
41-
$result = ($config->adapterFactory)(5432);
41+
$callable = $config->adapterFactory;
42+
\assert($callable !== null);
43+
$result = $callable(5432);
4244
$this->assertSame('adapter-for-port-5432', $result);
4345
}
4446

@@ -51,9 +53,11 @@ public function testAdapterFactoryClosureReceivesPort(): void
5153
};
5254

5355
$config = new Config(adapterFactory: $factory);
54-
($config->adapterFactory)(5432);
55-
($config->adapterFactory)(3306);
56-
($config->adapterFactory)(27017);
56+
$callable = $config->adapterFactory;
57+
\assert($callable !== null);
58+
$callable(5432);
59+
$callable(3306);
60+
$callable(27017);
5761

5862
$this->assertSame([5432, 3306, 27017], $receivedPorts);
5963
}

tests/OnResolveCallbackTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,17 @@ public function testCallbackTakesPriorityOverResolver(): void
156156
{
157157
$resolverCalled = false;
158158

159-
$mockResolver = new class ($resolverCalled) extends MockResolver {
160-
private bool $called;
159+
$mockResolver = new class extends MockResolver {
160+
public bool $wasCalled = false;
161161

162-
public function __construct(bool &$called)
162+
public function __construct()
163163
{
164-
$this->called = &$called;
165164
parent::setEndpoint('resolver.example.com:8080');
166165
}
167166

168167
public function resolve(string $resourceId): \Utopia\Proxy\Resolver\Result
169168
{
170-
$this->called = true;
169+
$this->wasCalled = true;
171170
return parent::resolve($resourceId);
172171
}
173172
};
@@ -181,7 +180,7 @@ public function resolve(string $resourceId): \Utopia\Proxy\Resolver\Result
181180
$result = $adapter->route('test-resource');
182181

183182
$this->assertSame('callback.example.com:8080', $result->endpoint);
184-
$this->assertFalse($resolverCalled);
183+
$this->assertFalse($mockResolver->wasCalled);
185184
}
186185

187186
/**

0 commit comments

Comments
 (0)