From a2c41dc1b1542f55d37562868863fa6c62c441e0 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Mon, 4 Dec 2023 23:02:23 -0600 Subject: [PATCH] Fix Psalm issues identified by new version --- src/Internal/PgSqlHandle.php | 14 ++++++++++++-- src/Internal/PqHandle.php | 2 +- src/Internal/functions.php | 1 + src/PgSqlConnection.php | 15 +++++++++++++-- src/PqConnection.php | 4 +++- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/Internal/PgSqlHandle.php b/src/Internal/PgSqlHandle.php index 33a64fd..a148067 100644 --- a/src/Internal/PgSqlHandle.php +++ b/src/Internal/PgSqlHandle.php @@ -62,6 +62,8 @@ public function __construct( ) { $this->handle = $handle; + $this->types = (self::$typeCache[$id] ??= self::fetchTypes($handle)); + $handle = &$this->handle; $lastUsedAt = &$this->lastUsedAt; $deferred = &$this->pendingOperation; @@ -75,6 +77,11 @@ public function __construct( &$handle, $onClose, ): void { + if (!$handle) { + EventLoop::disable($watcher); + return; + } + $lastUsedAt = \time(); \set_error_handler(self::getErrorHandler()); @@ -129,6 +136,11 @@ public function __construct( &$handle, $onClose, ): void { + if (!$handle) { + EventLoop::disable($watcher); + return; + } + \set_error_handler(self::getErrorHandler()); try { @@ -156,8 +168,6 @@ public function __construct( EventLoop::disable($await); parent::__construct($config, $poll, $await, $onClose); - - $this->types = (self::$typeCache[$id] ??= self::fetchTypes($handle)); } /** diff --git a/src/Internal/PqHandle.php b/src/Internal/PqHandle.php index 0e22976..252b4e4 100644 --- a/src/Internal/PqHandle.php +++ b/src/Internal/PqHandle.php @@ -87,7 +87,7 @@ public function __construct(pq\Connection $handle, PostgresConfig $config) $onClose, ): void { try { - if (!$handle->flush()) { + if (!$handle?->flush()) { return; // Not finished sending data, continue polling for writability. } } catch (pq\Exception $exception) { diff --git a/src/Internal/functions.php b/src/Internal/functions.php index 7ba2011..f872cf1 100644 --- a/src/Internal/functions.php +++ b/src/Internal/functions.php @@ -27,6 +27,7 @@ /** * @internal + * @psalm-suppress ReferenceConstraintViolation * * @param string $sql SQL statement with named and unnamed placeholders. * @param-out list $names Array of parameter positions mapped to names and/or indexed locations. diff --git a/src/PgSqlConnection.php b/src/PgSqlConnection.php index c15023b..d451a3c 100644 --- a/src/PgSqlConnection.php +++ b/src/PgSqlConnection.php @@ -35,8 +35,19 @@ public static function connect(PostgresConfig $config, ?Cancellation $cancellati $hash = \sha1($config->getHost() . $config->getPort() . $config->getUser()); $deferred = new DeferredFuture(); - /** @psalm-suppress MissingClosureParamType $resource is a resource and cannot be inferred in this context */ - $callback = static function (string $callbackId, $resource) use (&$poll, &$await, $connection, $config, $deferred, $hash): void { + + /** + * @psalm-suppress MissingClosureParamType $resource is a resource and cannot be inferred in this context. + * @psalm-suppress UndefinedVariable $poll is defined below. + */ + $callback = static function (string $callbackId, $resource) use ( + &$poll, + &$await, + $connection, + $config, + $deferred, + $hash, + ): void { switch ($result = \pg_connect_poll($connection)) { case \PGSQL_POLLING_READING: case \PGSQL_POLLING_WRITING: diff --git a/src/PqConnection.php b/src/PqConnection.php index 23a94c2..3284474 100644 --- a/src/PqConnection.php +++ b/src/PqConnection.php @@ -24,7 +24,9 @@ public static function connect(PostgresConfig $config, ?Cancellation $cancellati $connection->unbuffered = true; $deferred = new DeferredFuture(); - $callback = function () use (&$poll, &$await, $connection, $config, $deferred): void { + + /** @psalm-suppress UndefinedVariable $poll is defined below. */ + $callback = static function () use (&$poll, &$await, $connection, $config, $deferred): void { switch ($result = $connection->poll()) { case pq\Connection::POLLING_READING: case pq\Connection::POLLING_WRITING: