Skip to content

Commit

Permalink
Fix types and cs
Browse files Browse the repository at this point in the history
  • Loading branch information
micc83 committed Aug 25, 2020
1 parent c3ad9ec commit 9619792
Show file tree
Hide file tree
Showing 31 changed files with 152 additions and 104 deletions.
1 change: 1 addition & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ $finder = PhpCsFixer\Finder::create()
->in(__DIR__);

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'strict_param' => true,
Expand Down
2 changes: 1 addition & 1 deletion client.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
Expand Down
4 changes: 1 addition & 3 deletions config.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
<?php declare(strict_types=1);

return [
'version' => '1.0.0',

'smtp' => [
'host' => '127.0.0.1:8025',
],
Expand Down
13 changes: 10 additions & 3 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie;

use Exception;

class Config
{
/** @var array<string, string|array|null> */
private array $params;
/** @var array<string, string|array|null>|null */
private ?array $altParams;

const VERSION = "1.0.0";
const DATE_FORMAT = "Y-m-d H:i:s";

/**
* Config constructor.
* @param array<string|array|null> $params
* @param array<string|array|null>|null $altParams
*/
public function __construct(array $params, array $altParams = null)
{
$this->params = $params;
Expand All @@ -37,8 +44,8 @@ public function get(string $key)

/**
* @param string $key
* @param array $data
* @return array|mixed|null
* @param array<string, string|array|null> $data
* @return array<string, string|array|null>|null
* @throws Exception
*/
private static function dotGet(string $key, array $data)
Expand Down
12 changes: 5 additions & 7 deletions src/Console/Helpers.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie\Console;

use Exception;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Trait Helpers
* @package Mailamie\Console
* @mixin
*/
trait Helpers
{
protected ?OutputInterface $output;
protected ?ConsoleOutputInterface $output;
protected ?InputInterface $input;

private function getOutput(): OutputInterface
private function getOutput(): ConsoleOutputInterface
{
return $this->output;
}
Expand All @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion src/Emails/Attachment.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie\Emails;

Expand Down
39 changes: 32 additions & 7 deletions src/Emails/Message.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie\Emails;

Expand Down Expand Up @@ -29,6 +29,19 @@ class Message
/** @var Attachment[] */
private array $attachments;

/**
* Message constructor.
* @param string $raw
* @param string $sender
* @param string[] $recipients
* @param string[] $ccs
* @param string $subject
* @param string $htmlBody
* @param string $textBody
* @param string $replyTo
* @param string[] $allRecipients
* @param Attachment[] $attachments
*/
public function __construct(
string $raw,
string $sender,
Expand Down Expand Up @@ -68,7 +81,7 @@ public function getAttachment(string $id): Attachment
return $attachments[0];
}

public function getExcerpt()
public function getExcerpt(): string
{
return mb_strimwidth(strip_tags($this->htmlBody) ?: $this->textBody, 0, 30);
}
Expand Down Expand Up @@ -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)],
Expand Down Expand Up @@ -140,6 +156,9 @@ public function toTable()
return $table;
}

/**
* @return array<string|array>
*/
public function toArray(): array
{
return [
Expand All @@ -158,7 +177,10 @@ public function toArray(): array
];
}

private function attachmentsToArray()
/**
* @return array[]
*/
private function attachmentsToArray(): array
{
return array_map(function (Attachment $attachment) {
return [
Expand All @@ -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();
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Emails/Parser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie\Emails;

Expand Down
4 changes: 2 additions & 2 deletions src/Emails/Store.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie\Emails;

Expand Down Expand Up @@ -66,7 +66,7 @@ private function sortedByDate(): array
return $messages;
}

public function onNewMessage(Closure $callback)
public function onNewMessage(Closure $callback): void
{
$this->callbacks[] = $callback;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Events/DebugEvent.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie\Events;

Expand Down
2 changes: 1 addition & 1 deletion src/Events/Event.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie\Events;

Expand Down
2 changes: 1 addition & 1 deletion src/Events/Message.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie\Events;

Expand Down
2 changes: 1 addition & 1 deletion src/Events/Request.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie\Events;

Expand Down
2 changes: 1 addition & 1 deletion src/Events/Response.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie\Events;

Expand Down
2 changes: 1 addition & 1 deletion src/Events/ServerStarted.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie\Events;

Expand Down
2 changes: 1 addition & 1 deletion src/SmtpConnection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie;

Expand Down
12 changes: 4 additions & 8 deletions src/SmtpServer.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie;

use Mailamie\Events\DebugEvent;
use Mailamie\Events\ServerStarted;
use Psr\EventDispatcher\EventDispatcherInterface;
use React\EventLoop\StreamSelectLoop;
use React\EventLoop\LoopInterface;
use React\Socket\ConnectionInterface;
use React\Socket\Server;

class SmtpServer
{
private string $host;
private EventDispatcherInterface $events;
private LoopInterface $loop;

/**
* @var StreamSelectLoop
*/
private StreamSelectLoop $loop;

public function __construct(string $host, StreamSelectLoop $loop, EventDispatcherInterface $events)
public function __construct(string $host, LoopInterface $loop, EventDispatcherInterface $events)
{
$this->host = $host;
$this->events = $events;
Expand Down
15 changes: 10 additions & 5 deletions src/StartServer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie;

Expand All @@ -16,6 +16,7 @@
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand All @@ -35,7 +36,7 @@ public function __construct(Config $config)
$this->config = $config;
}

protected function configure()
protected function configure(): void
{
$this->setDescription('Mailamie is catch all SMTP server for testing.');
$this->setHelp(
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/WebController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Mailamie;

Expand Down Expand Up @@ -83,7 +83,7 @@ private function handleApiCall(ServerRequestInterface $request): Response
return $this->fileNotFoundError();
}

private function handleStaticFile(string $path)
private function handleStaticFile(string $path): Response
{
return new Response(
200,
Expand All @@ -95,7 +95,7 @@ private function handleStaticFile(string $path)
);
}

private function getMimeType($path): string
private function getMimeType(string $path): string
{
$mimeTypes = [
'css' => 'text/css',
Expand All @@ -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);
Expand Down Expand Up @@ -145,7 +145,7 @@ private function serverError(Throwable $e): Response
}

/**
* @param array|object $data
* @param array<int|string,mixed>|object $data
* @return Response
*/
private function json($data): Response
Expand Down
Loading

0 comments on commit 9619792

Please sign in to comment.