Skip to content

Commit fd7c266

Browse files
committed
Fix or suppress Psalm 6 issues
1 parent 779fab0 commit fd7c266

File tree

5 files changed

+67
-40
lines changed

5 files changed

+67
-40
lines changed

psalm.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
</projectFiles>
1515

1616
<issueHandlers>
17+
<ClassMustBeFinal>
18+
<errorLevel type="suppress">
19+
<file name="src/PostgresQueryError.php"/>
20+
</errorLevel>
21+
</ClassMustBeFinal>
22+
1723
<MethodSignatureMismatch>
1824
<errorLevel type="suppress">
1925
<file name="src/Internal/AbstractHandle.php"/>
@@ -33,6 +39,12 @@
3339
</errorLevel>
3440
</PossiblyUnusedMethod>
3541

42+
<PossiblyUnusedProperty>
43+
<errorLevel type="suppress">
44+
<file name="src/PostgresNotification.php"/>
45+
</errorLevel>
46+
</PossiblyUnusedProperty>
47+
3648
<RiskyTruthyFalsyComparison>
3749
<errorLevel type="suppress">
3850
<directory name="src"/>
@@ -44,5 +56,11 @@
4456
<directory name="src"/>
4557
</errorLevel>
4658
</UnsupportedPropertyReferenceUsage>
59+
60+
<UnusedClass>
61+
<errorLevel type="suppress">
62+
<directory name="src"/>
63+
</errorLevel>
64+
</UnusedClass>
4765
</issueHandlers>
4866
</psalm>

src/Internal/PgSqlHandle.php

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ final class PgSqlHandle extends AbstractHandle
6060
private array $statements = [];
6161

6262
/**
63-
* @param \PgSql\Connection $handle PostgreSQL connection handle.
63+
* @param \PgSql\Connection $connection PostgreSQL connection handle.
6464
* @param resource $socket PostgreSQL connection stream socket.
6565
* @param string $id Connection identifier for determining which cached type table to use.
6666
*/
6767
public function __construct(
68-
\PgSql\Connection $handle,
68+
\PgSql\Connection $connection,
6969
$socket,
7070
private readonly string $id,
7171
PostgresConfig $config,
7272
) {
73-
$this->handle = $handle;
73+
$this->handle = $connection;
7474

75-
$handle = &$this->handle;
75+
$connection = &$this->handle;
7676
$lastUsedAt = &$this->lastUsedAt;
7777
$deferred = &$this->pendingOperation;
7878
$listeners = &$this->listeners;
@@ -82,10 +82,10 @@ public function __construct(
8282
&$deferred,
8383
&$lastUsedAt,
8484
&$listeners,
85-
&$handle,
85+
&$connection,
8686
$onClose,
8787
): void {
88-
if (!$handle) {
88+
if (!$connection) {
8989
EventLoop::disable($watcher);
9090
return;
9191
}
@@ -95,15 +95,15 @@ public function __construct(
9595
\set_error_handler(self::getErrorHandler());
9696

9797
try {
98-
if (\pg_connection_status($handle) !== \PGSQL_CONNECTION_OK) {
98+
if (\pg_connection_status($connection) !== \PGSQL_CONNECTION_OK) {
9999
throw new SqlConnectionException("The connection closed during the operation");
100100
}
101101

102-
if (!\pg_consume_input($handle)) {
103-
throw new SqlConnectionException(\pg_last_error($handle));
102+
if (!\pg_consume_input($connection)) {
103+
throw new SqlConnectionException(\pg_last_error($connection));
104104
}
105105

106-
while ($result = \pg_get_notify($handle, \PGSQL_ASSOC)) {
106+
while ($result = \pg_get_notify($connection, \PGSQL_ASSOC)) {
107107
$channel = $result["message"];
108108

109109
if (!isset($listeners[$channel])) {
@@ -118,18 +118,18 @@ public function __construct(
118118
return; // No active query, only notification listeners.
119119
}
120120

121-
if (\pg_connection_busy($handle)) {
121+
if (\pg_connection_busy($connection)) {
122122
return;
123123
}
124124

125-
$deferred->complete(\pg_get_result($handle));
125+
$deferred->complete(\pg_get_result($connection));
126126
$deferred = null;
127127

128128
if (empty($listeners)) {
129129
EventLoop::unreference($watcher);
130130
}
131131
} catch (SqlConnectionException $exception) {
132-
$handle = null; // Marks connection as dead.
132+
$connection = null; // Marks connection as dead.
133133
EventLoop::disable($watcher);
134134

135135
self::shutdown($listeners, $deferred, $onClose, $exception);
@@ -141,29 +141,29 @@ public function __construct(
141141
$await = EventLoop::onWritable($socket, static function (string $watcher) use (
142142
&$deferred,
143143
&$listeners,
144-
&$handle,
144+
&$connection,
145145
$onClose,
146146
): void {
147-
if (!$handle) {
147+
if (!$connection) {
148148
EventLoop::disable($watcher);
149149
return;
150150
}
151151

152152
\set_error_handler(self::getErrorHandler());
153153

154154
try {
155-
$flush = \pg_flush($handle);
155+
$flush = \pg_flush($connection);
156156
if ($flush === 0) {
157157
return; // Not finished sending data, listen again.
158158
}
159159

160160
EventLoop::disable($watcher);
161161

162162
if ($flush === false) {
163-
throw new SqlConnectionException(\pg_last_error($handle));
163+
throw new SqlConnectionException(\pg_last_error($connection));
164164
}
165165
} catch (SqlConnectionException $exception) {
166-
$handle = null; // Marks connection as dead.
166+
$connection = null; // Marks connection as dead.
167167
EventLoop::disable($watcher);
168168

169169
self::shutdown($listeners, $deferred, $onClose, $exception);
@@ -211,7 +211,7 @@ private function fetchTypes(): Future
211211
try {
212212
$result = $queryDeferred->getFuture()->await();
213213
if (\pg_result_status($result) !== \PGSQL_TUPLES_OK) {
214-
throw new SqlException(\pg_result_error($result));
214+
throw new SqlException(\pg_result_error($result) ?: 'Unknown result error');
215215
}
216216

217217
$types = [];
@@ -284,6 +284,7 @@ private function send(\Closure $function, mixed ...$args): mixed
284284
}
285285

286286
while ($result = \pg_get_result($this->handle)) {
287+
/** @psalm-suppress UnusedFunctionCall */
287288
\pg_free_result($result);
288289
}
289290

@@ -337,7 +338,7 @@ private function createResult(\PgSql\Result $result, string $sql): PostgresResul
337338
foreach (self::DIAGNOSTIC_CODES as $fieldCode => $description) {
338339
$diagnostics[$description] = \pg_result_error_field($result, $fieldCode);
339340
}
340-
$message = \pg_result_error($result);
341+
$message = \pg_result_error($result) ?: 'Unknown result error';
341342
\set_error_handler(self::getErrorHandler());
342343
try {
343344
while (\pg_connection_busy($this->handle) && \pg_get_result($this->handle)) {
@@ -350,7 +351,7 @@ private function createResult(\PgSql\Result $result, string $sql): PostgresResul
350351

351352
case \PGSQL_BAD_RESPONSE:
352353
$this->close();
353-
throw new SqlException(\pg_result_error($result));
354+
throw new SqlException(\pg_result_error($result) ?: 'Unknown result error');
354355

355356
default:
356357
// @codeCoverageIgnoreStart
@@ -407,7 +408,7 @@ public function statementDeallocate(string $name): void
407408
}
408409

409410
$future = $storage->future;
410-
$storage->future = async(function () use ($future, $storage, $name): void {
411+
$storage->future = async(function () use ($future, $name): void {
411412
if (!$future->await()) {
412413
return; // Statement already deallocated.
413414
}
@@ -493,17 +494,21 @@ public function prepare(string $sql): PostgresStatement
493494
foreach (self::DIAGNOSTIC_CODES as $fieldCode => $description) {
494495
$diagnostics[$description] = \pg_result_error_field($result, $fieldCode);
495496
}
496-
throw new PostgresQueryError(\pg_result_error($result), $diagnostics, $sql);
497+
throw new PostgresQueryError(
498+
\pg_result_error($result) ?: 'Unknown result error',
499+
$diagnostics,
500+
$sql,
501+
);
497502

498503
case \PGSQL_BAD_RESPONSE:
499-
throw new SqlException(\pg_result_error($result));
504+
throw new SqlException(\pg_result_error($result) ?: 'Unknown result error');
500505

501506
default:
502507
// @codeCoverageIgnoreStart
503508
throw new SqlException(\sprintf(
504509
"Unknown result status: %d; error: %s",
505510
$status,
506-
\pg_result_error($result) ?: 'none',
511+
\pg_result_error($result) ?: 'Unknown result error',
507512
));
508513
// @codeCoverageIgnoreEnd
509514
}

src/Internal/PgSqlResultIterator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ private function getIterator(): \Iterator
5454
$result = \pg_fetch_array($this->handle, mode: \PGSQL_NUM);
5555

5656
if ($result === false) {
57-
throw new SqlException(\pg_result_error($this->handle));
57+
throw new SqlException(\pg_result_error($this->handle) ?: 'Unknown result error');
5858
}
5959

6060
/** @var list<int> $fieldTypes */
6161
yield \array_combine($fieldNames, \array_map($this->cast(...), $fieldTypes, $result));
6262
}
6363
} finally {
64+
/** @psalm-suppress UnusedFunctionCall */
6465
\pg_free_result($this->handle);
6566
}
6667
}

src/Internal/PqHandle.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ final class PqHandle extends AbstractHandle
2828
/** @var array<non-empty-string, StatementStorage<pq\Statement>> */
2929
private array $statements = [];
3030

31+
/**
32+
* @psalm-suppress UnusedVariable
33+
*/
3134
public function __construct(pq\Connection $handle, PostgresConfig $config)
3235
{
3336
$this->handle = $handle;
3437

35-
$handle = &$this->handle;
38+
$connection = &$this->handle;
3639
$lastUsedAt = &$this->lastUsedAt;
3740
$deferred = &$this->pendingOperation;
3841
$listeners = &$this->listeners;
@@ -42,21 +45,21 @@ public function __construct(pq\Connection $handle, PostgresConfig $config)
4245
&$deferred,
4346
&$lastUsedAt,
4447
&$listeners,
45-
&$handle,
48+
&$connection,
4649
$onClose,
4750
): void {
4851
$lastUsedAt = \time();
4952

5053
try {
51-
if ($handle->status !== pq\Connection::OK) {
54+
if ($connection->status !== pq\Connection::OK) {
5255
throw new SqlConnectionException("The connection closed during the operation");
5356
}
5457

55-
if ($handle->poll() === pq\Connection::POLLING_FAILED) {
56-
throw new SqlConnectionException($handle->errorMessage);
58+
if ($connection->poll() === pq\Connection::POLLING_FAILED) {
59+
throw new SqlConnectionException($connection->errorMessage);
5760
}
5861
} catch (SqlConnectionException $exception) {
59-
$handle = null; // Marks connection as dead.
62+
$connection = null; // Marks connection as dead.
6063
EventLoop::disable($watcher);
6164

6265
self::shutdown($listeners, $deferred, $onClose, $exception);
@@ -68,11 +71,11 @@ public function __construct(pq\Connection $handle, PostgresConfig $config)
6871
return; // No active query, only notification listeners.
6972
}
7073

71-
if ($handle->busy) {
74+
if ($connection->busy) {
7275
return; // Not finished receiving data, poll again.
7376
}
7477

75-
$deferred->complete($handle->getResult());
78+
$deferred->complete($connection->getResult());
7679
$deferred = null;
7780

7881
if (empty($listeners)) {
@@ -83,16 +86,16 @@ public function __construct(pq\Connection $handle, PostgresConfig $config)
8386
$await = EventLoop::onWritable($this->handle->socket, static function (string $watcher) use (
8487
&$deferred,
8588
&$listeners,
86-
&$handle,
89+
&$connection,
8790
$onClose,
8891
): void {
8992
try {
90-
if (!$handle?->flush()) {
93+
if (!$connection?->flush()) {
9194
return; // Not finished sending data, continue polling for writability.
9295
}
9396
} catch (pq\Exception $exception) {
9497
$exception = new SqlConnectionException("Flushing the connection failed", 0, $exception);
95-
$handle = null; // Marks connection as dead.
98+
$connection = null; // Marks connection as dead.
9699

97100
self::shutdown($listeners, $deferred, $onClose, $exception);
98101
}
@@ -329,7 +332,7 @@ public function statementDeallocate(string $name): void
329332
}
330333

331334
$future = $storage->future;
332-
$storage->future = async(function () use ($future, $storage, $name): void {
335+
$storage->future = async(function () use ($future, $name): void {
333336
$statement = $future->await();
334337
if (!$statement instanceof pq\Statement) {
335338
return; // Statement already deallocated.

src/Internal/functions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
/**
3333
* @internal
34-
* @psalm-suppress ReferenceConstraintViolation
34+
* @psalm-suppress InvalidNullableReturnType, NullableReturnStatement, ReferenceConstraintViolation
3535
*
3636
* @param string $sql SQL statement with named and unnamed placeholders.
3737
* @param-out list<int|string> $names Array of parameter positions mapped to names and/or indexed locations.
@@ -79,7 +79,7 @@ function parseNamedParams(string $sql, ?array &$names): string
7979
function replaceNamedParams(array $params, array $names): array
8080
{
8181
$values = [];
82-
foreach ($names as $index => $name) {
82+
foreach ($names as $name) {
8383
if (!\array_key_exists($name, $params)) {
8484
if (\is_int($name)) {
8585
$message = \sprintf("Value for unnamed parameter at position %s missing", $name);

0 commit comments

Comments
 (0)