From 96197923684f237d5d0a7fa2ebc664acbfccb93d Mon Sep 17 00:00:00 2001 From: Alessandro Benoit Date: Tue, 25 Aug 2020 17:16:29 +0200 Subject: [PATCH] Fix types and cs --- .php_cs.dist | 1 + client.php | 2 +- config.php | 4 +-- src/Config.php | 13 ++++++--- src/Console/Helpers.php | 12 ++++----- src/Emails/Attachment.php | 2 +- src/Emails/Message.php | 39 ++++++++++++++++++++++----- src/Emails/Parser.php | 2 +- src/Emails/Store.php | 4 +-- src/Events/DebugEvent.php | 2 +- src/Events/Event.php | 2 +- src/Events/Message.php | 2 +- src/Events/Request.php | 2 +- src/Events/Response.php | 2 +- src/Events/ServerStarted.php | 2 +- src/SmtpConnection.php | 2 +- src/SmtpServer.php | 12 +++------ src/StartServer.php | 15 +++++++---- src/WebController.php | 10 +++---- src/WebServer.php | 8 +++--- src/WebSocketComponent.php | 10 +++---- src/WebSocketServer.php | 10 +++---- tests/Integration/IntegrationTest.php | 24 +++++++++++------ tests/Traits/Messages.php | 6 ++++- tests/Unit/ConfigTest.php | 6 ++--- tests/Unit/Emails/MessageTest.php | 8 +++--- tests/Unit/Emails/ParserTest.php | 4 +-- tests/Unit/Emails/StoreTest.php | 8 +++--- tests/Unit/SmtpConnectionTest.php | 30 ++++++++++++--------- tests/Unit/WebSocketComponentTest.php | 4 +-- todo.md | 8 +++--- 31 files changed, 152 insertions(+), 104 deletions(-) diff --git a/.php_cs.dist b/.php_cs.dist index c04f458..af19a56 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -5,6 +5,7 @@ $finder = PhpCsFixer\Finder::create() ->in(__DIR__); return PhpCsFixer\Config::create() + ->setRiskyAllowed(true) ->setRules([ '@PSR2' => true, 'strict_param' => true, diff --git a/client.php b/client.php index 64adbde..cb5566c 100644 --- a/client.php +++ b/client.php @@ -1,4 +1,4 @@ - '1.0.0', - 'smtp' => [ 'host' => '127.0.0.1:8025', ], diff --git a/src/Config.php b/src/Config.php index 9232be4..cb0c699 100644 --- a/src/Config.php +++ b/src/Config.php @@ -1,4 +1,4 @@ - */ private array $params; + /** @var array|null */ private ?array $altParams; const VERSION = "1.0.0"; const DATE_FORMAT = "Y-m-d H:i:s"; + /** + * Config constructor. + * @param array $params + * @param array|null $altParams + */ public function __construct(array $params, array $altParams = null) { $this->params = $params; @@ -37,8 +44,8 @@ public function get(string $key) /** * @param string $key - * @param array $data - * @return array|mixed|null + * @param array $data + * @return array|null * @throws Exception */ private static function dotGet(string $key, array $data) diff --git a/src/Console/Helpers.php b/src/Console/Helpers.php index 2546b00..b28b83b 100644 --- a/src/Console/Helpers.php +++ b/src/Console/Helpers.php @@ -1,24 +1,22 @@ -output; } @@ -45,7 +43,7 @@ private function writeTable(array $rows): void $table->render(); } - private function writeInfoBlockOn(ConsoleSectionOutput $section, string $title, string $subtitle) + private function writeInfoBlockOn(ConsoleSectionOutput $section, string $title, string $subtitle): void { $formatter = $this->getHelper('formatter'); $message = [$title, $subtitle]; diff --git a/src/Emails/Attachment.php b/src/Emails/Attachment.php index 35af9b7..d4892d8 100644 --- a/src/Emails/Attachment.php +++ b/src/Emails/Attachment.php @@ -1,4 +1,4 @@ -htmlBody) ?: $this->textBody, 0, 30); } @@ -109,7 +122,10 @@ public function getAttachments(): array return $this->attachments; } - public function toTable() + /** + * @return array[] + */ + public function toTable(): array { $table = [ ['Date', $this->created_at->format(Config::DATE_FORMAT)], @@ -140,6 +156,9 @@ public function toTable() return $table; } + /** + * @return array + */ public function toArray(): array { return [ @@ -158,7 +177,10 @@ public function toArray(): array ]; } - private function attachmentsToArray() + /** + * @return array[] + */ + private function attachmentsToArray(): array { return array_map(function (Attachment $attachment) { return [ @@ -169,7 +191,10 @@ private function attachmentsToArray() }, $this->attachments); } - private function getAttachmentNames() + /** + * @return string[] + */ + private function getAttachmentNames(): array { return array_map(function (Attachment $attachment) { return $attachment->getFilename(); @@ -179,9 +204,9 @@ private function getAttachmentNames() /** * BCCs are recipients passed as RCPTs but not * in the body of the mail. - * @return array + * @return string[] */ - private function getBccs() + private function getBccs(): array { return array_values(array_filter($this->allRecipients, function (string $recipient) { foreach (array_merge($this->recipients, $this->ccs) as $publicRecipient) { diff --git a/src/Emails/Parser.php b/src/Emails/Parser.php index b6e6798..ed18b3e 100644 --- a/src/Emails/Parser.php +++ b/src/Emails/Parser.php @@ -1,4 +1,4 @@ -callbacks[] = $callback; } diff --git a/src/Events/DebugEvent.php b/src/Events/DebugEvent.php index 9e1026d..180d21a 100644 --- a/src/Events/DebugEvent.php +++ b/src/Events/DebugEvent.php @@ -1,4 +1,4 @@ -host = $host; $this->events = $events; diff --git a/src/StartServer.php b/src/StartServer.php index f6ef9e4..290a204 100644 --- a/src/StartServer.php +++ b/src/StartServer.php @@ -1,4 +1,4 @@ -config = $config; } - protected function configure() + protected function configure(): void { $this->setDescription('Mailamie is catch all SMTP server for testing.'); $this->setHelp( @@ -64,6 +65,10 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + if (!$output instanceof ConsoleOutputInterface) { + throw new Exception('Expected instance of ConsoleOutputInterface'); + } + $this->output = $output; $this->input = $input; @@ -98,7 +103,7 @@ protected function execute(InputInterface $input, OutputInterface $output) return Command::SUCCESS; } - private function registerEventListenersOn(EventDispatcher $dispatcher, MessageStore $messageStore) + private function registerEventListenersOn(EventDispatcher $dispatcher, MessageStore $messageStore): void { $startingSection = $this->startingBanner(); @@ -141,8 +146,8 @@ private function handleMessage(Message $message, MessageStore $messageStore): vo private function getHost(): string { - return (string)$this->getInput()->getOption('host') - ?: $this->config->get('smtp.host'); + return (string)($this->getInput()->getOption('host') + ?: $this->config->get('smtp.host')); } private function startingBanner(): ConsoleSectionOutput diff --git a/src/WebController.php b/src/WebController.php index 7b4d7a9..5135043 100644 --- a/src/WebController.php +++ b/src/WebController.php @@ -1,4 +1,4 @@ -fileNotFoundError(); } - private function handleStaticFile(string $path) + private function handleStaticFile(string $path): Response { return new Response( 200, @@ -95,7 +95,7 @@ private function handleStaticFile(string $path) ); } - private function getMimeType($path): string + private function getMimeType(string $path): string { $mimeTypes = [ 'css' => 'text/css', @@ -112,7 +112,7 @@ private function getMimeType($path): string return mime_content_type($path); } - private function handlePhpFile(string $path) + private function handlePhpFile(string $path): Response { ob_start(); require($path); @@ -145,7 +145,7 @@ private function serverError(Throwable $e): Response } /** - * @param array|object $data + * @param array|object $data * @return Response */ private function json($data): Response diff --git a/src/WebServer.php b/src/WebServer.php index 3ad4bbf..573aff3 100644 --- a/src/WebServer.php +++ b/src/WebServer.php @@ -1,20 +1,20 @@ -loop = $loop; $this->messageStore = $messageStore; diff --git a/src/WebSocketComponent.php b/src/WebSocketComponent.php index 68e2836..5a90dc2 100644 --- a/src/WebSocketComponent.php +++ b/src/WebSocketComponent.php @@ -1,4 +1,4 @@ -clients->attach($conn); } - public function onClose(ConnectionInterface $conn) + public function onClose(ConnectionInterface $conn): void { $this->clients->detach($conn); } - public function onError(ConnectionInterface $conn, Exception $e) + public function onError(ConnectionInterface $conn, Exception $e): void { echo "An error has occurred: {$e->getMessage()}\n"; $conn->close(); } - public function onMessage(ConnectionInterface $from, $msg) + public function onMessage(ConnectionInterface $from, $msg): void { // .. } diff --git a/src/WebSocketServer.php b/src/WebSocketServer.php index ae83581..42e9d5d 100644 --- a/src/WebSocketServer.php +++ b/src/WebSocketServer.php @@ -1,4 +1,4 @@ -loop = $loop; $this->store = $store; $this->host = $host; } - public function start() + public function start(): void { new IoServer( new HttpServer( diff --git a/tests/Integration/IntegrationTest.php b/tests/Integration/IntegrationTest.php index 47b4b5e..49dc0a2 100644 --- a/tests/Integration/IntegrationTest.php +++ b/tests/Integration/IntegrationTest.php @@ -1,4 +1,4 @@ -expectedSteps = 3; @@ -100,7 +102,7 @@ public function should_start_and_listen_for_smtp_calls() $this->endLoop(); } - private function endLoop() + private function endLoop(): void { $this->loop->run(); @@ -123,14 +125,17 @@ private function assertWebsocketWorking(): void }); } - private function connectToWebsocket(): PromiseInterface + private function connectToWebsocket(): ExtendedPromiseInterface { - return connect("ws://{$this->config->get('websocket.host')}", [], [], $this->loop) + /** @var ExtendedPromiseInterface $promise */ + $promise = connect("ws://{$this->config->get('websocket.host')}", [], [], $this->loop) ->then(function (WebSocket $conn) { $this->websocket = $conn; }, function (Exception $e) { $this->storeAsyncException($e); }); + + return $promise; } private function assertServerStarting(string $chunk): void @@ -153,7 +158,7 @@ private function assertMessageReceived(string $chunk): void $this->assertStringContainsString('Here is the subject, welcome to New york!', $chunk); } - private function assertWebBrowserWorking(): PromiseInterface + private function assertWebBrowserWorking(): ExtendedPromiseInterface { $client = new Browser($this->loop); @@ -177,10 +182,13 @@ private function assertWebBrowserWorking(): PromiseInterface $this->storeAsyncException($e); }); - return all([$request1, $request2]); + /** @var ExtendedPromiseInterface $promise */ + $promise = all([$request1, $request2]); + + return $promise; } - private function storeAsyncException(Exception $e) + private function storeAsyncException(Exception $e): void { $this->asyncException = new ExpectationFailedException($e->getMessage(), null, $e); } diff --git a/tests/Traits/Messages.php b/tests/Traits/Messages.php index 5bb20a8..73d1740 100644 --- a/tests/Traits/Messages.php +++ b/tests/Traits/Messages.php @@ -1,4 +1,4 @@ - $override + * @return Message + */ private function createMessage(array $override = []): Message { $params = array_merge([ diff --git a/tests/Unit/ConfigTest.php b/tests/Unit/ConfigTest.php index 1d8a1ac..962b0b8 100644 --- a/tests/Unit/ConfigTest.php +++ b/tests/Unit/ConfigTest.php @@ -1,4 +1,4 @@ - ['level2' => 'value']]); @@ -16,7 +16,7 @@ public function it_allows_to_retrieve_a_value_with_dot_notation() } /** @test */ - public function it_allows_to_retrieve_a_value_from_default_or_alt_values() + public function it_allows_to_retrieve_a_value_from_default_or_alt_values(): void { $config = new Config([ 'a' => 'a_default_value', diff --git a/tests/Unit/Emails/MessageTest.php b/tests/Unit/Emails/MessageTest.php index 2cba41b..bebbd13 100644 --- a/tests/Unit/Emails/MessageTest.php +++ b/tests/Unit/Emails/MessageTest.php @@ -1,4 +1,4 @@ -createMessage(); @@ -39,7 +39,7 @@ public function can_be_converted_to_array() } /** @test */ - public function can_be_converted_to_cli_table() + public function can_be_converted_to_cli_table(): void { $message = $this->createMessage(); @@ -57,7 +57,7 @@ public function can_be_converted_to_cli_table() } /** @test */ - public function it_allows_to_retrieve_an_attachment_by_id() + public function it_allows_to_retrieve_an_attachment_by_id(): void { $message = $this->createMessage(); $attachmentId = $message->getAttachments()[0]->getId(); diff --git a/tests/Unit/Emails/ParserTest.php b/tests/Unit/Emails/ParserTest.php index 5d0d61b..5822ad2 100644 --- a/tests/Unit/Emails/ParserTest.php +++ b/tests/Unit/Emails/ParserTest.php @@ -1,4 +1,4 @@ -createMessage(); @@ -25,7 +25,7 @@ public function it_allows_to_store_and_search_messages() } /** @test */ - public function it_allows_to_register_callbacks_for_new_messages() + public function it_allows_to_register_callbacks_for_new_messages(): void { $message = $this->createMessage(); @@ -41,7 +41,7 @@ public function it_allows_to_register_callbacks_for_new_messages() } /** @test */ - public function it_allows_to_retrieve_all_messages() + public function it_allows_to_retrieve_all_messages(): void { $message1 = $this->createMessage(); $message2 = $this->createMessage(); diff --git a/tests/Unit/SmtpConnectionTest.php b/tests/Unit/SmtpConnectionTest.php index adeed91..28134dc 100644 --- a/tests/Unit/SmtpConnectionTest.php +++ b/tests/Unit/SmtpConnectionTest.php @@ -21,9 +21,10 @@ class SmtpConnectionTest extends TestCase { /** @test */ - public function on_ready_send_220_service_ready() + public function on_ready_send_220_service_ready(): void { - $smtp = new SmtpConnection(...[$connection, $dispatcher] = $this->createMocks()); + [$connection, $dispatcher] = $this->createMocks(); + $smtp = new SmtpConnection($connection, $dispatcher); $this->expectWrite($connection, 220); @@ -36,9 +37,10 @@ public function on_ready_send_220_service_ready() } /** @test */ - public function it_allows_to_handle_EHLO_handshake() + public function it_allows_to_handle_EHLO_handshake(): void { - $smtp = new SmtpConnection(...[$connection, $dispatcher] = $this->createMocks()); + [$connection, $dispatcher] = $this->createMocks(); + $smtp = new SmtpConnection($connection, $dispatcher); $this->expectWrite($connection, 250); $this->expectDispatch( @@ -51,9 +53,10 @@ public function it_allows_to_handle_EHLO_handshake() } /** @test */ - public function it_allows_to_handle_RCPT_TO_commands() + public function it_allows_to_handle_RCPT_TO_commands(): void { - $smtp = new SmtpConnection(...[$connection, $dispatcher] = $this->createMocks()); + [$connection, $dispatcher] = $this->createMocks(); + $smtp = new SmtpConnection($connection, $dispatcher); $this->expectWrite($connection, 250); $this->expectDispatch( @@ -66,9 +69,10 @@ public function it_allows_to_handle_RCPT_TO_commands() } /** @test */ - public function it_allows_to_handle_DATA_commands() + public function it_allows_to_handle_DATA_commands(): void { - $smtp = new SmtpConnection(...[$connection, $dispatcher] = $this->createMocks()); + [$connection, $dispatcher] = $this->createMocks(); + $smtp = new SmtpConnection($connection, $dispatcher); $this->expectWrite($connection, 354); $this->expectDispatch( @@ -81,9 +85,10 @@ public function it_allows_to_handle_DATA_commands() } /** @test */ - public function it_allows_to_capture_a_new_message() + public function it_allows_to_capture_a_new_message(): void { - $smtp = new SmtpConnection(...[$connection, $dispatcher] = $this->createMocks()); + [$connection, $dispatcher] = $this->createMocks(); + $smtp = new SmtpConnection($connection, $dispatcher); $this->expectWrite($connection, 250, 354, 250); $this->expectDispatch( @@ -105,9 +110,10 @@ public function it_allows_to_capture_a_new_message() } /** @test */ - public function it_allows_to_handle_QUIT_commands() + public function it_allows_to_handle_QUIT_commands(): void { - $smtp = new SmtpConnection(...[$connection, $dispatcher] = $this->createMocks()); + [$connection, $dispatcher] = $this->createMocks(); + $smtp = new SmtpConnection($connection, $dispatcher); $this->expectWrite($connection, 221); $this->expectDispatch( diff --git a/tests/Unit/WebSocketComponentTest.php b/tests/Unit/WebSocketComponentTest.php index ea268b5..3c01641 100644 --- a/tests/Unit/WebSocketComponentTest.php +++ b/tests/Unit/WebSocketComponentTest.php @@ -1,4 +1,4 @@ -