diff --git a/.gitignore b/.gitignore index 4901aab..bad2e30 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /vendor /composer.lock /.phpunit.result.cache +/.phpcs-cache +/phpcs.xml diff --git a/Makefile b/Makefile index 785eb9b..b59ac47 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ phpmd: # Check code adheres to PSR-2 phpcs: - vendor/bin/phpcs --standard=PSR2 src/ + vendor/bin/phpcs # Run unit tests unit-tests: @@ -29,7 +29,7 @@ else endif phpstan: - vendor/bin/phpstan analyze --level max src/ tests/ + vendor/bin/phpstan analyze -c phpstan.neon composer-require-checker: vendor/bin/composer-require-checker diff --git a/composer.json b/composer.json index 19b6831..6eff3c6 100644 --- a/composer.json +++ b/composer.json @@ -38,6 +38,7 @@ "phpstan/phpstan": "^0.11", "pdepend/pdepend": "^2.5", "maglnet/composer-require-checker": "^2.0", - "nyholm/psr7": "^1.2" + "nyholm/psr7": "^1.2", + "doctrine/coding-standard": "^6.0" } } diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000..045582d --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,39 @@ + + + + + + + + + + + + + src + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..25cef5f --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,9 @@ +parameters: + level: max + paths: + - src/ + - tests/ + ignoreErrors: + - + message: '~Parameter #8 \$attachments of class rpkamp\\Mailhog\\Message\\Message constructor expects array, array given\.~' + path: tests/unit/Message/MessageTest.php diff --git a/src/MailhogClient.php b/src/MailhogClient.php index 7e2d4b3..d6388c3 100644 --- a/src/MailhogClient.php +++ b/src/MailhogClient.php @@ -10,6 +10,13 @@ use rpkamp\Mailhog\Message\MessageFactory; use rpkamp\Mailhog\Specification\Specification; use RuntimeException; +use function array_filter; +use function count; +use function iterator_to_array; +use function json_decode; +use function json_encode; +use function rtrim; +use function sprintf; class MailhogClient { @@ -38,7 +45,7 @@ public function __construct(HttpClient $client, RequestFactory $requestFactory, /** * @return Generator|Message[] */ - public function findAllMessages(int $limit = 50): Generator + public function findAllMessages(int $limit = 50) { $start = 0; while (true) { diff --git a/src/Message/Contact.php b/src/Message/Contact.php index 782c725..f906891 100644 --- a/src/Message/Contact.php +++ b/src/Message/Contact.php @@ -3,6 +3,10 @@ namespace rpkamp\Mailhog\Message; +use function preg_match; +use function stripslashes; +use function trim; + class Contact { /** @@ -11,7 +15,7 @@ class Contact public $emailAddress; /** - * @var null|string + * @var string|null */ public $name; diff --git a/src/Message/ContactCollection.php b/src/Message/ContactCollection.php index 9872d36..4e6a2b3 100644 --- a/src/Message/ContactCollection.php +++ b/src/Message/ContactCollection.php @@ -7,6 +7,10 @@ use Countable; use IteratorAggregate; use Traversable; +use function array_map; +use function count; +use function str_getcsv; +use function trim; class ContactCollection implements Countable, IteratorAggregate { @@ -31,7 +35,7 @@ public static function fromString(string $contacts): ContactCollection return new self( array_map( - function (string $contact) { + static function (string $contact) { return Contact::fromString($contact); }, array_map('trim', str_getcsv($contacts)) @@ -55,7 +59,7 @@ public function getIterator(): Traversable return new ArrayIterator($this->contacts); } - public function count() + public function count(): int { return count($this->contacts); } diff --git a/src/Message/Message.php b/src/Message/Message.php index 5afc9b9..888d1a8 100644 --- a/src/Message/Message.php +++ b/src/Message/Message.php @@ -5,6 +5,7 @@ use InvalidArgumentException; use rpkamp\Mailhog\Message\Mime\Attachment; +use function sprintf; class Message { @@ -48,6 +49,9 @@ class Message */ public $attachments; + /** + * @param Attachment[] $attachments + */ public function __construct( string $messageId, Contact $sender, diff --git a/src/Message/MessageFactory.php b/src/Message/MessageFactory.php index bd499e1..bf14b17 100644 --- a/src/Message/MessageFactory.php +++ b/src/Message/MessageFactory.php @@ -4,9 +4,13 @@ namespace rpkamp\Mailhog\Message; use rpkamp\Mailhog\Message\Mime\MimePartCollection; +use function quoted_printable_decode; class MessageFactory { + /** + * @param mixed[] $mailhogResponse + */ public static function fromMailhogResponse(array $mailhogResponse): Message { $mimeParts = MimePartCollection::fromMailhogResponse($mailhogResponse['MIME']['Parts'] ?? []); @@ -26,7 +30,10 @@ public static function fromMailhogResponse(array $mailhogResponse): Message ); } - private static function getBodyFrom(array $content) + /** + * @param mixed[] $content + */ + private static function getBodyFrom(array $content): string { if (isset($content['Headers']['Content-Transfer-Encoding'][0]) && $content['Headers']['Content-Transfer-Encoding'][0] === 'quoted-printable' diff --git a/src/Message/Mime/MimePart.php b/src/Message/Mime/MimePart.php index 4c38269..7781dbb 100644 --- a/src/Message/Mime/MimePart.php +++ b/src/Message/Mime/MimePart.php @@ -1,8 +1,14 @@ body = $body; } + /** + * @param mixed[] $mimePart + */ public static function fromMailhogResponse(array $mimePart): MimePart { $filename = null; diff --git a/src/Message/Mime/MimePartCollection.php b/src/Message/Mime/MimePartCollection.php index 1716d68..fb77c64 100644 --- a/src/Message/Mime/MimePartCollection.php +++ b/src/Message/Mime/MimePartCollection.php @@ -1,7 +1,12 @@ mimeParts = $mimeParts; } - public static function fromMailhogResponse(array $mimeParts) + /** + * @param mixed[] $mimeParts + */ + public static function fromMailhogResponse(array $mimeParts): self { return new self(self::flattenParts($mimeParts)); } - protected static function flattenParts(array $mimeParts) + /** + * @param mixed[] $mimeParts + * + * @return mixed[] + */ + protected static function flattenParts(array $mimeParts): array { $flattenedParts = []; foreach ($mimeParts as $mimePart) { @@ -34,11 +50,14 @@ protected static function flattenParts(array $mimeParts) return $flattenedParts; } - public function isEmpty() + public function isEmpty(): bool { return count($this->mimeParts) === 0; } + /** + * @return Attachment[] + */ public function getAttachments(): array { $attachments = []; @@ -57,18 +76,26 @@ public function getAttachments(): array return $attachments; } - public function getBody() + public function getBody(): string { - $textBody = ''; foreach ($this->mimeParts as $mimePart) { + if ($mimePart->isAttachment()) { + continue; + } if (stripos($mimePart->getContentType(), 'text/html') === 0) { return $mimePart->getBody(); } - if (stripos($mimePart->getContentType(), 'text/plain') === 0 && !$mimePart->isAttachment()) { - $textBody = $mimePart->getBody(); + } + + foreach ($this->mimeParts as $mimePart) { + if ($mimePart->isAttachment()) { + continue; + } + if (stripos($mimePart->getContentType(), 'text/plain') === 0) { + return $mimePart->getBody(); } } - return $textBody; + return ''; } } diff --git a/src/NoSuchMessageException.php b/src/NoSuchMessageException.php index 5c3f42b..e3d13a6 100644 --- a/src/NoSuchMessageException.php +++ b/src/NoSuchMessageException.php @@ -4,10 +4,11 @@ namespace rpkamp\Mailhog; use RuntimeException; +use function sprintf; class NoSuchMessageException extends RuntimeException { - public static function forMessageId(string $messageId) + public static function forMessageId(string $messageId): self { return new self( sprintf('No message found with messageId "%s"', $messageId) diff --git a/src/Specification/AndSpecification.php b/src/Specification/AndSpecification.php index 34264c3..df01a70 100644 --- a/src/Specification/AndSpecification.php +++ b/src/Specification/AndSpecification.php @@ -4,6 +4,8 @@ namespace rpkamp\Mailhog\Specification; use rpkamp\Mailhog\Message\Message; +use function array_slice; +use function count; final class AndSpecification implements Specification { diff --git a/src/Specification/BodySpecification.php b/src/Specification/BodySpecification.php index 8f458b3..75a0bf1 100644 --- a/src/Specification/BodySpecification.php +++ b/src/Specification/BodySpecification.php @@ -4,6 +4,7 @@ namespace rpkamp\Mailhog\Specification; use rpkamp\Mailhog\Message\Message; +use function strpos; final class BodySpecification implements Specification { diff --git a/src/Specification/OrSpecification.php b/src/Specification/OrSpecification.php index 0a449a0..c6d9af9 100644 --- a/src/Specification/OrSpecification.php +++ b/src/Specification/OrSpecification.php @@ -4,6 +4,8 @@ namespace rpkamp\Mailhog\Specification; use rpkamp\Mailhog\Message\Message; +use function array_slice; +use function count; final class OrSpecification implements Specification {