From 63949c3147e26aa010314d00668d754937207a82 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Tue, 7 Aug 2018 08:52:14 +0200 Subject: [PATCH 1/3] Adding type annotations all over the place --- src/ApplicationFactory.php | 7 ++-- src/ApplicationFactoryInterface.php | 4 +- src/CallbackKernel.php | 2 +- src/Command/DaemonRunCommand.php | 5 ++- src/DaemonFactoryInterface.php | 4 +- src/DaemonInterface.php | 6 +-- src/DaemonOptions.php | 6 +-- src/DaemonOptionsInterface.php | 2 +- src/DaemonTrait.php | 14 +++---- src/Driver/DriverContainer.php | 4 +- src/Driver/DriverContainerInterface.php | 2 +- .../Connection/ConnectionInterface.php | 8 ++-- .../Connection/ConnectionPoolInterface.php | 10 ++--- .../Connection/StreamSocketConnection.php | 10 ++--- .../Connection/StreamSocketConnectionPool.php | 19 ++++----- .../ConnectionHandler/ConnectionHandler.php | 40 ++++++++++--------- .../ConnectionHandlerFactory.php | 2 +- .../ConnectionHandlerFactoryInterface.php | 2 +- .../ConnectionHandlerInterface.php | 8 ++-- src/Driver/Userland/UserlandDaemon.php | 11 +++-- src/Driver/Userland/UserlandDaemonFactory.php | 6 +-- src/Http/Request.php | 15 +++---- src/Http/RequestInterface.php | 22 +++------- 23 files changed, 104 insertions(+), 105 deletions(-) diff --git a/src/ApplicationFactory.php b/src/ApplicationFactory.php index 4ef346a..f5abab9 100644 --- a/src/ApplicationFactory.php +++ b/src/ApplicationFactory.php @@ -6,6 +6,7 @@ use PHPFastCGI\FastCGIDaemon\Driver\DriverContainer; use PHPFastCGI\FastCGIDaemon\Driver\DriverContainerInterface; use Symfony\Component\Console\Application; +use Symfony\Component\Console\Command\Command; /** * The default implementation of the ApplicationFactoryInterface. @@ -30,7 +31,7 @@ public function __construct(DriverContainerInterface $driverContainer = null) /** * {@inheritdoc} */ - public function createApplication($kernel, $commandName = null, $commandDescription = null) + public function createApplication($kernel, string $commandName = null, string $commandDescription = null): Application { $command = $this->createCommand($kernel, $commandName, $commandDescription); @@ -43,7 +44,7 @@ public function createApplication($kernel, $commandName = null, $commandDescript /** * {@inheritdoc} */ - public function createCommand($kernel, $commandName = null, $commandDescription = null) + public function createCommand($kernel, string $commandName = null, string $commandDescription = null): Command { $kernelObject = $this->getKernelObject($kernel); @@ -60,7 +61,7 @@ public function createCommand($kernel, $commandName = null, $commandDescription * * @return KernelInterface The kernel as an object implementing the KernelInterface */ - private function getKernelObject($kernel) + private function getKernelObject($kernel): KernelInterface { if ($kernel instanceof KernelInterface) { return $kernel; diff --git a/src/ApplicationFactoryInterface.php b/src/ApplicationFactoryInterface.php index 3cb5933..9e2a1f8 100644 --- a/src/ApplicationFactoryInterface.php +++ b/src/ApplicationFactoryInterface.php @@ -20,7 +20,7 @@ interface ApplicationFactoryInterface * * @return Application The Symfony console application */ - public function createApplication($kernel, $commandName = null, $commandDescription = null); + public function createApplication($kernel, string $commandName = null, string $commandDescription = null): Application; /** * Create a Symfony console command. @@ -31,5 +31,5 @@ public function createApplication($kernel, $commandName = null, $commandDescript * * @return Command The Symfony console command */ - public function createCommand($kernel, $commandName = null, $commandDescription = null); + public function createCommand($kernel, string $commandName = null, string $commandDescription = null): Command; } diff --git a/src/CallbackKernel.php b/src/CallbackKernel.php index c90b5b8..5b558e1 100644 --- a/src/CallbackKernel.php +++ b/src/CallbackKernel.php @@ -22,7 +22,7 @@ final class CallbackKernel implements KernelInterface * * @throws \InvalidArgumentException When not given callable callback */ - public function __construct($handler) + public function __construct(callable $handler) { if (!is_callable($handler)) { throw new \InvalidArgumentException('Handler callback is not callable'); diff --git a/src/Command/DaemonRunCommand.php b/src/Command/DaemonRunCommand.php index 7ad0c5e..4dba192 100644 --- a/src/Command/DaemonRunCommand.php +++ b/src/Command/DaemonRunCommand.php @@ -4,6 +4,7 @@ use PHPFastCGI\FastCGIDaemon\DaemonInterface; use PHPFastCGI\FastCGIDaemon\DaemonOptions; +use PHPFastCGI\FastCGIDaemon\DaemonOptionsInterface; use PHPFastCGI\FastCGIDaemon\Driver\DriverContainerInterface; use PHPFastCGI\FastCGIDaemon\KernelInterface; use Symfony\Component\Console\Command\Command; @@ -70,9 +71,9 @@ public function __construct(KernelInterface $kernel, DriverContainerInterface $d * @param InputInterface $input The Symfony command input * @param OutputInterface $output The Symfony command output * - * @return DaemonOptions The daemon configuration + * @return DaemonOptionsInterface The daemon configuration */ - private function getDaemonOptions(InputInterface $input, OutputInterface $output) + private function getDaemonOptions(InputInterface $input, OutputInterface $output): DaemonOptionsInterface { $logger = new ConsoleLogger($output); diff --git a/src/DaemonFactoryInterface.php b/src/DaemonFactoryInterface.php index 65249ec..6ff274e 100644 --- a/src/DaemonFactoryInterface.php +++ b/src/DaemonFactoryInterface.php @@ -18,7 +18,7 @@ interface DaemonFactoryInterface * * @return DaemonInterface The FastCGI daemon */ - public function createDaemon(KernelInterface $kernel, DaemonOptions $options, $fd = DaemonInterface::FCGI_LISTENSOCK_FILENO); + public function createDaemon(KernelInterface $kernel, DaemonOptions $options, int $fd = DaemonInterface::FCGI_LISTENSOCK_FILENO): DaemonInterface; /** * Create a FastCGI daemon listening on a given address. The default host is @@ -31,5 +31,5 @@ public function createDaemon(KernelInterface $kernel, DaemonOptions $options, $f * * @return DaemonInterface The FastCGI daemon */ - public function createTcpDaemon(KernelInterface $kernel, DaemonOptions $options, $host, $port); + public function createTcpDaemon(KernelInterface $kernel, DaemonOptions $options, string $host, int $port): DaemonInterface; } diff --git a/src/DaemonInterface.php b/src/DaemonInterface.php index dc370f5..4e62700 100644 --- a/src/DaemonInterface.php +++ b/src/DaemonInterface.php @@ -46,13 +46,13 @@ interface DaemonInterface * * @throws \Exception On fatal error */ - public function run(); + public function run(): void; /** * Flag the daemon for shutting down. This will stop it from accepting * requests. - * + * * @param string|null Optional message. */ - public function flagShutdown($message = null); + public function flagShutdown(string $message = null): void; } diff --git a/src/DaemonOptions.php b/src/DaemonOptions.php index 6ade03f..f1110f2 100644 --- a/src/DaemonOptions.php +++ b/src/DaemonOptions.php @@ -19,13 +19,13 @@ final class DaemonOptions implements DaemonOptionsInterface * Constructor. * * The value of the LOGGER option must implement the PSR-3 LoggerInterface. - * + * * For the REQUEST_LIMIT, MEMORY_LIMIT and TIME_LIMIT options, NO_LIMIT can * be used to specify that these metrics should not cause the daemon to * shutdown. * * @param array $options The options to configure the daemon with - * + * * @throws \InvalidArgumentException On unrecognised option */ public function __construct(array $options = []) @@ -52,7 +52,7 @@ public function __construct(array $options = []) } } - public function getOption($option) + public function getOption(string $option) { if (!isset($this->options[$option])) { throw new \InvalidArgumentException('Unknown option: '.$option); diff --git a/src/DaemonOptionsInterface.php b/src/DaemonOptionsInterface.php index 7c6dedb..56e2f76 100644 --- a/src/DaemonOptionsInterface.php +++ b/src/DaemonOptionsInterface.php @@ -22,5 +22,5 @@ interface DaemonOptionsInterface * * @throws \InvalidArgumentException On unrecognised option */ - public function getOption($option); + public function getOption(string $option); } diff --git a/src/DaemonTrait.php b/src/DaemonTrait.php index e9712e2..f5b3cf4 100644 --- a/src/DaemonTrait.php +++ b/src/DaemonTrait.php @@ -38,10 +38,10 @@ trait DaemonTrait /** * Flags the daemon for shutting down. - * + * * @param string $message Optional shutdown message */ - public function flagShutdown($message = null) + public function flagShutdown(string $message = null): void { $this->isShutdown = true; $this->shutdownMessage = (null === $message ? 'Daemon flagged for shutdown' : $message); @@ -51,9 +51,9 @@ public function flagShutdown($message = null) * Loads to configuration from the daemon options and installs signal * handlers. * - * @param DaemonOptions $daemonOptions + * @param DaemonOptionsInterface $daemonOptions */ - private function setupDaemon(DaemonOptions $daemonOptions) + private function setupDaemon(DaemonOptionsInterface $daemonOptions): void { $this->requestCount = 0; $this->requestLimit = $daemonOptions->getOption(DaemonOptions::REQUEST_LIMIT); @@ -74,7 +74,7 @@ private function setupDaemon(DaemonOptions $daemonOptions) * * @param int[] $statusCodes The status codes of sent responses */ - private function considerStatusCodes($statusCodes) + private function considerStatusCodes(array $statusCodes): void { $this->requestCount += count($statusCodes); @@ -94,7 +94,7 @@ private function considerStatusCodes($statusCodes) * * @throws ShutdownException On receiving a SIGINT or SIGALRM */ - private function installSignalHandlers() + private function installSignalHandlers(): void { declare (ticks = 1); @@ -114,7 +114,7 @@ private function installSignalHandlers() * * @throws ShutdownException When limits in the daemon options are exceeded */ - private function checkDaemonLimits() + private function checkDaemonLimits(): void { if ($this->isShutdown) { throw new ShutdownException($this->shutdownMessage); diff --git a/src/Driver/DriverContainer.php b/src/Driver/DriverContainer.php index a7dfb82..4e3fddc 100644 --- a/src/Driver/DriverContainer.php +++ b/src/Driver/DriverContainer.php @@ -2,6 +2,8 @@ namespace PHPFastCGI\FastCGIDaemon\Driver; +use PHPFastCGI\FastCGIDaemon\DaemonFactoryInterface; + final class DriverContainer implements DriverContainerInterface { /** @@ -25,7 +27,7 @@ public function __construct() /** * {@inheritdoc} */ - public function getFactory($driver) + public function getFactory(string $driver): DaemonFactoryInterface { if (!isset($this->drivers[$driver])) { throw new \InvalidArgumentException('Unknown driver: '.$driver); diff --git a/src/Driver/DriverContainerInterface.php b/src/Driver/DriverContainerInterface.php index a3168d1..d7020e6 100644 --- a/src/Driver/DriverContainerInterface.php +++ b/src/Driver/DriverContainerInterface.php @@ -13,5 +13,5 @@ interface DriverContainerInterface * * @return DaemonFactoryInterface The daemon factory for the driver */ - public function getFactory($driver); + public function getFactory(string $driver): DaemonFactoryInterface; } diff --git a/src/Driver/Userland/Connection/ConnectionInterface.php b/src/Driver/Userland/Connection/ConnectionInterface.php index 4d1640c..d7bb910 100644 --- a/src/Driver/Userland/Connection/ConnectionInterface.php +++ b/src/Driver/Userland/Connection/ConnectionInterface.php @@ -19,7 +19,7 @@ interface ConnectionInterface * * @throws ConnectionException On failure */ - public function read($length); + public function read(int $length): string; /** * Write data to the connection. @@ -28,17 +28,17 @@ public function read($length); * * @throws ConnectionException On failure */ - public function write($buffer); + public function write(string $buffer): void; /** * Tests to see if the connection has been closed. * * @return bool True if the connection has been closed, false otherwise */ - public function isClosed(); + public function isClosed(): bool; /** * Closes the connection it from the pool. */ - public function close(); + public function close(): void; } diff --git a/src/Driver/Userland/Connection/ConnectionPoolInterface.php b/src/Driver/Userland/Connection/ConnectionPoolInterface.php index 73d8f8f..67a1c16 100644 --- a/src/Driver/Userland/Connection/ConnectionPoolInterface.php +++ b/src/Driver/Userland/Connection/ConnectionPoolInterface.php @@ -19,25 +19,25 @@ interface ConnectionPoolInterface * @param int $timeout Upper bound on the amount of time to wair for readable connections * * @return ConnectionInterface[] - * + * * @throws \RuntimeException On encountering fatal error */ - public function getReadableConnections($timeout); + public function getReadableConnections(int $timeout): array; /** * Returns the number of active connections in the pool. * * @return int */ - public function count(); + public function count(): int; /** * Stop the connection pool from accepting new connections. */ - public function shutdown(); + public function shutdown(): void; /** * Close the connection pool and free any associated resources. */ - public function close(); + public function close(): void; } diff --git a/src/Driver/Userland/Connection/StreamSocketConnection.php b/src/Driver/Userland/Connection/StreamSocketConnection.php index 19f9aad..d72cf01 100644 --- a/src/Driver/Userland/Connection/StreamSocketConnection.php +++ b/src/Driver/Userland/Connection/StreamSocketConnection.php @@ -39,7 +39,7 @@ public function __construct($socket) * * @return ConnectionException */ - protected function createExceptionFromLastError($function) + protected function createExceptionFromLastError(string $function): ConnectionException { $this->close(); @@ -49,7 +49,7 @@ protected function createExceptionFromLastError($function) /** * {@inheritdoc} */ - public function read($length) + public function read(int $length): string { if ($this->isClosed()) { throw new ConnectionException('Connection has been closed'); @@ -71,7 +71,7 @@ public function read($length) /** * {@inheritdoc} */ - public function write($buffer) + public function write(string $buffer): void { if ($this->isClosed()) { throw new ConnectionException('Connection has been closed'); @@ -85,7 +85,7 @@ public function write($buffer) /** * {@inheritdoc} */ - public function isClosed() + public function isClosed(): bool { return $this->closed; } @@ -93,7 +93,7 @@ public function isClosed() /** * {@inheritdoc} */ - public function close() + public function close(): void { if (!$this->isClosed()) { fclose($this->socket); diff --git a/src/Driver/Userland/Connection/StreamSocketConnectionPool.php b/src/Driver/Userland/Connection/StreamSocketConnectionPool.php index 61d4ff9..4c6428c 100644 --- a/src/Driver/Userland/Connection/StreamSocketConnectionPool.php +++ b/src/Driver/Userland/Connection/StreamSocketConnectionPool.php @@ -19,7 +19,7 @@ final class StreamSocketConnectionPool implements ConnectionPoolInterface private $clientSockets; /** - * @var Connection[] + * @var ConnectionInterface[] */ private $connections; @@ -47,7 +47,7 @@ public function __construct($socket) /** * {@inheritdoc} */ - public function getReadableConnections($timeout) + public function getReadableConnections(int $timeout): array { $this->removeClosedConnections(); @@ -76,7 +76,7 @@ public function getReadableConnections($timeout) /** * {@inheritdoc} */ - public function count() + public function count(): int { $this->removeClosedConnections(); @@ -86,7 +86,7 @@ public function count() /** * {@inheritdoc} */ - public function shutdown() + public function shutdown(): void { $this->shutdown = true; } @@ -94,7 +94,7 @@ public function shutdown() /** * {@inheritdoc} */ - public function close() + public function close(): void { $this->removeClosedConnections(); @@ -115,7 +115,7 @@ public function close() * @param resource[] $readSockets The sockets to test for readability (output parameter) * @param int $timeout The stream select call timeout */ - private function selectConnections(&$readSockets, $timeout) + private function selectConnections(&$readSockets, int $timeout): void { // stream_select will not always preserve array keys // call it with a (deep) copy so the original is preserved @@ -145,7 +145,7 @@ private function selectConnections(&$readSockets, $timeout) /** * Accept incoming connections from the server stream socket. */ - private function acceptConnection() + private function acceptConnection(): void { $clientSocket = @stream_socket_accept($this->serverSocket); @@ -161,10 +161,7 @@ private function acceptConnection() } } - /** - * Remove connections. - */ - private function removeClosedConnections() + private function removeClosedConnections(): void { foreach ($this->connections as $id => $connection) { if ($connection->isClosed()) { diff --git a/src/Driver/Userland/ConnectionHandler/ConnectionHandler.php b/src/Driver/Userland/ConnectionHandler/ConnectionHandler.php index 50b637d..ee18a2c 100644 --- a/src/Driver/Userland/ConnectionHandler/ConnectionHandler.php +++ b/src/Driver/Userland/ConnectionHandler/ConnectionHandler.php @@ -73,7 +73,7 @@ public function __construct(KernelInterface $kernel, ConnectionInterface $connec /** * {@inheritdoc} */ - public function ready() + public function ready(): array { try { $data = $this->connection->read(self::READ_LENGTH); @@ -85,7 +85,7 @@ public function ready() $statusCodes = []; - while (null !== ($record = $this->readRecord())) { + while (!empty($record = $this->readRecord())) { $statusCode = $this->processRecord($record); if (null != $statusCode) { @@ -104,7 +104,7 @@ public function ready() /** * {@inheritdoc} */ - public function shutdown() + public function shutdown(): void { $this->shutdown = true; } @@ -112,7 +112,7 @@ public function shutdown() /** * {@inheritdoc} */ - public function close() + public function close(): void { $this->buffer = null; $this->bufferLength = 0; @@ -131,7 +131,7 @@ public function close() /** * {@inheritdoc} */ - public function isClosed() + public function isClosed(): bool { return $this->closed; } @@ -142,13 +142,13 @@ public function isClosed() /** * Read a record from the connection. * - * @return array|null The record that has been read + * @return array The record that has been read */ - private function readRecord() + private function readRecord(): array { // Not enough data to read header if ($this->bufferLength < 8) { - return; + return []; } $headerData = substr($this->buffer, 0, 8); @@ -159,7 +159,7 @@ private function readRecord() // Not enough data to read rest of record if ($this->bufferLength - 8 < $record['contentLength'] + $record['paddingLength']) { - return; + return []; } $record['contentData'] = substr($this->buffer, 8, $record['contentLength']); @@ -182,7 +182,7 @@ private function readRecord() * * @throws ProtocolException If the client sends an unexpected record. */ - private function processRecord(array $record) + private function processRecord(array $record): int { $requestId = $record['requestId']; @@ -220,7 +220,7 @@ private function processRecord(array $record) * * @throws ProtocolException If the FCGI_BEGIN_REQUEST record is unexpected */ - private function processBeginRequestRecord($requestId, $contentData) + private function processBeginRequestRecord(int $requestId, string $contentData): void { if (isset($this->requests[$requestId])) { throw new ProtocolException('Unexpected FCGI_BEGIN_REQUEST record'); @@ -257,7 +257,7 @@ private function processBeginRequestRecord($requestId, $contentData) * @param int $requestId The request id that sent the name-value pair * @param string $buffer The buffer to read the pair from (pass by reference) */ - private function readNameValuePair($requestId, &$buffer) + private function readNameValuePair(int $requestId, string &$buffer): void { $nameLength = $this->readFieldLength($buffer); $valueLength = $this->readFieldLength($buffer); @@ -280,7 +280,7 @@ private function readNameValuePair($requestId, &$buffer) * * @return int The length of the field */ - private function readFieldLength(&$buffer) + private function readFieldLength(string &$buffer): int { $block = unpack('C4', $buffer); @@ -306,7 +306,7 @@ private function readFieldLength(&$buffer) * @param int $appStatus The application status to declare * @param int $protocolStatus The protocol status to declare */ - private function endRequest($requestId, $appStatus = 0, $protocolStatus = DaemonInterface::FCGI_REQUEST_COMPLETE) + private function endRequest(int $requestId, int $appStatus = 0, int $protocolStatus = DaemonInterface::FCGI_REQUEST_COMPLETE): void { $content = pack('NCx3', $appStatus, $protocolStatus); $this->writeRecord($requestId, DaemonInterface::FCGI_END_REQUEST, $content); @@ -329,7 +329,7 @@ private function endRequest($requestId, $appStatus = 0, $protocolStatus = Daemon * @param int $type The type of record * @param string $content The content of the record */ - private function writeRecord($requestId, $type, $content = null) + private function writeRecord(int $requestId, int $type, string $content = null): void { $contentLength = null === $content ? 0 : strlen($content); @@ -349,7 +349,7 @@ private function writeRecord($requestId, $type, $content = null) * @param string $headerData The header -data to write (including terminating CRLFCRLF) * @param resource $stream The stream to write */ - private function writeResponse($requestId, $headerData, $stream) + private function writeResponse(int $requestId, string $headerData, $stream): void { $data = $headerData; $eof = false; @@ -380,9 +380,11 @@ private function writeResponse($requestId, $headerData, $stream) * * @param int $requestId The request id that is ready to dispatch * + * @return int status code from the response. + * * @throws \LogicException If the kernel doesn't return a valid response */ - private function dispatchRequest($requestId) + private function dispatchRequest(int $requestId): int { $request = new Request( $this->requests[$requestId]['params'], @@ -415,7 +417,7 @@ private function dispatchRequest($requestId) * @param int $requestId The request id to respond to * @param ResponseInterface $response The PSR-7 HTTP response message */ - private function sendResponse($requestId, ResponseInterface $response) + private function sendResponse(int $requestId, ResponseInterface $response): void { $statusCode = $response->getStatusCode(); $reasonPhrase = $response->getReasonPhrase(); @@ -437,7 +439,7 @@ private function sendResponse($requestId, ResponseInterface $response) * @param int $requestId The request id to respond to * @param HttpFoundationResponse $response The HTTP foundation response message */ - private function sendHttpFoundationResponse($requestId, HttpFoundationResponse $response) + private function sendHttpFoundationResponse(int $requestId, HttpFoundationResponse $response): void { $statusCode = $response->getStatusCode(); diff --git a/src/Driver/Userland/ConnectionHandler/ConnectionHandlerFactory.php b/src/Driver/Userland/ConnectionHandler/ConnectionHandlerFactory.php index c88e7bf..02e255a 100644 --- a/src/Driver/Userland/ConnectionHandler/ConnectionHandlerFactory.php +++ b/src/Driver/Userland/ConnectionHandler/ConnectionHandlerFactory.php @@ -13,7 +13,7 @@ final class ConnectionHandlerFactory implements ConnectionHandlerFactoryInterfac /** * {@inheritdoc} */ - public function createConnectionHandler(KernelInterface $kernel, ConnectionInterface $connection) + public function createConnectionHandler(KernelInterface $kernel, ConnectionInterface $connection): ConnectionHandlerInterface { return new ConnectionHandler($kernel, $connection); } diff --git a/src/Driver/Userland/ConnectionHandler/ConnectionHandlerFactoryInterface.php b/src/Driver/Userland/ConnectionHandler/ConnectionHandlerFactoryInterface.php index e7e0784..a701414 100644 --- a/src/Driver/Userland/ConnectionHandler/ConnectionHandlerFactoryInterface.php +++ b/src/Driver/Userland/ConnectionHandler/ConnectionHandlerFactoryInterface.php @@ -19,5 +19,5 @@ interface ConnectionHandlerFactoryInterface * * @return ConnectionHandlerInterface The connection handler */ - public function createConnectionHandler(KernelInterface $kernel, ConnectionInterface $connection); + public function createConnectionHandler(KernelInterface $kernel, ConnectionInterface $connection): ConnectionHandlerInterface; } diff --git a/src/Driver/Userland/ConnectionHandler/ConnectionHandlerInterface.php b/src/Driver/Userland/ConnectionHandler/ConnectionHandlerInterface.php index d4ed025..de9dbd0 100644 --- a/src/Driver/Userland/ConnectionHandler/ConnectionHandlerInterface.php +++ b/src/Driver/Userland/ConnectionHandler/ConnectionHandlerInterface.php @@ -16,18 +16,18 @@ interface ConnectionHandlerInterface * * @return int[] The status codes of requests dispatched during the function call */ - public function ready(); + public function ready(): array; /** * Gracefully shutdown the connection being handled. */ - public function shutdown(); + public function shutdown(): void; /** * Closes the connection handler and free's associated resources. Calling * this method must also close the connection object that was being handled. */ - public function close(); + public function close(): void; /** * Returns true if the connection handler has been closed and false if it @@ -35,5 +35,5 @@ public function close(); * * @return bool */ - public function isClosed(); + public function isClosed(): bool; } diff --git a/src/Driver/Userland/UserlandDaemon.php b/src/Driver/Userland/UserlandDaemon.php index 9e52fcb..96007c4 100644 --- a/src/Driver/Userland/UserlandDaemon.php +++ b/src/Driver/Userland/UserlandDaemon.php @@ -6,6 +6,7 @@ use PHPFastCGI\FastCGIDaemon\DaemonOptions; use PHPFastCGI\FastCGIDaemon\DaemonTrait; use PHPFastCGI\FastCGIDaemon\Driver\Userland\Connection\ConnectionPoolInterface; +use PHPFastCGI\FastCGIDaemon\Driver\Userland\ConnectionHandler\ConnectionHandler; use PHPFastCGI\FastCGIDaemon\Driver\Userland\ConnectionHandler\ConnectionHandlerFactoryInterface; use PHPFastCGI\FastCGIDaemon\Driver\Userland\Exception\UserlandDaemonException; use PHPFastCGI\FastCGIDaemon\Exception\ShutdownException; @@ -65,7 +66,7 @@ public function __construct(KernelInterface $kernel, DaemonOptions $daemonOption /** * {@inheritdoc} */ - public function run() + public function run(): void { $this->setupDaemon($this->daemonOptions); $this->daemonOptions->getOption(DaemonOptions::LOGGER)->notice('Daemon is running and ready to accept connections'); @@ -93,8 +94,10 @@ public function run() * Wait for connections in the pool to become readable. Create connection * handlers for new connections and trigger the ready method when there is * data for the handlers to receive. Clean up closed connections. + * + * @throws \Exception */ - private function processConnectionPool() + private function processConnectionPool(): void { $readableConnections = $this->connectionPool->getReadableConnections(5); @@ -118,8 +121,10 @@ private function processConnectionPool() /** * Gracefully shutdown the daemon. + * + * @throws \Exception */ - private function shutdown() + private function shutdown(): void { $this->connectionPool->shutdown(); diff --git a/src/Driver/Userland/UserlandDaemonFactory.php b/src/Driver/Userland/UserlandDaemonFactory.php index 964b482..84002a4 100644 --- a/src/Driver/Userland/UserlandDaemonFactory.php +++ b/src/Driver/Userland/UserlandDaemonFactory.php @@ -26,7 +26,7 @@ final class UserlandDaemonFactory implements DaemonFactoryInterface * * @codeCoverageIgnore The FastCGI daemon */ - public function createDaemon(KernelInterface $kernel, DaemonOptions $options, $fd = DaemonInterface::FCGI_LISTENSOCK_FILENO) + public function createDaemon(KernelInterface $kernel, DaemonOptions $options, int $fd = DaemonInterface::FCGI_LISTENSOCK_FILENO): DaemonInterface { $socket = fopen('php://fd/'.$fd, 'r'); @@ -51,7 +51,7 @@ public function createDaemon(KernelInterface $kernel, DaemonOptions $options, $f * * @codeCoverageIgnore */ - public function createTcpDaemon(KernelInterface $kernel, DaemonOptions $options, $host, $port) + public function createTcpDaemon(KernelInterface $kernel, DaemonOptions $options, string $host, int $port): DaemonInterface { $address = 'tcp://'.$host.':'.$port; $socket = stream_socket_server($address); @@ -73,7 +73,7 @@ public function createTcpDaemon(KernelInterface $kernel, DaemonOptions $options, * * @return UserlandDaemon The FastCGI daemon */ - public function createDaemonFromStreamSocket(KernelInterface $kernel, DaemonOptions $options, $socket) + public function createDaemonFromStreamSocket(KernelInterface $kernel, DaemonOptions $options, $socket): DaemonInterface { $connectionPool = new StreamSocketConnectionPool($socket); $connectionHandlerFactory = new ConnectionHandlerFactory(); diff --git a/src/Http/Request.php b/src/Http/Request.php index 06354c4..74c053d 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -3,6 +3,7 @@ namespace PHPFastCGI\FastCGIDaemon\Http; use Nyholm\Psr7Server\ServerRequestCreatorInterface; +use Psr\Http\Message\ServerRequestInterface; use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest; /** @@ -67,7 +68,7 @@ public static function setServerRequestCreator(ServerRequestCreatorInterface $se /** * {@inheritdoc} */ - public function getParams() + public function getParams(): array { return $this->params; } @@ -108,7 +109,7 @@ public static function getUploadDir(): string /** * {@inheritdoc} */ - public function getQuery() + public function getQuery(): array { $query = null; @@ -122,7 +123,7 @@ public function getQuery() /** * {@inheritdoc} */ - public function getPost() + public function getPost(): array { $post = null; @@ -150,7 +151,7 @@ public function getPost() return $post ?: []; } - private function parseMultipartFormData($stream, $boundary) { + private function parseMultipartFormData($stream, string $boundary): array { $post = ""; $files = []; $fieldType = $fieldName = $filename = $mimeType = null; @@ -208,7 +209,7 @@ private function parseMultipartFormData($stream, $boundary) { /** * {@inheritdoc} */ - public function getCookies() + public function getCookies(): array { $cookies = []; @@ -235,7 +236,7 @@ public function getStdin() /** * {@inheritdoc} */ - public function getServerRequest() + public function getServerRequest(): ServerRequestInterface { if (null === self::$serverRequestCreator) { throw new \RuntimeException('You need to add an object of \Nyholm\Psr7Server\ServerRequestCreatorInterface to \PHPFastCGI\FastCGIDaemon\Http\Request::setServerRequestCreator to use PSR-7 requests. Please install and read more at https://github.com/nyholm/psr7-server'); @@ -262,7 +263,7 @@ public function getServerRequest() /** * {@inheritdoc} */ - public function getHttpFoundationRequest() + public function getHttpFoundationRequest(): HttpFoundationRequest { if (!class_exists(HttpFoundationRequest::class)) { throw new \RuntimeException('You need to install symfony/http-foundation:^4.0 to use HttpFoundation requests.'); diff --git a/src/Http/RequestInterface.php b/src/Http/RequestInterface.php index e03c75f..b53da88 100644 --- a/src/Http/RequestInterface.php +++ b/src/Http/RequestInterface.php @@ -16,28 +16,22 @@ interface RequestInterface * * @return array Associative array of FastCGI request params */ - public function getParams(); + public function getParams(): array; /** * Returns expected contents of $_GET superglobal array. - * - * @return array */ - public function getQuery(); + public function getQuery(): array; /** * Returns expected contents of $_POST superglobal array. - * - * @return array */ - public function getPost(); + public function getPost(): array; /** * Returns expected contents of $_COOKIES superglobal. - * - * @return array */ - public function getCookies(); + public function getCookies(): array; /** * Get the FastCGI stdin data. @@ -48,15 +42,11 @@ public function getStdin(); /** * Get the request as a PSR-7 server request. - * - * @return ServerRequestInterface The request object */ - public function getServerRequest(); + public function getServerRequest(): ServerRequestInterface; /** * Get the request as a Symfony HttpFoundation request. - * - * @return HttpFoundationRequest The request object */ - public function getHttpFoundationRequest(); + public function getHttpFoundationRequest(): HttpFoundationRequest; } From 587c00b21ad9cddb7e7eacd159c3fd8435d181bc Mon Sep 17 00:00:00 2001 From: Nyholm Date: Tue, 7 Aug 2018 08:54:06 +0200 Subject: [PATCH 2/3] Declare strict types --- src/ApplicationFactory.php | 2 ++ src/ApplicationFactoryInterface.php | 2 ++ src/CallbackKernel.php | 2 ++ src/Command/DaemonRunCommand.php | 2 ++ src/DaemonFactoryInterface.php | 2 ++ src/DaemonInterface.php | 2 ++ src/DaemonOptions.php | 2 ++ src/DaemonOptionsInterface.php | 2 ++ src/DaemonTrait.php | 2 ++ src/Driver/DriverContainer.php | 2 ++ src/Driver/DriverContainerInterface.php | 2 ++ src/Driver/Userland/Connection/ConnectionInterface.php | 2 ++ src/Driver/Userland/Connection/ConnectionPoolInterface.php | 2 ++ src/Driver/Userland/Connection/StreamSocketConnection.php | 2 ++ src/Driver/Userland/Connection/StreamSocketConnectionPool.php | 2 ++ src/Driver/Userland/ConnectionHandler/ConnectionHandler.php | 2 ++ .../Userland/ConnectionHandler/ConnectionHandlerFactory.php | 2 ++ .../ConnectionHandler/ConnectionHandlerFactoryInterface.php | 2 ++ .../Userland/ConnectionHandler/ConnectionHandlerInterface.php | 2 ++ src/Driver/Userland/Exception/ConnectionException.php | 2 ++ src/Driver/Userland/Exception/ProtocolException.php | 2 ++ src/Driver/Userland/Exception/UserlandDaemonException.php | 2 ++ src/Driver/Userland/UserlandDaemon.php | 2 ++ src/Driver/Userland/UserlandDaemonFactory.php | 2 ++ src/Exception/DaemonException.php | 2 ++ src/Exception/ShutdownException.php | 2 ++ src/Http/Request.php | 2 ++ src/Http/RequestInterface.php | 2 ++ src/KernelInterface.php | 2 ++ 29 files changed, 58 insertions(+) diff --git a/src/ApplicationFactory.php b/src/ApplicationFactory.php index f5abab9..5201817 100644 --- a/src/ApplicationFactory.php +++ b/src/ApplicationFactory.php @@ -1,5 +1,7 @@ Date: Tue, 7 Aug 2018 09:08:14 +0200 Subject: [PATCH 3/3] fixed tests --- .../Connection/StreamSocketConnectionPool.php | 4 ++-- .../ConnectionHandler/ConnectionHandler.php | 16 ++++++++-------- test/CallbackKernelTest.php | 2 +- .../Mocker/Driver/Connection/MockConnection.php | 14 +++++++------- .../Driver/Connection/MockConnectionPool.php | 12 ++++++------ .../ConnectionHandler/MockConnectionHandler.php | 12 ++++++------ .../MockConnectionHandlerFactory.php | 3 ++- .../Helper/Mocker/Driver/MockDriverContainer.php | 3 ++- test/Helper/Mocker/MockDaemon.php | 8 ++++---- test/Helper/Mocker/MockDaemonFactory.php | 4 ++-- test/Helper/Mocker/MockerTrait.php | 4 ++-- 11 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/Driver/Userland/Connection/StreamSocketConnectionPool.php b/src/Driver/Userland/Connection/StreamSocketConnectionPool.php index 27beefd..a872bff 100644 --- a/src/Driver/Userland/Connection/StreamSocketConnectionPool.php +++ b/src/Driver/Userland/Connection/StreamSocketConnectionPool.php @@ -37,7 +37,7 @@ final class StreamSocketConnectionPool implements ConnectionPoolInterface */ public function __construct($socket) { - stream_set_blocking($socket, 0); + stream_set_blocking($socket, false); $this->serverSocket = $socket; $this->clientSockets = []; @@ -152,7 +152,7 @@ private function acceptConnection(): void $clientSocket = @stream_socket_accept($this->serverSocket); if (false !== $clientSocket) { - stream_set_blocking($clientSocket, 0); + stream_set_blocking($clientSocket, false); $connection = new StreamSocketConnection($clientSocket); diff --git a/src/Driver/Userland/ConnectionHandler/ConnectionHandler.php b/src/Driver/Userland/ConnectionHandler/ConnectionHandler.php index 621e4f7..4c4368a 100644 --- a/src/Driver/Userland/ConnectionHandler/ConnectionHandler.php +++ b/src/Driver/Userland/ConnectionHandler/ConnectionHandler.php @@ -90,7 +90,7 @@ public function ready(): array while (!empty($record = $this->readRecord())) { $statusCode = $this->processRecord($record); - if (null != $statusCode) { + if (null !== $statusCode) { $statusCodes[] = $statusCode; } } @@ -180,11 +180,11 @@ private function readRecord(): array * * @param array $record The record that has been read * - * @return int Number of dispatched requests + * @return null|int Number of dispatched requests * * @throws ProtocolException If the client sends an unexpected record. */ - private function processRecord(array $record): int + private function processRecord(array $record): ?int { $requestId = $record['requestId']; @@ -195,7 +195,7 @@ private function processRecord(array $record): int } elseif (!isset($this->requests[$requestId])) { throw new ProtocolException('Invalid request id for record of type: '.$record['type']); } elseif (DaemonInterface::FCGI_PARAMS === $record['type']) { - while (strlen($content) > 0) { + while (null !== $content && strlen($content) > 0) { $this->readNameValuePair($requestId, $content); } } elseif (DaemonInterface::FCGI_STDIN === $record['type']) { @@ -218,11 +218,11 @@ private function processRecord(array $record): int * Process a FCGI_BEGIN_REQUEST record. * * @param int $requestId The request id sending the record - * @param string $contentData The content of the record + * @param null|string $contentData The content of the record * * @throws ProtocolException If the FCGI_BEGIN_REQUEST record is unexpected */ - private function processBeginRequestRecord(int $requestId, string $contentData): void + private function processBeginRequestRecord(int $requestId, ?string $contentData): void { if (isset($this->requests[$requestId])) { throw new ProtocolException('Unexpected FCGI_BEGIN_REQUEST record'); @@ -257,9 +257,9 @@ private function processBeginRequestRecord(int $requestId, string $contentData): * Read a FastCGI name-value pair from a buffer and add it to the request params. * * @param int $requestId The request id that sent the name-value pair - * @param string $buffer The buffer to read the pair from (pass by reference) + * @param null|string $buffer The buffer to read the pair from (pass by reference) */ - private function readNameValuePair(int $requestId, string &$buffer): void + private function readNameValuePair(int $requestId, ?string &$buffer): void { $nameLength = $this->readFieldLength($buffer); $valueLength = $this->readFieldLength($buffer); diff --git a/test/CallbackKernelTest.php b/test/CallbackKernelTest.php index 6c55001..bea30e0 100644 --- a/test/CallbackKernelTest.php +++ b/test/CallbackKernelTest.php @@ -18,7 +18,7 @@ class CallbackKernelTest extends TestCase */ public function testInvalidArgumentException() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(\TypeError::class); new CallbackKernel('not a callable function'); } diff --git a/test/Helper/Mocker/Driver/Connection/MockConnection.php b/test/Helper/Mocker/Driver/Connection/MockConnection.php index 58b8baf..98d45f6 100644 --- a/test/Helper/Mocker/Driver/Connection/MockConnection.php +++ b/test/Helper/Mocker/Driver/Connection/MockConnection.php @@ -9,23 +9,23 @@ class MockConnection implements ConnectionInterface { use MockerTrait; - public function read($length) + public function read(int $length): string { - return $this->delegateCall('read', func_get_args()); + $this->delegateCall('read', func_get_args()); } - public function write($buffer) + public function write(string $buffer): void { - return $this->delegateCall('write', func_get_args()); + $this->delegateCall('write', func_get_args()); } - public function isClosed() + public function isClosed(): bool { return $this->delegateCall('isClosed', func_get_args()); } - public function close() + public function close(): void { - return $this->delegateCall('close', func_get_args()); + $this->delegateCall('close', func_get_args()); } } diff --git a/test/Helper/Mocker/Driver/Connection/MockConnectionPool.php b/test/Helper/Mocker/Driver/Connection/MockConnectionPool.php index 12cb53e..a8a835d 100644 --- a/test/Helper/Mocker/Driver/Connection/MockConnectionPool.php +++ b/test/Helper/Mocker/Driver/Connection/MockConnectionPool.php @@ -9,23 +9,23 @@ class MockConnectionPool implements ConnectionPoolInterface { use MockerTrait; - public function getReadableConnections($timeout) + public function getReadableConnections(int $timeout): array { return $this->delegateCall('getReadableConnections', func_get_args()); } - public function count() + public function count(): int { return $this->delegateCall('count', func_get_args()); } - public function shutdown() + public function shutdown(): void { - return $this->delegateCall('shutdown', func_get_args()); + $this->delegateCall('shutdown', func_get_args()); } - public function close() + public function close(): void { - return $this->delegateCall('close', func_get_args()); + $this->delegateCall('close', func_get_args()); } } diff --git a/test/Helper/Mocker/Driver/ConnectionHandler/MockConnectionHandler.php b/test/Helper/Mocker/Driver/ConnectionHandler/MockConnectionHandler.php index d224d28..3c6e792 100644 --- a/test/Helper/Mocker/Driver/ConnectionHandler/MockConnectionHandler.php +++ b/test/Helper/Mocker/Driver/ConnectionHandler/MockConnectionHandler.php @@ -9,22 +9,22 @@ class MockConnectionHandler implements ConnectionHandlerInterface { use MockerTrait; - public function ready() + public function ready(): array { return $this->delegateCall('ready', func_get_args()); } - public function shutdown() + public function shutdown(): void { - return $this->delegateCall('shutdown', func_get_args()); + $this->delegateCall('shutdown', func_get_args()); } - public function close() + public function close(): void { - return $this->delegateCall('close', func_get_args()); + $this->delegateCall('close', func_get_args()); } - public function isClosed() + public function isClosed(): bool { return $this->delegateCall('isClosed', func_get_args()); } diff --git a/test/Helper/Mocker/Driver/ConnectionHandler/MockConnectionHandlerFactory.php b/test/Helper/Mocker/Driver/ConnectionHandler/MockConnectionHandlerFactory.php index 1bb4f84..51e094a 100644 --- a/test/Helper/Mocker/Driver/ConnectionHandler/MockConnectionHandlerFactory.php +++ b/test/Helper/Mocker/Driver/ConnectionHandler/MockConnectionHandlerFactory.php @@ -2,6 +2,7 @@ namespace PHPFastCGI\Test\FastCGIDaemon\Helper\Mocker\Driver\ConnectionHandler; +use PHPFastCGI\FastCGIDaemon\Driver\Userland\ConnectionHandler\ConnectionHandlerInterface; use PHPFastCGI\FastCGIDaemon\KernelInterface; use PHPFastCGI\FastCGIDaemon\Driver\Userland\Connection\ConnectionInterface; use PHPFastCGI\FastCGIDaemon\Driver\Userland\ConnectionHandler\ConnectionHandlerFactoryInterface; @@ -11,7 +12,7 @@ class MockConnectionHandlerFactory implements ConnectionHandlerFactoryInterface { use MockerTrait; - public function createConnectionHandler(KernelInterface $kernel, ConnectionInterface $connection) + public function createConnectionHandler(KernelInterface $kernel, ConnectionInterface $connection): ConnectionHandlerInterface { return $this->delegateCall('createConnectionHandler', func_get_args()); } diff --git a/test/Helper/Mocker/Driver/MockDriverContainer.php b/test/Helper/Mocker/Driver/MockDriverContainer.php index 7536022..c431a24 100644 --- a/test/Helper/Mocker/Driver/MockDriverContainer.php +++ b/test/Helper/Mocker/Driver/MockDriverContainer.php @@ -2,6 +2,7 @@ namespace PHPFastCGI\Test\FastCGIDaemon\Helper\Mocker\Driver; +use PHPFastCGI\FastCGIDaemon\DaemonFactoryInterface; use PHPFastCGI\FastCGIDaemon\Driver\DriverContainerInterface; use PHPFastCGI\Test\FastCGIDaemon\Helper\Mocker\MockerTrait; @@ -9,7 +10,7 @@ class MockDriverContainer implements DriverContainerInterface { use MockerTrait; - public function getFactory($driver) + public function getFactory(string $driver): DaemonFactoryInterface { return $this->delegateCall('getFactory', func_get_args()); } diff --git a/test/Helper/Mocker/MockDaemon.php b/test/Helper/Mocker/MockDaemon.php index 8f3ca73..07c0a6c 100644 --- a/test/Helper/Mocker/MockDaemon.php +++ b/test/Helper/Mocker/MockDaemon.php @@ -8,13 +8,13 @@ class MockDaemon implements DaemonInterface { use MockerTrait; - public function run() + public function run(): void { - return $this->delegateCall('run', func_get_args()); + $this->delegateCall('run', func_get_args()); } - public function flagShutdown($message = null) + public function flagShutdown(string $message = null): void { - return $this->delegateCall('flagShutdown', func_get_args()); + $this->delegateCall('flagShutdown', func_get_args()); } } diff --git a/test/Helper/Mocker/MockDaemonFactory.php b/test/Helper/Mocker/MockDaemonFactory.php index 578164c..3343087 100644 --- a/test/Helper/Mocker/MockDaemonFactory.php +++ b/test/Helper/Mocker/MockDaemonFactory.php @@ -11,12 +11,12 @@ class MockDaemonFactory implements DaemonFactoryInterface { use MockerTrait; - public function createDaemon(KernelInterface $kernel, DaemonOptions $options, $fd = DaemonInterface::FCGI_LISTENSOCK_FILENO) + public function createDaemon(KernelInterface $kernel, DaemonOptions $options, int $fd = DaemonInterface::FCGI_LISTENSOCK_FILENO): DaemonInterface { return $this->delegateCall('createDaemon', func_get_args()); } - public function createTcpDaemon(KernelInterface $kernel, DaemonOptions $options, $port, $host = 'localhost') + public function createTcpDaemon(KernelInterface $kernel, DaemonOptions $options, string $host = 'localhost', int $port): DaemonInterface { return $this->delegateCall('createTcpDaemon', func_get_args()); } diff --git a/test/Helper/Mocker/MockerTrait.php b/test/Helper/Mocker/MockerTrait.php index 8f0957b..30a4d0b 100644 --- a/test/Helper/Mocker/MockerTrait.php +++ b/test/Helper/Mocker/MockerTrait.php @@ -41,7 +41,7 @@ public function __construct(array $callbacks = null) * * @throws \InvalidArgumentException When no callback is set for the method */ - protected function delegateCall($method, $arguments) + protected function delegateCall(string $method, array $arguments) { if (!isset($this->callbacks[$method])) { throw new \InvalidArgumentException('Method not configured: '.$method); @@ -61,7 +61,7 @@ protected function delegateCall($method, $arguments) * * @return array The delegated call list */ - public function getDelegatedCalls() + public function getDelegatedCalls(): array { return $this->delegatedCalls; }