diff --git a/src/Connector/SocketConnector.php b/src/Connector/SocketConnector.php index ec61457c..dc129035 100644 --- a/src/Connector/SocketConnector.php +++ b/src/Connector/SocketConnector.php @@ -54,6 +54,27 @@ public function __construct(string $socketPath) } } + /** @return mixed */ + private function convertErrorsToExceptions(callable $functionToRun) + { + // phpcs:disable SlevomatCodingStandard.TypeHints.TypeHintDeclaration.IncorrectReturnTypeHint + set_error_handler( + static function (int $severity, string $message, string $file = '', int $line = 0, array $context = []) : bool { + throw new ErrorException($message, 0, $severity, $file, $line); + }, + E_STRICT | E_NOTICE | E_WARNING + ); + // phpcs:enable + + try { + $returnValue = $functionToRun(); + } finally { + restore_error_handler(); + } + + return $returnValue; + } + public function connect() : void { if ($this->connected()) { @@ -63,22 +84,14 @@ public function connect() : void try { socket_clear_error($this->socket); - // phpcs:disable SlevomatCodingStandard.TypeHints.TypeHintDeclaration.IncorrectReturnTypeHint - set_error_handler( - static function (int $severity, string $message, string $file = '', int $line = 0, array $context = []) : bool { - throw new ErrorException($message, 0, $severity, $file, $line); - }, - E_STRICT | E_NOTICE | E_WARNING - ); - // phpcs:enable + $this->connected = $this->convertErrorsToExceptions(function () { + return socket_connect($this->socket, $this->socketPath); + }); - $this->connected = socket_connect($this->socket, $this->socketPath); register_shutdown_function([&$this, 'shutdown']); } catch (Throwable $e) { $this->connected = false; throw FailedToConnect::fromSocketPathAndPrevious($this->socketPath, $e); - } finally { - restore_error_handler(); } } @@ -100,22 +113,22 @@ public function sendCommand(Command $message) : string // Socket error is a global state, so we must reset to a known state first... socket_clear_error($this->socket); - if (socket_send($this->socket, pack('N', $size), 4, 0) === false) { + if (@socket_send($this->socket, pack('N', $size), 4, 0) === false) { throw Exception\FailedToSendCommand::writingMessageSizeToSocket($message, $this->socket, $this->socketPath); } - if (socket_send($this->socket, $serializedJsonString, $size, 0) === false) { + if (@socket_send($this->socket, $serializedJsonString, $size, 0) === false) { throw Exception\FailedToSendCommand::writingMessageContentToSocket($message, $this->socket, $this->socketPath); } // Read the response back and drop it. Needed for socket liveness - $responseLength = socket_read($this->socket, 4); + $responseLength = @socket_read($this->socket, 4); if ($responseLength === false) { throw Exception\FailedToSendCommand::readingResponseSizeFromSocket($message, $this->socket, $this->socketPath); } - $dataRead = socket_read($this->socket, unpack('N', $responseLength)[1]); + $dataRead = @socket_read($this->socket, unpack('N', $responseLength)[1]); if ($dataRead === false) { throw Exception\FailedToSendCommand::readingResponseContentFromSocket($message, $this->socket, $this->socketPath); diff --git a/tests/Unit/Connector/SocketConnectorTest.php b/tests/Unit/Connector/SocketConnectorTest.php new file mode 100644 index 00000000..f2f3255b --- /dev/null +++ b/tests/Unit/Connector/SocketConnectorTest.php @@ -0,0 +1,22 @@ +expectException(FailedToConnect::class); + $this->expectExceptionMessage('socket_connect(): unable to connect [2]: No such file or directory'); + $connector->connect(); + } +}