Skip to content

Commit 28f1001

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

4 files changed

Lines changed: 28 additions & 92 deletions

File tree

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: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -64,47 +64,12 @@ class Swoole
6464

6565
protected ?Resolver $resolver;
6666

67-
/**
68-
* @param Resolver|string|null $resolver Resolver instance, or host string for named-param style
69-
* @param Config|array<string, mixed>|null $config Config object or array of settings
70-
* @param string|null $host Host address (named-param style)
71-
* @param array<int, int>|null $ports Port list (named-param style)
72-
* @param int|null $workers Worker count (named-param style)
73-
*/
7467
public function __construct(
75-
Resolver|string|null $resolver = null,
76-
Config|array|null $config = null,
77-
?string $host = null,
78-
?array $ports = null,
79-
?int $workers = null,
68+
?Resolver $resolver = null,
69+
?Config $config = null,
8070
) {
81-
// Detect named-param style: first arg is a string host, not a Resolver
82-
if (\is_string($resolver)) {
83-
$host = $resolver;
84-
$this->resolver = null;
85-
} else {
86-
$this->resolver = $resolver;
87-
}
88-
89-
// Build Config from array or named params
90-
if (\is_array($config)) {
91-
$this->config = self::buildConfig($config, $host, $ports, $workers);
92-
} elseif ($config instanceof Config) {
93-
$this->config = $config;
94-
} else {
95-
// Build from named params with defaults
96-
$args = [];
97-
if ($host !== null) {
98-
$args['host'] = $host;
99-
}
100-
if ($ports !== null) {
101-
$args['ports'] = $ports;
102-
}
103-
if ($workers !== null) {
104-
$args['workers'] = $workers;
105-
}
106-
$this->config = new Config(...$args);
107-
}
71+
$this->resolver = $resolver;
72+
$this->config = $config ?? new Config();
10873

10974
if ($this->config->isTlsEnabled()) {
11075
/** @var TLS $tls */
@@ -137,46 +102,6 @@ public function __construct(
137102
$this->configure();
138103
}
139104

140-
/**
141-
* Build a Config object from an associative array of settings
142-
*
143-
* @param array<string, mixed> $settings
144-
*/
145-
protected static function buildConfig(
146-
array $settings,
147-
?string $host = null,
148-
?array $ports = null,
149-
?int $workers = null,
150-
): Config {
151-
return new Config(
152-
host: $host ?? ($settings['host'] ?? '0.0.0.0'),
153-
ports: $ports ?? ($settings['ports'] ?? [5432, 3306, 27017]),
154-
workers: $workers ?? ($settings['workers'] ?? 16),
155-
maxConnections: $settings['max_connections'] ?? 200_000,
156-
maxCoroutine: $settings['max_coroutine'] ?? 200_000,
157-
socketBufferSize: $settings['socket_buffer_size'] ?? 16 * 1024 * 1024,
158-
bufferOutputSize: $settings['buffer_output_size'] ?? 16 * 1024 * 1024,
159-
reactorNum: $settings['reactor_num'] ?? null,
160-
dispatchMode: $settings['dispatch_mode'] ?? 2,
161-
enableReusePort: $settings['enable_reuse_port'] ?? true,
162-
backlog: $settings['backlog'] ?? 65535,
163-
packageMaxLength: $settings['package_max_length'] ?? 32 * 1024 * 1024,
164-
tcpKeepidle: $settings['tcp_keepidle'] ?? 30,
165-
tcpKeepinterval: $settings['tcp_keepinterval'] ?? 10,
166-
tcpKeepcount: $settings['tcp_keepcount'] ?? 3,
167-
enableCoroutine: $settings['enable_coroutine'] ?? true,
168-
maxWaitTime: $settings['max_wait_time'] ?? 60,
169-
logLevel: $settings['log_level'] ?? SWOOLE_LOG_ERROR,
170-
logConnections: $settings['log_connections'] ?? false,
171-
recvBufferSize: $settings['recv_buffer_size'] ?? 131072,
172-
connectTimeout: $settings['backend_connect_timeout'] ?? 5.0,
173-
skipValidation: $settings['skip_validation'] ?? false,
174-
readWriteSplit: $settings['read_write_split'] ?? false,
175-
tls: $settings['tls'] ?? null,
176-
adapterFactory: $settings['adapter_factory'] ?? null,
177-
);
178-
}
179-
180105
protected function configure(): void
181106
{
182107
$settings = [
@@ -244,6 +169,7 @@ public function onWorkerStart(Server $server, int $workerId): void
244169
// Initialize TCP adapter per worker per port
245170
foreach ($this->config->ports as $port) {
246171
if ($this->config->adapterFactory !== null) {
172+
/** @var TCPAdapter $adapter */
247173
$adapter = ($this->config->adapterFactory)($port);
248174
} else {
249175
$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)